Results & output
nerdamer('...') returns a value you can print, convert to TeX, evaluate to a decimal, take apart, or turn into a JavaScript function.
What you get back
The kind of value depends on the input:
| Input | Result | Read it with |
|---|---|---|
x^2+1 | Expression | .text(), .evaluate(), .variables() |
[1, x, 3] | Vector | .elements |
matrix([1,2],[3,4]) | Matrix | .rows(), .cols(), .get(row, col) |
{a, b} | Set | .elements |
{a=>1, b=>2} | Dictionary | .get(key), .keys(), .values() |
x+1=y | Equation | .LHS, .RHS |
solve(x^2-4,x) | SolutionSet | .elements |
nerdamer('solve(x^2-4,x)').elements.map(root => root.text()); // ['2', '-2']
nerdamer('matrix([1,2],[3,4])').get(1, 0).text(); // 3
nerdamer('{a=>1, b=>2}').get('a').text(); // 1
nerdamer('x+1=y').LHS.text(); // 1+x
Every result has .text(). Each type has its own page under Types & import paths.
Text
.text() gives the result as a string. Pass { sort: true } for conventional term order, highest power first.
import nerdamer from 'nerdamer';
const expression = nerdamer('x^2+2*x+1');
expression.text(); // 1+2*x+x^2
expression.text({ sort: true }); // x^2+2*x+1
The option affects only that call. To sort everywhere, use the SORT_TERMS setting.
Text options
| Option | Effect | Example |
|---|---|---|
sort | Conventional term order. | x^2+2*x+1 |
decimal | Numbers as decimals. | 1/3+x/7 → 0.33333333333333333333+0.14285714285714285714*x |
precision | Digits, with decimal. | { decimal: true, precision: 5 } → 0.33333+0.14286*x |
wrapPow | Parentheses around a power's base. | x^(1+a) → (x)^(1+a) |
import type { TextOptions } from 'nerdamer';
const options: TextOptions = { sort: true, decimal: false };
expression.text(options);
TextOptions is also exported from nerdamer/core.
A single expression also has toText(), which always uses conventional order. In TypeScript, check that the result is an Expression first, because vectors, sets and the other types don't have it:
const expression = nerdamer('x^2+2*x+1');
if (nerdamer.classes.Expression.isExpression(expression)) {
expression.toText(); // x^2+2*x+1
}
TeX
nerdamer.convertToTeX turns any result into TeX. A single expression also has .toTeX().
nerdamer.convertToTeX(nerdamer('x^2+2*x+1')); // x^{2}+2 \cdot x+1
nerdamer.convertToTeX(nerdamer('solve(x^2-4,x)')); // \left\{2, \, -2\right\}
nerdamer('x^2+2*x+1').toTeX(); // x^{2}+2 \cdot x+1
The older name convertToLaTeX still works, but is deprecated.
Decimals and JavaScript numbers
Results stay exact. .evaluate() gives a decimal result, optionally with values for the variables:
nerdamer('sqrt(2)').evaluate().text(); // 1.4142135623730950488
nerdamer('x^2+1').evaluate({ x: 3 }).text(); // 10
For a plain number, use .toDecimal() for a string at full precision, or .valueOf() for a JavaScript number:
nerdamer('2/3').toDecimal(); // '0.66666666666666666667'
nerdamer('2/3').valueOf(); // 0.6666666666666666
Variables
.variables() lists the variables in an expression:
nerdamer('a*x+pi').variables(); // ['a', 'x']
Fast numeric functions
.buildFunction() turns an expression into a JavaScript function. Use it to evaluate the same expression many times, for example to plot it. Pass the parameter order if there's more than one variable:
const f = nerdamer('x^2+1').buildFunction();
f(2); // 5
const g = nerdamer('x*y').buildFunction(['y', 'x']);
g(2, 3); // 6
It works on a single expression; on a vector, matrix, equation or set it throws an error.
Different names in JavaScript
Most functions have the same name in Notation and in JavaScript. These differ:
| Notation | JavaScript |
|---|---|
sqcomp | completeSquare |
polyfactors | polyFactors |
isprime | isPrime |
solveeqs | solveSystem (and nerdamer.solveeqs) |
import nerdamer from 'nerdamer';
import { solveSystem } from 'nerdamer/solve';
import { isPrime, polyFactors } from 'nerdamer/algebra';
nerdamer('solveeqs([x+y=3,x-y=1])').text(); // [{x => 2, y => 1}]
solveSystem(['x+y=3', 'x-y=1']).text(); // [{x => 2, y => 1}]
isPrime(29).text(); // 1
polyFactors('x^2-1').text(); // [1+x, -1+x]
solveEquations still works in Nerdamer Notation and as nerdamer.solveEquations, but is deprecated and emits an api.deprecatedFunction warning. Use solveeqs in Notation, or solveSystem in JavaScript / TypeScript.Functions such as conjugate, csgn and nullspace keep their names; see their pages in the function reference.
completeSquare returns the parts
In Notation, sqcomp(x^2+6*x+1) gives -8+(3+x)^2. In JavaScript, completeSquare also gives you the pieces of a*(x+h)^2+k:
import { completeSquare } from 'nerdamer/algebra';
const result = completeSquare('x^2+6*x+1');
result.expression.text(); // -8+(3+x)^2
result.a.text(); // 1
result.h.text(); // 3
result.k.text(); // -8
result.variable; // 'x'
Helpers only in JavaScript
Substitution with uSub and uUnSub
uSub replaces a subexpression with a new variable, and uUnSub puts it back. They have no Notation form.
import { uSub, uUnSub } from 'nerdamer/algebra';
const [substituted, substitutions] = uSub('cos(x)^2+cos(x)+1', 'cos(x)');
substituted.text({ sort: true }); // u0^2+u0+1
uUnSub(substituted, substitutions).text(); // 1+cos(x)+cos(x)^2
The new variable's name never clashes with one already in the expression. Pass the same substitutions object to later calls to keep their names apart too.
