# Part 3: Variables and Functions¶

Overview of commands for generic problems, displays all commands and options, grouped by topic. This can be used as a reference. TODO - create commands overview.

## Defining variables locally¶

Questions can become dynamic and personalised through the use of variables and functions. These must be defined in a variables environment and in the case of local variables within a question environment, for example:

\begin{question}
\begin{variables}
\number{a}{12}
\number{b}{4}
\number{c}{10}
\number{d}{5}
\function{f}{a/b}
\function{g}{c/d}
\end{variables}
...
\end{question}


Will result in variables $a=12, \quad b=4, \quad c=10, \quad d=5$ and functions $f=\frac{12}{4}, \quad g=\frac{10}{5}$

• \number: a fixed number variable, this can be any kind of number. The field command determines how the number will be interpreted.
\number{<variable>}{<value>}
• <variable> variable name for the number
• <value> the value of the number
• \function: a function definition, the definition may include variables and other functions. When an included variable is not defined, it will be a free variable. Again, the field command determines how the numbers within the function will be interpreted.
\function[<action>]{<variable>}{<expression>}
• <variable> variable name for the function
• <expression> definition of the function
• <action> this parameter is optional and can be used to define actions that have to be applied to the function. The following actions can be used:
• replace: replace all variables in the expression (this is the default action and can be omitted).
$f=\frac{10}{5}$ is the output of
  \number{a}{10}
\number{b}{5}
\function{f}{a/b}

is the same as, however replace is always done so can be left out

\function[replace]{f}{a/b}

• normalize: replaces all variables and normalizes the expression. Normalize applies the following rules.
$f=x^2+7$ is the output of
    \function[normalize]{f}{x*x+3+4+0*z}

• expand: replaces all variables and expands the expression. Expand applies the following rules.
$f=x^2- 3x +2$ is the output of
    \function[expand, normalize]{f}{(x-1)(x-2)}


Only applying expand currently gives a rather verbose (but correct) output. When you would like to expand a function, for now it is best to also apply normalize.
• sort: replaces all variables and sorts the expression. TODO - Update sort code rules and add documention on rules.
$f=abc-b+c$ is the output of
    \function[sort]{f}{c-b+a*c*b}

• calculate: replaces all variables and calculates the expression. Using this option will always result in a number, in the case where a variable is undefined it takes the value $0$.
$f=0.333333333333333$ is the output of
  \number{a}{1}
\number{b}{3}
\function[calculate]{f}{a/b}


As you might have noticed, functions evaluated with the calulate option will show the result with 15 digits behind the decimal mark. This is undesired when you want to output this value on the screen. In order to reduce the amount of digits behind the decimal mark, you can provide a second (optional) paramter that in order to change this precision, the calculated result will then be rounded to this precision. More details on precision can be found here.
Optional Second parameter:
• number: rounds the calculated result to the given number. This number must be higher then the specified corrector precision.
• display: rounds the calculated result to the specified display precision.
$f=0.333$ is the output of
  \number{a}{1}
\number{b}{3}
\function[calculate, 3]{f}{a/b}

% instead of a number one can use the option display
% the result will then be displayed based on \displayprecision

\function[calculate, display]{f}{a/b}


### Special function commands¶

There are two special commands with which one can define new function variables from expression or other existing variables:

• \derivative: creates a new function representing the derivative of the expression/variable that was passed into it.
  \derivative[<action>]{<variable>}{<expression>}{<free_variable>}

• <action>, <variable> and <expression> are identical to the input of a \function command.
• <free_variable>: defines the free variable in the given <expression>.
• $f_1 = 4x^3 + 2 \cdot 3x^2$ is the output of
  \derivative{f_1}{x^4 + 2*x^3}{x}

% and is equal to the following

\function{f}{x^4 + 2*x^3}
\derivative{f_1}{f}{x}

• \substitute: creates a new function that is the composition of two other functions.
  \substitute[<action>]{<variable>}{<expression>}{<free_variable>}{<substitute>}

• <action>, <variable> and <expression> are identical to the input of a \function command.
• <free_variable>: defines the free variable in the given <expression>.
• <substitute>: must be an earlier defined variable. The <free_variable> will be replaced with the <substitute> variable.
• $h = f \circ g$ is the output of
  \substitute{h}{f}{x}{g}

• This command can also be used to determine the value of a given function at a specific point $x_0$ (using a constant function variable). For example, $6^4 - 5 \cdot 6^3 + 4 \cdot 6^2 + 3 \cdot 6 + 7$ could be the output of
  \function{f}{x^4 - 5x^3 + 4x^2 + 3x + 7}    % defines a function named f
\randint{x0}{2}{9}                          % point where f is to be evaluated
\substitute{fx0}{f}{x}{x0}                  % this is the function f at point x_0


You can also normalize, expand, sort and calculate the \derative and \substitute command. They take the same option <action> as a normal \function. In fact, you would often want to normalize a derivative, compare the following output with the earlier example shown for derivative. $f_1 = 4x^3 + 6x^2$ is the output of
    \derivative[normalize]{f_1}{x^4 + 2*x^3}{x}


### Combining function actions together¶

So far, only single actions were passed on as option to a function, derivative or substitute command. However it is also possible to apply multiple actions in one go by separating each action with a comma.

You would often want to normalize the result of an expand action. Take the following example:
$f = xx - 4x -4x - 4 \cdot -4$ is the output of

  \function[expand]{f}{(x-4)^2}


However, $f = x^2 - 8x +16$ is the output of
  \function[expand, normalize]{f}{(x-4)^2}


You could even add sort in the example above (although that wouldn't do anything in this case).

Similar to expand, you would often want to add normalize to the \derivative and \substitute command.

## Global variables¶

In the example above, we have created variables that exists within a question. However, we can also create global variables that will be accessible from all questions in the problem.

\begin{problem}

\begin{variables}
% global variables
\end{variables}

\begin{question}
... % global variables can be used here
\begin{variables}
% local/question variables, only accessible in this question
\end{variables}
\end{question}

\begin{question}
... % global variables can be used here
\begin{variables}
% local/question variables, only accessible in this question
\end{variables}
\end{question}

\end{problem}


## Using variables in texts¶

All variables (including global variables) can be used in texts (e.g. question task, answer text) by using the tex command \var with the name of the variable as argument:

\begin{question}
\text{Determine the decimal expansion of $\var{f}$ correct to three decimal places.}
...
\begin{choice}
\text{$\var{solution1}$}
...
\end{choice}
...
\end{question}


## Number field¶

All variables and functions are interpreted in a number field which can be defined by the \field command. It can take one of the following values:
• real: real numbers (i.e. double values); this is the default value if the \field{number field} command is omitted
• integer: integer numbers
• rational: rational numbers
• complex: complex numbers with real numbers as real and imaginary parts
• complex-rational: complex numbers with rational numbers as real and imaginary parts

## Random numbers¶

Random numbers can be used instead of fixed numbers to personalize problem data. The following commands are allowed:
• \randint: a random integer number, including zero (same as randint[z])
• \randint[Z]: a random integer number, excluding zero
• \randdouble: a random double number, including zero
• \randrat: a random rational number, including zero
\begin{question}
...
\begin{variables}
\randint[Z]{x1}{-10}{10}
\randdouble{x2}{0}{1}
\randrat{name}{minNumerator}{maxNumerator}{minDenominator}{maxDenominator}
\end{variables}
\end{question}

• \drawFromSet: draw randomly a number (or string) from a set
- parameters: \drawFromSet[options]{name}{set}
- numbers in the set can be integer, rational or double numbers
- separator in the set can be any (non white space) character, default separator is the comma
- type is either number or string, default type is number
- multiple options are separated by a space (see example below)
\begin{question}
...
\begin{variables}
\drawFromSet{a}{1,2,3,4,5,6}
\drawFromSet{b}{1/2,5,1.3,9/11,42}
\drawFromSet[separator=;]{c}{1;2.1;3.5;9}
\drawFromSet[separator=; type=string]{d}{hello;world}
\end{variables}
\end{question}


Random variables can be adjusted to satisfy a specific constraint.
For example we might adjust the variable x to be always greater than the variable y.
• varlist = comma separated list of variables to be adjusted
• relation = a relation that defines an avoidance condition under which the variables in varlist should be adjusted
\begin{question}
...
\begin{variables}
\randint[Z]{x}{-10}{10}
\randint[Z]{y}{-10}{10}
\drawFromSet{a}{1,2,3}
\drawFromSet{b}{1,2,3}
\randadjustIf{x,y}{x <= y} %adjust x and y if x is not greater than y
\end{variables}
\end{question}


Note: \randadjustIf on a variable defined by \drawFromSet only works for (the default) type number.

You may call randadjustIf multiple times to adjust disjoint lists of variables as in the following example:

\begin{question}
...
\begin{variables}
\randint[Z]{x}{-10}{10}
\randint[Z]{y}{-10}{10}
\randint[Z]{c}{-10}{10}
\randint[Z]{d}{-10}{10}
\randadjustIf{x,y}{x <= y} %adjust x and y if x is not greater than y
\randadjustIf{c,d}{c = d OR c = x}
\end{variables}
\end{question}


Note: You should NEVER adjust the same variable twice. The following example might lead to problems:

\begin{question}
...
\begin{variables}
\randint[Z]{x}{-10}{10}
\randint[Z]{y}{-10}{10}
\randint[Z]{c}{-10}{10}
\randint[Z]{d}{-10}{10}
\randadjustIf{x,y}{x <= y} %adjust x and y if x is not greater than y
\randadjustIf{c,x}{c = d OR c = x} % here the variable x will be re-adjusted
% and the constraint from previous randadjustIf might be broken.
\end{variables}
\end{question}


Just as random variables defined inside the question environment, global variables (which are defined in the problem environment) can be adjusted, too. You may use \randadjustIf in the same manner as explained above, also multiple times for disjoint sets of global variables. You should NOT adjust global variables within a question, but only in the problem environment where they are defined.

## Switch/Case statements¶

Alternatively use a switch/case statement to make sure your variables satisfy specific constraints.

\begin{switch}

\begin{case}{>condition 1>}
<variable definitions>
\end{case}

\begin{case}{<condition 2>}
<variable definitions>
\end{case}

...

\begin{default}
<variable definitions>
\end{default}

\end{switch}


Every variable in a switch environment must have a default definition, but not a definition for every
single case environment. (see example below)

There is no limitation on how many case environments you can use inside a switch environment. The default
environment though is obligatory!

You can use switch/case statements globally and on question level.
Note: Never use switch/case statements in combination with \randadjustIf. It is meant as an alternative.

An example:

\begin{variables}
\randint{a}{1}{5}
\function{f0}{10*a}

\begin{switch}
\begin{case}{a>3}
\number{c}{3}
\randrat{d}{1}{2}{3}{7}
\drawFromSet{m}{1,2,3}
\function{f}{2*a}
\derivative{g}{3x^2+sqrt(x)}{x}
\substitute{h}{sqrt(y)}{y}{g}
\string{s}{case 1}
\end{case}

\begin{case}{a=3}
\number{c}{5}
\end{case}

\begin{default}
\randint{c}{1}{10}
\randrat{d}{-2}{-1}{3}{7}
\drawFromSet{m}{-1,-2,-3}
\function{f}{-2*a}
\derivative{g}{-3x^2+sqrt(x)}{x}
\substitute{h}{sqrt(y)}{y}{g}
\string{s}{default case}
\end{default}

\end{switch}

\end{variables}


## Variable pools¶

Instead of using random values for numbers it is possible to define several sets of values inside a so called variable pool. A pool consists of several variables environments holding each the elements for a single set of variables. A single set will be chosen randomly for every homework problem. The following example shows a pool with two sets of variables:

\begin{question}
...
\begin{pool}
\begin{variables}
\number{a}{7}
\number{b}{3}
\end{variables}

\begin{variables}
\number{a}{5}
\number{b}{6}
\end{variables}
\end{pool}
\end{question}


Note: Variable pools can only be used inside a question. There are no global variable pools at the moment.

Variable pools can also be used in combination with randomly generated numbers:

\begin{question}
...
\begin{pool}
\begin{variables}
\randint{a}{1}{3}
\drawFromSet{b}{1,2,3}
\function[calculate]{f}{2*a+b}
\end{variables}

\begin{variables}
\randint{a}{-3}{-1}
\drawFromSet{b}{-1,-2,-3}
\function[calculate]{f}{2*a-b}
\end{variables}
\end{pool}
\end{question}


In variable pools valid commands are: \randint, \randdouble, \randrat, \drawFromSet, \randadjustIf,
\number, \string, \text and \function.

## Compute solutions in MC questions¶

When using the compute option in multiple choice answers inside the \solution command an additional \iscorrect command is required. It constructs a relation and takes three arguments:
1. left side of the relation; must be a function variable
2. relation sign (i.e. <, >, =, >=, <=, !=)
3. right side of the relation; must be a function variable

When the relation solves to true then the correct answer for this choice will be yes:

\begin{question}
...
\begin{choice}
\text{$\var{g}$}
\solution{compute}
\iscorrect{g}{<}{f}
\end{choice}
...
\end{question}


## Complete example¶

\usepackage{mumie.genericproblem}

\begin{problem}

\begin{question}

\begin{variables}
\function{f}{a/b}
\function{g}{b/a}
\end{variables}

\begin{pool}
\begin{variables}
\number{a}{1}{7}
\number{b}{2}{7}
\end{variables}

\begin{variables}
\number{a}{5}
\number{b}{6}
\end{variables}
\end{pool}

\type{mc.multiple}

\begin{choice}
\text{$\var{g} < \var{f}$}
\solution{compute}
\iscorrect{g}{<}{f}
\end{choice}

\begin{choice}
\text{$\var{f} < 2$}
\solution{compute}
\iscorrect{f}{<}{2}
\end{choice}

\begin{choice}
\text{$\var{g} < 1$}
\solution{compute}
\iscorrect{g}{<}{1}
\end{choice}
\end{question}

\begin{question}

\begin{variables}
\function[expand]{h}{(x-a)(x-b)}
\randint[Z]{a}{-5}{5}
\randint[Z]{b}{-5}{5}
\function[calculate]{r_1_a}{a+1}
\function[calculate]{r_1_b}{b-1}
\function[calculate]{r_2_a}{a-2}
\function[calculate]{r_2_b}{b-3}
\function[calculate]{r_3_a}{a-3}
\function[calculate]{r_3_b}{b-1}
\end{variables}

\text{What are the roots of h(x)=$\var{h}$ ?}
\type{mc.unique}
\permutechoices{1}{4}

\begin{choice}
\text{a = $\var{r_1_a}$\\
b = $\var{r_1_b}$}
\solution{false}
\end{choice}

\begin{choice}
\text{$a = \var{r_2_a}$\\
$b = \var{r_2_b}$}
\solution{false}
\end{choice}

\begin{choice}
\text{$a = \var{r_3_a}$\\
$b = \var{r_3_b}$}
\solution{false}
\end{choice}

\begin{choice}
\text{$a = \var{a}$\\
$b = \var{b}$}
\solution{false}
\end{choice}

\end{question}

\end{problem}


Add picture from clipboard (Maximum size: 500 MB)