mXparser 的 iff() 条件不支持相等性测试
mXparser's iff() conditional does not support equality test
我在我的应用程序中使用 Java/.NET 符号数学库 mXparser
(5.0.2) 来支持用户提供的数学表达式。
问题
我发现the conditional clause iff
不支持相等性测试,例如
var f = new Function("f(x1, x2) = x1 + x2");
var g = new Function("g(x1, x2) = x1 * x2");
var k = new Function("k(x1, x2) = x1 - x2");
var h = new Function("h(x1, x2) = iff(x1 > x2, f(x1, x2); x1 < x2, g(x1, x2)); x1 == x2, k(x1, x2))", f, g, k);
e = new Expression("h(2, 1)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
e = new Expression("h(1, 2)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
e = new Expression("h(2, 2)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
这给了
[mXparser-v.5.0.2 bin NET6_0] Res: h(2, 1) = NaN
[mXparser-v.5.0.2 bin NET6_0] Res: h(1, 2) = NaN
[mXparser-v.5.0.2 bin NET6_0] Res: h(2, 2) = NaN
解决方法
我可以转向 if
子句来做类似
的事情
var h = new Function("h(x1, x2) = if(x1 == x2, k(x1, x2))", k);
但是使用单独的语句而不是集成表达式仍然很烦人。
问题
我是不是遗漏了什么或者这是设计使然?
当我 运行 下面的代码时似乎工作正常:
Expression e;
var f = new Function("f(x1, x2) = x1 + x2");
var g = new Function("g(x1, x2) = x1 * x2");
var k = new Function("k(x1, x2) = x1 - x2");
var h = new Function("h(x1, x2) = iff(x1 > x2, f(x1, x2); x1 < x2, g(x1, x2); x1 == x2, k(x1, x2))", f, g, k);
e = new Expression("h(2, 1)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
e = new Expression("h(1, 2)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
e = new Expression("h(2, 2)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
代码结果:
[mXparser-v.5.0.2 bin NET6_0] Res: h(2, 1) = 3
[mXparser-v.5.0.2 bin NET6_0] Res: h(1, 2) = 2
[mXparser-v.5.0.2 bin NET6_0] Res: h(2, 2) = 0
我在我的应用程序中使用 Java/.NET 符号数学库 mXparser
(5.0.2) 来支持用户提供的数学表达式。
问题
我发现the conditional clause iff
不支持相等性测试,例如
var f = new Function("f(x1, x2) = x1 + x2");
var g = new Function("g(x1, x2) = x1 * x2");
var k = new Function("k(x1, x2) = x1 - x2");
var h = new Function("h(x1, x2) = iff(x1 > x2, f(x1, x2); x1 < x2, g(x1, x2)); x1 == x2, k(x1, x2))", f, g, k);
e = new Expression("h(2, 1)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
e = new Expression("h(1, 2)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
e = new Expression("h(2, 2)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
这给了
[mXparser-v.5.0.2 bin NET6_0] Res: h(2, 1) = NaN
[mXparser-v.5.0.2 bin NET6_0] Res: h(1, 2) = NaN
[mXparser-v.5.0.2 bin NET6_0] Res: h(2, 2) = NaN
解决方法
我可以转向 if
子句来做类似
var h = new Function("h(x1, x2) = if(x1 == x2, k(x1, x2))", k);
但是使用单独的语句而不是集成表达式仍然很烦人。
问题
我是不是遗漏了什么或者这是设计使然?
当我 运行 下面的代码时似乎工作正常:
Expression e;
var f = new Function("f(x1, x2) = x1 + x2");
var g = new Function("g(x1, x2) = x1 * x2");
var k = new Function("k(x1, x2) = x1 - x2");
var h = new Function("h(x1, x2) = iff(x1 > x2, f(x1, x2); x1 < x2, g(x1, x2); x1 == x2, k(x1, x2))", f, g, k);
e = new Expression("h(2, 1)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
e = new Expression("h(1, 2)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
e = new Expression("h(2, 2)", h);
mXparser.consolePrintln($"Res: {e.getExpressionString()} = {e.calculate()}");
代码结果:
[mXparser-v.5.0.2 bin NET6_0] Res: h(2, 1) = 3
[mXparser-v.5.0.2 bin NET6_0] Res: h(1, 2) = 2
[mXparser-v.5.0.2 bin NET6_0] Res: h(2, 2) = 0