Algebra.js Symbolic Equation Solver
Algebra.js is a JavaScript library designed for building, displaying, and solving algebraic equations. As of version 0.2.6, it provides core functionalities for creating `Fraction` objects for precise arithmetic, `Expression` objects to represent algebraic expressions, and `Equation` objects to encapsulate equalities. A key differentiator is its ability to parse string representations of expressions and equations, enabling convenient construction from human-readable input. The library also includes methods for symbolic simplification, evaluation, and solving linear, quadratic, cubic, and quartic equations, along with a `toTex()` method for LaTeX output. It primarily targets Node.js and browser environments using CommonJS module syntax. The release cadence is infrequent, with recent updates focusing on bug fixes and stability within the 0.2.x series.
Warnings
- gotcha In versions prior to 0.2.0, solving single-variable equations with an infinite number of solutions would return `undefined`. This behavior changed in 0.2.0 to return `Fraction(1, 1)`.
- gotcha Versions prior to 0.2.4 contained a bug where solving quadratic equations with irrational solutions when the 'a' coefficient was greater than 1 yielded incorrect results. Relying on such solutions in older versions would have produced wrong output.
- gotcha The parser in versions prior to 0.2.2 incorrectly handled negative numbers (throwing an error) and misinterpreted adjacent parentheses. Code relying on previous buggy parser behavior or error handling for these cases will need adjustment.
- gotcha `Expression.simplify()` in versions prior to 0.2.1 would sometimes fail to fully simplify expressions consisting only of unsimplified constants, reducing them to two constants instead of a single one. This could lead to unexpected unsimplified numerical results.
Install
-
npm install algebra.js -
yarn add algebra.js -
pnpm add algebra.js
Imports
- algebra
import algebra from 'algebra.js';
const algebra = require('algebra.js'); - Expression
const { Expression } = require('algebra.js'); // or import { Expression } from 'algebra.js';const algebra = require('algebra.js'); const expr = new algebra.Expression('x'); - Equation
const { Equation } = require('algebra.js'); // or import { Equation } from 'algebra.js';const algebra = require('algebra.js'); const eq = new algebra.Equation(expr, 4); - Fraction
const { Fraction } = require('algebra.js'); // or import { Fraction } from 'algebra.js';const algebra = require('algebra.js'); const frac = new algebra.Fraction(1, 2); - parse
const { parse } = require('algebra.js'); // or import { parse } from 'algebra.js';const algebra = require('algebra.js'); const exp = algebra.parse("x^2 + 4 * x + 4");
Quickstart
const algebra = require('algebra.js');
// Create an expression
let expr = new algebra.Expression("x");
expr = expr.subtract(3);
expr = expr.add("x");
console.log(expr.toString()); // Expected: 2x - 3
// Create an equation from the expression
const eq = new algebra.Equation(expr, 4);
console.log(eq.toString()); // Expected: 2x - 3 = 4
// Solve the equation for 'x'
const x = eq.solveFor("x");
console.log("x = " + x.toString()); // Expected: x = 7/2
// Use the parser to create an equation directly from a string
const quadraticEq = algebra.parse("x^2 + 4 * x + 4 = 0");
console.log(quadraticEq.toString()); // Expected: x^2 + 4x + 4 = 0