Optimising maple calls


  • administrators

    Everytime to use a maple call you slow your question down. So the trick to optimise your questions is to include fewer maple calls. The way to do this is by evaluating mutiple expressions in one call and then pulling apart the returned list of values. Here's an example. Let's generate two random vectors calculate the cross product.

    $m=maple("
    randomize;
    roll := rand(-5 .. 5);
    a := Vector([roll(), roll(), roll()]);
    b := Vector([roll(), roll(), roll()]);
    LinearAlgebra:-CrossProduct(a, b)
    ");
    

    This returns the mathematical form of the cross product and sets it equal to $m.

    But we also want a typeset form and the vectors for a and b so we can add them to the question text. We can return a list of variables like this and use switch to pull the expression apart.

    $m=maple("
    randomize;
    roll := rand(-5 .. 5);
    a := Vector([roll(), roll(), roll()]);
    b := Vector([roll(), roll(), roll()]);
    [a,b,LinearAlgebra:-CrossProduct(a, b)]
    ");
    $a=switch(0,$m);
    $b=switch(1,$m);
    $TA=switch(2,$m);
    

    Here is the result

    We can see the three vectors return correctly but the switch fails. The reason is the implicit parse of the equals sign in the lines for the switch. The T.A. language is not able to parse the expression $m so is cannot assign the three variables $a, $b and $TA. The fix is to stop it parsing the expression by converting the expressions to strings before you return them from maple. Here's how

    $m=maple("
    randomize;
    roll := rand(-5 .. 5);
    a := Vector([roll(), roll(), roll()]);
    b := Vector([roll(), roll(), roll()]);
    [convert(a,string),convert(b,string),convert(LinearAlgebra:-CrossProduct(a, b),string)]
    ");
    $a=switch(0,$m);
    $b=switch(1,$m);
    $TA=switch(2,$m);
    

    This gives

    If you want typeset maths you can just all the commands to the list of returned values from the maple expressions. You'll notice the typeset maths is already converted to a string which means you do not have to add the convert lines for these expressions. Here's the code

    $m=maple("
    randomize;
    roll := rand(-5 .. 5);
    a := Vector([roll(), roll(), roll()]);
    b := Vector([roll(), roll(), roll()]);
    [convert(a,string),convert(b,string),convert(LinearAlgebra:-CrossProduct(a, b),string),
    MathML[ExportPresentation]($a), MathML[ExportPresentation]($b)]
    ");
    $a=switch(0,$m);
    $b=switch(1,$m);
    $TA=switch(2,$m);
    $aD=switch(3,$m);
    $bD=switch(4,$m);

    Here's the result


    Now you can use the variables in your grading code or question text. With this trick you can optimise all you maple calls.


Log in to reply