# Mathematical expression¶

Mathematical expression parts require the student to enter an algebraic expression, using JME syntax.

These parts are marked by picking a sample of random values for the free variables in the expression, and evaluating both the student’s answer and the correct answer on those values. If the two expressions agree on enough inputs, then they are considered to be equivalent and the student’s answer is marked as correct.

For questions where the student is asked to rearrange an expression, just evaluating both answers won’t detect the difference - you want to look at the *form* of the student’s answer, as well as the values it produces.
Use a pattern restriction to check that the student’s answer is in the form you want.

You can find the mathematical expression part’s built-in marking algorithm at GitHub.

## Marking¶

- Correct answer¶
The expected answer to the part. Question variables (or, more broadly, JME expressions which should be evaluated to a single value when the question is generated), can be included by enclosing them in curly braces.

If the answer is an equation, see the note on Marking an equation.

- Show preview of student’s answer?¶
If ticked, a rendering of the student’s answer in mathematical notation is displayed beside the input box. You should leave this on unless you expect the answer to be very simple and need the space - the feedback about how their answer is interpreted is very useful to students.

- Answer simplification rules¶
Simplification rules to apply to the correct answer, if it is displayed to the student (for example, after clicking the Reveal answers button). This shouldn’t affect marking.

If this field is empty, the following rules are applied:

`basic`

,`unitFactor`

,`unitPower`

,`unitDenominator`

,`zeroFactor`

,`zeroTerm`

,`zeroPower`

,`collectNumbers`

,`zeroBase`

,`constantsFirst`

,`sqrtProduct`

,`sqrtDivision`

,`sqrtSquare`

,`otherNumbers`

.- Expression is case-sensitive?¶
If ticked, then the student’s answer and the correct answer will be considered case-sensitively.

If not ticked, then names which are exactly the same when converted to lower-case will be considered as representing the same variable or function.

See case-sensitivity in the JME reference.

## Restrictions¶

The Restrictions tab provides several methods for restricting the form of the student’s answer.

### Pattern restriction¶

- Pattern student’s answer must match¶
The student’s answer must match the given pattern. If it does not, then a penalty is applied.

You can use this to ensure the student’s answer is in a particular form.

See Pattern-matching examples for examples of patterns which ensure the expression is in particular forms.

- Part of expression to mark¶
If Whole expression is selected, then the student’s entire expression is compared against the correct answer. If the name of a subexpression captured by the pattern is selected, then only the subexpression captured in the student’s answer is compared against the corresponding sub-expression in the correct answer.

You can use this to mark answers which could not otherwise be marked using the standard marking algorithm, for example function definitions or equations where one side is fixed, such as \(y = f(x)\).

- Partial credit for not matching pattern¶
If the student’s answer does not match the given pattern, their score is multiplied by this percentage.

### Variables¶

- Warn if student uses an unexpected variable name?¶
If this is ticked, all variable names used in the student’s are checked against the variable names used in the correct answer. The first variable name which is not used in the correct answer will trigger a warning. You can use this option to prevent students incorrectly entering answers such as

`xy`

, which is interpreted as a single variable, when they mean`x*y`

, the product of two variables.- Force single letter variable names?¶
If this is ticked, long variable names will be interpreted as implicit multiplication of variables with single-letter names. For example,

`xyz`

will be interpreted as`x * y * z`

. Digits, primes and single-letter underscores are still valid in variable names:`a'x12y_z`

will be interpreted as`a' * x12 * y_z`

. Greek letters are considered to be a single letter:`pix`

will be interpreted as`pi * x`

.This option is recommended when the expected answer for the part only uses single-letter variable names, so that students who don’t always use the multiplication symbol aren’t caught out.

- Allow unknown function names?¶
If this is not ticked, the application of a function that is not defined in JME will be reinterpreted. If the function name can be split into several shorter names, each of which is defined in JME, it will be: for example,

`lnabs(x)`

will be interpreted as`ln(abs(x))`

. Function names are recognised from right to left. Any remaining characters are interpreted as implicit multiplication by a variable. For example,`xsin(x)`

will be interpreted as`x * sin(x)`

.Use this option if you want to allow students to use implicit multiplication with function names, without any spaces.

- Use implicit function composition?¶
If this is ticked, the multiplication symbol (or implicit multiplication) will be interpreted as function composition when the right-hand side is a function application with one argument, and the left-hand side is the name of a function defined in JME. For example,

`ln * abs(x)`

and`ln abs(x)`

will be interpreted as`ln(abs(x))`

.

### String restrictions¶

Note

String restrictions are an unreliable method of restricting the form of a student’s answer. They are deprecated and retained only for backwards compatibility; use a pattern restriction instead.

Before string restrictions are applied, surplus brackets and whitespace are removed, and spaces are inserted between some operations, to minimise the possibility of the length restrictions being triggered for the wrong reasons.

- Minimum length restriction¶
If the student’s answer contains fewer than this many characters, the penalty is applied. A value of zero means no restriction is applied. See the comment above on how the length is calculated.

- Maximum length restriction¶
If the student’s answer contains more than this many characters, the penalty is applied. A value of zero means no restriction is applied. The student’s answer is tidied up slightly so that things like extra or missing space characters don’t affect the calculated length. All spaces are removed, and then spaces are inserted between binary operations. For example, the answer

`1+x`

(three characters) is marked as`1 + x`

(five characters).- Required strings¶
If the student’s answer doesn’t contain all of these strings, the penalty is applied.

- Forbidden strings¶
If the student’s answer contains any of these strings, the penalty is applied.

## Accuracy¶

These settings define the range of points over which the student’s answer will be compared with the correct answer, and the method used to compare them.

For each of the variables in the correct answer, a value is chosen at random. How this value is chosen depends on the type of the variable: for example, in the expression \(k \det(A)\), the variable \(A\) must be a matrix, and \(k\) can be assumed to be a number. The system can usually infer the type of each variable and pick an appropriate value automatically.

Numbers are chosen uniformly at random from the defined checking range. Matrices and vectors have entries chosen uniformly at random from the defined checking range.

Care must be taken if the correct answer has a singularity or is undefined for some values of the variables. Either set the checking range to a safe interval on which the expression is always defined, or write a variable value generator.

- Checking type¶
The rule to use to compare the student’s answer with the correct answer. In the lines below, \(x\) represents the value of the student’s answer at a particular point and \(y\) represents the value of the correct answer, while \(\delta\) is the value of the checking accuracy property.

Absolute difference. Fail if \(\left| x-y \right| > \delta\).

Relative difference. Fail if \(\left| \frac{x}{y} - 1 \right| > \delta\).

Decimal points. \(x\) and \(y\) are rounded to \(\delta\) decimal places, and the test fails if the rounded values are unequal.

Significant figures. \(x\) and \(y\) are rounded to \(\delta\) significant figures, and the test fails if the rounded values are unequal.

- Checking accuracy¶
The parameter for the checking type.

- Points to check¶
The number of comparisons to make between the student’s answer and the correct answer.

- Maximum no. of failures¶
If the comparison fails this many times or more, the student’s answer is marked as wrong.

- Checking range start¶
The minimum value sample points can take.

- Checking range end¶
The maximum value sample points can take.

### Variable value generators¶

Variable value generators override the default method used to pick values for variables when comparing the correct answer with the student’s answer.

A text field for each variable used in the correct answer appears in this section.
If left blank, the default value generator will be used.
To override it, enter a JME expression producing a value for the variable.
The variable `vRange`

represents the checking range defined for this part: a continuous interval between the checking range start and checking range end.

The expression for each variable can be written in terms of the other variables, as long as there are no circular dependencies. The values will be evaluated in order, like question variables.

## Marking an equation¶

If the correct answer is an equation, such as \(A = 6t\) or \(x^2 + y^2 = 1\), it will produce a `boolean`

value, representing whether the values of the variables constitute a solution of the equation.

Two equations are equivalent if they have the same solution sets. For example, the equations \(y=2x\) and \(y-2x=0\) are equivalent because exactly the same sets of \((x,y)\) pairs satisfy them both. We can make a fairly confident decision about whether two equations are equivalent by checking that they agree on a few randomly-chosen values.

We need to check both solutions and non-solutions of the expected equation. If we don’t check any solutions, then an equation which can never be satisfied would be marked correct. Conversely, if we don’t check any non-solutions, then an equation which holds for any input would be marked correct.

It’s extremely unlikely that randomly-chosen values for the variables will satisfy any given equation, so you need to change the way values are chosen to produce solutions about half of the time, using variable value generators.

For example, in a part with correct answer \(x^2+y^2=1\), the expression `random(sqrt(1-x^2), random(vRange))`

for the variable \(y\) will produce a solution of the equation roughly half of the time.
By setting the points to check to a big enough number, say 10, we can be reasonably confident that the student’s answer is equivalent to the expected answer.

See this example question where the correct answer is a differential equation.

## Marking settings¶

This part type provides the following properties to the `settings`

object:

- correctAnswer
The Correct answer to the question.

- answerSimplification¶

- checkingType¶
The Checking type setting, representing the name of the checking function to use. One of

`"absdiff"`

,`"reldiff"`

,`"dp"`

or`"sigfig"`

. See`resultsequal`

.

- checkingAccuracy¶
See Checking accuracy.

- failureRate¶

- vsetRangeStart¶
See Checking range start.

- vsetRangeEnd¶
See Checking range end.

- vsetRangePoints¶
See Points to check.

- valueGenerators¶
A dictionary of variable value generator expressions.

- checkVariableNames¶

- mustMatchPattern¶

- mustMatchPC¶
The proportion of credit awarded if the student’s answer does not match the pattern.

- mustMatchMessage¶
Message to add to marking feedback if the student’s answer does not match the pattern.

- nameToCompare¶
The name of the captured subexpression to compare against the corresponding subexpression in the correct answer. See Part of expression to mark.

- maxLength¶
The maximum length, in characters, of the student’s answer, as set in Maximum length restriction.

- maxLengthPC¶
The proportion of credit awarded if the student’s answer is too long.

- maxLengthMessage¶
Message to add to marking feedback if the student’s answer is too long.

- minLength¶
The minimum length, in characters, of the student’s answer, as set in Minimum length restriction.

- minLengthPC¶
The proportion of credit to award if the student’s answer is too short.

- minLengthMessage¶
Message to add to the marking feedback if the student’s answer is too short.

- mustHave¶
A list of strings which must be present in the student’s answer, as set in Required strings.

- mustHavePC¶
The proportion of credit to award if any must-have string is missing.

- mustHaveMessage¶
Message to add to the marking feedback if the student’s answer is missing a must-have string.

- mustHaveShowStrings¶
Tell the students which strings must be included in the marking feedback, if they’re missing a must-have?

- notAllowed¶
A list of strings which must not be present in the student’s answer, as set in Forbidden strings.

- notAllowedPC¶
The proportion of credit to award if any not-allowed string is present.

- notAllowedMessage¶
Message to add to the marking feedback if the student’s answer contains a not-allowed string.

- notAllowedShowStrings¶
Tell the students which strings must not be included in the marking feedback, if they’ve used a not-allowed string?

## Example¶

A question in the Numbas demo demonstrates a variety of ways of using mathematical expression parts.