- Part 4: Create input field problems

# Part 4: Create input field problems¶

Creating problems with input fields is similar to creating multiple choice problems, i.e. the definition of questions, variables and functions is common to both question types.The differences between them are mainly:

- input field questions must have the question type
`input.number`

`input.function`

`input.matrix`

`input.text`

- instead of defining
*choices*, this question type contains one or several*answers*.

## Defining answers and precision of real numbers¶

Every answer must be defined in an *answer* environment and will be represented by a single input field for entering values. Answers *must* use the ** \solution{<variable>}** command which will be compared to the users answer to determine its correctness. The comparison of the user answer will be done with a certain

*precision*, this value can be specified in the question environment.

**\displayprecision{<number of decimal digits>}**- defines the number of digits used for displaying real numbers in the mathlet.**If not specified, than the generic problem will use the default value of 2.****\correctorprecision[<correctorOption, default is atleast>]{<number of decimal digits>}**- defines the precision used for the corrector to compare answer and solution. corrector precision should not be greater than display precision. If corrector precision is not defined, then the display precision will be used.

`floor`

function to achieve that.
**Example**

Let's say corrector precision is 2, but you want the number a displayed truncated or rounded without any decimal places. You can do the following (two options):

\randdouble{a}{0}{10} \function[calculate]{a1}{floor(a)} % truncated \function[calculate]{a2}{floor(a+0.5)} % rounded, no decimal places

### Corrector Option¶

There are three option for comparing the real answer and solution:**atleast**: means that the answer must be typed in (rounded) with atleast the given precision.**This is the default option.****rounded**: means that the answer must be rounded.**If you use this option, make sure you tell the user that they must round the result with the given precision.****truncate**: means that the answer should not be rounded, and should just be typed in up to the defined number of decimal digits.

## Type: Input Number¶

Optionally, one can use the following commands within the*answer*environment:

: will a a label in front of the input field. It's probably best not to make this label to long. It would be better to put longer descriptions within the question task.`\text{<description>}`

: this explanation will be visible in the corrector page. It will be shown below the explanation defined for the question and have the label of the task in front of it.`\explanation{<description>}`

- If
`\showExplanation{always}`

is used within the question, it will always show both the question and answer explanation, regardless of the correctness of the users answer.

- If

*must be a variable name defined in the variables environment.*

**solution**The following example shows a number input question with one single answer as a constant:

```
\begin{question}
\begin{variables}
\function{f}{a/b}
\number{a}{11}
\number{b}{16}
\end{variables}
\type{input.number}
\field{real}
\displayprecision{3}
\correctorprecision[rounded]{3}
\text{Determine the decimal expansion of $\var{f}$ correct to three decimal places.}
\explanation{Think about what rounded off to three decimal places means.}
\begin{answer}
\text{Answer: }
\explanation{The explanation belong to this specific answer.}
\solution{f}
\end{answer}
\end{question}
```

In the above example the following new commands where used.
: an optional label that will be placed`\text`

*in front*of an answer (input) field: the solution that will be used to compare with the answer of the user.`\solution`

: determines,`\precision`

*a.)*at how many digits behind the decimal mark the corrector will be using to determine if the user answer was correct and*b.)*the amount of digits behind the decimal mark when displaying a number of type real. The default value for precision is set to 2. Both*a.)*and*b.)*can be overridden using`\correctorprecision`

and`\displayprecision`

respectively.: overrides the (default) value of`\correctorprecision`

`\precision`

when comparing the correct solution with the user answer.: overrides the (default) value of`\displayprecision`

`\precision`

when displaying real numbers on the screen. Logically`displayprecision`

$\geq$`correctorprecision`

% Real numbers will be shown with 4 digits behind the decimal mark, % but correct with only 2 (default). \displayprecision{4}

% The following is NOT allowed, % since displayprecision (default 2) is now smaller then the correctorprecision \correctorprecision{4}

`calculate`

option, the precision defined with the above command will be ignored. This is because computations should not be rounded if they are not visible to the user. In case you do want to display calculated value then set the precision within the function command. Again this value should be $\geq$ then the corrector precision.## Type : Input Function¶

If the question has the type *input.function*, then it is appropriate to compare the user's answer *numerically* with the pre-given solution.

This can be done using the command `\checkAsFunction`

. Here is an example:

```
\begin{question}
\begin{variables}
\function{f}{x^2+7*x}
\end{variables}
\type{input.function}
\field{real}
\text{Enter the solution to this problem as a function of x.}
\begin{answer}
\text{Answer: $f =$}
\solution{f}
\checkAsFunction{x}{-10}{10}{100}
\end{answer}
\end{question}
```

## Corrector commands of input.function¶

### 1. \checkAsFunction¶

The corrector now compares the values of the function entered by the user to those of the function f = x²+7x at one hundred randomly chosen points (*steps*) between -10 (*min*) and 10 (*max*) and accepts the solution if and only if none of these differ by more than the standard *epsilon* = 1E-8. I.e. $|\text{UserAnswer} - \text{CorrectAnswer}| < 10^{-8}$

So the syntax is given by **\checkAsFunction**{ *variable*}{ *min*}{ *max*}{ *steps*}.

In case you wish to use a different tolerance value *epsilon* instead of the default 1E-8, you may submit it by an optional parameter:

**\checkAsFunction**[ *epsilon*]{ *variable*}{ *min*}{ *max*}{ *steps*}.

In fact, the implemented comparison method uses randomly chosen points (as mentioned), and it does not take into account points, where the function f takes values which are very large.

These points are excluded for reasons of numerical stability. 'Very large' means larger in absolute value than the default *cutoff* = 1E5.

If you have to take into account a situation, where the correct result is determined only up to a constant (because the user is expected to find an indefinite integral, e.g.),

you can change the behavior of the method accordingly. So there is another extended syntax:

**\checkAsFunction**[ *epsilon* | *cutoff* | *random* | *constDiff* ]{ *variable*}{ *min*}{ *max*}{ *steps*}.

This is an example:

```
\checkAsFunction[1E-2|1e7|false|true]{x}{-10}{10}{100}
```

The consequence of using **\checkAsFunction** with that syntax is that

-now the tolerance is 1E-2 instead of default 1E-8

-the cutoff is now at 1E7 instead of 1E5

-the comparison values are *not* chosen at random, but at equal spacing between *min* =-10 and *max* =10 and

-the function entered by the user is considered as correct if it is numerically equal to f + C for some constant C (the same at each of the comparision points).

Observe that in case you want to use this extended syntax version, **you have to fill in all four fields, with '|' as separator**.

### 2. \allowForInput¶

With the command**\allowForInput**[true|false]{<expression list>} you can decide if some operations or symbols are permitted/prohibited for answer input. The default value for the optional argument is

**true**. The entries in the expression list should be seperated by blanks. Remarks:

- Using this command doesn't effect the correction, but only the input itself.
- The digits 0-9 are allways permitted if the optional argument is set to
**true**, but you can exclude single digits or numbers by setting the optional command to**false**.

In the following examples (i) *sin* and *pi* are excluded from input, so the user can't give the given task as answer.

```
\begin{problem}
\begin{question}
\begin{variables}
\function{f}{sin(pi)}
\end{variables}
\type{input.function}
\field{real}
\text{\textit{Compute} $\var{f}$.}
\explanation{}
\begin{answer}
\text{$ \var{f} = $}
\solution{f}
\allowForInput[false]{sin pi}
\end{answer}
\end{question}
\end{problem}
```

### 3. \checkStringsForRelation¶

For the correction you can give conditions the user answer will be checked for by using the commands**\inputAsFunction**{<variable name>}{<identifier>} to first give the function an identifier so you can use it then in the

*expression*part of

**\checkStringsForRelation**{<logical expression>} to define the conditions. (see Input text questions for further details on the possible conditions) Remarks:

- If you use
**\checkStringsForRelation**you need*equal()*(see the following example) as part of your conditions if you want an equals check with the correct solution. If you don't use*equal()*there will be no equals check. Here is list of possible**relations** - Observe that the identity of operations with
*equal()*is tested here algebraically, not numerically. - You can use
**\checkAsFunction**(see above) in addition to achieve a numeric comparison.`\begin{problem} \begin{question} \begin{variables} \function{f}{x^2 + 2x + 1} \end{variables} \type{input.function} \field{real} \text{\textit{Expand the expression} $(x+1)^2$.} \explanation{} \begin{answer} \text{$(x+1)^2 = $} \inputAsFunction{x}{g} \solution{f} \checkStringsForRelation{count(x,g)=2 AND count(+,g)=2 AND count(-,g)=0 AND count((,g)=0 AND equal(g,f)} \end{answer} \end{question}`

### 4. Check of a functional of answers (checkFuncForZero)¶

In some cases, the correct user answer is not unique. For instance, if the user is expected to find two functions f, g with f[g] = sqrt(2x²+1) , the number of possible correct answers is unlimited.

So f(y) = sqrt(2y+1) with g(x) = x² or f(y) = sqrt(y) with g(x) = 2x²+1 are correct, as well as f = sqrt(y+1) with g(x) = 2x²,...

Consider the following code snippet:

```
\begin{problem}
\begin{question}
\begin{variables}
\function{c}{sqrt(2x^2+1)}
\function{f}{sqrt(y)}
\function{g}{2x^2+1}
\end{variables}
\type{input.function}
\field{real}
\text{\textit{Find two functions such that their composition is} $\var{c}$}
\explanation{}
\begin{answer}
\text{$ f(y) = $}
\solution{f}
\inputAsFunction{y}{h}
\end{answer}
\begin{answer}
\text{$ g(x) =$ }
\solution{g}
\inputAsFunction{x}{k}
\checkFuncForZero{h[k]-c}{-10}{10}{100}
\score{2.0}
\end{answer}
\end{question}
\end{problem}
```

- The first user input is marked by \inputAsFunction{y}{h} as a new function (of 'y') for subsequent use. It cannot be evaluated by its own.
- The second user input is also marked by \inputAsFunction{x}{k} as new function k, and the following command checks, whether h[k] coincides numerically with c = sqrt(2x²+1).
- Example solutions are given by f and g listed under 'variables'.
- So \checkFuncForZero checks whether its first argument is zero on the interval from - 10 to 10, using 100 randomly generated checkpoints. The default precision value is 1E-8. If you prefer another precision, you may specify it by an optional

parameter*epsilon*included in square brackets:**\checkFuncForZero**[*epsilon*]{*expression*}{*start_value*}{*end_value*}{*number_of_checkpoints*}. - The correct answer would have been evaluated by score 1.0 (default), this was changed to 2.0 by the \score command, since it needs two correctly adjusted inputs.
- The first argument could be any
*expression*of the function symbols and numbers defined under 'variables' and the user defined function symbols, where the basic binary operations +,-,*,/,^ are allowed, as well as the standard functions as

sin(..), ln(..), abs(..), theta(..) (the Heaviside function), sign(..). These functions have to be written with round brackets. Square brackets have to be used in case of function symbols. - Observe that all used function symbols may occur in
*expression*without argument, but standard functions as**sin**or**ln**may only be used with an argument included in round brackets. The argument may be another*expression*in the declared number- and

function-variables plus 'x' as**standard (placeholder) variable**. - Instead of abs(f) you may write |f|, alternatively. Hint: To check some
*expression*for positivity, you may write \checkFuncForZero{1-sign(*expression*)*theta(*expression*)}{-10}{10}{100} .

Finally, you may use the syntax D[f] to refer to the derivative of a function f, see the following code

```
\begin{question}
\begin{variables}
\function{f}{-cos(7x)}
\function{g}{7sin(7x)}
\end{variables}
\type{input.function}
\field{real}
\text{\textit{Find an anti-derivative F of $\var{g}$}}
\explanation{}
\begin{answer}
\text{$F(x) =$}
\solution{f}
\inputAsFunction{x}{k}
\checkFuncForZero{D[k]-g}{-10}{10}{100}
\end{answer}
\end{question}
```

### 5. Multivariate Functions¶

#### inputAsFunction, checkAsFunction¶

`\inputAsFunction`

and `\checkAsFunction`

accept at first mandatory argument a comma separated list of

variable identifiers instead of only one identifier.

`\begin{question} \begin{variables} \function{f}{x^2+2xy+y^2} \end{variables} \type{input.function} \field{real} \text{Expand:} \explanation{} \begin{answer} \text{$(x+y)^2 = $} \solution{f} \inputAsFunction{x,y}{g} \checkStringsForRelation{count(x,g)=2 AND count(y,g)=2 AND count(+,g)=2 AND count(-,g)=0 AND count((,g)=0 AND equal(f,g)} \end{answer} \end{question}`

Remark: Whitespaces in the list are not allowed.

#### checkFuncForZero and checkStringsForRelation (substitution, derivative)¶

**D[k]** computes the derivative for function **k** and the (default) variable **x**. If you want to

compute the derivative for a different variable e.g. **y**, you can achieve that by the following

notation: **D[k,y]** (with function **k** and variable **y**)

D[<function identifier>,<variable identifier>]

The same notation can be used for substitution: `f[k,y]`

with `y`

being the variable in function `f`

that

should be replaced with function `k`

.

<function 1 identifier>[<function 2 identifier>,<variable identifier>]

If the function has more than one variable identifier, you have to use the syntax above

and define explicitly the variable you want to substitute or the variable with respect

to which you want to compute the derivative of the function.

#### Example¶

```
\begin{question}
\begin{variables}
\function{f}{sin(y)+cos(x)}
\derivative[normalize]{g}{f}{y}
\end{variables}
\type{input.function}
\field{real}
\text{Compute the partial derivative of $\var{f}$ with respect to $y$}
\begin{answer}
\text{Give a multivariate function $f$ with variables $x$ and $y$. $f(x,y) = $}
\solution{f}
\inputAsFunction{x,y}{k}
\end{answer}
\begin{answer}
\text{$\frac{\delta f}{\delta y} = $}
\solution{g}
\inputAsFunction{x,y}{l}
\checkFuncForZero{D[k,y]-l}{-10}{10}{100}
\end{answer}
\end{question}
```

## Permutable answers¶

In the case the solutions of a question are a set, i.e. the answers x _{1}, x _{2}, x _{3} are interchangeable, the answers must be permutable (e.g. the roots x _{1} , x _{2} and x _{3} of a cubic equation). This can be achieved with the command `\permuteAnswers`

which takes a comma separated list of answer indices (numbering starts with one).

- input.number
- input.function (
**WARNING: not available if \inputAsFunction is used**) - input.finite-number-set
- input.interval
- input.matrix

**The function can't be used if you have different answer types in one question!**

The following example demonstrates this technique for a question with two answers (index 1 and 2):

```
begin{question}
\text{What are the roots of f(x)=$\var{f}$ ?}
\explanation{}
\type{input.number}
\field{real}
```

```
\permuteAnswers{1, 2, 3}
```

```
\begin{answer}
\text{$x_1 = $}
\solution{a}
\end{answer}
\begin{answer}
\text{$x_2 = $}
\solution{b}
\end{answer}
\begin{answer}
\text{$x_3 = $}
\solution{c}
\end{answer}
\begin{variables}
\function[expand]{f}{(x-a)(x-b)(x-c)}
\randint[Z]{a}{-5}{5}
\randint[Z]{b}{-5}{5}
\randint[Z]{c}{-5}{5}
\randadjustIf{a,b,c}{a=b OR a=c OR b=c}
\end{variables}
\end{question}
```

## Adjust randomized variables¶

If you randomize variables, it could happen, that the random values do not satisfy some conditions the author wishes to be fulfilled in the question, or that the problem is not correctly posed for some combinations of the random values.

So they have to be adjusted. Therefore, you can use the following syntax to achieve this:

**\randadjustIf**{ *list_of_variables* }{ *avoidance_relation* }

**Example**

\begin{variables} \randint{a}{2}{5} \randint[Z]{b}{-5}{5} \randint{c}{2}{5} \randint{d}{-4}{4} \randadjustIf{a,b,c}{a^2+b^2 > c^2 OR a=b} \end{variables}This has the following consequence:

If the random integer point [a,b] is outside the circle (centered at the origin) with radius c, or if it is situated at the main diagonal, then new values for a,b and c are picked up randomly, possibly several times, until the given

**avoidance condition**is no longer fulfilled.

The

*list_of_variables*is a comma separated list which contains those

**random**variables of the

**same**variables environment, for which the author wishes adjustment in case the

*avoidance_relation*is fulfilled. This

*avoidance_relation*has to be a logical (boolean) combination (use NOT, AND, OR) of simple comparison relations (=, !=, <, <=, >, >=) between

**in the variables. These**

*expressions**expressions*may include

**random and non-random**variables defined in the same environment. See ExpressionsAndRelations for a more detailed description.

**Hint**:

- Make sure that the avoidance relation can be avoided at all for the random variables with the ranges given. The \randint commands jointly define a product set which has to have a non-empty intersection with the complement of the avoidance set. Otherwise you get a runtime error when the problem is run.
- Even if the intersection is non-empty but has a equi-distribution probability being too small, a runtime error might be the result, though the algorithm tries its best to find an admissible point.

## Input matrix questions¶

If the solution to a question is a row vector, a columne vector or a matrix, the generic problem is of the type input.matrix.

You can define a matrix within the `variables`

environment using the following syntax:

\matrix[<options>]{<variable>}{<entries>}Within the

`<entries>`

you can use `&`

to to start a new matrix column and use `\\`

to start a new matrix row.Entries can be:

- numbers
- functions
- other variables

The `\matrix`

takes the same `<options>`

as a function, that is `calculate`

, `normalize`

, `expand`

, and `sort`

.

`\format{<row_count>}{<col_count>}`

within the `answer`

environment.
- When specifying this format an empty matrix will be displayed with the given
`row`

and`col`

count as format and it's dimension can not be changed by the user. - When
`-1`

is used as either the row or column count, then the user has te determine this property himself. - When no
`\format`

is specified, the must specify both the row and column count himself.

Example:

```
\begin{problem}
%%% QUESTION 1 %%%
\begin{question}
\type{input.matrix}
\begin{variables}
% Row vector (1x4)
\number{a}{1/3}
\number{b}{1/7}
\matrix[calculate]{v_r}{a +1 & 1 & 3 & b}
% Column vector (4x1)
\matrix[calculate]{v_c}{3^2/7 \\ x \\ 10 \\ 0}
\end{variables}
\displayprecision{4}
\correctorprecision{3}
\field{real}
\text{
\textbf{Question 1}\\
Determine the decimal expansion of the entries in\\
$\var{v_r}$ and \\
$\var{v_c}$ \\
rounded to three decimal places.}
\explanation{Think about what rounded off to three decimal places means.}
}
\begin{answer}
\text{Answer: }
\solution{v_r}
\format{1}{-1}
\end{answer}
\begin{answer}
\text{Answer: }
\solution{v_c}
\format{-1}{1}
\end{answer}
\end{question}
%%% QUESTION 2 %%%
\begin{question}
\type{input.matrix}
\displayprecision{4}
\correctorprecision{2}
\field{real}
\begin{variables}
\matrix{m}{
3/7 & x^2 & 0 \\ % 3/7 is shown as a fraction
5 & 2 & 3
}
\matrix[calculate]{m_1}{
3/7 & x^2 & 0 \\ %3/7 is shown as a decimal rounded to _displayprecision_
5 & 2 & 3
}
\end{variables}
\text{
\textbf{Question 2}\\
Determine the decimal expansion of the entries in $\var{m}$ rounded to two decimal places.}
\explanation{Think about what rounded off to three decimal places means.}
}
\begin{answer}
\text{Answer: }
\solution{m_1}
\end{answer}
\end{question}
\end{problem}
\embedapplet{applet}
```

## Input text questions¶

A new question `type input.text`

has been implemented in generic framework.

In generic problem TeX files a new variable type `\string{ varname }{ string-content }`

can be used.

The user is expected to fill in fields of type MMString. The user answer can be checked for

- coincidence with a pre-given string variable
*varname*(default) by simply specifying this via \solution{*varname*} as usual - fulfilling some conditions; for this sake it first has to be given a variable name via: \inputAsString{
*user-varname*} (this is completely analogous to \inputAsFunction in input.function problems), and then several conditions can be checked. As by now you can check:

1. length(*user-varname*) : e.g. \checkStringsForRelation{length(*user-varname*) < length(*varname*) AND length(*varname*) > 3}

2. count(*symbol*,*user-varname*): e.g. \checkStringsForRelation{count((,*user-varname*)+count(),*user-varname*)<10} i.e. total number of parentheses less than 10

3. valid(*user-varname*): checks for user inputs to be valid math operations, e.g. \checkStringsForRelation{valid(*user-varname1*) AND valid(*user-varname2*)}

4. equal(*user-varname*,*varname*): checks whether*user-varname*stores a valid operation which is identical to the one stored in*varname*, e.g. \checkStringsForRelation{equal(*user-varname*,*varname*)} Observe that identity of operations is tested here algebraically, not numerically, so only rather simple cases can be expected to work flawlessly

5. equalString(*user-varname*,*varname*): Checks whether the stored strings are identical. There is no validity or identity check of operations. Just a character comparision.

6. equalTrimmedString(*user-varname*,*varname*): This does the same as*equalString*, but removes all spaces before comparing the two strings.

Here is an example:

\usepackage{mumie.genericproblem} \lang{de}{ \title{Eingabe von Textstrings} } \begin{problem} \begin{variables} \string{q}{Hallo} \end{variables} \begin{question} \lang{de}{ \text{\textbf{Frage 1}\\ Schreib doch mal 'Hallo'!} \explanation{'Hallo' und nix anderes!} } \type{input.text} \begin{variables} \string{f}{Hallo} \end{variables} \begin{answer} \text{Answer: } \solution{f} %%only 'Hallo' is accepted \end{answer} \end{question} \begin{question} \lang{de}{ \text{\textbf{Frage 2}\\ Schreibe bitte einen gültigen mathematischen Ausdruck!} \explanation{} } \type{input.text} \begin{variables} \string{f}{x^2+3x+1} \end{variables} \begin{answer} \text{Answer: } \solution{q} \inputAsString{g} \checkStringsForRelation{valid(g)} %%if you write 'sin x' it is not accepted, since only sin(x) is valid in Mumie expressions \end{answer} \end{question} \begin{question} \lang{de}{ \text{\textbf{Frage 3}\\ Schreibe den Ausdruck '(sin(x))^2+(cos(x))^2'!} \explanation{} } \type{input.text} \begin{variables} \string{h}{(sin(x))^2+(cos(x))^2} \end{variables} \begin{answer} \text{Answer: } \solution{h} \inputAsString{w} \checkStringsForRelation{equal(w,h)} %% observe that the input '(cos(x))^2+(sin(x))^2' is accepted (commutativity), but '1' is rejected, since the system does not know trigonometric identities \end{answer} \end{question} \end{problem} \embedapplet{applet}

## Input function cases questions¶

Another input type in the generic framework is `input.cases.function`

. This type is designed for case

differentiations of e.g. absolute value functions. *Note that you can only use this input type in ananswer environment.* The questions input type has to be set to

`input.generic`

.In answer you must use the command **\solution**{<variable>=<if-else expression>} where <variable> is an

identifier of an in the `variables`

environment defined variable. With the if-else expression a correct solution

is defined. The syntax should be as followed: **IFELSE{<if condition>}{<then implication>}{<else implication>}**.

The else implication can be another if-else expression.

**and**in dependence

of the users given answer. At the moment you can use the following commands in the answer environment:

**\checkAsFunction**(see above for details) to customize the numeric comparison

The default paramters of \checkAsFunction in`input.cases.function`

are set to:

min=-100, max=100, steps=300, epsilon=1E-8.**\allowForInput**(see above for details) to limit the users input for the implications in the case

differentiation**\allowForConditionInput**(analog to \allowForInput) to limit the users input for the conditions in

the case differentiation

An example:

\begin{problem} \begin{question} \type{input.cases.function} \text{Fallunterscheidung, distinguish cases} \begin{variables} \function{g}{abs(abs(x-1)+2x)} \function{h}{sin(x)} \end{variables} \begin{answer} \text{$\var{g} =$} \solution{g=IFELSE{x>=1}{3x-1}{IFELSE{x<-1}{-x-1}{x+1}}} \allowForInput[false]{abs} \allowForConditionInput[false]{abs} \end{answer} \begin{answer} \text{$(\sin(x))^2+(\cos(x))^2=1$. Bestimme für $0\le x\le 2\pi$ $\var{h} =$} \solution{h=IFELSE{0<=x<=pi}{sqrt(1-(cos(x)^2))}{-sqrt(1-(cos(x)^2))}} \allowForInput[false]{sin} \allowForConditionInput[false]{sin} \checkAsFunction[1E-6]{x}{0}{6.283}{100} \end{answer} \end{question} \end{problem}