Skip to main content

Core

The functions described in this section are part of the core of the Compute Engine.

Constants

The symbols below are inert constants. They are used as tags and have no value other than themselves.

SymbolDescription
AllAll the possible values apply
NoneNone of the possible values apply
NothingAn optional expression is not present. Used in sparse list to indicate skipped elements.
UndefinedThe result is not defined.
Note that for numbers, the equivalent is NaN (Not a Number)
\lbrack 2, ,3 \rbrack
$$$\lbrack 2, ,3 \rbrack$$
["List", 2, "Nothing", 3]

Declaring, Assigning and Assuming

A ["Declare"] expression is used to declare a symbol in the current scope.

Once a symbol has been declared, its value can be changed using an ["Assign"] expression.

An ["Assume"] expression is used to assert a predicate about an expression. It is used to provide additional information to the system, for example to indicate that a variable is positive.

Declare: (symbol, type_)

Declare: (symbol, type, value)

Declare a new symbol in the current scope, and set its value and type.

If the symbol already has a definition in the current scope, evaluate to an error, otherwise evaluate to value.

This is equivalent to let in JavaScript or var in Python.

To change the value of an existing symbol, use an ["Assign"] expression.

Declare is not a pure function since it changes the state of the Compute Engine.

Assign: (symbol, value)

Set the value of symbol to value.

If symbol has not been declared in the current scope, consider parent scopes until a definition for the symbol is found.

If a definition is found, change the value of the symbol to value if the value is compatible with the type of the symbol: once set, the type of a symbol cannot be changed.

If there is no definition for the symbol, add a new definition in the current scope, and use the value to infer the type of the symbol.

This is equivalent to = in may programming languages.

Assign is not a pure function.

Assume: (predicate)

The predicate is an expression that evaluates to True or False.

The symbols in the predicate expression may be free, i.e. they may not have been declared yet. Asserting an assumption does not declare the symbols in the predicate.

The predicate can take the form of:

  • an equality: ["Assume", ["Equal", "x", 3]]
  • an inequality: ["Assume", ["Greater", "x", 0]]
  • a membership expression: ["Assume", ["Element", "x", "Integers"]]

Assign is not a pure function since it changes the state of the Compute Engine.

Structural Operations

The following functions can be applied to non-canonical expressions. The do not depend on the canonical form, but reflect the structure of the expression.

About: (symbol_)

Evaluate to a dictionary expression containing information about a symbol such as its type, its attributes, its value, etc...

Tail: (expression)

Evaluate to a sequence of the arguments of expression.

["Tail", ["Add", 2, 3]]
// ➔ ["Sequence", 2, 3]

Tail can be used to change the head of an expression, for example:

["Multiply", ["Tail", ["Add", 2, 3]]]
// ➔ ["Multiply", 2, 3]

Hold: (expression)

Tag an expression that should be kept in an unevaluated form

Identity: (expression)

Evaluate to its argument

In the mathematical sense, this is an operator (a function that takes a function as an argument and returns a function).

Inspecting an Expression

The following functions can be used to obtain information about an expression.

Domain: (expression)

Evaluate to the domain of expression

["Domain", 2.4531]

// ➔ "RealNumbers"

IsSame: (expression1, expression2)

Evaluate to True if the two expressions are structurally identical, otherwise evaluate to False.

["IsSame", ["Add", 2, 3], ["Add", 2, 3]]
// ➔ True

To compare two expressions for mathematical equality, use Equal.

To compare two expressions structurally, but ignoring the order of the arguments of commutative functions, use CanonicalForm.

See Comparing Expressions for other options to compare two expressions, such as the Equal function.

Transforming an Expression

Evaluate: (expression)

Apply a sequence of definitions to an expression in order to reduce, simplify and calculate its value. Overrides Hold and hold attributes of a function.

Evaluate only performs exact calculations. To perform numerical approximations, use N.

Read more about exact calculations and approximate calculations.

Expand: (expression)

Apply the distributive law if the expression is a product or power of sums.

For example: a(b + c) = ab + ac

  • Expand the terms of the expression if it is a sum or negate.
  • If the expression is a fraction, expand the numerator.
["Expand", ["Power", ["Add", "x", 1], 2]]
// ➔ ["Add", 1, ["Multiply", 2, "x"], ["Power", "x", 2]]

ExpandAll: (expression)

Expand an expression, recursively.

["ExpandAll", ["Power", ["Multiply", ["Add", "x", 1], 3], 2]]
// ➔ ["Add", 1, ["Multiply", 6, "x"], ["Multiply", 6, ["Power", "x", 2]], ["Power", "x", 3]]

Factor: (expression)

Factor an expression.

["Factor", ["Add", ["Multiply", 2, "x"], ["Multiply", 2, "y"]]]
// ➔ ["Multiply", 2, ["Add", "x", "y"]]

Together: (expression)

Combine the terms of a sum of fractions into a single fraction.

["Together", ["Add", ["Divide", 1, 2], ["Divide", 1, 3]]]
// ➔ ["Divide", 5, 6]

Simplify: (expression)

The Simplify function applies a sequence of transformations to an expression in order to reduce, simplify and calculate its value.

CanonicalForm: (expression)

CanonicalForm: (expression, form-1, form-2, ...)

If expression is already canonical, this function has no effect.

If there are no form-n arguments, the expression is transformed to its canonical form.

If some form-n arguments are provided, they indicate one or more canonical transformations to apply to the expression. The following canonical forms are supported:

  • Order: If expression is a commutative function, sort the arguments according to the canonical order of the arguments of the function.
["CanonicalForm", ["Add", 3, 2, 1], "Order"]
// -> ["Add", 1, 2, 3]

This can be useful to compare two non-canonical expressions for equality, for example:

["IsSame",
["Add", 1, "x"],
["Add", "x", 1]
]
// -> False

["IsSame",
["CanonicalForm", ["Add", 1, "x"], "Order"],
["CanonicalForm", ["Add", "x", 1], "Order"]
]
// -> True
  • Flatten: Simplify associative expressions, remove any unnecessary delimiters indicating the order of operations, flattens any Sequence expressions.
["CanonicalForm", ["Add", 1, ["Add", 2, 3]], "Flatten"]
// -> ["Add", 1, 2, 3]

["CanonicalForm", ["Add", 1, ["Delimiter", ["Sequence", 2, 3]]], "Flatten"]
// -> ["Add", 1, 2, 3]

["CanonicalForm", ["Add", 1, ["Sequence", 2, 3]], "Flatten"]
// -> ["Add", 1, 2, 3]
  • Number: Transform some number forms, for example ["Add", 2, ["Multiply", 3, "ImaginaryI"]] to ["Complex", 2, 3], simplify and normalize numerator and denominator of rational numbers, etc...

  • InvisibleOperator: Remove any invisible operators that may be contained in the expression and replace them with Multiply or function application, depending on the context

["CanonicalForm", ["InvisibleOperator", "2", "x"], "InvisibleOperator"]
// -> ["Multiply", 2, "x"]
  • Multiply: If expression is a Multiply function, simplify it by combining the coefficients and the factors, transform product to a Power expression when possible.
["CanonicalForm", ["Multiply", 2, 3, "x"], "Multiply"]
// -> ["Multiply", 6, "x"]
  • Add: If expression is an Add function, remove any 0, transform sum into multiplication when possible. If expression is a Subtract transform it into an Add. If expression is a Negate transform it into a Multiply or negate number literals.

  • Power: Transform Exp, Square, Sqrt, Root function to a Power expression;

["CanonicalForm", ["Exp", "x"], "Power"]

```json example
["CanonicalForm", ["Power", 2, 3], "Power"]
// -> ["Power", 8]

To compare the input from a mathfield with an expected answer, you could use:

const correct = ce.parse(mf.value, {canonical: "Order"})
.isSame(ce.parse("1+x"))

Both 1+x and x+1 will return true, but 2-1+x will return false.

Note: see also the options for the canonical option of ce.parse() and ce.box() which can also be used to specify a custom canonical form:

const correct = ce.parse(mf.value, {canonical: "Order"})
.isSame(ce.parse("x+1"))

N: (expression)

Evaluate to a numerical approximation of the expression.

["N", "Pi"]

// ➔ 3.141592653589793

Core Functions

Error: (error-code, context)

Tag an expression that could not be interpreted correctly. It may have a syntax error, a reference to an unknown symbol or some other problem.

The first argument, error-code is either a string, or an ["ErrorCode"] expression.

The context is an optional expression that provides additional information about the error.

InverseFunction: (symbol)

Evaluate to the inverse function of its argument for example Arcsin for Sin.

\sin^{-1}(x)
$$$\sin^{-1}(x)$$
[["InverseFunction", "Sin"], "x"]

In the mathematical sense, this is an operator (a function that takes a function as an argument and returns a function).

String: (expression)

Evaluate to a string made from the concatenation of the arguments converted to strings

["String", "x", 2]

// ➔ "'x2'"

Symbol: (expression)

Evaluate to a new symbol made of a concatenation of the arguments.

["Symbol", "x", 2]

// ➔ "x2"

The symbol is not declared, it remains a free variable. To declare the symbol use Declare.

["Declare", ["Symbol", "x", 2], "RealNumbers"]

Parsing and Serializing Latex

Parse: (string)

If expr is a ["LatexString"] expression, evaluate to a MathJSON expression corresponding to the LaTeX string.

["Parse", ["LatexString", "'\frac{\pi}{2}'"]]

// ➔ ["Divide", "Pi", 2]

Latex: (expression)

Evaluate to a LatexString which is the expression serialized to LaTeX

LatexString: (string)

Tag a string as a LaTeX string

Superscripts and Subscripts

These functions are all inert functions, that is they evaluate to themselves.

FunctionDescription
Subminusx_-
Subplus x_+
Subscriptx_{n}
Substar x_*
Superdagger x^\dagger
Superminus x^-
Superplus x^+
Superstar x^*When the argument is a complex number, indicate the conjugate.