1

I'm looking for a method in Math.Net Symbolics that can eliminate terms that are not needed for UI purposes.

Since these expressions are generated automatically I can't control some of the artifacts it creates.

I noticed that Expand() flattens the expression tree which helps since my expressions tend to get pretty deep, but once I reach the expression in the title I can't find any other method to eliminate redundant operations such as "0 + ", "^ 1.0", "1 * x", "0 * x", "^ 0" etc...

If there's no method in Math.net Symbolics to do this, is it possible to extend it to implement such functionality?

Edit: Should have added an example (thank's Xerillio for the tip about floating numbers):

using Expr = MathNet.Symbolics.SymbolicExpression;
public static void Main()
{
    var c = (Expr)(double)0;
    var a = (Expr)(double)1;
    var x = Expr.Variable("X1");
    var ee = c + a * x.Pow(a);
    
    Console.WriteLine(ee);
}

With that example the result would be "0.0 + 1.0*X1^1.0", but using integers instead it becomes "X1" which was what I needed.

Thank you again Xerillio.

blit
  • 396
  • 5
  • 12
  • 1
    What's the expression you're starting with? I think the problem is you have floating point numbers, which are not treated as integers (i.e. `1.0 != 1`). See [this fiddle](https://dotnetfiddle.net/XrCIDV) as an example. – Xerillio Apr 12 '21 at 20:54
  • You are correct, I use doubles to hold constants so this example ends up as "0.0 + 1.0*X1^1.0": var c = (MathNet.Symbolics.SymbolicExpression)((double)0); var a = (MathNet.Symbolics.SymbolicExpression)((double)1); var x = MathNet.Symbolics.SymbolicExpression.Variable($"X1"); var ee = c + a * x.Pow(a); If I instead remove those casts it becomes "X1", therefore that's the issue. I may be able to use that to workaround it. – blit Apr 13 '21 at 16:45

0 Answers0