Inequalities

Inequalities: the set where it holds

The solution of an inequality is a set of numbers, usually a few intervals. solveInequality finds the points where the answer can change, tests one point in each piece between them, and returns the set with its ends exact.

Using it

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

const s = solveInequality("(x - 1)/(x + 2) <= 0");
String(s);             // (-2, 1]
s.pieces;              // [ { lo: -2, hi: 1, loClosed: false, hiClosed: true } ]
s.contains(0);         // true
s.contains(-2);        // false: the quotient is undefined there
s.toRegionText();      // "-2:!x:1"   Core's region notation

String(solveInequality("x^2 > 2"));          // (-inf, -sqrt(2)) U (sqrt(2), inf)
String(solveInequality("abs(x - 3) < 2"));   // (1, 5)
String(solveInequality("x^3 - x >= 0"));     // [-1, 0] U [1, inf)
String(solveInequality("1/x < 1"));          // (-inf, 0) U (1, inf)
String(solveInequality("sqrt(x) < x - 2"));  // (4, inf)
String(solveInequality("x^2 < -1"));         // no real number

The comparison is one of <, <=, >, >= and !=. A chain such as -3 < 2x + 1 <= 7 is read as both inequalities at once, the intersection of the two sets.

Combining sets

const a = solveInequality("x^2 < 4"), b = solveInequality("x >= 1");
String(a.intersect(b));    // [1, 2)
String(a.union(b));        // (-2, inf)
String(a.complement());    // (-inf, -2] U [2, inf)
String(solveInequality("-3 < 2x + 1 <= 7"));   // (-2, 3]

intersect, union and complement put every end of both sets in order (comparing them exactly, so 2\sqrt2 and 9970\tfrac{99}{70} are told apart and 8/2\sqrt8/2 and 2\sqrt2 are proven equal) and decide each end and each gap between ends separately. No end falls inside a gap, so a gap is wholly inside a set or wholly outside it.

The method of critical points

Write the inequality as h(x)<0h(x) < 0 with h=f−gh = f - g. A function built from the usual pieces can change sign only where it is zero or where it stops being continuous or defined. Those are the critical points: the solutions of h(x)=0h(x) = 0, the zeros of every denominator, and the ends of the domains of roots and logarithms. Between two neighbouring critical points hh keeps one sign, so one test point decides the whole piece.

For x−1x+2≤0\dfrac{x-1}{x+2} \le 0 the critical points are −2-2 (denominator) and 11 (zero). The three pieces give

PieceTest pointValueKept?
x<−2x < -2−3-344no
−2<x<1-2 < x < 100−1/2-1/2yes
x>1x > 1221/41/4no

and then each critical point is tested on its own: x=1x = 1 gives 0, which satisfies ≤\le, and x=−2x = -2 is not in the domain. The answer is (−2,1](-2, 1].

This is why a common shortcut fails. Multiplying 1/x<11/x < 1 by xx to get 1<x1 < x loses the negative numbers, because multiplying by a negative quantity reverses an inequality. The method of critical points never multiplies by anything whose sign is unknown.

Where the proof comes from

The critical points come from solve, and the method is only as good as that list: a missed zero would merge two pieces that should be separate. So solveInequality refuses to answer when the equation's solution is not known to be complete. Signs at test points are decided by certified evaluation at exact rational points, so a sign is never read off a rounded number.

Periodic inequalities

For an inequality with infinitely many pieces, give a range.

String(solveInequality("sin(x) > 1/2", "x", { range: [0, "2*pi"] }));   // (pi/6, 5*pi/6)
solveInequality("sin(x) > 1/2");
// refuses: the inequality is periodic: give a bounded range for x, such as { range: [0, "2*pi"] }
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