Jump to navigation

Namespace: jme

Numbas.jme

Members

(static, readonly) arity :number

Arities of built-in operations.

Type:
Properties:
Name Type Description
"!" number
not number
fact number
"+u" number
"-u" number
"/u" number
sqrt number
Source:

(static, readonly) associative :boolean

Operations which are associative, i.e. (a∘b)∘c = a∘(b∘c).

Type:
Properties:
Name Type Description
"*" boolean
"+" boolean
and boolean
or boolean
xor boolean
Source:

(static) builtinScope :Numbas.jme.Scope

The built-in JME evaluation scope.

Type:
Source:

(static) builtin_constants :Array.<Numbas.jme.constant_definition>

Definitions of constants to include in Numbas.jme.builtinScope.

Type:
Source:

(static) checkingFunctions :Numbas.jme.checkingFunction

Numerical comparison functions.

Type:
Properties:
Name Type Description
absdiff Numbas.jme.checkingFunction

Absolute difference between variables - fail if Math.abs(r1-r2) is bigger than tolerance.

reldiff Numbas.jme.checkingFunction

Relative (proportional) difference between variables - fail if r1/r2 - 1 is bigger than tolerance.

dp Numbas.jme.checkingFunction

Round both values to tolerance decimal places, and fail if unequal.

sigfig Numbas.jme.checkingFunction

Round both values to tolerance significant figures, and fail if unequal.

Source:

(static, readonly) commutative :boolean

Operations which commute.

Type:
Properties:
Name Type Description
"*" boolean
"+" boolean
and boolean
or boolean
"=" boolean
xor boolean
Source:

(static) converseOps :string

Binary operations which have an equivalent operation written the other way round.

Type:
Properties:
Name Type Description
"<" string
">" string
"<=" string
">=" string
Source:

(static) findvarsOps :Numbas.jme.findvars

Custom findvars behaviour for specific functions - for a given usage of a function, work out which variables it depends on.

Type:
Source:
See:

(static, readonly) funcSynonyms :string

Synonyms of function names - keys in this dictionary are translated to their corresponding values.

Type:
Properties:
Name Type Description
sqr string
gcf string
sgn string
len string
length string
dec string
Source:

(static) getNameInfo

Establish properties of a variable name, for the purposes of display.

Source:

(static) lazyOps

Operations which evaluate lazily - they don't need to evaluate all of their arguments.

Source:

(static) mapFunctions :function

Functions for 'map', by the type of the thing being mapped over. Functions take a JME expression lambda, a name or list of names to map, a value to map over, and a scope to evaluate against.

Type:
Source:

(static, readonly) opSynonyms :string

Synonyms of operator names - keys in this dictionary are translated to their corresponding values.

Type:
Properties:
Name Type Description
"&" string
"&&" string
divides string
"||" string
"÷" string
"×" string
"∈" string
"∧" string
"∨" string
"¬" string
"⟹" string
"≠" string
"≥" string
"≤" string
"ˆ" string
identical string
Source:

(static, readonly) postfixForm :string

Some names represent different operations when used as prefix. This dictionary translates them.

Type:
Properties:
Name Type Description
"!" string
Source:

(static, readonly) precedence :number

Operator precedence - operators with lower precedence are evaluated first.

Type:
Properties:
Name Type Description
";" number
fact number
not number
sqrt number
"+u" number
"-u" number
"/u" number
"^" number
"*" number
"/" number
"+" number
"-" number
"|" number
".." number
"#" number
except number
in number
"<" number
">" number
"<=" number
">=" number
"<>" number
"=" number
isa number
and number
or number
xor number
implies number
"of:" number
"where:" number
"for:" number
":" number
Source:

(static, readonly) prefixForm :string

Some names represent different operations when used as prefix. This dictionary translates them.

Type:
Properties:
Name Type Description
"+" string
"-" string
"/" string
"!" string
not string
sqrt string
Source:

(static, readonly) relations :boolean

Operations representing relations.

Type:
Properties:
Name Type Description
"<" boolean
">" boolean
"<=" boolean
">=" boolean
"=" boolean
"<>" boolean
in boolean
Source:

(static) rightAssociative

Right-associative operations.

Source:

(static) standardParser :Numbas.jme.Parser

A standard parser for JME expressions.

Type:
Source:

(static) substituteTreeOps :Numbas.jme.substituteTree

Custom substituteTree behaviour for specific functions - for a given usage of a function, substitute in variable values from the scope.

Functions have the signature <tree with function call at the top, scope, allowUnbound>.

Type:
Source:
See:

(static) tokenComparisons

Functions to compare two tokens of the same type. Returns -1 if a<b, 0 if a=b, and 1 if a>b.

Source:
See:

(static) treesSame

Are the two given trees exactly the same?

Source:

(static) typeToDisplayString :function

Dictionary of functions which convert a JME token to a string for display.

Type:
Properties:
Name Type Description
number function
rational function
decimal function
string function
html function
Source:

Methods

(static) addBinaryOperator(name, options)

Add a binary operator to the standard parser.

Parameters:
Name Type Description
name string
options Numbas.jme.operatorOptions
Source:

(static) addPostfixOperator(name, alt, options)

Add a postfix operator to the parser.

Parameters:
Name Type Description
name string
alt string

The "interpreted" name of the operator, e.g. '!' is interpreted as 'fact'. If not given, the value of name is used.

options Numbas.jme.operatorOptions
Source:

(static) addPrefixOperator(name, alt, options)

Add a prefix operator to the parser.

Parameters:
Name Type Description
name string
alt string

The "interpreted" name of the operator, e.g. '!' is interpreted as 'fact'. If not given, the value of name is used.

options Numbas.jme.operatorOptions
Source:

(static) castArgumentsToSignature(signature, args) → {Array.<Numbas.jme.token>}

Cast a list of arguments to match a function signature.

Parameters:
Name Type Description
signature Array.<Numbas.jme.signature_grammar_match>

A list of either types to cast to, or 'missing', representing a space that should be fillined in with 'nothing'.

args Array.<Numbas.jme.token>

The arguments to the function.

Returns:
Type Description
Array.<Numbas.jme.token>
Source:

(static) castToType(tok, type) → {Numbas.jme.token}

Cast a token to the given type, if possible. If type is an object, it can give more detailed information about the types of items in a collection. The object should have a property type describing the type of the resulting collection object, and one of items, an array or object describing the type of each item individually, or all_items, a string or object describing the type of every item. For lists, the items array can contain an object with property missing: true for items that are not present in the input token. A placeholder 'nothing' value is included in the output list instead.

Parameters:
Name Type Description
tok Numbas.jme.token
type string | object
Returns:
Type Description
Numbas.jme.token
Source:

(static) compare(tree1, tree2, settings, scope) → {boolean}

Compare two expressions over some randomly selected points in the space of variables, to decide if they're equal.

Parameters:
Name Type Description
tree1 JME
tree2 JME
settings Numbas.jme.compare_settings
scope Numbas.jme.Scope
Returns:
Type Description
boolean
Source:

(static) compareTokens(a, b) → {number}

Compare two tokens, for the purposes of sorting. Uses JavaScript comparison for numbers, strings and booleans, and Numbas.jme.compareTrees for everything else, or when types differ.

Parameters:
Name Type Description
a Numbas.jme.token
b Numbas.jme.token
Returns:
Type Description
number

-1 if a < b, 1 if a > b, else 0.

Source:
See:

(static) compareTokensByValue(a, b) → {boolean}

Use JS comparison operators to compare the value property of both tokens. Used when the token wraps a JS built-in type, such as string, number or boolean.

Parameters:
Name Type Description
a Numbas.jme.token
b Numbas.jme.token
Returns:
Type Description
boolean
Source:
See:

(static) compareTrees(a, b) → {number}

Compare two trees.

  • Compare lists of variables lexically using Numbas.jme.varsUsed; longest goes first if one is a prefix of the other
  • then monomials before anything else
  • then by data type
  • then by function name
  • otherwise return 0.
Parameters:
Name Type Description
a Numbas.jme.tree
b Numbas.jme.tree
Returns:
Type Description
number

-1 if a should appear to the left of b, 0 if equal, 1 if a should appear to the right of b.

Source:

(static) compile(expr) → {Numbas.jme.tree}

Wrapper around Numbas.jme.Parser#compile.

Parameters:
Name Type Description
expr JME
Returns:
Type Description
Numbas.jme.tree
Source:
See:

(static) compileList(expr) → {Array.<Numbas.jme.tree>}

Compile a list of expressions, separated by commas.

Parameters:
Name Type Description
expr JME
Returns:
Type Description
Array.<Numbas.jme.tree>
Source:
See:

(static) contentsubvars(str, scope, sub_texopt) → {string}

Substitute variables into a string. To substitute variables into an HTML element, use Numbas.jme.variables.DOMcontentsubvars.

Parameters:
Name Type Attributes Default Description
str string
scope Numbas.jme.Scope
sub_tex boolean <optional>
false

Substitute into TeX? Normally this is left to MathJax.

Returns:
Type Description
string
Source:

(static) escape(str) → {string}

Escape a string so that it will be interpreted correctly by the JME parser.

Parameters:
Name Type Description
str string
Returns:
Type Description
string
Source:
See:

(static) evaluate(tree, scope) → {Numbas.jme.token}

Evaluate a syntax tree (or string, which is compiled to a syntax tree), with respect to the given scope.

Parameters:
Name Type Description
tree Numbas.jme.tree | string
scope Numbas.jme.Scope
Returns:
Type Description
Numbas.jme.token
Source:

(static) findCompatibleType(a, b) → {string}

Find a type that both types a and b can be automatically cast to, or return undefined.

Parameters:
Name Type Description
a string
b string
Returns:
Type Description
string
Source:

(static) findvars(tree, boundvars, scope) → {Array.<string>}

Find all variables used in given syntax tree.

Parameters:
Name Type Description
tree Numbas.jme.tree
boundvars Array.<string>

Variables to be considered as bound (don't include them).

scope Numbas.jme.Scope
Returns:
Type Description
Array.<string>
Source:

(static) isDeterministic(expr, scope) → {boolean}

Does this expression behave deterministically?

True if all functions or operations in the expression are marked deterministic.

Note that this is not just the converse of Numbas.jme.isRandom: to be conservative, a third option of "unknown", corresponding to "not isRandom and not isDeterministic", is possible. In that case, this function returns false.

Parameters:
Name Type Description
expr Numbas.jme.tree
scope Numbas.jme.Scope
Returns:
Type Description
boolean
Source:

(static) isFunction(tok, name) → {boolean}

Is a token a function with the given name?

Parameters:
Name Type Description
tok Numbas.jme.token
name string
Returns:
Type Description
boolean
Source:

(static) isMonomial(tree) → {object}

Is this a monomial - a single term of the form x^n or m*x^n, where m and n are numbers?

Parameters:
Name Type Description
tree Numbas.jme.tree
Returns:
Type Description
object

The base, degree and coefficient of the monomial, as trees.

Source:

(static) isName(tok, name) → {boolean}

Is a token the given name?

Parameters:
Name Type Description
tok Numbas.jme.token
name string
Returns:
Type Description
boolean
Source:

(static) isOp(tok, op) → {boolean}

Is a token an operator with the given name?

Parameters:
Name Type Description
tok Numbas.jme.token
op string
Returns:
Type Description
boolean
Source:

(static) isRandom(expr, scope) → {boolean}

Does this expression behave randomly? True if it contains any instances of functions or operations, defined in the given scope, which could behave randomly.

Parameters:
Name Type Description
expr Numbas.jme.tree
scope Numbas.jme.Scope
Returns:
Type Description
boolean
Source:

(static) isType(tok, type) → {boolean}

Is a token of the given type, or can it be automatically cast to the given type?

Parameters:
Name Type Description
tok Numbas.jme.token
type string
Returns:
Type Description
boolean
Source:

(static) isTypeCompatible(a, b) → {boolean}

Can type a be automatically cast to type b?

Parameters:
Name Type Description
a string
b string
Returns:
Type Description
boolean
Source:

(static) jme.sortTokensBy(fn) → {function}

Produce a comparison function which sorts tokens after applying a function to them.

Parameters:
Name Type Description
fn function

take a token and return a token

Returns:
Type Description
function
Source:

(static) makeSafe(t) → {Numbas.jme.token}

Mark a token as 'safe', so it doesn't have Numbas.jme.subvars applied to it, or any strings it contains, when it's evaluated.

Parameters:
Name Type Description
t Numbas.jme.token
Returns:
Type Description
Numbas.jme.token
Source:

(static) resultsEqual(r1, r2, checkingFunction, checkingAccuracy, scope) → {boolean}

Check that two values are equal.

Parameters:
Name Type Description
r1 Numbas.jme.token
r2 Numbas.jme.token
checkingFunction function

One of Numbas.jme.checkingFunctions.

checkingAccuracy number
scope Numbas.jme.Scope

The scope to use for normalising names.

Returns:
Type Description
boolean
Source:

(static) shunt(tokens) → {Numbas.jme.tree}

Wrapper around Numbas.jme.Parser#shunt.

Parameters:
Name Type Description
tokens Array.<Numbas.jme.token>
Returns:
Type Description
Numbas.jme.tree
Source:
See:

(static) substituteTree(tree, scope, allowUnboundopt, unwrapExpressionsopt) → {Numbas.jme.tree}

Substitute variables defined in scope into the given syntax tree (in place).

Parameters:
Name Type Attributes Default Description
tree Numbas.jme.tree
scope Numbas.jme.Scope
allowUnbound boolean <optional>
false

Allow unbound variables to remain in the returned tree.

unwrapExpressions boolean <optional>
false

Unwrap TExpression tokens?

Returns:
Type Description
Numbas.jme.tree
Source:

(static) subvars(str, scope, displayopt) → {string}

Substitute variables into a text string (not maths).

Warning: when display = true, subbed-in values might not be bracketed correctly. Use Numbas.jme.display.subvars to substitute values into JME expressions.

Parameters:
Name Type Attributes Default Description
str string
scope Numbas.jme.Scope
display boolean <optional>
false

Is this string going to be displayed to the user? If so, avoid unnecessary brackets and quotes.

Returns:
Type Description
string
Source:

(static) texsplit(s) → {Array.<string>}

Split up a TeX expression, finding the \var and \simplify commands. Returns an array [normal tex,var or simplify,options,argument,normal tex,...].

Parameters:
Name Type Description
s string
Returns:
Type Description
Array.<string>
Source:

(static) tokenToDisplayString(v, scope) → {string}

Produce a string representation of the given token, for display.

Parameters:
Name Type Description
v Numbas.jme.token
scope Numbas.jme.Scope
Returns:
Type Description
string
Source:
See:

(static) tokenise(expr) → {Array.<Numbas.jme.token>}

Wrapper around Numbas.jme.Parser#tokenise.

Parameters:
Name Type Description
expr JME
Returns:
Type Description
Array.<Numbas.jme.token>
Source:
See:

(static) unescape(str) → {string}

Unescape a string - backslashes escape special characters.

Parameters:
Name Type Description
str string
Returns:
Type Description
string
Source:
See:

(static) unwrapSubexpression(tree) → {Numbas.jme.tree}

Unwrap TExpression tokens: if tree.tok is a TExpression token, just return its tree property. Applies recursively.

Parameters:
Name Type Description
tree Numbas.jme.tree
Returns:
Type Description
Numbas.jme.tree
Source:

(static) unwrapValue(v) → {object}

Unwrap a Numbas.jme.token into a plain JavaScript value.

Parameters:
Name Type Description
v Numbas.jme.token
Returns:
Type Description
object
Source:

(static) varsUsed(tree) → {Array.<string>}

List names of variables used in tree, obtained by depth-first search.

Differs from Numbas.jme.findvars by including duplicates, and ignoring Numbas.jme.findvarsOps.

Parameters:
Name Type Description
tree Numbas.jme.tree
Returns:
Type Description
Array.<string>
Source:

(static) wrapValue(v, typeHint) → {Numbas.jme.token}

Wrap up a plain JavaScript value (number, string, bool or array) as a Numbas.jme.token.

Parameters:
Name Type Description
v object
typeHint string

Name of the expected type (to differentiate between, for example, matrices, vectors and lists.

Returns:
Type Description
Numbas.jme.token
Source:

Type Definitions

call_signature

Type:
Properties:
Name Type Description
fn Numbas.jme.funcObj

The function to call.

signature Numbas.jme.signature

The signature to use.

Source:

checkingFunction(r1, r2, tolerance) → {boolean}

Decide if two numbers are close enough to count as equal.

Parameters:
Name Type Description
r1 number | Numbas.math.ComplexDecimal
r2 number | Numbas.math.ComplexDecimal
tolerance number

A measure of how close the results need to be to count as equal. What this means depends on the checking function.

Returns:
Type Description
boolean
  • True if r1 and r2 are close enough to be equal.
Source:

compare_settings

Settings for Numbas.jme.compare.

Type:
Properties:
Name Type Description
checkingType string

The name of the method to determine if two results are equal. See Numbas.jme.checkingFunctions.

vsetRangeStart number

The lower bound of the range to pick variable values from.

vsetRangeEnd number

The upper bound of the range to pick variable values from.

vsetRangePoints number

The number of values to pick for each variable.

checkingAccuracy number

A parameter for the checking function to determine if two results are equal. See Numbas.jme.checkingFunctions.

failureRate number

The number of times the comparison must fail to declare that the expressions are unequal.

sameVars boolean

If true, then both expressions should have exactly the same free variables.

Source:

constant_definition

A definition of a custom constant.

Properties:
Name Type Description
tex TeX

A TeX rendering of the constant

value Numbas.jme.token

The JME value of the constant.

Source:

evaluate_fn(args, scope) → {Numbas.jme.token|object}

Evaluate a JME function on a list of arguments and in a given scope.

Parameters:
Name Type Description
args Array.<(Numbas.jme.tree|Numbas.jme.token|object)>

Arguments of the function. If the function is lazy, syntax trees are passed, otherwise arguments are evaluated to JME tokens first. If the unwrapValues option is set, the arguments are unwrapped to raw JavaScript values.

scope Numbas.jme.Scope

Scope in which the function is evaluated.

Returns:
Type Description
Numbas.jme.token | object

If unwrapValues is set, the raw value of the result, otherwise a JME token.

Source:

expand_juxtapositions_options

Options for Numbas.jme.Scope.expandJuxtapositions.

Type:
Properties:
Name Type Description
singleLetterVariables boolean

Enforce single-letter variables names: a name token like xy is rewritten to x*y.

noUnknownFunctions boolean

Rewrite applications of functions not defined in this scope to products, e.g. x(y) is rewritten to x*y.

implicitFunctionComposition boolean

If function names are juxtaposed, either as a single token or as (implicit) multiplication, rewrite as composition: e.g. lnabs(x) and ln abs(x) are both rewritten to ln(abs(x)).

Source:

funcObj_options

Options for the Numbas.jme.funcObj constructor.

Type:
Properties:
Name Type Description
typecheck Numbas.jme.typecheck_fn

Check that this function can be evaluated on the given arguments.

evaluate Numbas.jme.evaluate_fn

Evaluate the function on a list of arguments and in a given scope.

unwrapValues boolean

Unwrap list elements in arguments into javascript primitives before passing to the evaluate function?

Source:

name_info

Type:
Properties:
Name Type Description
root string

The 'letters' part of the name, without subscripts or primes.

letterLength number

The number of letters in the name's root. For Greek letters, this is 1, not the the number of characters in root.

isGreek boolean

Is the root a Greek letter?

isLong boolean

Is this name 'long'? True if letterLength is more than 1.

subscript string

The subscript part of the name.

subscriptGreek string

Is the subscript a Greek letter?

primes string

The primes part of the name - a string of zero or more ' characters.

Source:

operatorOptions

Options for a JME operator.

Type:
Properties:
Name Type Description
synonyms Array.<string>

Synonyms for this operator. See Numbas.jme.opSynonyms.

precedence number

An operator with lower precedence is evaluated before one with high precedence. Only makes sense for binary operators. See Numbas.jme.precedence.

commutative boolean

Is this operator commutative? Only makes sense for binary operators.

rightAssociative boolean

Is this operator right-associative? Only makes sense for unary operators.

Source:

parser_options

Options for Numbas.jme.Parser

Type:
Properties:
Name Type Description
closeMissingBrackets boolean

Silently ignore "missing right bracket" errors?

addMissingArguments boolean

When an op or function call is missing required arguments, insert ? as a placeholder.

Source:

scope_deletions

Options for the Numbas.jme.funcObj constructor.

Type:
Properties:
Name Type Description
variables object

Names of deleted variables.

functions object

Names of deleted functions.

rulesets object

Names of deleted rulesets.

Source:

signature()

A signature-checker function. Takes a list of Numbas.jme.token objects, and returns a Numbas.jme.signature_result representing the matched arguments, or false if the signature doesn't match.

Properties:
Name Type Description
kind string

The kind of this signature checker, e.g. "type", "anything", "multiple". Used by the type inference routine, among other things.

Source:

signature_grammar_match

A match returned by @ref{Numbas.jme.parse_signature}.

Type:
Properties:
Name Type Description
0 Numbas.jme.signature
1 string
Source:

signature_result

A list of arguments matched by a signature checker. At most one per argument passed in.

Type:
Source:

signature_result_argument

Information about an argument matched by a signature checker. The main purpose is to specify the desired type of the argument, but there are other properties for certain types.

Type:
Properties:
Name Type Description
type string

The data type that the argument should be cast to.

missing boolean

Does this represent an optional argument that wasn't given?

nonspecific boolean

Does this represent an argument matched with an 'anything' signature? If so, don't use it when comparing two signature results.

Source:

token

Type:
Properties:
Name Type Description
type string
Source:
See:

tree

Type:
Properties:
Name Type Description
args Array.<Numbas.jme.tree>

The token's arguments (if it's an op or function).

tok Numbas.jme.token

The token at this node.

Source:

typecheck_fn(variables) → {boolean}

A function which checks whether a Numbas.jme.funcObj can be applied to the given arguments.

Parameters:
Name Type Description
variables Array.<Numbas.jme.token>
Returns:
Type Description
boolean
Source: