AngouriMath

Navigation

Solvers


← Back to the main page

Here we shall review different ways to solve equations, inequalities, statements, systems.

Solving a classic equation

The method to solve a classic equation over a single variable in AM is SolveEquation. Since an equation generally has a form of f(x) = g(x), the method SolveEquation expects your expression to be f(x) - g(x), that is, it should be a function. Example:
Entity expr = "2sin(a x) - b";
Console.WriteLine(expr.SolveEquation("x"));
Output:
{ (arcsin(b / 2) + 2 * pi * n_1) / a, (pi - arcsin(b / 2) + 2 * pi * n_1) / a }
Nonetheless, AM allows you to solve Statements, be that equalities, inequalities, and more advanced cases. Extension: string.SolveEquation(Variable).

Solving a statement

A Statement is an expression, that is considered to be true or false. For example, a and b is a statement, as well as x2 = 4, or x > a and x2 = b. Let us start from a simple example:
Entity expr = "x2 = 16";
Console.WriteLine(expr.Solve("x"));
Output:
{ 4, -4 }
Here is a more advanced example:
Entity expr = "x4 = 16 and x in RR and x > 0";
Console.WriteLine(expr.Solve("x"));
Output:
{ 2 }
Adding another condition:
Entity expr = "x4 = 16 and x in RR and x > 0 or x^a = 6";
Console.WriteLine(expr.Solve("x"));
Output:
{ 2, 6 ^ (1 / a) }
Example with inequalities:
Entity expr = "2x2 - 3 > 0 and x > 0";
Console.WriteLine(expr.Solve("x").Simplify());
Entity expr2 = "2x2 - 3 > 0 or x > 0";
Console.WriteLine(expr2.Solve("x").Simplify());
Output:
(sqrt(6) / 2; +oo)
(-oo; -1/2 * sqrt(6)) \/ (sqrt(6) / 2; +oo) \/ (0; +oo)
The endpoints of a quadratic inequality are written as |a|, which is the form that is right for either sign of a symbolic coefficient. Where the coefficient is concrete, as here, the absolute value folds away. Extension: string.Solve(Variable). The returned value is a Set, which could be of four types: FiniteSet, Interval, SpecialSet, ConditionalSet. Let us consider a basic example:
Entity expr = "a x2 + b x + c = 0";
var solutions = expr.Solve("x");
if (solutions is Entity.Set.FiniteSet finiteSet)
{
    foreach (var root in finiteSet)
        Console.WriteLine($"Root: {root}");
}
Output:
Root: (-b - sqrt(b ^ 2 - 4 * a * c)) / (2 * a)
Root: (-b + sqrt(b ^ 2 - 4 * a * c)) / (2 * a)

Solving system of equations

Every equation of the system should be written as a simple equation (as in the first part of the article). The method of MathS named Equations:
var system = Equations(
    "x2 + a y3",
    "y - x - b"
);
This creates an instance of EquationSystem. Let us print it out:
Console.WriteLine(system);
Output:
x ^ 2 + a * y ^ 3 = 0
y - x - b = 0
The method Solve() of instance EquationSystem takes variables in the same order, as their values are then written to a solution matrix. Let us consider a very simple example:
var system = Equations(
    "x2 + y",
    "y - x - 3"
);
Console.WriteLine(system.Solve("x", "y"));
Output:
[[-(-(5 - sqrt(-11)) / 2 + 3), (5 - sqrt(-11)) / 2], [-(-(5 + sqrt(-11)) / 2 + 3), (5 + sqrt(-11)) / 2]]
The first (left) column are values for x, the right one for y. The first row is the first solution set, the second row is the second one. Every entry here is x = (-1 ± sqrt(-11)) / 2 and y = x + 3 written the long way round. ToString(multilineFormat: true) lays the matrix out as a grid, which is easier to read:
var system = Equations(
    "x2 + y",
    "y - x - 3"
);
var solutions = (Entity.Matrix)system.Solve("x", "y");
Console.WriteLine(solutions.ToString(multilineFormat: true));
Output:
Matrix[2 x 2]
-(-(5 - sqrt(-11)) / 2 + 3)   (5 - sqrt(-11)) / 2           
-(-(5 + sqrt(-11)) / 2 + 3)   (5 + sqrt(-11)) / 2
Simplify() on the matrix does not shorten these, because Simplify rewrites the node it is given and not the entries inside it — #882. Simplifying an entry on its own does work, so read the entries out and simplify each. Extensions: (string, string).Solve(Variable, Variable), (string, string, string).Solve(Variable, Variable, Variable), etc.























Angouri © 2019-2023 · Project's repo · Site's repo · Octicons · Transparency · 1953 pages online