Home > OS >  mXparser's iff() conditional does not support equality test
mXparser's iff() conditional does not support equality test

Time:05-04

I'm using the Java/.NET symbolic math library mXparser (5.0.2) to support user-provided math expressions in my app.

Problem

I found that the conditional clause iff does not support equality test, e.g.,

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()}");

This gives

[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

Workaround

I could turn to if clause to do something like

var h = new Function("h(x1, x2) = if(x1 == x2, k(x1, x2))", k);

But it's still annoying to use separate statements instead of an integrated expression.

Question

Am I missing something or is this by design?

CodePudding user response:

Seems to work properly when I run below code:

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()}");

Code result:

[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
  • Related