Exporting examples for use in other applications



On 11/12/2012 04:09 PM, Christian Stengg wrote:
> Hello,
>
> I use Maxima for automatically generating exercises for my students. 
> For some type of exercise, Maxima produces a list of examples, e.g. a 
> list of equations that should be solved by the students. I'd like to 
> epxort these equations automatically for use in other applications, 
> e.g. for presentation in a webbrowser or for insertion as pictures in 
> some document or for filing in a database. Is it possible to get this 
> done by Maxima? If yes, what different options are there?
Hi,
I do something similar for my students. I have several files with 
multiple-choice questions such as the two sample questions in the 
attached file questions.mac.

Notice that each question has the following structure:
    question (id, param, txt, answers, [aux])
where id is a string, param is a list with one or more lists of possible 
values for each of the parameters in the question, txt is a list of 
substrings which concatenated will form the text of the question, 
answers is the list of the multiple choice answers (the first-one given 
is always the correct one, but they will be shuffled) and [aux] is one 
or more auxiliary expressions (or it can be omitted).

I then parse the questions to produce latex files that I print for the 
quizes and xml files that I import into Moodle's database (Moodle is an 
e-learning program). The attached file quiz.mac parses the questions 
into Latex. It should be used like this:

First, load quiz.mac with load("quiz.mac")
Then, load the file with the sample questions using: load("questions.mac")
At that point some arrays have been created with the information on the 
questions. You can then print the latex output of one of the two 
questions available, with random parameters, using their ids:

quiz_question("q1");
quiz_question("q2");

If you repeat those commands you can see the random changes introduced.
The array v[1], v[2],... referred in the text of the questions and in 
the answers refer to the values assigned to the parameters and aux[1], 
aux[2],... referred to the auxiliary expressions.

I hope this will serve you as an example for what you want to do. I 
intend to make some contributed module for Maxima at sometime whenever 
the code becomes more stable.

Regards,
Jaime

-------------- next part --------------
question("q1",
[[t^2, 2*t^2, 3*t^2, 4*t^2]],
["The speed of an object following a circular trajectory of radius 1 m is
given by the expression ", 'exptotex (v[1]), " where $t$ is the time
and SI units are being used. Find the expression for the acceleration's
modulus."],
['exptotex ('ratsimp ('sqrt (aux^2+v[1]^4))), 'exptotex (aux), 
 'exptotex ('ratsimp ('sqrt (aux^2+v[1]^2))), 'exptotex (aux+v[1]),
 'exptotex ('ratsimp ('sqrt (aux+v[1]^2)))],
'diff (v[1],t))$

question("q2",
[makelist (a, a, 2, 4), makelist (b, b, 2, 5)],
["An electric field is given by the expression (in SI units)
$\\vec{E}=x^", v[1], "\\,\\vec{e}_x$. Compute the potential difference
$V_b-V_a$, between the points with coordinates $a=(1,0,0)$ and
$b=(", v[2], ",0,0)$."],
'strappend ('numtofloat ([(1-v[2]^(v[1]+1))/(v[1]+1),
       (v[2]^(v[1]+1)-1)/(v[1]+1), 1-v[2]^(v[1]+1), v[2]^(v[1]+1)-1,
       (v[1]+1)*(1-v[2]^(v[1]+1))], 2), " V"))$
-------------- next part --------------
set_random_state(make_random_state(true))$
fpprintprec: 10$

list_permute(list,order):= block([permuted:[]],
  for i in order do permuted: endcons(part(list,i), permuted), permuted)$

list_cycle(list,n):= block([permuted:[], m:length(list)],
  for i thru m do permuted: endcons(part(list,mod(i+n-2,m)+1), permuted),
  permuted)$

numtofloat([x]) := 
   if (length(x) > 0) then
     (if (listp(first(x))) then
         map (lambda ([y],apply (numtofloat,cons(y,rest(x)))),first(x))
      else 
        (if (length (x) < 2) then
           float(first(x))
         else
            (if (second (x) = 0) then
               floor (first(x)+1/2)
             else
               float(floor(10^second(x)*first(x)+1/2)/10^second(x)))))$

numtoint(x) := if (listp(x)) then
                map(numtoint,x)
              else 
                floor(x+1/2)$

numtostr(x) := if (listp(x)) then
                map(numtostr,x)
              else 
                printf(false,"~a",x)$

exptotex(s) := block([ts],
              if (listp (s)) then
                map (exptotex, s)
              else
                (ts: tex (s, false),
                 substring (ts, 2, slength (ts) - 1)))$

strappend([x]):= if (length(x) > 0) then
             (if (listp(first(x))) then
                 map(lambda([y],apply(strappend,cons(y,rest(x)))),first(x))
             else 
                 (if (length(x) < 2) then
                     printf(false,"~a",first(x))
                  else concat(printf(false,"~a",first(x)),
                              printf(false,"~a",second(x)))))$

strprefix([x]):= if (length(x) > 0) then
             (if (listp(first(x))) then
                 map(lambda([y],apply(strprefix,cons(y,rest(x)))),first(x))
             else 
                 (if (length(x) < 2) then
                     printf(false,"~a",first(x))
                  else concat(printf(false,"~a",second(x)),
                              printf(false,"~a",first(x)))))$

question (id, param, txt, answers, [aux]) := ( 
   quiz_text[id]: txt,
   quiz_answers[id]: answers,
   quiz_parameters[id]: param,
   if (length(aux)>0) then
      (if (length(aux) > 1) then
          quiz_aux[id]: aux
       else
          quiz_aux[id]: first(aux)))$

quiz_question(id,[par]) :=
block([params,question,answers,order,aux,n:length(par),num:[],v:[],r,m,k],
params: quiz_parameters[id],
for x in params do
  (num: endcons(if (n>length(num)) then par[length(num)+1]
                else random(length(x))+1, num),
   v: endcons(x[last(num)],v)),
aux: ev(quiz_aux[id], nouns),
question: apply(concat, ev(quiz_text[id], nouns)),
answers: ev(quiz_answers[id], nouns),
m: length(answers),
if (n > 0 and listp(last(par))) then (order: last(par), n: n-1)
   else order: random_permutation(makelist(i,i,m)),
r: list_permute(answers, order),
printf (true,"%%~a~a~%~a~%", id,
         printf(false,"~{-~a~}",endcons(apply(concat,order),num)), question),
   printf (true,"\\begin{enumerate}~%~{\\item ~a~%~}\\end{enumerate}~%",r))$