-
Notifications
You must be signed in to change notification settings - Fork 79
Simplification
Here we alternate the given expression into different possible forms.
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) + sqrt(e - x) + sin(x / a)
cos(3) + sin(x / a) + 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.
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) ^ 2As 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().
A rewrite that removes a singularity carries a condition, so that the answer does not claim a
value the input never had. x/x has no value at zero; 1 has one; so the simplified form says
where it stands for the input:
Entity expr = "x / x";
Console.WriteLine(expr.Simplify());Output:
1 provided not x = 0
a provided c is a where c holds and has no value where it does not. A condition the answer
already states on its own is not written twice: the derivative of x ^ x is 0 ^ 0 at zero and
the derivative of ln(x) / x is a quotient by x ^ 2, and each excludes zero by its own domain
condition, so a provided not x = 0 beside either says nothing and is dropped.
Entity expr = "x ^ x";
Console.WriteLine(expr.Differentiate("x").Simplify());
Entity expr2 = "ln(x) / x";
Console.WriteLine(expr2.Differentiate("x").Simplify());Output:
(1 + ln(x)) * x ^ x
(1 - ln(x)) / x ^ 2
What decides it is the expression's own domain condition, read in the codomain the question is
asked in (Entity.DomainConditionIn, under MathS.Settings.Codomain): a quotient accepts a
nonzero divisor, a power a nonzero base or a positive exponent, a tangent an argument whose
cosine is nonzero, and so on. A condition that only excludes points that domain condition
already excludes is redundant, whatever shape the expression has:
Entity expr = "tan(x) provided not cos(x) = 0";
Console.WriteLine(expr.Simplify());Output:
tan(x)
A condition the expression does not state stays — 1 / (1 + x ^ 2) provided not x = 0 keeps
its clause, since the quotient is defined at zero and the condition takes that point away.
If you did not find what you were looking for, feel free to create an issue raising your problem.