MathEngine.Algebra

The MathEngine.Algebra library contains the basic necessities required to perform algebraic computation, including expression & equation abstractions, polynomial representations, and the ability to solve select classes of polynomials.

Below are some code examples for the Algebra library and a list of the main types contained within the library.

Examples

Analyzing a Polynomial Expression

using MathEngine.Algebra;
using MathEngine.Algebra.Expressions.Polynomial;

var x = Variable.X;

var expr = PolynomialExpression.From(
	(x^2) + 2*x + 1 + (-4*x)
);

NormalizedPolynomialExpression normalized = expr.Normalize();

foreach (var term in normalized.NormalizedTerms)
{
	Console.WriteLine($"Degree of Term: {NormalizedPolynomialExpression.DegreeOfNormalizedTerm(term)}");
	Console.WriteLine($"Coefficient of Term: {NormalizedPolynomialExpression.CoefficientOfNormalizedTerm(term)}");
}

Solving a Polynomial Equation

using MathEngine.Algebra;
using MathEngine.Algebra.Solver.Polynomial;

var x = Variable.X;

var eq = ((x^2)+5*x+6).SetEqualTo(2*x+8).ToPolynomial();

var solns = eq.Solve(strategy: PolynomialSolvingStrategy.UseFormula).Select(soln => soln.Simplify()).Distinct();

foreach (var soln in solns)
{
	Console.WriteLine(soln.LaTeX());
}

Simplifying an Expression

using MathEngine.Algebra;
using MathEngine.Algebra.Expressions.Simplification;

var x = Variable.X;

var expr = (x^2) + 2*x + 1 + (-4*x)/2 - 3;

Console.WriteLine(expr);
Console.WriteLine(expr.Simplify(SimplificationStrategy.Default)); /* SimplificationStrategy.Default is the default/implicit parameter value, so it does not need to be passed. If you choose not to use SimplificationStrategy, you do not need to use the namespace MathEngine.Algebra.Expressions.Simplification either */

Substituting in for a Variable

using MathEngine.Algebra;
using MathEngine.Algebra.Expressions;

Variable h = new('h');

Expression expr = (h^4)+2*h+3;

Console.WriteLine(expr.Substitute(h, Expression.OneHalf).Simplify());
Console.WriteLine(expr.Substitute(Variable.T, Expression.OneHalf).Simplify()); /* does nothing because the variable 't' is not in the expression */

Main Types

Namespace MathEngine.Algebra.Expressions

Expression

The abstract base class for all mathematical expressions. Implements IEquatable<Expression>.

ValueExpression Expression.One

Represents the integral value 1.

ValueExpression Expression.Zero

Represents the integral value 0.

ValueExpression Expression.NegativeOne

Represents the integral value -1.

ValueExpression Expression.OneHalf

Represents the rational value 1/2.

ValueExpression Expression.PI

Represents the transcendental value π.

ValueExpression Expression.E

Represents the transcendental value e.

Expression Expression.Undefined

Represents an undefined expression.

string ToString()

Returns the string representation of this expression, grouped in order of mathematical precendence.

string LaTeX()

Returns the LaTeX markup that represents this expression.

string Repr()

Returns a string representation of this expression in a heirarchical view.

Expression Simplify(SimplificationStrategy strat = SimplificationStrategy.Default)

Simplifies this expression using the requested SimplificationStrategy. See SimplificationStrategy for more details on what each strategy accomplishes.

PowerExpression Square()

Returns the square of this expression (this expression raised to the power of two).

PowerExpression Sqrt()

Returns the square root of this expression.

PowerExpression Cube()

Returns the cube of this expression (this expression raised to the power of three).

PowerExpression Cbrt()

Returns the cube root of this expression.

PolynomialExpression ToPolynomial()

Attempts to convert this expression to a PolynomialExpression. May throw an ArgumentException on failure (if the expression could not be interpreted as a polynomial).

ValueExpression