Skip to content

Algebra

Arthur Guiot edited this page May 27, 2018 · 7 revisions

In order to create a function, you'll need to use the f function like that:

const f = t.f("x", "2*x+1") // => TheoremJS function object

You can also create function using real JS code:

const f = t.f(x => 2*x + 1) // => TheoremJS function object

You can "run" these functions (or polynomials) using run:

t.run(f, 5) // = 11

You can create polynomials using polynomial like that:

const poly1 = t.polynomial(2, 1) // f(x) = 2x + 1 => TheoremJS polynomial object
const poly2 = t.polynomial(3, 2, 1) // f(x) = 3x^2 + 2x + 1 => TheoremJS polynomial object

The following functions will only work for polynomials

Find Roots

You can find the roots of a polynomial using findRoots. It will return an array of Strings that you can parse using eval() in JavaScript to get the solution as a number.

Example:

t.findRoots(t.polynomial(1, -1, -1)) // => ['(1 + Math.sqrt(5)) / 2', '(1 - Math.sqrt(5)) / 2']

You can easily get the derivative of any polynomials using derivative:

t.derivate(t.polynomial(1, -1, -1)) // f(x) = 2x - 1 => TheoremJS polynomial object

You can get the slope of a function at any number x like that:

t.slope(t.f("x", "x ** 2"), 2) // 4 => BigNumber

Same as derivative but for integrals:

t.integrate(t.polynomial(1, -1, -1) // f(x) = x^3/3 - x^2/2 - x + 0 => TheoremJS polynomial object

You can get the gradient using 2 points like that:

t.gradient({
	2: 3
}, {
	6: 8
}) // => 1.25

Solve Equations (brute-force)

Will work for any functions You can solve an equation like f(x) = y using numeralSolve:

t.numeralSolve(t.f("x", "2*x+1"), 0) // => ['-0.5', 0] , Returns -0.5 with 0 error rate

You can choose the range by adding arguments like: ... from = -100, to = 100, step = 0.1

Get f(0)

t.y_intercept(t.f("x", "2*x+1")) // => 1