JME¶
JME expressions are used by students to enter answers to algebraic questions, and by question authors to define variables. JME syntax is similar to what you’d type on a calculator.
Syntax¶
Expressions are strings of text, made of variable names, literal values, grouped terms, function applications, operators, collections and indices.
Whitespace (space characters, tabs, newlines, etc.) can be used to separate literal values, for example a 2 is not interpreted the same as a2.
For all other purposes, whitespace is ignored.
Variable names¶
The first character of a variable name must be an alphabet letter; after that, any combination of letters, numbers and underscores is allowed, with any number of ' on the end.
- Examples:
xx_1time_between_trialsvar1row1val2y''
This screencast describes which variable names are valid, and gives some advice on how you should pick names:
Variable name annotations¶
Names can be given annotations to change how they are displayed. The following annotations are built-in:
verb– does nothing, but names likei,piandeare not interpreted as the famous mathematical constants.op– denote the name as the name of an operator — wraps the name in the LaTeX\operatornamecommand when displayedvorvector– denote the name as representing a vector — the name is displayed in boldfaceunit– denote the name as representing a unit vector — places a hat above the name when displayeddot– places a dot above the name when displayed, for example when representing a derivativemormatrix– denote the name as representing a matrix — displayed using a non-italic fontdiff- denote the name as a differential - the name is prefixed with\mathrm{d}when displayed, e.g. \(\mathrm{d}x\)degrees- denote the name as representing an angle in degrees - the name has a suffix of^{\circ}when displayed, e.g. \(x^{\circ}\)
Any other annotation is taken to be a LaTeX command.
For example, a name vec:x is rendered in LaTeX as \vec{x}, which places an arrow above the name.
You can apply multiple annotations to a single variable.
For example, v:dot:x produces a bold x with a dot on top: \(\boldsymbol{\dot{x}}\).
Names with different annotations are considered to represent different values, for the purpose of simplification and evaluation.
For example, in the expression dot:x + x, the two terms will not be collected together.
Literal values¶
boolean, integer, number and string data types can be constructed with literal values.
Examples: true, 1, 4.3, "Numbas"
There are also some built-in constants which are interpreted as number values:
piorπ- the ratio of a circle’s circumference to its diameter;e- the base of the natural logarithm;i- the square root of -1;infinity,inftyor∞- infinity;nan- “Not a number”, sometimes returned by JavaScript functions.
You can disable, override or define new constants in the question editor’s Constants tab.
Grouped terms¶
Use parentheses ( ) to group terms.
For example:
(a+2)(a+1)
No other symbols can be used to group terms - square brackets [ ] and curly braces { } have other meanings in Numbas.
Function application¶
A name followed immediately by a bracketed group is interpreted as a function application. Function names can be annotated similarly to variables.
A function takes one or more arguments, separated by commas. Each argument is a JME expression.
- Examples:
f(x)g(a,b)
Operators¶
There are three kinds of operators:
- Binary¶
A symbol written between two terms, for example
a + b(“\(a\) plus \(b\)”). Each binary operator has a precedence which determines when it is evaluated in relation to other operators.- Prefix¶
A symbol written to the left of a term, for example
!a(“not \(a\)”).- Postfix¶
A symbol written to the right of a term, for example
a!(“\(a\) factorial”).
Relations¶
Some binary operators are marked as relations.
Relations can be chained together, for example a<b<c is interpreted as a<b and b<c.
The relation operators are <, <=, >, >=, =, <> and in.
Collections¶
There are two kinds of collection: lists and dictionaries.
Both are written as a series of terms written between square brackets and separated by commas.
For a dictionary, each term is a keypair: a variable name or string followed by a colon and an expression.
For example, [a: 1, "first name": "Owen"] is a dictionary with keys "a" and "first name".
A collection made of any other kind of term is interpreted as a list.
An empty pair of brackets [] is interpreted as an empty list.
To construct an empty dictionary, use dict().
- Examples:
[1,2,3][a][]
Indices¶
Many JME data types support indexing.
An index is a single term inside square brackets immediately following another term.
The first element in a list has index 0.
Negative indices read from the end: the index -1 corresponds to the last item.
- Examples:
[1,2,3][0]produces the first element in the list,1.x[3..7]produces the fourth to the eighth elements of the listx.id(4)[1]produces the second row of a \(4 \times 4\) identity matrix,vector(0,1,0,0).info["name"]returns the value corresponding to the key"name"in the dictionaryinfo."Numbas"[0]produces the first letter of the string"Numbas","N".
Implicit multiplication¶
JME supports implicit multiplication in some cases, allowing you to omit the multiplication symbol:
- Examples:
(a+1)2 = (a+1)*2(x+y)z = (x+y)*z2x = 2*xx y = x*y
Warning
Note that a name followed by a bracket is not always interpreted as implicit multiplication. Instead, it’s interpreted as a function application.
To interpret such expressions as products, in a mathematical expression part turn off the Allow unknown function names? option, and when dealing with expression values, use expand_juxtapositions().
Case-sensitivity¶
The default behaviour of variable and function names in JME expressions is that they are considered case-insensitively: two names are considered equivalent if they are exactly equal when converted to lower-case.
You can force the JME system to be case-sensitive using the scope_case_sensitive() function.
Data types¶
JME expressions are composed of the following data types. Some extensions add new data types.
- number¶
A real or complex floating-point number.
i,e,infinityandpiare reserved keywords for the imaginary unit, the base of the natural logarithm, ∞ and π, respectively.Examples:
0.0,-1.0,0.234,i,e,piNumbers of this type are represented using JavaScript’s built-in
Numberobject, which is a 64-bit IEEE 754 floating-point number. This representation offers a very good compromise between precision and the range of values that can be stored, but can behave in unexpected ways. Accuracy is easily lost when dealing with very big or very small numbers, and on division.See functions related to Arithmetic, Number operations, Trigonometry and Number theory.
- Automatically converts to:
- integer¶
An element of the set of integers, \(\mathbb{Z}\).
Examples:
0,1,-2,431.
- rational¶
A fraction; an element of the set of rationals, \(\mathbb{Q}\). The numerator and denominator are integers.
Instances of this data type may be top-heavy, with numerator bigger than the denominator, and are not required to be reduced.
Examples:
1/1,-34/2,3/4.
- decimal¶
A number with a guaranteed level of precision, and arbitrary order of magnitude.
Numbers of this type are represented using the Decimal.js library. They’re guaranteed to be accurate to 40 significant figures. The order of magnitude is stored separately from the significant digits, so there’s no less of precision for very big or very small numbers.
Examples:
dec(0),dec("1.23e-5"),6.0221409*10^23- Automatically converts to:
- boolean¶
Booleans represent either truth or falsity. The logical operations and, or and xor operate on and return booleans.
Examples:
true,falseSee functions related to Logic and Control flow.
- string¶
Use strings to create non-mathematical text. Either
'or"can be used to delimit strings.You can escape a character by placing a single backslash character before it. The following escape codes have special behaviour:
\nNew-line
\{\{\}\}If you want to write a string which contains a mixture of single and double quotes, you can delimit it with triple-double-quotes or triple-single-quotes, to save escaping too many characters.
Examples:
"hello there",'hello there',""" I said, "I'm Mike's friend" """See functions related to Strings.
- list¶
An ordered list of elements of any data type.
Examples:
[0,1,2,3],[a,b,c],[true,false,true]See functions related to Lists.
- dict¶
A ‘dictionary’, mapping key strings to values of any data type.
A dictionary is created by enclosing one or more key-value pairs (a string followed by a colon and any JME expression) in square brackets, or with the
dictfunction.Key strings are case-sensitive.
Examples:
["a": 1, "b": 2]["name": "Tess Tuser", "age": 106, "hobbies": ["reading","writing","arithmetic"] ]dict("key1": 0.1, "key2": 1..3)dict([["key1",1], ["key2",2]])
Warning
Because lists and dicts use similar syntax,
[]produces an empty list, not an empty dictionary. To create an empty dictionary, usedict().See functions related to Dictionaries and JSON.
- range¶
A range
a..b#crepresents (roughly) the set of numbers \(\{a+nc \: | \: 0 \leq n \leq \frac{b-a}{c} \}\). If the step size is zero, then the range is the continuous interval \([a,b]\).Examples:
1..3,1..3#0.1,1..3#0See functions related to Ranges.
- set¶
An unordered set of elements of any data type. The elements are pairwise distinct - if you create a set from a list with duplicate elements, the resulting set will not contain the duplicates.
Examples:
set(a,b,c),set([1,2,3,4]),set(1..5)See functions related to Sets.
- Automatically converts to:
- vector¶
The components of a vector must be numbers.
When combining vectors of different dimensions, the smaller vector is padded with zeros to make up the difference.
Examples:
vector(1,2),vector([1,2,3,4])See functions related to Vector and matrix arithmetic.
- matrix¶
Matrices are constructed from lists of numbers, representing the rows.
When combining matrices of different dimensions, the smaller matrix is padded with zeros to make up the difference.
To retrieve the
nth row of a matrixm, writem[n]. The row is returned as avectorvalue. To retrieve the cell in rowaand columnb, writem[a][b]. Rows and columns are both numbered starting from zero.Examples:
matrix([1,2,3],[4,5,6]),matrix(row1,row2,row3)See functions related to Vector and matrix arithmetic.
- name¶
A variable name. When an expression is evaluated, all variable names are replaced withe their corresponding value in the current scope.
- function¶
An application of a function.
Examples:
f(x),sin(x)
- op¶
An infix binary operation, or a pre-/post-fix unary operation.
Examples:
x+y,n!,a and b
- expression¶
A JME sub-expression. Sub-expressions can be simplified, rearranged, pattern-matched, or evaluated using given values for their free variables.
See functions related to Sub-expressions.
Automatic data type conversion¶
Some data types can be automatically converted to others when required.
For example, the number-like types such as integer and decimal can be automatically converted to number values.
The data types of the arguments to a function application determine which version of the function is used. Ideally, this will do what you expect without you having to think about it. For reference, the process for deciding on what conversions to perform is as follows:
If there is a version of the function which takes exactly the given data types, that is used.
Otherwise, each definition of the function is compared by looking at each of the arguments, working from left to right.
A definition which does not convert an argument is preferred to one that does.
If both definitions being compared need to convert an argument, the type that occurs first in the input type’s list of automatic conversion methods is used. (This follows the order of the types under the “Automatically converts to” headers above)
The following examples illustrate how this works.
Expression |
Type of result |
Explanation |
|
The |
|
|
||
|
|
|
|
|
|
|
|
Function reference¶
Arithmetic¶
- x+y¶
Addition.
- Definitions:
dict,dict→dict- merge two dictionaries, with values from the right-hand side taking precedence when the same key is present in both dictionaries.string, anything →string- convert the right-hand argument to a string, and concatenateanything,
string→string- convert the left-hand argument to a string, and concatenate
- Examples:
1+2→3vector(1,2)+vector(3,4)→vector(4,6)matrix([1,2],[3,4])+matrix([5,6],[7,8])→matrix([6,8],[10,12])[1,2,3]+4→[1,2,3,4][1,2,3]+[4,5,6]→[1,2,3,4,5,6]"hi "+"there"→"hi there"
- x-y¶
Subtraction.
- Definitions:
- Examples:
1-2→-1vector(3,2)-vector(1,4)→vector(2,-2)matrix([5,6],[3,4])-matrix([1,2],[7,8])→matrix([4,4],[-4,-4])
- x*y¶
Multiplication.
- Definitions:
- Examples:
1*2→22*vector(1,2,3)→vector(2,4,6)matrix([1,2],[3,4])*2→matrix([2,4],[6,8])matrix([1,2],[3,4])*vector(1,2)→vector(5,11)
- x/y¶
Division. Only defined for numbers.
- x^y¶
Exponentiation.
exp(x)is a synonym fore^x.
Number operations¶
- decimal(n)¶
- decimal(x)
- dec(x)¶
Construct a
decimalvalue. Any string accepted by Decimal.js is accepted.
- rational(n)¶
Convert
nto a rational number, taking an approximation when necessary.
- int(n)¶
Convert
nto an integer, rounding to the nearest integer.
- abs(x)¶
- len(x)¶
- length(x)¶
Absolute value, length, or modulus.
- Definitions:
- Examples:
abs(-8)→8abs(3-4i)→5abs("Hello")→5abs([1,2,3])→3len([1,2,3])→3len(set([1,2,2]))→2length(vector(3,4))→5abs(vector(3,4,12))→13len(["a": 1, "b": 2, "c": 1])→3
- isint(x)¶
Returns
trueifxis an integer - that is, it is real and has no fractional part.
- log(x, b)¶
Logarithm with base
b, or base 10 ifbis not given.
- radians(x)¶
Convert degrees to radians.
- Example:
radians(180)→pi
- max(a, b)¶
Greatest of the given numbers.
- min(a, b)¶
- min(numbers)
Least of the given numbers.
- clamp(x, a, b)¶
Return the point nearest to
xin the interval \([a,b]\).Equivalent to
max(a,min(x,b)).
- precround(n, d)¶
Round
ntoddecimal places. On matrices and vectors, this rounds each element independently.
- siground(n, f)¶
Round
ntofsignificant figures. On matrices and vectors, this rounds each element independently.
- withintolerance(a, b, t)¶
Returns
trueif \(b-t \leq a \leq b+t\).
- dpformat(n, d[, style])¶
Round
ntoddecimal places and return a string, padding with zeros if necessary.If
styleis given, the number is rendered using the given notation style. See the page on Number notation for more on notation styles.
- countdp(n)¶
Assuming
nis a string representing a number, return the number of decimal places used. The string is passed throughcleannumber()first.
- sigformat(n, d[, style])¶
Round
ntodsignificant figures and return a string, padding with zeros if necessary.
- countsigfigs(n)¶
Assuming
nis a string representing a number, return the number of significant figures. The string is passed throughcleannumber()first.
- togivenprecision(str, precisionType, precision, strict)¶
Returns
trueifstris a string representing a number given to the desired number of decimal places or significant figures.precisionTypeis either"dp", for decimal places, or"sigfig", for significant figures.If
strictistrue, then trailing zeroes must be included.
- tonearest(a, b)¶
Round
ato the nearest multiple ofb.
- formatnumber(n, style)¶
Render the number
nusing the given number notation style.See the page on Number notation for more on notation styles.
- scientificnumberlatex(n)¶
Return a LaTeX string representing the given number in scientific notation, \(a \times 10^b\).
This function exists because scientific notation may use superscripts, which aren’t easily typeset in plain text.
- scientificnumberhtml(n)¶
Return an HTML element representing the given number in scientific notation, \(a \times 10^b\).
This function exists because scientific notation may use superscripts, which aren’t easily typeset in plain text.
- cleannumber(str, styles)¶
Clean a string potentially representing a number. Remove space, and then try to identify a notation style, and rewrite to the
plain-enstyle.stylesis a list of notation styles. Ifstylesis given,strwill be tested against the given styles. If it matches, the string will be rewritten using the matched integer and decimal parts, with punctuation removed and the decimal point changed to a dot.
- matchnumber(str, styles)¶
Try to match a string representing a number in any of the given styles at the start of the given string, and return both the matched text and a corresponding
numbervalue.
- parsenumber(string, style)¶
Parse a string representing a number written in the given style.
If a list of styles is given, the first that accepts the given string is used.
See the page on Number notation for more on notation styles.
- Examples:
parsenumber("1 234,567","si-fr")→1234.567parsenumber("1.001",["si-fr","eu"])→1001
- parsenumber_or_fraction(string, style)¶
Works the same as
parsenumber(), but also accepts strings of the formnumber/number, which it interprets as fractions.- Example:
parsenumber_or_fraction("1/2")→0.5
- parsedecimal(string, style)¶
Parse a string representing a number written in the given style, and return a
decimalvalue.If a list of styles is given, the first that accepts the given string is used.
See the page on Number notation for more on notation styles.
- parsedecimal_or_fraction(string, style)¶
Works the same as
parsedecimal(), but also accepts strings of the formnumber/number, which it interprets as fractions.
- tobinary(n)¶
Write the given number in binary: base 2.
- tooctal(n)¶
Write the given number in octal: base 8.
- tohexadecimal(n)¶
Write the given number in hexadecimal: base 16.
- tobase(n, base)¶
Write the given number in the given base.
basecan be any integer between 2 and 36.
- frombinary(s)¶
Convert a string representing a number written in binary (base 2) to a
integervalue.
- fromhexadecimal(s)¶
Convert a string representing a number written in hexadecimal (base 16) to a
integervalue.
Trigonometry¶
Trigonometric functions all work in radians, and have as their domain the complex numbers.
- tan(x)¶
Tangent: \(\tan(x) = \frac{\sin(x)}{\cos(x)}\)
- arcsin(x)¶
Inverse of
sin(). When \(x \in [-1,1]\),arcsin(x)returns a value in \([-\frac{\pi}{2}, \frac{\pi}{2}]\).
- arccos(x)¶
Inverse of
cos(). When \(x \in [-1,1]\),arccos(x)returns a value in \([0, \frac{\pi}]\).
- arctan(x)¶
Inverse of
tan(). When \(x\) is non-complex,arctan(x)returns a value in \([-\frac{\pi}{2}, \frac{\pi}{2}]\).
- atan2(y, x)¶
The angle in radians between the positive \(x\)-axis and the line through the origin and \((x,y)\). This is often equivalent to
arctan(y/x), except when \(x \lt 0\), when \(pi\) is either added or subtracted from the result.
- sinh(x)¶
Hyperbolic sine: \(\sinh(x) = \frac{1}{2} \left( \mathrm{e}^x - \mathrm{e}^{-x} \right)\)
- cosh(x)¶
Hyperbolic cosine: \(\cosh(x) = \frac{1}{2} \left( \mathrm{e}^x + \mathrm{e}^{-x} \right)\)
- tanh(x)¶
Hyperbolic tangent: \(\tanh(x) = \frac{\sinh(x)}{\cosh(x)}\)
- cosech(x)¶
Hyperbolic cosecant: \(\operatorname{cosech}(x) = \frac{1}{\sinh(x)}\)
- sech(x)¶
Hyperbolic secant: \(\operatorname{sech}(x) = \frac{1}{\cosh(x)}\)
Number theory¶
- x!¶
- fact(x)
Factorial. When
xis not an integer, \(\Gamma(x+1)\) is used instead.fact(x)is a synonym forx!.
- factorise(n)¶
Factorise
n. Returns the exponents of the prime factorisation ofnas a list.
- gamma(x)¶
Gamma function.
- ceil(x)¶
Round up to the nearest integer. When
xis complex, each component is rounded separately.
- floor(x)¶
Round down to the nearest integer. When
xis complex, each component is rounded separately.
- round(x)¶
Round to the nearest integer.
0.5is rounded up.
- trunc(x)¶
If
xis positive, round down to the nearest integer; if it is negative, round up to the nearest integer.
- fract(x)¶
Fractional part of a number. Equivalent to
x-trunc(x).
- rational_approximation(n[, accuracy])¶
Compute a rational approximation to the given number by computing terms of its continued fraction, returning the numerator and denominator separately. The approximation will be within \(e^{-\text{accuracy}}\) of the true value; the default value for
accuracyis 15.
- mod(a, b)¶
Modulo; remainder after integral division, i.e. \(a \bmod b\).
- perm(n, k)¶
Count permutations, i.e. \(^n \kern-2pt P_k = \frac{n!}{(n-k)!}\).
- comb(n, k)¶
Count combinations, i.e. \(^n \kern-2pt C_k = \frac{n!}{k!(n-k)!}\).
- gcd_without_pi_or_i(a, b)¶
Take out factors of \(\pi\) or \(i\) from
aandbbefore computing their greatest common denominator.
- coprime(a, b)¶
Are
aandbcoprime? True if theirgcd()is \(1\), or if either ofaorbis not an integer.
- lcm(a, b)¶
Lowest common multiple of integers
aandb. Can be used with any number of arguments; it returns the lowest common multiple of all the arguments.
Vector and matrix arithmetic¶
- vector(a1, a2, ..., aN)¶
Create a vector with given components. Alternately, you can create a vector from a single list of numbers.
- matrix(row1, row2, ..., rowN)¶
Create a matrix with given rows, which should be either vectors or lists of numbers. Or, you can pass in a single list of lists of numbers.
- id(n)¶
Identity matrix with \(n\) rows and columns.
- numrows(matrix)¶
The number of rows in the given matrix
- numcolumns(matrix)¶
The number of columns in the given matrix
- rowvector(a1, a2, ..., aN)¶
Create a row vector (\(1 \times n\) matrix) with the given components. Alternately, you can create a row vector from a single list of numbers.
- dot(x, y)¶
Dot (scalar) product. Inputs can be vectors or column matrices.
- cross(x, y)¶
Cross product. Inputs can be vectors or column matrices.
- angle(a, b)¶
Angle between vectors
aandb, in radians. Returns0if eitheraorbhas length 0.
- is_zero(x)¶
Returns
trueif every component of the vectorxis zero.
- det(x)¶
Determinant of a matrix. Throws an error if used on anything larger than a 3×3 matrix.
- transpose(x)¶
Matrix transpose.
Strings¶
- x[n]¶
Get the Nth character of the string
x. Indices start at 0.- Example:
"hello"[1]→"e"
- x[a..b]
Slice the string
x- get the substring between the given indices. Note that indices start at 0, and the final index is not included.- Example:
"hello"[1..4]→"ell"
- substring in string¶
Test if
substringoccurs anywhere instring. This is case-sensitive.- Example:
"plain" in "explains"→true
- string(x)¶
Convert
xto a string.When converting a
expressionvalue to a string, you can give a list of display options as a second argument, either as a comma-separated string or a list of strings.- Definitions:
- Example:
string(123)→"123"string(x)→"x"string(expression("0.5"))→"0.5"string(expression("0.5"),"fractionNumbers")→"1/2"
- latex(x)¶
Mark string
xas containing raw LaTeX, so when it’s included in a mathmode environment it doesn’t get wrapped in a\textrmenvironment.If
xis aexpressionvalue, it’s rendered to LaTeX.Note that backslashes must be double up, because the backslash is an escape character in JME strings.
- Definitions:
- Example:
latex('\\frac{1}{2}').latex(expression("x^2 + 3/4"))→"x^{2} + \\frac{3}{4}"
- safe(x)¶
Mark string
xas safe: don’t substitute variable values into it when this expression is evaluated.Use this function to preserve curly braces in string literals.
- render(x, values)¶
Substitute variable values into the string
x, even if it’s marked as safe (seesafe()).The optional dictionary
valuesoverrides any previously-defined values of variables.- Definitions:
- Example:
render(safe("I have {num_apples} apples."), ["num_apples": 5])→"I have 5 apples."render(safe("Let $x = \\var{x}$"), ["x": 2])→"Let $x = {2}$"
Note
The variable dependency checker can’t establish which variables will be used in the string until
renderis evaluated, so you may encounter errors if usingrenderin the definition of a question variable. You can ensure a variable has been evaluated by including it in thevaluesargument, e.g.:render("a is {}",["a": a])
This function is intended for use primarily in content areas.
- capitalise(x)¶
Capitalise the first letter of a string.
- pluralise(n, singular, plural)¶
Return
singularifnis 1, otherwise returnplural.
- lower(x)¶
Convert string to lower-case.
- join(strings, delimiter)¶
Join a list of strings with the given delimiter.
- split(string, delimiter)¶
Split a string at every occurrence of
delimiter, returning a list of the the remaining pieces.
- match_regex(pattern, str, flags)¶
If
strmatches the regular expressionpattern, returns a list of matched groups, otherwise returns an empty list.This function uses JavaScript regular expression syntax.
flagsis an optional string listing the options flags to use. If it’s not given, the default value of"u"is used.
- split_regex(string, pattern, flags)¶
Split a string at every occurrence of a substring matching the given regular expression pattern, returning a list of the the remaining pieces.
flagsis an optional string listing the options flags to use. If it’s not given, the default value of"u"is used.
- replace_regex(pattern, replacement, string, flags)¶
Replace a substring of
stringmatching the given regular expressionpatternwith the stringreplacement.flagsis an optional string listing the options flags to use. If it’s not given, the default value of"u"is used.Remember that backslashes must be doubled up inside JME strings, and curly braces are normally used to substitute in variables. You can use the
safe()function to avoid this behaviour.To replace all occurrences of the pattern, add the flag
"g".- Definitions:
- Example:
replace_regex("day","DAY","Monday Tuesday Wednesday")→"MonDAY Tuesday Wednesday"replace_regex("day","DAY","Monday Tuesday Wednesday","g")→"MonDAY TuesDAY WednesDAY"replace_regex("a","o","Aardvark")→"Aordvark"replace_regex("a","o","Aardvark","i")→"oardvark"replace_regex("a","o","Aardvark","ig")→"oordvork"replace_regex(safe("(\\d+)x(\\d+)"),"$1 by $2","32x24")→"32 by 24"replace_regex(safe("a{2}"),"c","a aa aaa")→"a c aaa"
- trim(str)¶
Remove whitespace from the start and end of
str.
- currency(n, prefix, suffix)¶
Write a currency amount, with the given prefix or suffix characters.
- separateThousands(n, separator)¶
Write a number, with the given separator character between every 3 digits
To write a number using notation appropriate to a particular culture or context, see
formatnumber().
- unpercent(str)¶
Get rid of the
%on the end of a percentage and parse as a number, then divide by 100.
- lpad(str, n, prefix)¶
Add copies of
prefixto the start ofstruntil the result is at leastncharacters long.
- rpad(str, n, suffix)¶
Add copies of
suffixto the end ofstruntil the result is at leastncharacters long.
- formatstring(str, values)¶
For each occurrence of
%sinstr, replace it with the corresponding entry in the listvalues.
- letterordinal(n)¶
Get the \(n\)th element of the sequence
a, b, c, ..., aa, ab, ....Note that the numbering starts from 0.
- translate(str, arguments)¶
Translate the given string, if it’s in the localisation file.
Look at the default localisation file for strings which can be translated. This function takes a key representing a string to be translated, and returns the corresponding value from the current localisation file.
argumentsis a dictionary of named substitutions to make in the string.
Logic¶
- x<y¶
Returns
trueifxis less thany.
- x>y¶
Returns
trueifxis greater thany.
- x<=y¶
Returns
trueifxis less than or equal toy.
- x>=y¶
Returns
trueifxis greater than or equal toy.
- x<>y¶
Returns
trueifxis not equal toy. Returnstrueifxandyare not of the same data type.- Definitions:
anything, anything →
boolean
- Examples:
'this string' <> 'that string'1<>2'1' <> 1
- x=y¶
Returns
trueifxis equal toy. Returnsfalseifxandyare not of the same data type.- Definitions:
anything, anything →
boolean
- Examples:
vector(1,2)=vector(1,2,0)4.0=4
- isclose(x, y[, rel_tol][, abs_tol])¶
Returns
trueifxis close toy.The arguments
rel_tolandabs_tolare optional, with default values of \(10^{-15}\).Equivalent to the following expression:
abs(x-y) <= max( rel_tol*max(abs(a),abs(b)), abs_tol )
- resultsequal(a, b, checkingFunction, accuracy)¶
Returns
trueifaandbare both of the same data type, and “close enough” according to the given checking function.Vectors, matrices, and lists are considered equal only if every pair of corresponding elements in
aandbis “close enough”.checkingFunctionis the name of a checking function to use. These are documented in the Numbas runtime documentation.
- x and y¶
- x && y
- x & y
Logical AND. Returns
trueif bothxandyare true, otherwise returnsfalse.
- x or y¶
Logical OR. Returns
truewhen at least one ofxandyis true. Returns false when bothxandyare false.
- x xor y¶
Logical XOR. Returns
truewhen at eitherxoryis true but not both. Returnsfalsewhenxandyare the same expression.
Collections¶
- x[y]
Get the
yth element of the collectionx.For matrices, the
yth row is returned.For dictionaries, the value corresponding to the key
yis returned. If the key is not present in the dictionary, an error will be thrown.
- x[a..b]
- x[a..b#c]
Slice the collection
x- return elements with indices in the given range. Note that list indices start at 0, and the final index is not included.
- x in collection
Is element
xincollection?- Definitions:
- Examples:
3 in [1,2,3,4]→true3 in (set(1,2,3,4) and set(2,4,6,8))→false"a" in ["a": 1]→true1 in ["a": 1]throws an error because dictionary keys must be strings.
Ranges¶
- a .. b¶
Define a range. Includes all integers between and including
aandb.
- range # step¶
Set the step size for a range. Default is 1. When
stepis 0, the range includes all real numbers between the limits.
- a except b¶
Exclude a number, range, or list of items from a list or range.
- Definitions:
- Examples:
-9..9 except 0-9..9 except [-1,1]3..8 except 4..6[1,2,3,4,5] except [2,3]
Lists¶
- repeat(expression, n)¶
Evaluate
expressionntimes, and return the results in a list.
- all(list)¶
Returns
trueif every element oflististrue.
- some(list)¶
Returns
trueif at least one element oflististrue.
- map(expression,name[s],d)¶
Evaluate
expressionfor each item in list, range, vector or matrixd, replacing variablenamewith the element fromdeach time.You can also give a list of names if each element of
dis a list of values. The Nth element of the list will be mapped to the Nth name.
- filter(expression, name, d)¶
Filter each item in list or range
d, replacing variablenamewith the element fromdeach time, returning only the elements for whichexpressionevaluates totrue.
- foldl(expression, accumulator_name, item_name, first_value, d)¶
Accumulate a value by iterating over a collection. This can be used as an abstraction of routines such as “sum of a list of numbers”, or “maximum value in a list”.
Evaluate
expressionfor each item in the list, range, vector or matrixd, accumulating a single value which is returned.At each iteration, the variable
item_nameis replaced with the corresponding value fromd. The variableaccumulator_nameis replaced withfirst_valuefor the first iteration, and the result ofexpressionfrom the previous iteration subsequently.
- iterate(expression, name, initial, times)¶
Iterate an expression on the given initial value the given number of times, returning a list containing the values produced at each step.
You can also give a list of names. The Nth element of the value will be mapped to the Nth name.
- iterate_until(expression, name, initial, condition, max_iterations)¶
Iterate an expression on the given initial value until the condition is satisfied, returning a list containing the values produced at each step.
You can also give a list of names. The Nth element of the value will be mapped to the Nth name.
max_iterationsis an optional parameter specifying the maximum number of iterations that may be performed. If not given, the default value of 100 is used. This parameter prevents the function from running indefinitely, when the condition is never met.
- take(n, expression, name, d)¶
Take the first
nelements from list or ranged, replacing variablenamewith the element fromdeach time, returning only the elements for whichexpressionevaluates totrue.This operation is lazy - once
nelements satisfying the expression have been found, execution stops. You can use this to filter a few elements from a large list, where the condition might take a long time to calculate.
- flatten(lists)¶
“Flatten” a list of lists, returning a single list containing the concatenation of all the entries in
lists.- Definitions:
list of list→list
- Example:
flatten([ [1,2], [3,4] ])→[1,2,3,4]
- let(name, definition, ..., expression)¶
- let(definitions, expression)
Evaluate
expression, temporarily defining variables with the given names. Use this to cut down on repetition. You can define any number of variables - in the first calling pattern, follow a variable name with its definition. Or you can give a dictionary mapping variable names to their values. The last argument is the expression to be evaluated.
- sort_destinations(x)¶
Return a list giving the index that each entry in the list will occupy after sorting.
- sort_by(key, list)¶
Sort the given list of either
listordictvalues by their entries corresponding to the given key. When sorting a list of lists, the key is a number representing the index of each list to look at. When sorting a list of dictionaries, the key is a string.- Definitions:
- Examples:
sort_by(0, [[5,0], [3,2], [4,4]])→[[3,2], [4,4], [5,0]]sort_by("width", [["label": "M", "width": 20], ["label": "L", "width": 30], ["label": "S", "width": 10]])→[["label": "S", "width": 10], ["label": "M", "width": 20], ["label": "L", "width": 30]]
- group_by(key, list)¶
Group the entries in the given list of either
listordictvalues by their entries corresponding to the given key. The returned value is a list of lists of the form[key, group], wherekeyis the value all elements of the listgrouphave in common.When grouping a list of lists, the
keyargument is a number representing the index of each list to look at. When grouping a list of dictionaries, thekeyargument is a string.- Definitions:
- Examples:
group_by(0, [[0,0], [3,2], [0,4]])→[[0, [[0,0], [0,4]]], [3, [[3,2]]]]group_by("a", [["a": 1, "b": "M"], ["a": 2, "b": "S"], ["a": 1, "b": "XL"]])→[[1,[["a": 1, "b": "M"], ["a": 1, "b": "XL"]]], [2, [["a": 2, "b": "S"]]]]
- indices(list, value)¶
Find the indices at which
valueoccurs inlist.
- distinct(x)¶
Return a copy of the list
xwith duplicates removed.
- list(x)¶
Convert a value to a list of its components (or rows, for a matrix).
- Definitions:
- Examples:
list(set(1,2,3))→[1,2,3](note that you can’t depend on the elements of sets being in any order)list(vector(1,2))→[1,2]list(matrix([1,2],[3,4]))→[[1,2], [3,4]]
- make_variables(definitions)¶
Evaluate a dictionary of variable definitions and return a dictionary containing the generated values.
definitionsis a dictionary mapping variable names toexpressionvalues corresponding to definitions.The definitions can refer to other variables to be evaluated, or variables already defined in the current scope. Variables named in the dictionary which have already been defined will be removed before evaluation begins.
- Definitions:
dictofexpression,range→dict
- Example:
make_variables(["a": expression("random(1..5)"), "b": expression("a^2")])→["a": 3, "b": 9]
- satisfy(names, definitions, conditions, maxRuns)¶
Each variable name in
namesshould have a corresponding definition expression indefinitions.conditionsis a list of expressions which you want to evaluate totrue. The definitions will be evaluated repeatedly until all the conditions are satisfied, or the number of attempts is greater thanmaxRuns. IfmaxRunsisn’t given, it defaults to 100 attempts.Note
This function is deprecated, and retained only for backwards compatibility. Use
make_variables()instead.
- sum(numbers)¶
Add up a list of numbers
- prod(list)¶
Multiply a list of numbers together
- product(list1, list2, ..., listN)¶
Cartesian product of lists. In other words, every possible combination of choices of one value from each given list.
If one list and a number are given, then the
n-th Cartesian power of the list is returned: the Cartesian product ofncopies of the list.
- zip(list1, list2, ..., listN)¶
Combine two (or more) lists into one - the Nth element of the output is a list containing the Nth elements of each of the input lists.
- combinations(collection, r)¶
All ordered choices of
relements fromcollection, without replacement.
- combinations_with_replacement(collection, r)¶
All ordered choices of
relements fromcollection, with replacement.
- permutations(collection, r)¶
All choices of
relements fromcollection, in any order, without replacement.
- frequencies(collection)¶
Count the number of times each distinct element of
collectionappears.Returns a list of pairs
[value, frequency], wherevalueis a value from the list, andfrequencyis the number of times it appeared.
- enumerate(collection)¶
Enumerate the elements of
collection: this function returns a list containing, for each elementvofcollection, a new list of the form[i,v], whereiis the index of the element incollection.
Dictionaries¶
- dict[key]
Get the value corresponding to the given key string in the dictionary
d.If the key is not present in the dictionary, an error will be thrown.
- get(dict, key, default)¶
Get the value corresponding to the given key string in the dictionary.
If the key is not present in the dictionary, the
defaultvalue will be returned.
- dict(a:b, c:d, ...)¶
- dict(pairs)
Create a dictionary with the given key-value pairs. Equivalent to
[ .. ], except when no key-value pairs are given:[]creates an empty list instead.You can alternately pass a list of pairs of the form
[key, value], to transform a list into a dictionary.- Definitions:
multiple
keypair→dict
- Examples:
dict()dict("a": 1, "b": 2)dict([ ["a",1], ["b",2] ])
- keys(dict)¶
A list of all of the given dictionary’s keys.
- values(dict[, keys])¶
A list of the values corresponding to each of the given dictionary’s keys.
If a list of keys is given, only the values corresponding to those keys are returned, in the same order.
Sets¶
- set(elements)¶
Create a set with the given elements. Either pass the elements as individual arguments, or as a list.
- union(a, b)¶
Union of sets
aandb
- intersection(a, b)¶
Intersection of sets
aandb, i.e. elements which are in both sets.
- a - b
Set minus - elements which are in a but not b
- Example:
set(1,2,3,4) - set(2,4,6)→set(1,3)
Randomisation¶
- random(x)¶
Pick uniformly at random from a range, list, or from the given arguments.
- weighted_random(x)¶
Pick random from a weighted list of items. Each element in the input list is a pair of the form
[item, probability], whereprobabilityis anumbervalue.Items with negative weight are ignored.
- deal(n)¶
Get a random shuffling of the integers \([0 \dots n-1]\)
- reorder(list, order)¶
Reorder a list given a permutation. The
i``th element of the output is the ``order[i]``th element of ``list.
- shuffle(x)¶
Random shuffling of list or range.
- shuffle_together(lists)¶
Shuffle several lists together - each list has the same permutation of its elements applied. The lists must all be the same length, otherwise an error is thrown.
Control flow¶
- award(a, b)¶
Return
aifbistrue, else return0.
- if(p, a, b)¶
If
pistrue, returna, else returnb. Only the returned value is evaluated.- Definitions:
boolean, anything, anything → unspecified
- Example:
if(false,1,0)→0
- switch(p1, a1, p2, a2, ..., pn, an, d)¶
Select cases. Alternating boolean expressions with values to return, with the final argument representing the default case. Only the returned value is evaluated.
- Definitions:
multiple
boolean,anything, anything → unspecified
- Examples:
switch(true,1,false,0,3)→1switch(false,1,true,0,3)→0switch(false,1,false,0,3)→3
- assert(condition, value)¶
If
conditionisfalse, then returnvalue, otherwise don’t evaluatevalueand returnfalse. This is intended for use in marking scripts, to apply marking feedback only if a condition is met.- Definitions:
boolean, anything → unspecified
- Example:
assert(studentAnswer<=0, correct("Student answer is positive"))
- try(expression, name, except)¶
Try to evaluate
expression. If it is successfully evaluated, return the result. Otherwise, evaluateexcept, with the error message available asname.- Definitions:
anything,
name, anything → unspecified
- Examples:
try(eval(expression("x+")),err, "Error: "+err)→"Error: Not enough arguments for operation <code>+</code>"try(1+2,err,0)→3
HTML¶
- isnonemptyhtml(str)¶
Does
strrepresent a string of HTML containing text? Returns false for the empty string, or HTML elements with no text content.
- table(data)¶
- table(data, headers)
Create an HTML with cell contents defined by
data, which should be a list of lists of data, and column headers defined by the list of stringsheaders.
- image(url[, width][, height])¶
Create an HTML
imgelement loading the image from the given URL. Images uploaded through the resources tab are stored in the relative URLresources/images/<filename>.png, where<filename>is the name of the original file.The optional
widthandheightparameters specify the size the image should be displayed at, in em units. If only the width is given, the height will be automatically set to maintain the image’s original proportions. 1 em is the width of the letter ‘m’.If neither width nor height are given, the image is displayed at its original size.
- Definitions:
- Examples:
image('resources/images/picture.png')image(chosenimage)
- max_width(width, element)¶
Apply a CSS
max-widthattribute to the given element. You can use this to ensure that an image is not displayed too wide. The givenwidthis inemunits.
- max_height(width, element)¶
Apply a CSS
max-heightattribute to the given element. You can use this to ensure that an image is not displayed too long. The givenheightis inemunits.
JSON¶
JSON is a lightweight data-interchange format. Many public data sets come in JSON format; it’s a good way of encoding data in a way that is easy for both humans and computers to read and write.
For an example of how you can use JSON data in a Numbas question, see the exam Working with JSON data.
- json_decode(json)¶
Decode a JSON string into JME data types.
JSON is decoded into numbers, strings, booleans, lists, or dictionaries. To produce other data types, such as matrices or vectors, you will have to post-process the resulting data.
Warning
The JSON value
nullis silently converted to an empty string, because JME has no “null” data type. This may change in the future.- Definitions:
string→ unspecified
- Example:
json_decode(safe(' {"a": 1, "b": [2,true,"thing"]} '))→["a": 1, "b": [2,true,"thing"]]
Sub-expressions¶
The expression data type represents a JME expression.
You can use it to store and manipulate expressions symbolically, substituting in other variables before evaluating or displaying to the student.
There are functions to construct sub-expressions from strings of code, or by joining other sub-expressions together as the arguments to operators or functions.
Once you’ve got a sub-expression, you can evaluate it to a normal JME data type, test if it matches a pattern, substitute values or other sub-expressions for free variables, rearrange using simplification rules, or test if it is equivalent to another sub-expression.
- expression(string)¶
- parse(string)¶
Parse a string as a JME expression. The expression can be substituted into other expressions, such as the answer to a mathematical expression part, or the
\simplifyLaTeX command.parse(string)is a synonym forexpression(string).Warning
Note that the argument to
expressionis evaluated using the same rules as any other JME expression, so for exampleexpression("2" + "x")is equivalent toexpression("2x"), notexpression("2 + x"). A good way to construct a randomised sub-expression is usingsubstitute().- Definitions:
- Example:
- eval(expression, values)¶
Evaluate the given sub-expression.
If
valuesis given, it should be a dictionary mapping names of variables to their values.- Definitions:
expression→ unspecifiedexpression,dict→ unspecified
- Example:
eval(expression("1+2"))→3eval(expression("x+1"), ["x":1])→2
- args(expression)¶
Returns the arguments of the top-level operation of
expression, as a list of sub-expressions. Ifexpressionis a data type other than an operation or function, an empty list is returned.Binary operations only ever have two arguments. For example,
1+2+3is parsed as(1+2)+3.- Definitions:
- Examples:
args(expression("f(x)"))→[expression("x")]args(expression("1+2+3"))→[expression("1+2"), expression("3")]args(expression("1"))→[]
- type(expression)¶
Returns the name of the data type of the top token in the expression, as a string.
- Definitions:
- Examples:
type(expression("x"))→"name"type(expression("1"))→"integer"type(expression("x+1"))→"op"type(expression("sin(x)"))→"function"
- function(name)¶
Construct a function token with the given name.
- Definitions:
string→func
- Example:
function("sin")→sin
- exec(op, arguments)¶
Returns a sub-expression representing the application of the given operation to the list of arguments.
- Definitions:
op,list→expression
- Example:
exec(op("+"), [2,1])→expression("2+1")exec(op("-"), [2,name("x")])→expression("2-x")
- findvars(expression)¶
Return a list of all unbound variables used in the given expression. Effectively, this is all the variables that need to be given values in order for this expression to be evaluated.
Bound variables are those defined as part of operations which also assign values to those variables, such as
maporlet.- Definitions:
- Examples:
findvars(expression("x+1"))→["x"]findvars(expression("x + x*y"))→["x","y"]findvars(expression("map(x+2, x, [1,2,3])"))→[]
- substitute(variables, expression)¶
Substitute the given variable values into
expression.variablesis a dictionary mapping variable names to values.- Definitions:
- Examples:
substitute(["x": 1], expression("x + y"))→expression("1 + y")substitute(["x": 1, "y": expression("sqrt(z+2)")], expression("x + y"))→expression("1 + sqrt(z + 2)")
- simplify(expression, rules)¶
Apply the given simplification rules to
expression, until no rules apply.rulesis a list of names of rules to apply, given either as a string containing a comma-separated list of names, or a list of strings.Unlike the
\\simplifycommand in content areas, thebasicrule is not turned on by default.See Substituting variables into displayed maths for a list of rules available.
- Definitions:
- Examples:
simplify(expression("1*x+cos(pi)"),"unitfactor")→expression("x+cos(pi)")simplify(expression("1*x+cos(pi)"),["basic","unitfactor","trig"])→expression("x-1")
- expand_juxtapositions(expression, options)¶
Expand juxtapositions in variable and function names for implicit multiplication of terms or composition of functions. This is to do with strings of letters with no spaces or operator symbols between them.
optionsis an optional dictionary of settings for the process. It can contain the following keys.singleLetterVariables- Insist that all variable names consist of a single letter, interpreting longer strings of characters as implicit multiplication.Greek letters are considered to be one letter long.
noUnknownFunctions- When a name appears before a bracket, but it’s not the name of a defined function, interpret it as a multiplication instead. This does not apply function applications with more than one argument.implicitFunctionComposition- When several function names are juxtaposed together to form a string that is not the name of a defined function, or several function names are joined with the multiplication symbol*, interpret it as implicity composition of functions.
If
optionsis not given, all of these are turned on.Variable name annotations, subscripts and primes do not count towards the number of letters in a name.
- Definition:
- Examples:
expand_juxtapositions(expression("xy"))→expression("x*y")expand_juxtapositions(expression("x'y"))→expression("x\'*y")expand_juxtapositions(expression("pizza"))→expression("pi*z*z*a")expand_juxtapositions(expression("hat:abc"))→expression("hat:a*b*c")expand_juxtapositions(expression("xcos(x)"))→expression("x*cos(x)")expand_juxtapositions(expression("lnabs(x)"))→expression("ln(abs(x))")expand_juxtapositions(expression("ln*abs(x)"))→expression("ln(abs(x))")expand_juxtapositions(expression("xy"),["singleLetterVariables": false])→expression("xy")expand_juxtapositions(expression("x(x+1)"))→expression("x*(x+1)")expand_juxtapositions(expression("x(x+1)"),["noUnknownFunctions": false])→expression("x(x+1)")expand_juxtapositions(expression("ln*abs(x)"),["implicitFunctionComposition": false, "singleLetterVariables": true, "noUnknownFunctions": true])→expression("l*n*abs(x)")expand_juxtapositions(expression("xy^z"))→expression("x*y^z")expand_juxtapositions(expression("xy!"))→expression("x*y!")
- canonical_compare(expr1, expr2)¶
Compare expressions
aandbusing the “canonical” ordering. Returns-1ifashould go beforeb,0if they are considered “equal”, and1ifashould go afterb.Expressions are examined in the following order:
Names used: all variable names used in each expression are collected in a depth-first search and the resulting lists are compared lexicographically.
Data type: if
aandbare of different data types,opandfunctiongo first, and then they are compared using the names of their data types.Polynomials: terms of the form
x^bora*x^b, whereaandbare numbers andxis a variable name, go before anything else.Function name: if
aandbare both function applications, they are compared using the names of the functions. If the functions are the same, the arguments are compared. Powers, or multiples of powers, go after anything else.Number: if
aandbare both numbers, the lowest number goes first. Complex numbers are compared by real part and then by imaginary part.Elements of other data types are considered to be equal to any other value of the same data type.
- Definitions:
anything, anything →
number
- Examples:
canonical_compare(a,b)→-1canonical_compare(f(y),g(x))→1canonical_compare(f(x),g(x))→-1canonical_compare("a","b")→0
- numerical_compare(a, b)¶
Compare expression
aandbby substituting random values in for the free variables.Returns
trueifaandbhave exactly the same free variables, and produce the same results when evaluated against the randomly chosen values.For more control over the evaluation, see
resultsequal().- Definitions:
- Example:
numerical_compare(expression("x^2"), expression("x*x"))→truenumerical_compare(expression("x^2"), expression("2x"))→falsenumerical_compare(expression("x^2"), expression("y^2"))→false
- scope_case_sensitive(expression[, case_sensitive])¶
Set the evaluation scope to be case-sensitive or not, depending on the value of
case_sensitive, and then evaluateexpression.If
case_sensitiveis not given, it defaults totrue.Case-sensitivity affects variable and function names. The names
xandXare considered equivalent when not in case-sensitive mode, but are considered to be different when in case-sensitive mode.- Definitions:
expression,boolean→ unspecified
- Example:
scope_case_sensitive(findvars(expression("x+X")))→["X","x"]scope_case_sensitive(let(x,1,X,2,x+X), true)→3
Calculus¶
- diff(expression, variable)¶
Differentiate the given expression with respect to the given variable name
- Definitions:
- Example:
diff(expression("x^2 + 2x + 4"), "x")→expression("2x + 2")diff(expression("x * y + 3x + 2y"), "x")→expression("y + 3")diff(expression("cos(x^2)"), "x")→expression("-2 * sin(x^2) * x")
Pattern-matching sub-expressions¶
- match(expr, pattern, options)¶
If
exprmatchespattern, return a dictionary of the form["match": boolean, "groups": dict], where"groups"is a dictionary mapping names of matches to sub-expressions.See the documentation on pattern-matching mathematical expressions.
If you don’t need to use any parts of the matched expression, use
matches()instead.- Definitions:
expression,string→dictexpression,string,string→dict
- Examples:
match(expression("x+1"),"?;a + ?;b")→["match": true, "groups": ["a": expression("x"), "b": expression("1"), "_match": expression("x+1")]]match(expression("sin(x)"), "?;a + ?;b")→["match": false, "groups": dict()]match(expression("x+1"),"1+?;a")→["match": true, "groups": ["a": expression("x"), "_match": expression("x+1")]]
- matches(expr, pattern, options)¶
Return
trueifexprmatchespattern.Use this if you’re not interested in capturing any parts of the matched expression.
- Definitions:
- Examples:
matches(expression("x+1"),"?;a + ?;b")→truematches(expression("sin(x)"), "?;a + ?;b")→false
- replace(pattern, replacement, expr)¶
Replace occurrences of
patterninexprwith the expression created by substituting the matched items intoreplacement.- Definitions:
- Examples:
replace("?;x + ?;y", "x*y", expression("1+2"))→expression("1*2")replace("?;x + ?;y", "f(x,y)", expression("1+2+3"))→expression("f(f(1,2),3)")replace("0*?", "0", expression("0*sin(x) + x*0 + 2*cos(0*pi)"))→expression("0 + 0 + 2*cos(0)")
Identifying data types¶
- type(x)
Returns the name of the data type of
x.- Example:
type(1)→"integer"
- Keywords
- x as type¶
Convert
xto the given data type, if possible.If
xcan not be automatically converted totype, an error is thrown.- Definitions:
anything,
string→ given type
- Examples:
dec(1.23) as "number"→1.23set(1,2,3) as "list"→[1,2,3]
- infer_variable_types(expression)¶
Attempt to infer the types of free variables in the given expression.
There can be more than one valid assignment of types to the variables in an expression. For example, in the expression
a+a, the variableacan be any type which has a defined addition operation.Returns the first possible assignment of types to variables, as a dictionary mapping variable names to the name of its type. If a variable name is missing from the dictionary, the algorithm can’t establish any constraint on it.
- Definitions:
- Examples:
infer_variable_types(expression("x^2"))→["x": "number"]infer_variable_types(expression("union(a,b)"))→["a": "set", "b": "set"]infer_variable_types(expression("k*det(a)"))→[ "k": "number", "a": "matrix" ]
- infer_type(expression)¶
Attempt to infer the type of the value produced by the given expression, which may contain free variables.
First, the types of any free variables are inferred. Then, definitions of an operations or functions in the function are chosen to match the types of their arguments.
Returns the name of the expression’s output type as a string, or
"?"if the type can’t be determined.- Definitions:
- Examples:
infer_type(expression("x+2"))→"number"infer_type(expression("id(n)"))→"matrix"infer_type(expression("random(2,true)"))→"?"
Inspecting the evaluation scope¶
- definedvariables()¶
Returns a list containing the names of every variable defined in the current scope, as strings.
- Definitions:
() →
list
- isset(name)¶
Returns
trueif the variable with the given name has been defined in the current scope.