Get started

Install & first steps

Install the package, import nerdamer, and pass it a formula as a string.

Install

npm install nerdamer

Your first expression

import nerdamer from 'nerdamer';

const expression = nerdamer('x + 2*x + 1');
console.log(expression.text());
// 1+3*x

The string is Nerdamer Notation. The result is an expression you can simplify, evaluate, format or use in another calculation.

The string is read as mathematics, not run as JavaScript. To get a fast numeric JavaScript function from an expression, use buildFunction().

Use functions in the string

const result = nerdamer('factor(x^4-1)');
console.log(result.text());
// (1+x)*(-1+x)*(1+x^2)

factor here is part of the formula, not a JavaScript call. The function reference lists every function you can write this way.

Or call them from JavaScript

import nerdamer from 'nerdamer';

console.log(nerdamer.factor('x^4-1').text());
// (1+x)*(-1+x)*(1+x^2)

Most functions are also methods on nerdamer, such as nerdamer.diff and nerdamer.solve. They give the same result as writing them in the string.

Substitute values

const result = nerdamer('x^2 + y', {
  x: 3,
  y: 't'
});

console.log(result.text());
// 9+t

To substitute inside a formula instead, use subst.

Import functions and classes

import { Matrix, determinant } from 'nerdamer/structures';

const M = new Matrix(
  [1, 2],
  ['x', 'y']
);

console.log(determinant(M).text());
// y-2*x

Import what you need when your data is already in JavaScript, or when you want TypeScript types. Ways to use Nerdamer compares the options.

CommonJS

const nerdamer = require('nerdamer');

console.log(nerdamer('expand((x+1)^3)').text());
// 1+3*x+3*x^2+x^3

Try it in the browser

The Playground runs Nerdamer in your browser, with no install. It shows the result as text and TeX, and as an expression tree.