In this article, you learned how to create and edit variables. It was mentioned that variables are written in PHP, but this programming language doesn’t suffice for complex mathematical operations. Instead, we want to use the programming language Maxima. This is an open-source Computer Algebra System, so it is very suitable for programming mathematics. Documentation on this programming language can be found here. It is also possible to use Python (see this article) or R (see this article) for variable definitions.
To explain how defining variables using Maxima works, we start with an easy example. Say we want to make an exercise where students have to add two random integers together. Using only PHP, the Variables tab of this exercise would look something like this:
Step 1
If instead, we want to use Maxima, we need to follow some steps. First, we create a variable that will be a Maxima code block. For the example where we add two integers, this can look as follows:
The function
sw_maxima_native
is a SOWISO-made PHP function that evaluates its argument as Maxima code.The function
set_random_state
enables random variable generation.This part defines a function called
random_exclude(n,m,list)
. When called, this function generates a random integer in the closed interval [n,m] excluding all integers in list. When no integers should be excluded, the list should be an empty list [].This is the most important part, namely where we generate our variables (
number_1 and number_2
) and the solution (sum
).The function
return(list)
returns all elements in its input list.In the preview of the
variable $code
, we see the Maxima list that was returned by the function return(list).
Step 2
After writing our Maxima code block, we make two new variables named $maxima_list
and $latex_list
that have the following definitions:
These functions generate two PHP lists, one in Maxima code and one in LaTeX code. The lists look the same in this particular case because an integer in ‘Maxima language’ looks the same as a number in ‘LaTeX language’. In general, the two lists look different, as can be seen in the following picture:
Step 3
Once we have defined variables $maxima_list
and $latex_list
, we need to export the items in the lists to SOWISO variables that we can use in the exercise. To do this, we click on the button “Explode” under $maxima_list
and $latex_list
.
This will automatically generate a SOWISO variable for every item in the list, so in this case 2x3 variables. The names of the variables are $name_language
, where “name” is the name of the variable as written down in the Maxima code block $code
, and “language” is either max
for Maxima or tex
for LaTeX.
In the example of the adding integers exercise, this will look as follows:
These variables can be used in, for instance, the “Texts”, “Positive feedback”, “Negative feedback” and “Solution” tabs of the exercise.
Summary
In short, we need to do the following steps to create variables using Maxima code:
Write a SOWISO variable named
$code
that contains all variables you need and return those in the return function.Export these returned variables to a Maxima list using
$maxima_list -> sw_maxima_list($code,false)
and to a LaTeX list using
$latex_list -> sw_maxima_list($code,true)
For both of these lists, click “Explode” to generate SOWISO variables that we can use in the rest of the exercise
Below we provide some code that you can copy-paste into your $code
variable:
sw_maxima_native("block(
set_random_state(make_random_state(true)),
random_exclude(n, m, list):= block([r, succes, simp:true],
/*Function to create random integers in the interval [n,m] excluding all integers in list.
If no integers should be excluded, list should be an empty list: [] */
succes: false,
while not succes do(
r: random(m - n + 1) + n,
succes: not member(r, list)
),
return(r)
),
var_1: 42,
return([
var_1
])
)")