Solving

Solving equations, and checking the answers

The methods are the ones taught: factor, undo a function, square both sides, substitute. What differs is the last step. Every candidate is put back into the equation as it was written, and the answer says which candidates passed, which failed and why, and whether the list is known to be complete.

One call

import { solve } from "@ratmath/symbolic";

const s = solve("sqrt(x + 3) = x - 3");
String(s);        // x = 6
                  //   rejected x = 1: the two sides differ there by about 4
s.solutions;      // [ Solution ]   each with value, kind, verified
s.rejected;       // [ { value: 1, reason: "the two sides differ there by about 4" } ]
s.complete;       // true: there are no other real solutions
s.steps;          // [ { rule: "radicals eliminated", detail: "… leads to x^2 - 7*x + 6 = 0, whose roots are candidates only" } ]

An equation is text with one =; an expression alone means "equals zero". The variable defaults to x, and any other letters are treated as parameters.

Why candidates must be checked

Many steps of algebra are not reversible. Squaring both sides of x+3=x−3\sqrt{x+3} = x - 3 gives x+3=(x−3)2x + 3 = (x-3)^2, that is x2−7x+6=0x^2 - 7x + 6 = 0, with roots 1 and 6. But squaring also keeps the solutions of x+3=−(x−3)\sqrt{x+3} = -(x-3), and x=1x = 1 is one of those. In the same way, multiplying by a denominator can add a root where the denominator is zero, and combining logarithms can add one where an argument is negative.

So the roots of the simplified equation are only candidates. Each is substituted into the original equation. It is accepted as "proven" when the difference of the two sides reduces to zero in the normal form, and rejected when a certified evaluation shows the two sides apart or one side undefined. A candidate that can be neither proven nor refuted is kept and marked "enclosed": the two sides agree to the precision tried, which is not a proof and is not presented as one.

What it solves

String(solve("x^2 - 2x - 1 = 0"));            // x = 1 - sqrt(2),  sqrt(2) + 1
String(solve("abs(2x - 1) = 5"));             // x = -2,  3
String(solve("exp(2x) - 3exp(x) + 2 = 0"));   // x = 0,  log(2)
String(solve("log(x) + log(x - 1) = log(6)"));
// x = 3
//   rejected x = -2: a side of the equation is undefined there
String(solve("x^2 + 1 = 0"));                 // no real solution
String(solve("(x + 1)^2 = x^2 + 2x + 1"));    // every x where the equation is defined
String(solve("abs(x) = x"));                  // x in [0, inf)

The solver works over the real numbers. It brings everything to one side, takes the normal form, factors the numerator and solves each factor: linear and quadratic factors by formula, a function of the unknown by undoing it (exponentials, logarithms, roots, powers, absolute values, the trigonometric functions), a polynomial in one such function by substitution. Radicals and absolute values that cannot be undone directly are eliminated with resultants, which is the systematic form of "isolate the root and square".

Roots with no formula

A polynomial of degree five or more usually has no solution in radicals, and a cubic's formula is rarely what anyone wants. Such roots are returned as named algebraic numbers: exact objects, each known by its polynomial and an isolating interval that can be narrowed as far as you like.

const c = solve("x^3 - x - 1 = 0");
String(c);                 // x = alpha1
                           //   alpha1 ≈ 1.3247179572…: a root of x^3 - x - 1 = 0
c.solutions[0].kind;       // "algebraic"
c.solutions[0].verified;   // "proven"
c.solutions[0].enclosure(60);   // a RationalInterval of width below 2^-60
c.complete;                // true: Sturm's theorem counts the real roots, so none is missing

Where no method applies at all, as for cos⁡x=x\cos x = x, solve says so. Give it an interval and it searches for certified sign changes using analytic; those solutions have kind "numeric" and complete is false, because a root where the function touches zero without crossing would be missed.

String(solve("cos(x) = x"));
// no solution found (the search was not complete)
String(solve("cos(x) = x", "x", { interval: [0, 2] }));
// one solution near 0.7390851332, verified by a sign change

Families of solutions

A trigonometric equation has infinitely many solutions, in families one period apart. The answer gives each family with the whole number nn; member(n) picks one out, and a range lists the members that fall inside it.

const t = solve("2sin(x) = 1");
String(t);                      // x = pi/6 + 2*n*pi,  5*pi/6 + 2*n*pi   (n any whole number)
String(t.solutions[0].member(1));   // 13*pi/6
String(t.solutions[0].period);      // 2*pi

String(solve("sin(2x) = sin(x)", "x", { range: [0, "2*pi"] }));
// x = 0,  pi/3,  pi,  5*pi/3,  2*pi

Parameters

With other letters in the equation, the solution is a formula together with the conditions under which it holds.

String(solve("a*x + b = c", "x"));
// x = (c - b)/a
//   provided a != 0
String(solve("a*x^2 + b*x + c = 0", "x"));
// x = (-sqrt(b^2 - 4*a*c) - b)/(2*a),  (sqrt(b^2 - 4*a*c) - b)/(2*a)
//   provided a != 0 and b^2 - 4*a*c >= 0

Inequalities have their own page.

Try itEdit the code and run it with @ratmath/symbolic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs