AngouriMath
ComplexityCriteria
Property
Summary
Criteria for simplifier so you could control which expressions are considered easier by you.
The higher the value - the more complicated an expression is.
The higher the value - the more complicated an expression is.
Example
Consider a situation, when we simplify this simple expression:
The output is
Assume we now hate division operators. For simplicity, our new complexity criteria
equals to how many division operators are there. Basically, the more of them there are in
expression, the more complicated it is. Now we add it:
The output:
Note that in the second case, this expression is simpler, because our complexity
criteria gets lower when there's fewer division operators. Let's check the
property SimplifiedRate:
Prints
By default criteria it cannot simplify it further, however, the custom one
it simplified from 2 to 1.
using System;
using AngouriMath.Extensions;
Console.WriteLine("a / b + b / c".ToEntity().Simplify());The output is
a / b + b / cAssume we now hate division operators. For simplicity, our new complexity criteria
equals to how many division operators are there. Basically, the more of them there are in
expression, the more complicated it is. Now we add it:
using System;
using System.Linq;
using AngouriMath;
using AngouriMath.Extensions;
using static AngouriMath.MathS;
Console.WriteLine("a / b + b / c".ToEntity().Simplify());
using var _ = Settings.ComplexityCriteria.Set(
expr => expr.Nodes.Count(node => node is Entity.Divf)
);
Console.WriteLine("a / b + b / c".ToEntity().Simplify());The output:
a / b + b / c
(a * c + b ^ 2) / (b * c)Note that in the second case, this expression is simpler, because our complexity
criteria gets lower when there's fewer division operators. Let's check the
property SimplifiedRate:
using System;
using System.Linq;
using AngouriMath;
using AngouriMath.Extensions;
using static AngouriMath.MathS;
Console.WriteLine("a / b + b / c".ToEntity().SimplifiedRate);
Console.WriteLine("a / b + b / c".ToEntity().Simplify().SimplifiedRate);
using var _ = Settings.ComplexityCriteria.Set(
expr => expr.Nodes.Count(node => node is Entity.Divf)
);
Console.WriteLine("a / b + b / c".ToEntity().SimplifiedRate);
Console.WriteLine("a / b + b / c".ToEntity().Simplify().SimplifiedRate);Prints
24
24
2
1By default criteria it cannot simplify it further, however, the custom one
it simplified from 2 to 1.
Remarks
alternatives beside it — SmallestTree,
FewestDivisions, FewestRadicals.
It is referenced rather than repeated so the setting's default and the model
cannot drift apart.
its text and SimplifiedRate cached one rate per instance —
so the number computed under the default criteria was handed back under this one.
That is fixed: a rate is cached only while nobody has scoped this setting, and a
scoped criteria is computed afresh. Reading a rate under a criteria of your own no
longer needs the parser cache defeated to be right.
Angouri © 2019-2023 · Project's repo · Site's repo · Octicons · Transparency · 4401 pages online