From d53a541ff9041ffec7d38d6649c360de134e12ff Mon Sep 17 00:00:00 2001 From: Evgeny Metelkin Date: Wed, 7 Feb 2024 11:55:24 +0200 Subject: [PATCH] replace log(x, base) by logbase(base, x) --- export-formats.md | 8 ++++---- src/container/core-items.json | 4 ++++ src/core/math-calc-unit.js | 9 ++++++--- src/dbsolve-export/expression.js | 4 ++-- src/julia-export/expression.js | 6 +++--- src/matlab-export/expression.js | 12 ++++-------- src/module-system/to-math-expr.js | 2 +- test/check-units/calc-unit-warnings.js | 2 +- test/core/expression.js | 6 +++--- test/julia-export/to-julia-string.js | 4 ++-- 10 files changed, 30 insertions(+), 27 deletions(-) diff --git a/export-formats.md b/export-formats.md index f7bb2827..dbabf71f 100644 --- a/export-formats.md +++ b/export-formats.md @@ -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)`| | | @@ -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)`| @@ -479,7 +479,7 @@ _Conversion to SBML's MathML_ |`floor(x)`|`(x)`| |`ln(x)`|`(x)`| |`log(x)`|`(x)`| -|`log(x, base)`|`(base)(x)`| +|`logbase(base, x)`|`(base)(x)`| |`log10(x)`|`(x)`| |`log2(x)`|`2(x)`| |`multiply(x, y)`|`(x) (y)`| diff --git a/src/container/core-items.json b/src/container/core-items.json index cee5730b..58f31180 100644 --- a/src/container/core-items.json +++ b/src/container/core-items.json @@ -183,6 +183,10 @@ "id": "log", "action": "defineFunction", "arguments": ["x"] }, + { + "id": "logbase", "action": "defineFunction", + "arguments": ["logbase", "x"] + }, { "id": "log10", "action": "defineFunction", "arguments": ["x"] diff --git a/src/core/math-calc-unit.js b/src/core/math-calc-unit.js index fcdc986d..d9c2d08f 100644 --- a/src/core/math-calc-unit.js +++ b/src/core/math-calc-unit.js @@ -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(); } diff --git a/src/dbsolve-export/expression.js b/src/dbsolve-export/expression.js index dd8cacbc..b1ce139b 100644 --- a/src/dbsolve-export/expression.js +++ b/src/dbsolve-export/expression.js @@ -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 diff --git a/src/julia-export/expression.js b/src/julia-export/expression.js index 3fb3dd2e..12216740 100644 --- a/src/julia-export/expression.js +++ b/src/julia-export/expression.js @@ -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 diff --git a/src/matlab-export/expression.js b/src/matlab-export/expression.js index 1761768d..e1789ae4 100644 --- a/src/matlab-export/expression.js +++ b/src/matlab-export/expression.js @@ -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))`; diff --git a/src/module-system/to-math-expr.js b/src/module-system/to-math-expr.js index ecb1b986..50766db8 100644 --- a/src/module-system/to-math-expr.js +++ b/src/module-system/to-math-expr.js @@ -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') { diff --git a/test/check-units/calc-unit-warnings.js b/test/check-units/calc-unit-warnings.js index 9e850f85..d5c8420f 100644 --- a/test/check-units/calc-unit-warnings.js +++ b/test/check-units/calc-unit-warnings.js @@ -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)'}}, diff --git a/test/core/expression.js b/test/core/expression.js index cf098766..716431d7 100644 --- a/test/core/expression.js +++ b/test/core/expression.js @@ -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)'); diff --git a/test/julia-export/to-julia-string.js b/test/julia-export/to-julia-string.js index 1c330726..4d9086c2 100644 --- a/test/julia-export/to-julia-string.js +++ b/test/julia-export/to-julia-string.js @@ -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)"', () => {