Skip to content

Solvers

WhiteBlackGoose edited this page Feb 15, 2023 · 1 revision

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:

((-oo; -sqrt(24) / 4) \/ (sqrt(24) / 4; +oo)) /\ (0; +oo)
(-oo; -sqrt(24) / 4) \/ (sqrt(24) / 4; +oo) \/ (0; +oo)

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:

Matrix[2 x 2]
(1 - sqrt(-11)) / (-2)          -((1 - sqrt(-11)) / (-2)) ^ 2
(1 + sqrt(-11)) / (-2)          -((1 + sqrt(-11)) / (-2)) ^ 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.

Extensions: (string, string).Solve(Variable, Variable), (string, string, string).Solve(Variable, Variable, Variable), etc.

Clone this wiki locally