Skip to content

Simplification

WhiteBlackGoose edited this page Feb 15, 2023 · 1 revision

Here we alternate the given expression into different possible forms.

Alternate

The main method for Simplification is method Alternate. It finds alternative forms of the given expression and returns a sorted IEnumerable, from the simplest to the most complicated. Example:

Entity expr = "sin(x / a) + cos(3) + sqrt(e - x)";
foreach (var alt in expr.Alternate(5))
    Console.WriteLine(alt);

Output:

sin(x / a) + cos(3) + sqrt(e - x)
cos(3) + sin(x / a) + sqrt(e - x)
sin(x / a) + cos(3) + sqrt(e - x)
cos(3) + sin(1 / a * x) + sqrt(e + -x)

The only argument of this method is responsible for the number of iterations the simplification will go through.

The measure of the complexity of an expression is Entity's property SimplifiedRate. It has a type of double. The higher its value is, the more complicated the expression is. That is the number by which method Alternate sorts expressions. Example:

Entity expr = "sin(x / a) + cos(3) + sqrt(e - x)";
foreach (var alt in expr.Alternate(5))
    Console.WriteLine(alt.SimplifiedRate);

Output:

37
37
37
49

The user's algorithms should not rely on the number, produced by SimplifiedRate, as it might vary from version to version.

Method Simplify simply returns the first expression of all those returned by Alternate, that is, the one with the lowest SimplifiedRate.

Critera of complexity and other methods

To change the critera by which an expression's complexity is computed, you may want to change setting MathS.Settings.ComplexityCriteria (dicussed in further chapters). The higher value for the given expression you return - the worse it appears for the AM's simplification algorithm.

Simplify is an active and expensive operation. Entity has also property InnerSimplified, which is usually called as "automatic simplification". Since there exists no automatic simplification in AM, you can address this property to get a naively simplified result. For example, it will cancel out zeros in sums, ones in products, and other basic operations. Example:

Entity expr1 = "a + 0";
Console.WriteLine(expr1.InnerSimplified); // a
Entity expr2 = "sin(x)2 + cos(x)2";
Console.WriteLine(expr2.InnerSimplified); // sin(x) ^ 2 + cos(x) ^ 2

As it could be seen, it will not simplify cases which require applying patterns. To simplify the latter case, call Simplify.

Same way as with Evaled, consider InnerSimplified as a free-to-call property.

Extension: string.Simplify().

Clone this wiki locally