Skip to content

Commit

Permalink
replace log(x, base) by logbase(base, x)
Browse files Browse the repository at this point in the history
  • Loading branch information
metelkin committed Feb 7, 2024
1 parent 17a6ce0 commit d53a541
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 27 deletions.
8 changes: 4 additions & 4 deletions export-formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ _Skipped cell means no conversion_
|`floor(x)`| | | | |
|`ln(x)`| |`NaNMath.log(x)`| | |
|`log(x)`| |`NaNMath.log(x)`| |`log(x)`|
|`log(x, base)`|`log(x) / log(base)`|`NaNMath.log(base, x)`| |`(log(x)/log(base))`|
|`logbase(base, x)`|`log(x) / log(base)`|`NaNMath.log(base, x)`| |`(log(x)/log(base))`|
|`log10(x)`| |`NaNMath.log10(x)`| | |
|`log2(x)`|`log(x) / log(2)`|`NaNMath.log2(x)`| |`(log(x)/log(2))`|
|`multiply(x, y)`|`x * y`|`*(x, y)`| | |
Expand All @@ -457,8 +457,8 @@ _Skipped cell means no conversion_
|`Infinity`| |`Inf`| | |
|`NaN`| |`NaN`| | |
|`t`| | |`SOLVERTIME`|`time`|
|`a and b`| |`a && b`| |`&`|
|`a or b`| |`a \|\| b`| |`\|`|
|`a and b`| |`a && b`| |`a & b`|
|`a or b`| |`a \|\| b`| |`a \| b`|
|`a xor b`| |`xor(a, b)`| |`xor(a, b)`|
|`not a`| |`!a`| |`~a`|
|`b1 < b2 ? x : y`| `ifgt(b1, b2, x, y)`| | |`tern__(b1 < b2, x, y)`|
Expand All @@ -479,7 +479,7 @@ _Conversion to SBML's MathML_
|`floor(x)`|`<apply><floor/>(x)</apply>`|
|`ln(x)`|`<apply><ln/>(x)</apply>`|
|`log(x)`|`<apply><log/>(x)</apply>`|
|`log(x, base)`|`<apply><log/><logbase>(base)</logbase>(x)</apply>`|
|`logbase(base, x)`|`<apply><log/><logbase>(base)</logbase>(x)</apply>`|
|`log10(x)`|`<apply><log/>(x)</apply>`|
|`log2(x)`|`<apply><log/><logbase><cn>2</cn></logbase>(x)</apply>`|
|`multiply(x, y)`|`<apply><times/>(x) (y)</apply>`|
Expand Down
4 changes: 4 additions & 0 deletions src/container/core-items.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@
"id": "log", "action": "defineFunction",
"arguments": ["x"]
},
{
"id": "logbase", "action": "defineFunction",
"arguments": ["logbase", "x"]
},
{
"id": "log10", "action": "defineFunction",
"arguments": ["x"]
Expand Down
9 changes: 6 additions & 3 deletions src/core/math-calc-unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,12 @@ function _calcUnit(_this, record) {
}
}
if (_this.fn.name === 'log' || _this.fn.name === 'ln' || _this.fn.name === 'log10' || _this.fn.name === 'log2' ) {
if (args.length > 1 && !argUnitDimensionless[1]) {
let unitExpr = `log(${argUnit[0].toString()}, ${argUnit[1].toString()})`;
logger.warn(`Units inconsistency for "${record.index}": second arguments of log() must be dimensionless "${_this.toString()}" => "${unitExpr}"`);
return new Unit();
}
if (_this.fn.name === 'logbase') {
if (!argUnitDimensionless[0]) {
let unitExpr = `logbase(${argUnit[0].toString()}, ${argUnit[1].toString()})`;
logger.warn(`Units inconsistency for "${record.index}": first arguments of logbase() must be dimensionless "${_this.toString()}" => "${unitExpr}"`);
}
return new Unit();
}
Expand Down
4 changes: 2 additions & 2 deletions src/dbsolve-export/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ Expression.prototype.toSLVString = function(powTransform = 'keep') {
return `${args[0]} ^ (1 / ${args[1]})`;
}
}
if (node.type === 'FunctionNode' && node.fn.name === 'log' && node.args.length === 2) {
if (node.type === 'FunctionNode' && node.fn.name === 'logbase') {
let args = node.args
.map((arg) => arg.toString(options));
return `log(${args[0]}) / log(${args[1]})`;
return `log(${args[1]}) / log(${args[0]})`;
}
if (node.type === 'FunctionNode' && node.fn.name === 'log2') {
let args = node.args
Expand Down
6 changes: 3 additions & 3 deletions src/julia-export/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ Expression.prototype.toJuliaString = function(){
.map((arg) => arg.toString(options));
return `NaNMath.log(${args[0]})`;
}
if(node.type==='FunctionNode' && node.fn.name==='log' && node.args.length === 1){
if(node.type==='FunctionNode' && node.fn.name==='log'){
let args = node.args
.map((arg) => arg.toString(options));
return `NaNMath.log(${args[0]})`;
}
if(node.type==='FunctionNode' && node.fn.name==='log' && node.args.length >= 2){
if(node.type==='FunctionNode' && node.fn.name==='logbase'){
let args = node.args
.map((arg) => arg.toString(options));
return `NaNMath.log(${args[1]}, ${args[0]})`;
return `NaNMath.log(${args[0]}, ${args[1]})`;
}
if(node.type==='FunctionNode' && node.fn.name==='factorial'){
let args = node.args
Expand Down
12 changes: 4 additions & 8 deletions src/matlab-export/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ Expression.prototype.toMatlabString = function(){
}
}
if (node.type==='FunctionNode' && node.fn.name==='log') {
if(node.args.length===1){
return `log(${node.args[0].toString(options)})`;
}else if(node.args.length===2){ // converts log(a, b) => log(a)/log(b)
let args = node.args
.map((arg) => `log(${arg.toString(options)})`)
.join('/');
return `(${args})`;
}
return `log(${node.args[0].toString(options)})`;
}
if (node.type==='FunctionNode' && node.fn.name==='logbase') {
return `(log(${node.args[1].toString(options)})/log(${node.args[0].toString(options)}))`;
}
if (node.type==='FunctionNode' && node.fn.name==='log2') {
return `(log(${node.args[0].toString(options)})/log(2))`;
Expand Down
2 changes: 1 addition & 1 deletion src/module-system/to-math-expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function _toMathExpr(element, useParentheses = false){
return `log2(${expr[0]})`;
} else {
let base = _toMathExpr(logbase.elements[0]); // skip ()
return `log(${expr[0]}, ${base})`;
return `logbase(${base}, ${expr[0]})`;
}
// === trigonometry ===
} else if (element.name === 'apply' && first.name === 'arcsin') {
Expand Down
2 changes: 1 addition & 1 deletion test/check-units/calc-unit-warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let qArr = [
{id: 'y5', class: 'Record', assignments: {start_: 'max(k2, x4, k1) + min(k2, x4, 1)'}},
{id: 'y6', class: 'Record', assignments: {start_: 'square(k2+k1)*cube(x4)*sqrt(x1a)'}},
{id: 'y7', class: 'Record', assignments: {start_: 'nthRoot(4,x4)'}},
{id: 'y8', class: 'Record', assignments: {start_: 'log(2,k2)'}},
{id: 'y8', class: 'Record', assignments: {start_: 'logbase(k2, 2)'}},
{id: 'y9', class: 'Record', assignments: {start_: 'sign(k3 + 1.1)'}},
{id: 'y10', class: 'Record', assignments: {start_: 'ifge(k1,k2,k3,k4)'}},
{id: 'y11', class: 'Record', assignments: {start_: 'piecewise(k1,k2,k3,k4)'}},
Expand Down
6 changes: 3 additions & 3 deletions test/core/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ describe('Expession exports', () => {
let expr = Expression.fromString('log(x)');
expect(expr.toMatlabString()).to.be.equal('log(x)');
});
it('toMatlabString() for "log(x, y)"', () => {
let expr = Expression.fromString('log(x, y)');
expect(expr.toMatlabString()).to.be.equal('(log(x)/log(y))');
it('toMatlabString() for "log(b, x)"', () => {
let expr = Expression.fromString('logbase(b, x)');
expect(expr.toMatlabString()).to.be.equal('(log(x)/log(b))');
});
it('toMatlabString() for "log10(x)"', () => {
let expr = Expression.fromString('log10(x)');
Expand Down
4 changes: 2 additions & 2 deletions test/julia-export/to-julia-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ describe('Expression exports to Julia', () => {
let expr = Expression.fromString('log(exp(1))');
expect(expr.toJuliaString()).to.be.equal('NaNMath.log(exp(1e+0))');
});
it('toJuliaString() for "log(8, 2)"', () => {
let expr = Expression.fromString('log(8, 2)');
it('toJuliaString() for "logbase(2, 8)"', () => {
let expr = Expression.fromString('logbase(2, 8)');
expect(expr.toJuliaString()).to.be.equal('NaNMath.log(2e+0, 8e+0)');
});
it('toJuliaString() for "log10(100)"', () => {
Expand Down

0 comments on commit d53a541

Please sign in to comment.