Simplifying and evaluating mathematical strings

158 Views Asked by At

What would be the simplest way to evaluate/simplify a mathematical string in VB?

For example: "k*k+(5+2*5)k+k" would simplify to "k^2+15k+k" and "5^2+3" would evaluate to 28.

To evaluate, I'm using NCalc by simply using the evaluate function, but it doesn't simplify expressions. What would be the simplest way of simplifying the equations?

1

There are 1 best solutions below

1
On BEST ANSWER

You could try Math.NET Symbolics.

It doesn't "know" about the implied multiplication for (a)b, so you would have to work out how to insert an * if you can't require it.

Imports MathNet.Symbolics
Imports Expr = MathNet.Symbolics.SymbolicExpression

Module Module1

    Sub Main()
        Dim a = Expr.Parse("k*k+(5+2*5)*k+k")
        Console.WriteLine(a.ToString())
        Console.WriteLine(Expr.Parse("5^2+3"))

        Console.ReadLine()

    End Sub

End Module

Outputs:

16*k + k^2
28