asc-community/AngouriMath

Logical calculation

Closed this issue · 4 comments

hi.In my project how many parameters are deciding by user And it will use Logical calculation
just like "a+b>0 and c<0 or d>5 and not e".
In example i found that
Entity expr = "a and x > 3";
var func = expr.Compile<bool, double, bool>("a", "x");
WriteLine(func(true, 6));
but if i have more than 8 parameters ,what should I do?

Hi, you need to use a bit lower level API. Here what API for three parameters looks like. Here's the public API you need:

using AngouriMath.Core.Compilation.IntoLinq;

var del = expr.Compile<Func<int, double, bool>>(new(), typeof(bool), new [] { (typeof(int), x1), (typeof(double), x2) });

But the number of parameters are design by users.So the method of expr.Compile<Func>(.....) is hard to use in my case.....is any other way else?

I see.

1. Reflection (easy way)

You (theoreticall) can construct type Func on the fly, using typeof(Func<>) and method MakeGeneric, and then invoking method Compile using reflection too

2. Rewriting the compiler (hard way)

There's also old compiler, which is as simple as

myExpr.Compile("a", "b", "c")

It returns FastExpression type instead of a delegate. It isn't as fast as delegate, but its main problem is that it's far less feature-rich (here is the interpreter, here is the compiler).

And becuse it's not as feature rich, to make it work you will have to fork AngouriMath and

  1. Add bool operators to the compiler and interpreter (it's not too hard, changes are expected here, here, and here)
  2. Make FastExpression.Substitute return a bool (or make SubstituteBool or something)

thanks a lot. I will try it later。