The BoxedExpression interface includes most of the member functions applicable to any kind of expression, for example get symbol() or get ops().

When a member function is not applicable to this BoxedExpression, for example get symbol() on a BoxedNumber, it returns null.

This convention makes it convenient to manipulate expressions without having to check what kind of instance they are before manipulating them.

interface Pattern {
    baseDefinition: undefined | BoxedBaseDefinition;
    complexity: undefined | number;
    description: undefined | string[];
    engine: IComputeEngine;
    errors: BoxedExpression[];
    freeVariables: string[];
    functionDefinition: undefined | BoxedFunctionDefinition;
    hash: number;
    head: string | BoxedExpression;
    isAlgebraic: undefined | boolean;
    isComplex: undefined | boolean;
    isComposite: undefined | boolean;
    isConstant: boolean;
    isEven: undefined | boolean;
    isExact: boolean;
    isExtendedComplex: undefined | boolean;
    isExtendedReal: undefined | boolean;
    isFinite: undefined | boolean;
    isImaginary: undefined | boolean;
    isInfinity: undefined | boolean;
    isInteger: undefined | boolean;
    isNaN: undefined | boolean;
    isNegative: undefined | boolean;
    isNegativeOne: undefined | boolean;
    isNonNegative: undefined | boolean;
    isNonPositive: undefined | boolean;
    isNothing: boolean;
    isNotZero: undefined | boolean;
    isNumber: undefined | boolean;
    isOdd: undefined | boolean;
    isOne: undefined | boolean;
    isPositive: undefined | boolean;
    isPrime: undefined | boolean;
    isPure: boolean;
    isRational: undefined | boolean;
    isReal: undefined | boolean;
    isValid: boolean;
    isZero: undefined | boolean;
    json: Expression;
    keys: null | IterableIterator<string, any, any>;
    keysCount: number;
    nops: number;
    numericValue:
        | null
        | number
        | Decimal
        | Complex
        | Rational;
    op1: BoxedExpression;
    op2: BoxedExpression;
    op3: BoxedExpression;
    ops: null | BoxedExpression[];
    rank: number;
    rawJson: Expression;
    scope: null | RuntimeScope;
    sgn:
        | undefined
        | null
        | 0
        | 1
        | -1;
    shape: number[];
    string: null | string;
    subexpressions: BoxedExpression[];
    symbol: null | string;
    symbolDefinition: undefined | BoxedSymbolDefinition;
    symbols: string[];
    unknowns: string[];
    url: undefined | string;
    wikidata: undefined | string;
    get canonical(): BoxedExpression;
    domain: undefined | BoxedDomain;
    isCanonical: boolean;
    latex: string;
    value:
        | undefined
        | string
        | number
        | boolean
        | number[];
    [toPrimitive](hint: "string" | "number" | "default"): null | string | number;
    bind(): void;
    compile(to?: "javascript", options?: {
        optimize: ("evaluate" | "simplify")[];
    }): undefined | ((args: Record<string, any>) => any);
    count(exprs: Iterable<BoxedExpression, any, any>, options?: PatternMatchOptions): number;
    evaluate(options?: EvaluateOptions): BoxedExpression;
    getKey(key: string): undefined | BoxedExpression;
    getSubexpressions(head: string): BoxedExpression[];
    has(v: string | string[]): boolean;
    hasKey(key: string): boolean;
    infer(domain: BoxedDomain): boolean;
    is(rhs: unknown): boolean;
    isEqual(rhs: BoxedExpression): boolean;
    isGreater(rhs: BoxedExpression): undefined | boolean;
    isGreaterEqual(rhs: BoxedExpression): undefined | boolean;
    isLess(rhs: BoxedExpression): undefined | boolean;
    isLessEqual(rhs: BoxedExpression): undefined | boolean;
    isSame(rhs: BoxedExpression): boolean;
    match(rhs: BoxedExpression, options?: PatternMatchOptions): null | BoxedSubstitution;
    N(options?: NOptions): BoxedExpression;
    print(): void;
    replace(rules: BoxedRuleSet, options?: ReplaceOptions): null | BoxedExpression;
    reset(): void;
    simplify(options?: SimplifyOptions): BoxedExpression;
    solve(vars: Iterable<string, any, any>): null | BoxedExpression[];
    subs(sub: Substitution<SemiBoxedExpression>, options?: {
        canonical?: boolean;
    }): BoxedExpression;
    test(expr: BoxedExpression, options?: PatternMatchOptions): boolean;
    toJSON(): Expression;
    toString(): string;
    valueOf():
        | string
        | number
        | boolean
        | any[];
}

Hierarchy (view full)

Dictionary Expression

keys: null | IterableIterator<string, any, any>

The keys of the dictionary.

If this expression not a dictionary, return null

keysCount: number
  • If this expression is a dictionary, return the value of the key entry.

    Parameters

    • key: string

    Returns undefined | BoxedExpression

  • If this expression is a dictionary, return true if the dictionary has a key entry.

    Parameters

    • key: string

    Returns boolean

Domain Properties

isAlgebraic: undefined | boolean

The value of this expression is a number that is the root of a non-zero univariate polynomial with rational coefficients.

All integers and rational numbers are algebraic.

Transcendental numbers, such as \( \pi \) or \( e \) are not algebraic.

isComplex: undefined | boolean

The value of this expression is a number, but not NaN or any Infinity

isReal || isImaginary

isExtendedComplex: undefined | boolean

isReal || isImaginary || isInfinity

isExtendedReal: undefined | boolean

Real or ±Infinity

isReal || isInfinity

isImaginary: undefined | boolean

The value of this expression is a number with a imaginary part

isInteger: undefined | boolean

The value of this expression is an element of the set ℤ: ...,-2, -1, 0, 1, 2...

isNumber: undefined | boolean

true if the value of this expression is a number.

isExtendedComplex || isNaN = isReal || isImaginary || isInfinity || isNaN

Note that in a fateful twist of cosmic irony, NaN ("Not a Number") is a number.

isRational: undefined | boolean

The value of this expression is an element of the set ℚ, p/q with p ∈ ℕ, q ∈ ℤ ⃰ q >= 1

Note that every integer is also a rational.

isReal: undefined | boolean

The value of this expression is real number: finite and not imaginary.

isFinite && !isImaginary

Expression Properties

isComposite: undefined | boolean
isEven: undefined | boolean
isFinite: undefined | boolean

This expression is a number, but not ±Infinity and not NaN

isInfinity: undefined | boolean

The numeric value of this expression is ±Infinity or Complex Infinity

isNaN: undefined | boolean

"Not a Number".

A value representing undefined result of computations, such as 0/0, as per the floating point format standard IEEE-754.

Note that if isNaN is true, isNumber is also true (yes, NaN is a number).

isNegative: undefined | boolean

The numeric value of this expression is < 0, same as isLess(0)

isNegativeOne: undefined | boolean

The numeric value of this expression is not -1.

isNonNegative: undefined | boolean

The numeric value of this expression is >= 0, same as isGreaterEqual(0)

isNonPositive: undefined | boolean

The numeric value of this expression is <= 0, same as isLessEqual(0)

isNotZero: undefined | boolean

The numeric value of this expression is not 0.

isOdd: undefined | boolean
isOne: undefined | boolean

The numeric value of this expression is not 1.

isPositive: undefined | boolean

The numeric value of this expression is > 0, same as isGreater(0)

isPrime: undefined | boolean
isZero: undefined | boolean

The numeric value of this expression is 0.

Function Expression

nops: number

If this expression is a function, the number of operands, otherwise 0.

Note that a function can have 0 operands, so to check if this expression is a function, check if this.ops !== null instead.

Note applicable to canonical and non-canonical expressions.

First operand, i.e.this.ops[0]

Note applicable to canonical and non-canonical expressions.

Second operand, i.e.this.ops[1]

Note applicable to canonical and non-canonical expressions.

Third operand, i.e. this.ops[2]

Note applicable to canonical and non-canonical expressions.

ops: null | BoxedExpression[]

The list of arguments of the function, its "tail".

If the expression is not a function, return null.

Note applicable to canonical and non-canonical expressions.

Numeric Expression

numericValue:
    | null
    | number
    | Decimal
    | Complex
    | Rational

Return the value of this expression, if a number literal.

Note it is possible for numericValue to be null, and for isNotZero to be true. For example, when a symbol has been defined with an assumption.

Conversely, isNumber may be true even if numericValue is null, example the symbol Pi return true for isNumber but numericValue is null. It's value can be accessed with .value.numericValue

sgn:
    | undefined
    | null
    | 0
    | 1
    | -1

Return the following, depending on the value of this expression:

  • -1 if it is < 0
  • 0 if it is = 0
  • +1 if it is > 0
  • undefined this value may be positive, negative or zero. We don't know right now (a symbol with an Integer domain, but no currently assigned value, for example)
  • null this value will never be positive, negative or zero (NaN, a string or a complex number for example)

Note that complex numbers have no natural ordering, so if the value is a complex number, sgn is either 0, or null

If a symbol, this does take assumptions into account, that is this.sgn will return 1 if isPositive is true, even if this expression has no value

Other

baseDefinition: undefined | BoxedBaseDefinition

For symbols and functions, a possible definition associated with the expression. baseDefinition is the base class of symbol and function definition.

Note undefined if not a canonical expression.

complexity: undefined | number

Expressions with a higher complexity score are sorted first in commutative functions

Note undefined if not a canonical expression.

description: undefined | string[]

An optional short description if a symbol or function expression.

May include markdown. Each string is a paragraph.

Note undefined if not a canonical expression.

The Compute Engine associated with this expression provides a context in which to interpret it, such as definition of symbols and functions.

ComputeEngine is the class that implements IComputeEngine.

errors: BoxedExpression[]

All the ["Error"] subexpressions

Note applicable to canonical and non-canonical expressions.

freeVariables: string[]

All the identifiers (symbols and functions) in the expression that are not a local variable or a parameter of that function.

functionDefinition: undefined | BoxedFunctionDefinition

For functions, a possible definition associated with the expression.

Note undefined if not a canonical expression or not a function.

hash: number
head: string | BoxedExpression

All boxed expressions have a head.

If not a function this can be Symbol, String, Number or Dictionary.

If the head expression can be represented as a string, it is returned as a string.

Note applicable to canonical and non-canonical expressions. The head of a non-canonical expression may be different than the head of its canonical counterpart. For example the canonical counterpart of ["Divide", 5, 7] is ["Rational", 5, 5].

isConstant: boolean

True if the expression is a constant, that is a symbol with an immutable value

isExact: boolean

An exact value is not further transformed when evaluated. To get an approximate evaluation of an exact value, use .N().

Non-exact values includes:

  • numbers with a fractional part
  • complex numbers with a real or imaginary fractional part
isNothing: boolean

If this is the Nothing symbol, return true.

Note applicable to canonical and non-canonical expressions.

isPure: boolean

If true, the value of the expression never changes and evaluating it has no side-effects. If false, the value of the expression may change, if the value of other expression changes or for other reasons.

If this.isPure is false, this.value is undefined. Call this.evaluate() to determine the value of the expression instead.

As an example, the Random function is not pure.

Note applicable to canonical and non-canonical expressions.

MathJSON representation of this expression.

Note applicable to canonical and non-canonical expressions.

rank: number

Return 0 for a scalar, 1 for a vector, 2 for a matrix, > 2 for a multidimensional matrix. It's the length of expr.shape

rawJson: Expression

For debugging, the raw MathJSON representation of this expression without decanonicalization.

scope: null | RuntimeScope

The scope in which this expression has been defined. Is null when the expression is not canonical.

shape: number[]

The shape describes the axis of the expression. When the expression is a scalar (number), the shape is []. When the expression is a vector, the shape is [n]. When the expression is a matrix, the shape is [n, m].

subexpressions: BoxedExpression[]

All the subexpressions in this expression, recursively

Note applicable to canonical and non-canonical expressions.

symbolDefinition: undefined | BoxedSymbolDefinition

For symbols, a possible definition associated with the expression.

Note undefined if not a symbol

symbols: string[]

All the symbols in the expression, recursively

Note applicable to canonical and non-canonical expressions.

unknowns: string[]

All the identifiers used in the expression that do not have a value associated with them, i.e. they are declared but not defined.

url: undefined | string

An optional URL pointing to more information about the symbol or function head.

Note undefined if not a canonical expression.

wikidata: undefined | string

Wikidata identifier.

Note undefined if not a canonical expression.

  • get canonical(): BoxedExpression
  • Return the canonical form of this expression.

    If this is a function expressin, a definition is associated with the canonical expression.

    When determining the canonical form the following function definition flags are applied:

    • associative: \( f(a, f(b), c) \longrightarrow f(a, b, c) \)
    • idempotent: \( f(f(a)) \longrightarrow f(a) \)
    • involution: \( f(f(a)) \longrightarrow a \)
    • commutative: sort the arguments.

    If his expression is already canonical, the value of canonical is this.

    Returns BoxedExpression

  • get domain(): undefined | BoxedDomain
  • The domain of the value of this expression.

    If a function expression, the domain of the value of the function (the codomain of the function).

    If a symbol the domain of the value of the symbol.

    Use expr.head to determine if an expression is a symbol or function expression.

    Note: if non-canonical or not valid, return undefined.

    Returns undefined | BoxedDomain

  • set domain(domain): void
  • Modify the domain of a symbol.

    Note: If non-canonical, does nothing.

    Parameters

    Returns void

  • get isCanonical(): boolean
  • If true, this expression is in a canonical form.

    Note applicable to canonical and non-canonical expressions.

    Returns boolean

  • set isCanonical(val): void
  • Internal

    For internal use only, set when a canonical expression is created.

    Parameters

    • val: boolean

    Returns void

  • get latex(): string
  • LaTeX representation of this expression.

    The serialization can be customized with ComputeEngine.latexOptions

    Note applicable to canonical and non-canonical expressions.

    Returns string

  • set latex(val): void
  • Internal

    Note applicable to canonical and non-canonical expressions.

    Parameters

    • val: string

    Returns void

  • get value():
        | undefined
        | string
        | number
        | boolean
        | number[]
  • Return a JavaScript primitive representing the value of this expression.

    Equivalent to expr.N().valueOf().

    Returns
        | undefined
        | string
        | number
        | boolean
        | number[]

  • set value(value): void
  • Only the value of variables can be changed (symbols that are not constants).

    Throws a runtime error if a constant.

    Note: If non-canonical, does nothing.

    Parameters

    • value:
          | undefined
          | string
          | number
          | boolean
          | number[]
          | Decimal
          | Complex
          | BoxedExpression
          | {
              im: number;
              re: number;
          }
          | {
              denom: number;
              num: number;
          }

    Returns void

  • Internal

    Update the definition associated with this expression, using the current scope (ce.context).

    Returns void

  • Parameters

    • Optionalto: "javascript"
    • Optionaloptions: {
          optimize: ("evaluate" | "simplify")[];
      }
      • optimize: ("evaluate" | "simplify")[]

    Returns undefined | ((args: Record<string, any>) => any)

  • Return the number of exprs that matched the pattern

    Parameters

    Returns number

  • Return the value of the canonical form of this expression.

    A pure expression always return the same value and has no side effects. If expr.isPure is true, expr.value and expr.evaluate() are synonyms.

    For an impure expression, expr.value is undefined.

    Evaluating an impure expression may have some side effects, for example modifying the ComputeEngine environment, such as its set of assumptions.

    Only exact calculations are performed, no approximate calculations on decimal numbers (non-integer numbers). Constants, rational numbers and square root of rational numbers are preserved.

    To perform approximate calculations, use expr.N() instead.

    The result of expr.evaluate() may be the same as expr.simplify().

    The result is in canonical form.

    Parameters

    Returns BoxedExpression

  • All the subexpressions matching the head

    Note applicable to canonical and non-canonical expressions.

    Parameters

    • head: string

    Returns BoxedExpression[]

  • True if the expression includes a symbol v or a function head v.

    Note applicable to canonical and non-canonical expressions.

    Parameters

    • v: string | string[]

    Returns boolean

  • Internal

    Infer the domain of this expression.

    If the domain of this expression is already known, return false.

    If the domain was not set, set it to the inferred domain, return true If the domain was previously inferred, adjust it by widening it, return true

    Parameters

    Returns boolean

  • Attempt to match this expression to the rhs expression.

    If rhs does not match, return null.

    Otherwise return an object literal.

    If this expression includes wildcards (symbols with a name that starts with _), the object literal will include a prop for each matching named wildcard.

    If rhs matches this pattern but there are no named wildcards, return the empty object literal, {}.

    Note applicable to canonical and non-canonical expressions.

    Parameters

    Returns null | BoxedSubstitution

  • Return a numeric approximation of the canonical form of this expression.

    Any necessary calculations, including on decimal numbers (non-integers), are performed.

    The calculations are performed according to the numericMode and precision properties of the ComputeEngine.

    To only perform exact calculations, use this.evaluate() instead.

    If the function is not numeric, the result of this.N() is the same as this.evaluate().

    The result is in canonical form.

    Parameters

    Returns BoxedExpression

  • Transform the expression by applying the rules: if the lhs of a rule matches, it is replaced by its rhs.

    If no rules apply, return null.

    See also subs for a simple substitution.

    Note applicable to canonical and non-canonical expressions.

    Parameters

    Returns null | BoxedExpression

  • Internal

    Reset the cached value associated with this expression.

    Use when the environment has changed, for example the numeric mode or precision, to force the expression to be re-evaluated.

    Returns void

  • Return a simpler form of the canonical form of this expression.

    A series of rewriting rules are applied repeatedly, until no more rules apply.

    If a custom simplify handler is associated with this function definition, it is invoked.

    The values assigned to symbols and the assumptions about symbols may be used, for example arg.isInteger or arg.isPositive.

    No calculations involving decimal numbers (numbers that are not integers) are performed but exact calculations may be performed, for example:

    \( \sin(\frac{\pi}{4}) \longrightarrow \frac{\sqrt{2}}{2} \).

    The result is in canonical form.

    Parameters

    Returns BoxedExpression

  • Parameters

    Returns null | BoxedExpression[]

  • Replace all the symbols in the expression as indicated.

    Note the same effect can be achieved with this.replace(), but using this.subs() is more efficient, and simpler.

    Note applicable to canonical and non-canonical expressions.

    Parameters

    Returns BoxedExpression

  • If expr matches the pattern, return true, otherwise false

    Parameters

    Returns boolean

Primitive Methods

  • Similar toexpr.valueOf() but includes a hint.

    Parameters

    • hint: "string" | "number" | "default"

    Returns null | string | number

  • From Object.is(). Equivalent to BoxedExpression.isSame()

    Parameters

    • rhs: unknown

    Returns boolean

  • Output to the console a string representation of the expression.

    Returns void

  • Used by JSON.stringify() to serialize this object to JSON.

    Method version of expr.json.

    Returns Expression

  • From Object.toString(), return a string representation of the expression. This string is suitable to be output to the console for debugging, for example. To get a LaTeX representation of the expression, use expr.latex.

    Used when coercing a BoxedExpression to a String.

    Returns string

  • From Object.valueOf(), return a primitive value for the expression.

    If the expression is a machine number, or bignum or rational that can be converted to a machine number, return a JavaScript number.

    If the expression is a symbol, return the name of the symbol as a string.

    Otherwise return a JavaScript primitive representation of the expression.

    Returns
        | string
        | number
        | boolean
        | any[]

Relational Operator

  • Mathematical equality (strong equality), that is the value of this expression and of rhs are numerically equal.

    The numeric value of both expressions are compared.

    Numbers whose difference is less than engine.tolerance are considered equal. This tolerance is set when the engine.precision is changed to be such that the last two digits are ignored.

    Parameters

    Returns boolean

  • The numeric value of both expressions are compared.

    Parameters

    Returns undefined | boolean

  • The numeric value of both expressions are compared.

    Parameters

    Returns undefined | boolean

  • If the expressions cannot be compared, return undefined

    The numeric value of both expressions are compared.

    Parameters

    Returns undefined | boolean

  • The numeric value of both expressions are compared.

    Parameters

    Returns undefined | boolean

  • Structural/symbolic equality (weak equality).

    ce.parse('1+x').isSame(ce.parse('x+1')) is false

    Note applicable to canonical and non-canonical expressions.

    Parameters

    Returns boolean

String Expression

string: null | string

If this expression is a string, return the value of the string. Otherwise, return null.

Note applicable to canonical and non-canonical expressions.

Symbol Expression

isValid: boolean

true if this expression or any of its subexpressions is an ["Error"] expression.

Note applicable to canonical and non-canonical expressions. For non-canonical expression, this may indicate a syntax error while parsing LaTeX. For canonical expression, this may indicate argument domain mismatch, or missing or unexpected arguments.

symbol: null | string

If this expression is a symbol, return the name of the symbol as a string. Otherwise, return null.

Note applicable to canonical and non-canonical expressions.