Nerdamer Scripting

Nerdamer Scripting Beta

Beta: Nerdamer Scripting is supported in Nerdamer 2.0. Some syntax and API details may still change before Nerdamer 2.0 final.

Nerdamer Scripting is the compact programming layer available inside text passed to nerdamer(...). Use it when a single mathematical expression is not enough and you need assignments, user-defined functions, conditions, loops, or local scope.

Not JavaScript: the scripting layer is evaluated by the library's parser. For ordinary mathematical expressions and functions, see Nerdamer Notation. For methods and imported APIs, see the JavaScript / TypeScript API.

Notation and scripting side by side


import nerdamer from 'nerdamer';

// Nerdamer Notation: one mathematical expression
const expression = nerdamer('factor(x^2-1)');

// Nerdamer Scripting: define and use a function
const script = nerdamer(`
square(x):=x^2;
square(6)
`);

User-defined functions

Define a symbolic function in scripting with f(x):=..., or define it from application code with nerdamer.setFunction(...). See User-defined functions for both forms and how they relate.

For larger workflows built entirely in the scripting language, see the Scripting examples.

Scripting reference

The entries below are the parser functions classified as scripting by the generated documentation data.

Assignment

Use : to assign a parser variable. Assignments are retained by the parser and can affect later calls until they are removed.


a:5;
b:a+2;
b
: and = are not interchangeable. : stores a parser value. = denotes a mathematical equation. For example, x:5 assigns 5 to x, while x=5 produces the equation x=5.

This distinction also applies to structured results such as solver output. To store a SolutionSet, assign it with ::


sol: solve(sin(x),x);
evaluate(sol,{_n=>0})    // {0}

_n is the reserved integer index used by solver-generated periodic families. It cannot be assigned globally with _n:0, but it can be supplied in the substitution Dictionary passed to evaluate. That Dictionary applies only to the evaluation and does not create a stored value for _n.

Use unassign(a) to remove one stored assignment, nerdamer.clearVars() from application code to clear them all, or let(...) for temporary local bindings.

Statement separators

Newlines are formatting whitespace, not statement separators. Use ; between multiple statements passed in one call.


f(x):=x^2+1;
f(12)

Blocks and local scope

block(...) evaluates expressions in sequence. let(...) creates temporary local bindings while its body is evaluated.


block(a:2, b:3, a+b)
let(x, 2, x^3)

Conditions and loops

if, while, and for let a script choose or repeat expressions.


if(x>0, x, -x)

let(k,0,block(
  while(3-k, k:k+1),
  k
))

Control flow

return, break, and continue control how blocks, functions, and loops finish. Their individual reference pages describe where each form applies.