JavaScript / TypeScript API

Text output and public helpers

This page covers text-formatting controls and public helper APIs that are useful when application code needs more than the shortest Nerdamer Notation form.

Text output and term ordering

text() returns Nerdamer's normal textual representation. Pass { sort: true } when the returned string should use the conventional sorted term order for that call.

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 is local to that formatting call. It does not modify the expression and does not change the process-wide SORT_TERMS setting.

TextOptions

import type { TextOptions } from 'nerdamer';

const options: TextOptions = {
  sort: true,
  decimal: false
};

expression.text(options);

TextOptions is also exported from nerdamer/core. The supported fields are sort, decimal, precision, and wrapPow.

toText()

Expressions loaded through the full package also provide toText(), which uses Nerdamer's text converter and produces conventional display ordering.

const expression = nerdamer('x^2+2*x+1');

if (nerdamer.classes.Expression.isExpression(expression)) {
  expression.toText();
  // x^2+2*x+1

}
TypeScript note: nerdamer(...) can return several parser entity types. Narrow the result to Expression before calling the Expression-only toText() method. text(...) is available across parser entities.

Complete the square

Nerdamer Notation uses the lowercase parser name sqcomp(...). JavaScript and TypeScript use completeSquare(...).

import nerdamer from 'nerdamer';
import { completeSquare } from 'nerdamer/algebra';

const notation = nerdamer('sqcomp(x^2+6*x+1)');

const result = completeSquare('x^2+6*x+1');
result.expression;
result.a;
result.h;
result.k;
result.variable;

The notation form returns the completed-square expression. The direct API returns a CompleteSquareResult so callers can also inspect the coefficient, shift, remainder, and selected variable.

Polynomial factors

polyfactors(...) is the Nerdamer Notation spelling. The direct API uses polyFactors(...) and returns the individual factors in a Vector.

import { polyFactors } from 'nerdamer/algebra';

const factors = polyFactors('x^2-1');

U-substitution

uSub() and uUnSub() are JavaScript / TypeScript helpers. They are not Nerdamer Notation functions.

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();
// restores the original expression

uSub() chooses a generated variable name that does not collide with the expression or an existing substitution map. Preserve the returned map when several substitutions need to share the same generated-variable namespace.

Primality

Use isprime(...) in Nerdamer Notation and isPrime(...) in JavaScript or TypeScript.

import { isPrime } from 'nerdamer/algebra';

isPrime(29).text(); // 1

isPrime(21).text(); // 0

Inputs that cannot be classified as exact integers remain symbolic.

Complex helpers

conjugate(...) and csgn(...) remain valid Nerdamer Notation names and are also callable directly from the full nerdamer export.

nerdamer.conjugate('3+2*i').text();
// 3-2*i


nerdamer.csgn('-3+2*i').text();
// -1

csgn returns the sign of the real part when that sign is known. If the real part is zero, it uses the sign of the imaginary part. An unresolved symbolic sign remains as csgn(...).

Matrix nullspace

nullspace(...) is available through Nerdamer Notation, the root API, and nerdamer/structures.

import { Matrix, nullspace } from 'nerdamer/structures';

const M = new Matrix(
  [1, 2, 3],
  [2, 4, 6]
);

nullspace(M).text();
// [[-2, 1, 0], [-3, 0, 1]]

An optional prime modulus selects finite-field arithmetic.

System solving

The canonical JavaScript / TypeScript function is solveSystem(...). Nerdamer Notation and the retained root compatibility spelling use solveeqs(...).

import nerdamer from 'nerdamer';
import { solveSystem } from 'nerdamer/solve';

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}]
Naming: solveEquations is not part of the current public API.

Related references

See the Nerdamer Notation function reference for parser syntax and the JavaScript / TypeScript API for package-level APIs.