Nerdamer Notation

Writing expressions

Nerdamer Notation is the text you pass to nerdamer('...'). It reads like ordinary mathematics, and covers expressions, equations, vectors, matrices, sets and dictionaries.

2*x + 3
2x + 3
(x+1)(x-1)
sqrt(x^2+1)
sin(x)^2 + cos(x)^2

Operators and precedence

The usual rules apply. Use parentheses when you want to be explicit.

2+3*4       // 14

(2+3)*4     // 20

8/4/2       // 1

2^3^2       // 512

Multiplication and division go left to right. Powers go right to left, so 2^3^2 is 2^(3^2). Factorial binds tighter than a leading minus:

-4!+1       // -23

(3!)!       // 720

Implicit multiplication

You can leave out * where the meaning is clear:

2x                   // 2*x

x(9)                 // 9*x

(a+b)(c+d)           // (a+b)*(c+d)

2+4(9+1)             // 42

y^-2z                // y^-2*z

A function name followed by parentheses is a call, as in sin(x). Any other name followed by parentheses is multiplication, as in x(9).

Signs, percent and modulo

--1          // 1

5^-3         // 1/125

50%          // 1/2

10%3         // 1

4%5          // 4

% after a number is a percentage. Between two values it is the remainder (modulo).

Numbers and names

Write integers, decimals or scientific notation. Spaces around an expression don't matter.

42
3.14159
1.234e+1     // 12.34

12.3e-1      // 1.23

Put repeating digits in square brackets to write a recurring decimal. It becomes an exact fraction:

0.[3]        // 1/3

1.2[34]      // 611/495

0.[142857]   // 1/7

-0.[6]       // -2/3

0.[3]+0.[6]  // 1

Quotes work too: 0.'3' is also 1/3. The leading zero is optional: .[3] is also 1/3.

Names can be longer than one letter: velocity and xy are each one variable. To read xy as x*y instead, turn on USE_SINGLE_LETTER_VARIABLES (see below).

Common symbols work too:

π + pi       // 2*pi
∞ + Inf      // Inf

Reserved names

These names have a fixed meaning, and you can't assign to them:

  • e and pi: the constants.
  • i: the imaginary unit.
  • _n: "any integer" in periodic solutions, as in solve(sin(x),x), which gives {_n*pi}.
  • all: "every value", for an equation that is always true.

You can still give _n a value for a single evaluation: evaluate(solve(sin(x),x),{_n=>0}) gives {0}.

Using i as a variable

i:0 throws an error, because i is the imaginary unit. If you need i as an ordinary variable, move the imaginary unit to another symbol:

import nerdamer from 'nerdamer';
import { Parser } from 'nerdamer/parser';

Parser.setImaginary('_'); // the imaginary unit is now _

console.log(nerdamer('i:x; i*i').text()); // x^2
Complex numbers still work, written with the new symbol. The change applies to everything parsed afterwards in your app, so choose a symbol you don't use otherwise.

Exact or decimal

Results stay exact where possible: nerdamer('sqrt(2)') gives 2^(1/2), not a decimal. Call .evaluate() for a number.

const exact = nerdamer('sqrt(2)');     // 2^(1/2)

const numeric = exact.evaluate();      // 1.4142135623730950488

Equations and assignment

= makes an equation. : assigns a value (this is Scripting).

x=5                    // the equation x=5

x:5                    // assign 5 to x


sol: solve(sin(x),x);  // store the solutions

evaluate(sol,{_n=>0})  // {0}

An equation is not an ordinary expression. Functions that take an equation, such as solve, say so on their pages.

Several statements

Separate statements with ;. A new line alone doesn't end a statement.

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

Values stay until you remove them

A value you assign is used in every later expression, in any call. Remove one with unassign(name), remove all with nerdamer.clearVars(), or use let for a value that only lives inside one expression.

nerdamer('x:4');
nerdamer('x^2').text();     // 16

nerdamer('unassign(x)');
nerdamer('x^2').text();     // x^2

Vectors, sets, matrices and dictionaries

[a, b, c]              // vector

{a, b, c}              // set

{a=>1, b=>2}           // dictionary

matrix([1,2],[3,4])    // matrix

dot([a,b],[x,y])       // a*x+b*y

Square brackets make a vector. Curly braces make a set, or a dictionary when the entries are key=>value. Many functions work on each element of a vector, matrix or set.

Indexing

Get an element with brackets. Indexes start at 0; set INDEX_BASE to 1 to start at 1.

v: [a,b,c];
v[0]                   // a

Functions

Functions such as factor, diff, solve and matrix are part of Notation. The function reference lists them all. For variables, conditions, loops and your own functions, see Scripting.

Errors

Nerdamer doesn't guess at incomplete or unbalanced input. Each of these throws an error:

cos(        // Missing closing bracket

(x+1))      // Missing opening bracket

/2
5+

Settings that change how expressions are read

nerdamer.set('ALLOW_IMPLICIT_MULTIPLICATION', false);   // 2x becomes an error

nerdamer.set('USE_SINGLE_LETTER_VARIABLES', true);      // ab means a*b

nerdamer.set('INDEX_BASE', 1);                          // v[1] is the first element

Settings apply to your whole app, not to one expression. Settings lists every setting and its default, including precision and evaluation limits, and shows how to change one temporarily.