A computer algebra system for JavaScript. Coffeequate

Algebra in the browser

No matter how hard you try, -webkit-calc() is not going to be able to solve an equation. Neither is JavaScript. That's where Coffeequate comes in. Coffeequate is a computer algebra system (CAS) for JavaScript. It can manipulate algebraic expressions, and solve and simplify equations.




To include Coffeequate on your page, just use a script tag: <script type="text/javascript" src="coffeequate.min.js"></script> You can also load it with an AMD loader like RequireJS. define(["lib/coffeequate.min"], function (CQ) {
// Use Coffeequate here!

If you really want, you can play around with Coffeequate in development by using this site's version of Coffeequate, CDN style. Obviously, this is not a good idea for production. <script src=""></script>

Using Coffeequate

Coffeequate gives you a CQ function (or coffeequate if you'd prefer). This function converts strings to Coffeequate expressions, which you can then operate on with methods. equ = CQ("Ek = 1/2 * m * v**2");
m = equ.solve("m"); // 2 * Ek * v**-2

expr = CQ("m * c**2");
expr = expr.sub({"m": CQ("F * a**-1"), "c": 10}); // 100 * F * a**-1
Valid terminals are variables, which are alphanumeric strings that begin with a letter; constants, which are numbers as digits which may include decimal points, or scientific notation (e.g. `2e-3`); or symbolic constants, which are variables preceded by a backslash. A list of known symbolic constants is located here. The methods of an expression are listed below.

equivalencies is an object mapping variable labels to an array of variable labels that that variable is equivalent to - for example, if x and y are equivalent, then you could have equivalencies = {"x":["x", "y"], "y":["x", "y"]}. Equivalencies is always an optional parameter.

If you find yourself in need of checking the type of a CQ expression node, or you want to monkeypatch CQ, the raw nodes are provided in CQ.raw: {Add, Mul, Pow, Terminal, Variable, Constant, SymbolicConstant, Uncertainty}.

Examples and REPL


Here's a JS REPL with Coffeequate v1.2.0 already loaded. Try it out!


Here are some examples of Coffeequate in use.

Finding freefall speed

kineticEnergy = CQ("Ek = 1/2 * m * v**2");
potentialEnergy = CQ("Ep = m * g * h");
// Substitute the potential energy into the kinetic energy.
// Then solve for v.
v = kineticEnergy.sub({Ek: potentialEnergy.solve("Ep")[0]}).solve("v");
v.toString(); // -1*sqrt(g*h*m)/sqrt(m/2), sqrt(g*h*m)/sqrt(m/2)

Finding freefall time

projectile = CQ("s = u * t + 1/2 * a * t**2");
freefall = projectile.sub({u: 0, a: CQ("g")});
time = freefall.solve("t");
time.toString(); // -1*sqrt(s)/sqrt(g/2),sqrt(s)/sqrt(g/2)

Time dilation

dilation = function(v) {
  var td = CQ("Δt * (1 + -v**2 * c**-2)**(-1/2)");
  return td.sub({"Δt": 1, "c": 3e8, "v": v});
dilation(10).toString(); // 1
dilation(1000000000).toString(); // 0.287348
dilation(CQ("v")).toString(); // 1 + 1.11111e-17*v**2**-1/2

Peak of projectile flight

s = CQ("u*t + 0.5*a*t**2"); // Vertical position as a function of time.
maxT = s.differentiate("t").solve("t"); // Differentiate and equate to zero to find times where s is maximal or minimal.
s.sub({"t":maxT[0]}).toString(); // -((0.5*u**2)/a)
This project is maintained by Matthew Alger and Buck Shlegeris.