Systems of equations
solveSystem solves several equations in several unknowns: linear systems by elimination, letters and all, and nonlinear ones by substitution or by eliminating an unknown with the resultant. Every solution is put back into every equation.
Using it
import { solveSystem } from "@ratmath/symbolic";
String(solveSystem(["x + y = 3", "x - y = 1"]));
// x = 2, y = 1
String(solveSystem(["2x + 3y - z = 1", "x - y + 2z = 4", "3x + y + z = 5"]));
// x = 6/5, y = 0, z = 7/5
String(solveSystem(["a*x + y = 1", "x - y = 0"], ["x", "y"]));
// x = 1/(a + 1), y = 1/(a + 1)
// provided a + 1 != 0
String(solveSystem(["x + y + z = 1", "x - y = 0"]));
// x = 1/2 - z/2, y = 1/2 - z/2, z = z (z free)
String(solveSystem(["x + y = 1", "2x + 2y = 3"]));
// no solution
String(solveSystem(["x^2 + y^2 = 25", "x + y = 7"]));
// x = 3, y = 4
// x = 4, y = 3
The unknowns default to every letter in the equations, in alphabetical order; pass them as the second argument when some letters are parameters. The result has solutions (each with values, free and verified), complete, conditions, notes and steps.
Linear systems: Gauss–Jordan with letters
When every equation is linear in the unknowns, the system is the matrix equation and is reduced to reduced row echelon form. The entries are expressions, not just numbers, and each one is kept in normal form as the rows are combined. A pivot must not be zero:
- a number that is not zero is used first;
- otherwise an entry with letters that is not proven zero is used, and the condition that it is not zero is returned, unless the assumptions already prove it.
A row that ends as decides the rest. If is a nonzero number the system is inconsistent and has no solution. If has letters, the equations agree only when , and that is returned as a condition. Columns without a pivot are free: the unknowns there can take any value, and the others are written in terms of them. The number of free unknowns is the number of unknowns minus the rank.
The linear algebra library, linalg, solves systems with number entries much faster; this one is for coefficients with letters in them, which is where a first course spends its time (for which does the system have one solution, none, infinitely many?).
Nonlinear systems
Substitution. If some equation contains an unknown only to the first power, with free of , it is solved for and that is put into the other equations, leaving one unknown fewer. A number coefficient is preferred. When contains other unknowns, the case is solved separately, so that nothing is lost by dividing.
Elimination. When every equation is a polynomial, the resultant of two of them with respect to an unknown is a polynomial without that vanishes wherever both do. Eliminating one unknown at a time leaves one equation in one unknown, which solve handles; each value is then carried back and the next unknown found. A resultant can have roots that belong to no solution, which is one reason every candidate is checked at the end.
The check. Each candidate is substituted into every equation as written, and the difference of the two sides must reduce to 0 in normal form. A candidate that fails is dropped; one that cannot be decided is left out and noted, and the answer is marked incomplete.
What is not handled: values with no closed form (a root of a quintic) are noted and not followed, and a curve of solutions (two equations with a common factor) is reported as incomplete.
@ratmath/symbolic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.