

(4 intermediate revisions by the same user not shown) 
Line 1: 
Line 1: 
−  [https://docs.moodle.org/33/en/Formulas_question_type < Formulas question type]
 +  <div style="background:#d9edf7;color:#3a87ad;0px;padding:8px;border:1px solid #bce8f1;borderradius:4px;margin:0;"> 
−   +  <p style="margin:2px 8px;"> 
−   +  This page has been moved to '''[[Formulas:_VariablesFormulas: Variables]]''' ←''click the link'' 
−  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 the error, the system is designed to only have deterministic variables type, constant length lists and no branching.
 +  </p> 
−   +  </div> 
−  ==Scope of variables==
 +  ''Do not delete this page.'' 
−  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 questions.
 
−  
 
−   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. _0, _1 ) 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 the substitution
 
−  
 
−   Subquestion text
 
−   All local variables for the subquestion can be used in the substitution
 
−  
 
−   Answer
 
−   All local variables for the subquestion can be used in the expression
 
−  
 
−   Grading criteria
 
−   All grading variables can be used in the expression
 
−  }
 
−   
−  ==Names of variables==
 
−   
−  A variable name is a string of alphanumeric characters (az, AZ, 09 and _) that cannot start with a number or the underscore. Examples of valid variable names are:
 
−  <pre>x, y1, z_1, foo_bar, myFirstVariable</pre>
 
−   
−  Variable names are casesensitive so that, for example, the following are four different variables:
 
−  <pre>foobar, FooBar, fooBar, FOOBAR</pre>
 
−   
−  ==Types of variables==
 
−   
−  Each variable will be assigned implicitly with a type. A type is either number, string or algebraic variable, plus list of numbers or list of strings. List is defined as the elements enclosed by [ and ]. An element in list can be referred using the syntax A[b], for example [4,5,6][0] gives the first number, that is, 4. These types are listed below:
 
−   
−  { class="nicetable"
 
−  
 
−  ! Type
 
−  ! Description
 
−  
 
−   Number
 
−   A number, for example: 1.2e3
 
−  
 
−   String
 
−   Characters enclosed by two double quotes, for example: "Hello"
 
−  
 
−   List of number
 
−   Numbers enclosed by [ and ] , for example: [4,5,6] , The equivalent short hand syntax are [4:7] or [4:7:1.]
 
−  
 
−   List of string
 
−   Strings enclosed by [ and ] , for example: ["A", "B", "C"]
 
−  
 
−   Algebraic variable
 
−   It is simply a set of numbers (see syntax) defined in the nonrandom variable scope, for example: {1:100}
 
−  }
 
−   
−  ==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 only defined in the field Random variables. 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 equal chance to be drawn. There are three main types of expressions:
 
−  ===Set of elements===
 
−  A set of elements is elements enclosed by { and }. The element can be either a number, a string, a number list or a string list. For example, the variable below is a set of list of numbers, and the probability for element [2,3] is 1/5:
 
−   
−  F = {[0,0], [1,1], [2,4], [3,9], [4,16]};
 
−  ===Set of numbers===
 
−  A set of numbers is numbers enclosed by { and }. You can also specify a range in the format of {start:stop:interval}, where the numbers satisfying (start + n*interval) < stop, n = 0,1,2,3,..., will be generated. If the interval is not specified, value 1 will be used. Note that the stop value is not included by definition, but may be included for nonintegers due to numerical errors. The set 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.
 
−   
−  A = {1,2,3,4,5,6,7,8};
 
−   
−  B = {0:1:0.1};
 
−   
−  C = {1:10, 10:100:10, 100, 200};
 
−   
−  ===Shuffled list===
 
−  A list can be passed to the shuffle function. In the example below, the instantiated variable S with take one of the permutation 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 option 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 in the Appendix. 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 'name = expression ;'
 
−  
 
−   For loop
 
−   Allows a simple iteration in the form of 'for(element:list)'. All elements in the list are iterated.
 
−  }
 
−   
−  Examples of assignments are shown below:
 
−   
−  { class="nicetable"
 
−  
 
−  ! Example
 
−  ! Comments
 
−  
 
−   a = 1;
 
−   # text after "#" and the end of line are treated as comment
 
−  
 
−   b = exp(3.333);
 
−  
 
−  
 
−  d = round(b, 1);
 
−   # the function round() can round the number to the desired decimal
 
−  
 
−   e = 1 + sin(2) + pow(a,2);
 
−  
 
−  
 
−   A = [1,2,3];
 
−  
 
−  
 
−   x = A[0];
 
−  
 
−  
 
−   y = A[a];
 
−  
 
−  
 
−   z = A[2];
 
−  
 
−  
 
−   w = A[0] + A[1] + A[2];
 
−  
 
−  
 
−   m = max(x, y);
 
−  
 
−  
 
−   distance = sqrt(x*x + y*y + z*z);
 
−  
 
−  
 
−   theta = atan2(y, x);
 
−  
 
−  
 
−   smaller = x < y;
 
−   # smaller is 1
 
−  
 
−   con = (x < y) + (y < z);
 
−   # con is 2
 
−  
 
−   B = fill(3,0);
 
−   # B is now [0,0,0]
 
−  
 
−   C = map("sqrt",fill(3,16));
 
−   # square root of [16,16,16], so C is now [4,4,4]
 
−  
 
−   s = 0;
 
−  
 
−  
 
−   for (i:A) s = s + i;
 
−   # s is 6 after the loop  
−  
 
−   for (i:[0:3]) {
 
−  B[i] = sum(map("+",A,fill(3,i)));
 
−   
−  }
 
−   # B will be [6,9,12]
 
−  
 
−   p = pick(a+9,"","A","B");
 
−   # pick() always choose the first element if index out of range
 
−  
 
−   u = {3,2,1,1:100};
 
−   # u and v define algebraic variables, the numbers are the points for evaluation
 
−  
 
−   v = {100:100:1};
 
−  
 
−  }
 
−   
−  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 pick() 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 (condition) ? (true) : (false) for numbers or the pick() function for general case.</li>
 
−  <li>The available functions are listed in the Appendix. There are many additional functions for the numeric list operation, and a few for the list of strings.</li>
 
−  </ul>
 
−   
−  [https://docs.moodle.org/33/en/Formulas_question_type < Formulas question type]
 
−  [[Category:Formulas question type]]
 