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
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) 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
"=" Boolean
xor Boolean
Source:

(static) constants

Mathematical constants

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
verb String
dec String
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
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
"+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
":" 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
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

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

Dictionary of functions type: function(value,display:boolean) -> string which convert a JME token to a string for display

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) castToType(tok, type) → {Numbas.jme.token}

Cast a token to the given type, if possible

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 content. To substitute variables, 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) 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) 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) → {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
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) → {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

Returns:
Type Description
Numbas.jme.tree
Source:

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

Substitute variables into a text string (not maths).

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,...]a

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

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

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

(static) tokenToDisplayString(v) → {String}

Produce a string representation of the given token, for display

Parameters:
Name Type Description
v Numbas.jme.token
Returns:
Type Description
String
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) 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

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

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

Parameters:
Name Type Description
r1 Number
r2 Number
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:

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,

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:

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_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: