School algebra

School algebra, checked

Five rewrites that a course does by hand: completing the square, composing functions, reading a sinusoid, implicit differentiation and differentiating an integral with variable limits. Each follows the method of the course and then checks its answer with the normal form.

Using it

import { completeSquare, compose, sinusoid, implicitDiff, leibniz } from "@ratmath/symbolic";

String(completeSquare("x^2 - 8x + 11").form);      // (x - 4)^2 - 5, vertex (4, -5)
String(compose("2x - 3", "x^2 + 1").value);        // 2*x^2 - 1
compose("sqrt(x)", "x - 3").note;                  // defined only where x - 3 >= 0: x in [3, inf)
const s = sinusoid("3sin(2x - pi/2) + 1");
[s.amplitude, s.period, s.phaseShift, s.midline].map(String);   // ["3", "pi", "pi/4", "1"]
String(sinusoid("sin(x) + cos(x)").form);          // sqrt(2)*sin(x + pi/4)
implicitDiff("x^2 + y^2 = 25").at({ x: 3, y: 4 }).first;   // -3/4
String(leibniz("cos(t)", "t", "0", "x^2").value);  // 2*x*cos(x^2)

Completing the square

For ax2+bx+cax^2 + bx + c the vertex is at h=−b2ah = -\tfrac{b}{2a}, and k=c−b24ak = c - \tfrac{b^2}{4a} is the value there:

ax2+bx+c=a(x+b2a)2+c−b24a. ax^2 + bx + c = a\Bigl(x + \frac{b}{2a}\Bigr)^2 + c - \frac{b^2}{4a}.

completeSquare reads aa, bb, cc from the derivatives at 0 (so the input can be written any way), builds the form and expands it back to prove it equal. The coefficients can contain letters: completeSquare("x^2 + b*x + c") is (x+b/2)2+(c−b2/4)(x + b/2)^2 + (c - b^2/4).

Composition and its domain

f∘gf \circ g puts gg in for the variable of ff. It is defined where gg is defined and g(x)g(x) is in the domain of ff. compose collects the conditions of ff (a square root needs its argument ≥0\ge 0, a logarithm >0> 0, a quotient a nonzero denominator), writes them in terms of gg, and solves them with solveInequality. When that set is smaller than the domain of gg, the note says so.

Sinusoids

For y=Asin⁡(Bx+C)+Dy = A\sin(Bx + C) + D: the amplitude is ∣A∣|A|, the period 2π/∣B∣2\pi/|B|, the midline y=Dy = D, and the phase shift −C/B-C/B (the graph of Asin⁡BxA\sin Bx moved right by that much). sinusoid also takes a sum of sines and cosines of one frequency and writes it as one sine:

Psin⁡Bx+Qcos⁡Bx=Rsin⁡(Bx+φ),R=P2+Q2,Rcos⁡φ=P,Rsin⁡φ=Q. P\sin Bx + Q\cos Bx = R\sin(Bx + \varphi), \qquad R = \sqrt{P^2 + Q^2},\quad R\cos\varphi = P,\quad R\sin\varphi = Q.

The angle is arctan⁡(Q/P)\arctan(Q/P), plus π\pi when P<0P < 0, and becomes an exact multiple of π\pi at the standard values. The identity is proven through cos⁡(arctan⁡w)=1/1+w2\cos(\arctan w) = 1/\sqrt{1 + w^2}, which the normal form knows.

Implicit differentiation

Differentiate F(x,y)=0F(x, y) = 0 with yy a function of xx: Fx+Fy y′=0F_x + F_y\,y' = 0, so y′=−Fx/Fyy' = -F_x/F_y wherever Fy≠0F_y \ne 0. With { order: 2 }, y′′=∂xy′+∂yy′⋅y′y'' = \partial_x y' + \partial_y y' \cdot y'. at(point) checks that an exact point is on the curve before evaluating.

The Leibniz rule

ddx∫a(x)b(x)f(x,t) dt=f(x,b(x)) b′(x)−f(x,a(x)) a′(x)+∫a(x)b(x)∂f∂x dt. \frac{d}{dx}\int_{a(x)}^{b(x)} f(x, t)\,dt = f\bigl(x, b(x)\bigr)\,b'(x) - f\bigl(x, a(x)\bigr)\,a'(x) + \int_{a(x)}^{b(x)} \frac{\partial f}{\partial x}\,dt.

leibniz gives the boundary terms, and the last integral when ff depends on xx: in closed form when integrate finds it, otherwise as the integrand left to do. When ff does not depend on xx the answer is compared with integrating first and differentiating.

ExportReturns
completeSquare(expr, x){ form, a, h, k, vertex, verified }
compose(f, g, x){ value, unsimplified, domain, note }
sinusoid(expr, x){ fn, amplitude, A, B, C, D, period, frequency, phaseShift, midline, form, verified }
implicitDiff(F, x, y, { order }){ first, second, steps, at(point) }
leibniz(f, t, lower, upper, x){ value, boundary, integral, steps }
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