Start here

Getting started

The quickest way to begin is to install the package, import the nerdamer function, and pass it mathematical notation.

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 'x + 2*x + 1' is Nerdamer Notation. The result is a symbolic value that you can simplify, evaluate, format, or use in later calculations.

Strings passed to nerdamer(...) are interpreted as Nerdamer Notation, not executed as JavaScript. The exception is buildFunction(), which compiles an expression into a numerical JavaScript function.

Call functions in Nerdamer Notation

const result = nerdamer('factor(x^4-1)');
console.log(result.text());
Important: factor(...) above is part of the expression string. It is a Nerdamer Notation function, not a JavaScript function call. The function reference documents this form.

Call common functions directly from nerdamer

Common operations can also be called directly from JavaScript or TypeScript. For example, you can factor an expression without writing factor(...) inside a Nerdamer string.

import nerdamer from 'nerdamer';

console.log(nerdamer.factor('x^4-1').text());

For example, nerdamer.factor('x^4-1') and nerdamer('factor(x^4-1)') perform the same factoring operation through two different interfaces. Other commonly used operations such as diff and solve are also available from the full-package export.

Substitute JavaScript values

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

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

Also see subst for substitution written directly in Nerdamer Notation.

Use the JavaScript / TypeScript API

You do not have to express everything as a string. The library also exposes methods, functions, classes, and types for direct use from application code.

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

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

const d = determinant(M);

Use the API when your application already has JavaScript data, when you want a specific public class, or when TypeScript types are useful to your code.

The same operation can have more than one form

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

// Nerdamer Notation

const a = nerdamer('factor(x^2-1)');

// Full-package method

const b = nerdamer.factor('x^2-1');

// JavaScript / TypeScript subpath import

const c = factor('x^2-1');

Choosing how to use Nerdamer explains the difference between Nerdamer Notation, Nerdamer Scripting, and the JavaScript / TypeScript API.

CommonJS

const nerdamer = require('nerdamer');

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

Try it in the browser

The Playground lets you run Nerdamer Notation, inspect text and TeX output, and view the resulting expression tree without installing the package.