Symbols
A symbol is an identifier representing a named mathematical object. It belongs to a domain and it may hold a value. A symbol without a value represents a mathematical unknown in an expression.
To change the value or domain of a symbol, use the value
and domain
properties of the symbol.
A symbol does not have to be declared before it can be used. A previously
unknown symbol has a domain of ce.defaultDomain
and no value.
To get a list of all the symbols in an expression use expr.symbols
.
Scope
Symbols are defined within a scope.
Unknowns and Constants
A symbol that has been declared, but has no values associated with it, is said to be an unknown.
A symbol whose value cannot be changed is a constant. Constants are identified by a special flag in their definition.
The property expr.isConstant
is true
if a symbol is a constant.
console.log(ce.box("x").isConstant);
// ➔ false
console.log(ce.box("Pi").isConstant);
// ➔ true
The value of constants may depend on settings of the Compute Engine. For
example, the value of Pi
is determined based on the value of the precision
property. The values of constants in scope when the precision
setting is
changed will be updated.
ce.precision = 4;
const smallPi = ce.box("Pi"); // π with 4 digits
console.log(smallPi.latex);
// ➔ 3.1415
ce.precision = 10;
const bigPi = ce.box("Pi"); // π with 10 digits
console.log(bigPi.latex);
// ➔ 3.1415926535
ce.precision = 100; // Future computations will be done with 100 digits
console.log("pi = ", smallPi.numericValue, "=", bigPi.numericValue);
// ➔ pi = 3.1415 = 3.1415926535
Automatic Declaration of Symbols
If ce.defaultDomain
is not null
, an unknown symbol is automatically declared
when it is first used in an expression.
The new definition has a domain of ce.defaultDomain
and no value associated
with it, so the symbol will be an unknwon.
By default, defaultDomain
is "ExtendedRealNumbers"
so any unknown variable
is automatically assumed to be a real number. Use ce.declare()
to explictly
specify the domain of a symbol.
const symbol = ce.box("m"); // m for mystery
console.log(symbol.domain.symbol);
// ➔ "ExtendedRealNumbers"
symbol.value = 5;
console.log(symbol.numericValue);
// ➔ 5
If ce.defaultDomain
is null
, and no definition exist for the symbol, the
symbol is unbound (no name binding). This will limit the usefulness of the
symbol and the symbol will evaluate to an ["Error"]
expression.
Forgetting a Symbol
To reset what is known about a symbol use the ce.forget()
function.
The ce.forget()
function will remove the definition associated with a symbol,
including its domain and value, and any
assumptions about the symbol.
To forget about a specific symbol, pass the name of the symbol as an
argument to ce.forget()
.
To forget about all the symbols in the current scope, use ce.forget()
without any arguments.
Note that only symbols in the current scope are forgotten. If a definition for the symbol existed in a previous scope, that definition will now be in effect.