Get started

Ways to use Nerdamer

You can write mathematics as text (Notation), write small programs in that text (Scripting), or call Nerdamer from JavaScript or TypeScript. They work together: most apps use Notation inside JavaScript.

Nerdamer Notation

Write the formula as a string. Functions in the string, such as factor, are part of the formula.

import nerdamer from 'nerdamer';

const result = nerdamer('factor(x^2-1)');
// (1+x)*(-1+x)

This fits input that arrives as text: a formula typed by a user or loaded from a file. Nerdamer reads it as mathematics and never runs it as JavaScript. You should still catch errors from invalid input, and limit how much work you accept from users.

See Writing expressions and the function reference.

Nerdamer Scripting

Use Scripting when one formula isn't enough: you need variables, your own functions, conditions or loops.

const result = nerdamer(`
f(x):=x^2+1;
a:2;
if(a>0, f(a), 0)
`);
// 5

These statements are Nerdamer Scripting, not JavaScript. See Scripting and User-defined functions.

JavaScript / TypeScript

There are two ways to call Nerdamer's functions from code.

Methods on nerdamer

nerdamer is a function that reads Notation, and it also has methods for most operations.

import nerdamer from 'nerdamer';

const factored = nerdamer.factor('x^2-1');
const tex = nerdamer.convertToTeX(nerdamer('x^2+1'));   // x^{2}+1

const f = nerdamer('8*cos(x)').buildFunction(['x']);   // f(0) is 8

nerdamer.setFunction('f', ['x'], 'x^2+1');

buildFunction() turns an expression into a JavaScript function for fast numeric evaluation. It works on a single expression; on a vector, matrix, equation or set it throws an error.

See The nerdamer object.

Importing functions and classes

import { factor } from 'nerdamer/algebra';
import { Matrix } from 'nerdamer/structures';

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

Import a function, class or TypeScript type by itself. Types & import paths lists what each path provides.

Three ways, one result

import nerdamer from 'nerdamer';
import { factor } from 'nerdamer/algebra';

nerdamer('factor(x^2-1)');  // Notation

nerdamer.factor('x^2-1');   // method on nerdamer

factor('x^2-1');            // imported function

All three return (1+x)*(-1+x). Every function page shows its forms side by side.