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 library parses it and returns a symbolic value that you can inspect or manipulate.
eval(). Input given to nerdamer(...) is processed by the library's parser. Normal parsing, simplification, solving, evaluation, and symbolic manipulation do not use eval() or new Function. Only buildFunction() generates JavaScript dynamically, using new Function(...) to compile a numerical function.Call functions in Nerdamer Notation
const result = nerdamer('factor(x^4-1)');
console.log(result.text());
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
The full package also exposes commonly used operations as methods on the default nerdamer export. This lets application code call an operation directly instead of placing its name inside Nerdamer Notation.
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
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.
