Choosing how to use Nerdamer
There are three main ways to use Nerdamer: write mathematical notation, write Nerdamer scripts, or call the JavaScript / TypeScript API directly.
1. Nerdamer Notation
Use notation when you want to write mathematics as text. Functions inside the string are symbolic functions, not JavaScript calls.
import nerdamer from 'nerdamer';
const result = nerdamer('factor(x^2-1)');
Nerdamer Notation is especially useful when an application accepts mathematical input from users. A formula entered in a text field, loaded from stored data, or otherwise received as text can be passed to nerdamer('...') without first translating it into a sequence of JavaScript API calls. The library parses that text as mathematical notation; it does not evaluate the input as JavaScript.
Use Nerdamer Notation when your application receives formulas as text or when writing the mathematics directly is simpler than constructing it through JavaScript calls. Applications should still handle invalid syntax and set appropriate limits for workloads they accept from users. See Expression syntax and the function reference.
2. Nerdamer Scripting
Use scripting when the text passed to nerdamer(...) needs more than one expression: assignments, user-defined functions, conditions, loops, blocks, or local bindings.
const result = nerdamer(`
f(x):=x^2+1;
if(a>0, f(a), 0)
`);
Nerdamer evaluates these statements as Nerdamer Scripting; they are not JavaScript statements. See Nerdamer Scripting. For function definitions specifically, see User-defined functions.
3. JavaScript / TypeScript API
Use the API when your application wants to call the library directly from JavaScript or TypeScript. There are two common forms.
The nerdamer API
The default import is callable: it can parse Nerdamer Notation and also provides commonly used operations such as factor(), solve(), and setFunction().
import nerdamer from 'nerdamer';
const expression = nerdamer('x^2+1');
const tex = nerdamer.convertToTeX(expression);
const f = nerdamer('8*cos(x)').buildFunction(['x']);
nerdamer.setFunction('f', ['x'], 'x^2+1');
const factored = nerdamer.factor('x^2-1');
Use convertToTeX() when you need TeX math markup from the root API. Expressions also provide toTeX(). The older convertToLaTeX() name remains available as a deprecated compatibility alias.
buildFunction() is available directly on values returned by nerdamer(...) so scalar expressions can be compiled without a manual type cast or type guard. It only compiles an Expression. If the parsed result is an equation, vector, matrix, set, or another structured value, the method throws UnexpectedDataType rather than compiling the contained values independently.
Browse the generated nerdamer API to see the callable export and its attached members.
Direct imports
import { factor } from 'nerdamer/algebra';
import { Matrix } from 'nerdamer/structures';
const result = factor('x^2-1');
const M = new Matrix([1, 2], ['x', 'y']);
Direct imports are useful when you want a specific function, class, or TypeScript type. See the JavaScript / TypeScript API for public package paths.
The same operation in different forms
import nerdamer from 'nerdamer';
import { factor } from 'nerdamer/algebra';
nerdamer('factor(x^2-1)'); // Nerdamer Notation
nerdamer.factor('x^2-1'); // JavaScript / TypeScript API
factor('x^2-1'); // JavaScript / TypeScript API
All three factor the same expression. The first writes the operation in Nerdamer Notation; the other two call it directly from JavaScript or TypeScript.
What about nerdamer/parser?
nerdamer/parser is a JavaScript / TypeScript package entry point for programmatic parser access. It belongs to the API reference and is separate from the documentation for writing Nerdamer Notation.
