Note: You are currently viewing documentation for Moodle 3.3. Up-to-date documentation for the latest stable version of Moodle is probably available here: Formulas: Variables.

Formulas: Variables: Difference between revisions

From MoodleDocs
(Redirected page to Formulas question type)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Formulas question type}}
#REDIRECT [[Formulas_question_type]]
 
Variables can be used to substitute question texts, define the answers and specify grading criteria. One of the main purpose of the system of variables is to simplify the task to make variation of the questions. Since the variables are generated at the beginning of a quiz, the quiz will be halted if any errors occur. Hence, in order to minimize errors, the system is designed to only have deterministic variables type, constant length lists and no branching.
 
==Scope of variables==
There are four types of variables (random, global, local and grading) which you can define and manipulate in the following fields:
 
{| class="nicetable"
|-
! Field
! Scope of application
|-
| Random variables
| The place to define the variation of variables for the whole question.
|-
| Global variables
| The scope includes all (instantiated) random variables.
|-
| Local variables
| The scope includes all global variables. Note: Each subquestion has its own local variables scope.
|-
| Grading variables
| The scope includes all local variables, plus special variables (e.g. <span style="font-family:monospace;font-size:113%;">_0</span>, <span style="font-family:monospace;font-size:113%;">_1</span>) depending on students' response.
|}
 
Furthermore, you can use the four types of variables in the following fields:
 
{| class="nicetable"
|-
! Field
! Scope of application
|-
| Main text
| All global variables can be used in substitution.
|-
| Subquestion text
| All local variables for the part can be used in substitution.
|-
| Answer
| All local variables for the part can be used in the expression.
|-
| Grading criteria
| All grading variables can be used in the expression.
|}
 
==Name of variables==
 
A variable name is a string of alphanumeric characters (<span style="font-family:monospace;font-size:113%;">a-z</span>, <span style="font-family:monospace;font-size:113%;">A-Z</span>, <span style="font-family:monospace;font-size:113%;">0-9</span> and <span style="font-family:monospace;font-size:113%;">_</span>) that cannot start with a number or the underscore. Examples of valid variable names are:
 
<pre style="font-size:113%;">
x, y1, z_1, foo_bar, myFirstVariable
</pre>
 
Variable names are case-sensitive so that, for example, the following names denote four different variables:
 
<pre style="font-size:113%;">
foobar, FooBar, fooBar, FOOBAR
</pre>
 
==Types of variables==
 
Each variable will be implicitly assigned with a type. The type is either ''number'', ''string'' or ''algebraic variable'', or ''list of numbers'' or ''list of strings''. A list is defined as elements enclosed by <span style="font-family:monospace;font-size:113%;">[</span> and <span style="font-family:monospace;font-size:113%;">]</span>. An element in a list can be referred to using the syntax <span style="font-family:monospace;font-size:113%;">A[b]</span>, for example <span style="font-family:monospace;font-size:113%;">[4,5,6][0]</span> gives the first number, that is, <span style="font-family:monospace;font-size:113%;">4</span>. These types are listed below:
 
{| class="nicetable"
|-
! Type
! Description
|-
| Number
| A number, for example <span style="font-family:monospace;font-size:113%;">1.2e-3</span>.
|-
| String
| Characters enclosed by two double quotes, for example <span style="font-family:monospace;font-size:113%;">"Hello"</span>.
|-
| List of numbers
| Numbers enclosed by <span style="font-family:monospace;font-size:113%;">[</span> and <span style="font-family:monospace;font-size:113%;">]</span>, for example <span style="font-family:monospace;font-size:113%;">[4,5,6]</span>. The equivalent short syntax is <span style="font-family:monospace;font-size:113%;">[4:7]</span> or <span style="font-family:monospace;font-size:113%;">[4:7:1]</span>.
|-
| List of strings
| Strings enclosed by <span style="font-family:monospace;font-size:113%;">[</span> and <span style="font-family:monospace;font-size:113%;">]</span>, for example <span style="font-family:monospace;font-size:113%;">["A","B","C"]</span>.
|-
| Algebraic variable
| It is simply a set of numbers (see syntax) defined in the non-random variables scopes, for example <span style="font-family:monospace;font-size:113%;">{1:100}</span>.
|}
 
==Random variables==
 
During the quiz creation, each random variable will be assigned by ONE value defined by the expression. Hence, each student can have their own set of values for the quiz attempt.
 
A random variable has different syntax than other variables and it is defined only in the '''Random variables''' field. These variables can be defined by assigning a set of elements or a shuffling of a list. The probability of selecting each element is equal so that each element has the same chance of being drawn. There are three main types of expressions:
===Set of elements===
A set of elements consists of elements delimited by <span style="font-family:monospace;font-size:113%;">{</span> and <span style="font-family:monospace;font-size:113%;">}</span>. Elements can be numbers, strings, lists of numbers, or lists of strings. For example, the variable below is a set of list of numbers, and the probability for element <span style="font-family:monospace;font-size:113%;">[2,4]</span> is 1/5:
 
<pre style="font-size:113%;">
F = {[0,0], [1,1], [2,4], [3,9], [4,16]};
</pre>
 
===Set of numbers===
A set of numbers consists of numbers delimited by <span style="font-family:monospace;font-size:113%;">{</span> and <span style="font-family:monospace;font-size:113%;">}</span>. You can also specify a range in the format of <span style="font-family:monospace;font-size:113%;">{start:stop:interval}</span>, where the numbers satisfying <span style="font-family:monospace;font-size:113%;">(start + n*interval) < stop, n = 0,1,2,3...</span> will be generated. If the interval is not specified, the value 1 will be used. Note that the stop value is not included by definition, but may be included for non-integers due to numerical errors. The sets of numbers defined below demonstrate different range syntaxes. The probabilities of the elements in each set are 1/8, 1/10 and 1/20 respectively.
 
<pre style="font-size:113%;">
A = {1,2,3,4,5,6,7,8};
 
B = {0:1:0.1};
 
C = {1:10, 10:100:10, 100, 200};
</pre>
 
===Shuffled list===
A list can be passed to the shuffle function. In the example below, the instantiated variable <span style="font-family:monospace;font-size:113%;">S</span> will be one of the permutations of the input list, say [4,3,2,5] or [2,5,4,2]. The probability of each list is therefore 1/4! = 1/24.
 
S = shuffle([2,3,4,5]);
 
==Variable assignments==
 
Variable assignments allow you to define and manipulate variables. It can be defined in the field of global variables, local variables and grading variables.
 
{| class="nicetable"
|-
! Component
! Definition
|-
| Expression
| Any combination of numbers and variables together with operators and functions listed herein ('''[[Formulas:_Operators|operators]]''',  '''[[Formulas:_Numerical_functions|numerical functions]]''' and '''[[Formulas:_Non-numerical_functions|non-numerical_functions]]'''). Typically, it is just a simple mathematical formula evaluated to a number. The variables used in the expression must be defined before.
|-
| Assignment
| Used to assign the evaluated result of an expression to a variable, in the form of <span style="font-family:monospace;font-size:113%;">name = expression;</span>.
|-
| For loop
| Allows a simple iteration in the form of <span style="font-family:monospace;font-size:113%;">for(element:list)</span>. All elements in the list are iterated.
|}
 
Examples of assignments are shown below:
 
{| class="nicetable"
|-
! Example
! Comments
|-
| <span style="font-family:monospace;font-size:113%;">a = 1; # comment</span>
| The text between <span style="font-family:monospace;font-size:113%;">#</span> and the end of the line is treated as a comment.
|-
| <span style="font-family:monospace;font-size:113%;">b = exp(3.333);</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">d = round(b,1);</span>
| The function <span style="font-family:monospace;font-size:113%;">round()</span> can round a number to the desired decimal.
|-
| <span style="font-family:monospace;font-size:113%;">e = 1 + sin(2) + pow(a,2);</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">A = [1,2,3];</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">x = A[0];</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">y = A[a];</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">z = A[2];</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">w = A[0] + A[1] + A[2];</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">m = max(x, y);</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">distance = sqrt(x*x + y*y + z*z);</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">theta = atan2(y, x);</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">smaller = x<y;</span>
| <span style="font-family:monospace;font-size:113%;">smaller</span> is 1.
|-
| <span style="font-family:monospace;font-size:113%;">con = (x<y) + (y<z);</span>
| <span style="font-family:monospace;font-size:113%;">con</span> is 2.
|-
| <span style="font-family:monospace;font-size:113%;">B = fill(3,0);</span>
| <span style="font-family:monospace;font-size:113%;">B</span> is now <span style="font-family:monospace;font-size:113%;">[0,0,0]</span>.
|-
| <span style="font-family:monospace;font-size:113%;">C = map("sqrt",fill(3,16));</span>
| Square root of <span style="font-family:monospace;font-size:113%;">[16,16,16]</span>, so <span style="font-family:monospace;font-size:113%;">C</span> is now <span style="font-family:monospace;font-size:113%;">[4,4,4]</span>.
|-
| <span style="font-family:monospace;font-size:113%;">s = 0;</span>
|
|-
| <span style="font-family:monospace;font-size:113%;">for (i:A)  s = s + i;</span>
| <span style="font-family:monospace;font-size:113%;">s</span> is 6 after the loop.
|-
| <span style="font-family:monospace;font-size:113%;">for (i:[0:3]) {<br>
  B[i] = sum(map("+",A,fill(3,i)));<br>
}</span> 
| <span style="font-family:monospace;font-size:113%;">B</span> will be <span style="font-family:monospace;font-size:113%;">[6,9,12]</span>.
|-
| <span style="font-family:monospace;font-size:113%;">p = pick(a+9,"","A","B");</span>
| <span style="font-family:monospace;font-size:113%;">pick()</span> always choose the first element if index out of range.
|-
| <span style="font-family:monospace;font-size:113%;">u = {-3,-2,-1,1:100};</span>
| <span style="font-family:monospace;font-size:113%;">u</span> and <span style="font-family:monospace;font-size:113%;">v</span> define algebraic variables, the numbers are the points for evaluation.
|-
| <span style="font-family:monospace;font-size:113%;">v = {-100:100:1}</span>;
|
|}
 
Notes:
<ul>
<li>Important! Index out of range cannot be checked by the validation during question save, so you have to check it yourself. Otherwise, the quiz initialization for some students may fail. Use <span style="font-family:monospace;font-size:113%;">pick()</span> as a safe variant if applicable.</li>
<li>Logical true is treated as 1 and false is 0, so the variable con above gets the value 2.</li>
<li>There is no branching, however, you may use the ternary operator <span style="font-family:monospace;font-size:113%;">(condition)?(true):(false)</span> for numbers or the <span style="font-family:monospace;font-size:113%;">pick()</span> function for general case.</li>
<li>The available functions are listed in the Appendix. There are many other functions for numeric list operations, and some for string list operations.</li>
</ul>
 
 
[[#top|Back to top of page]]
 
[[Formulas_question_type|← FORMULAS QUESTION TYPE]]
 
===In other languages===
[https://docs.moodle.org/all/es/F%C3%B3rmulas:_Variables Español]<br>
[https://docs.moodle.org/3x/fr/Formules:_Variables Français]

Latest revision as of 00:58, 1 February 2018