Search code examples
listcommon-lispmoodlemaximajsxgraph

How to sum two Maxima/LISP-lists elementwise, in order to use them in a JSXgraph embedded in a Moodle STACK-exam?


What the title says. I'm trying to build a graphical, two-variable optimization problem for students to solve using the STACK-type Moodle-question, which uses Maxima as a backend for dealing with the math. I've tried the following:

/*Constants*/
c1 : rand_with_step(0,15,0.1);
c2 : c1 + 2;
c3 : c1 + 1;
c4 : c1;

/*Solutions*/
OptimiPa : [c3,c1];
za : -1 * OptimiPa[1] + 2 * OptimiPa[2];

OptimiPb : [c3 + 1/2,c3 - 1/2];
zb : 2 * OptimiPb[1] + OptimiPb[2];

OptimiPc : [c1,c1];
zc : OptimiPc[1]  + 3 * OptimiPc[2] 

OptimiPd : [];
zd : inf;

/*Variables for drawing*/
a : 1000000;
b : a - 1;

/*Variables for drawing lines*/
OptimiPa2 : OptimiPa + [1,0.5];

This, however does not produce the desired result when plugged into a JSXgraph:

<jsxgraph style="width=500em;height=500em"; box="uniqueName">(function() {

var board = JXG.JSXGraph.initBoard('uniqueName', {boundingbox:['{#c3-5#}','{#c2+2#}','{#c3+3#}', '{#c1-2#}'], keepaspectratio: true,zoom:false});

/*Axes*/
var axis1 = board.create('axis', [['{#c3-5#}','{#c1#}'], ['{#c3+3#}', '{#c1#}']]);
var axis2 = board.create('axis', [['{#c1#}','{#c1-5#}'], ['{#c1#}', '{#c2+3#}']]);

/*Polygon angles*/
var k1 = board.create('point', ['{#c3#}','{#c1#}'],{visible:false});
var k2 = board.create('point', ['{#c1#}','{#c1#}'],{visible:false});
var k3 = board.create('point', ['{#c1-a#}','{#-(-c1-a)#}'],{visible:false});
var k4 = board.create('point', ['{#c1-b#}','{#2 - (-c1 - b)#}'],{visible:false});
var k5 = board.create('point', ['{#c1+3/2#}','{#(c1 + 3/2) - 1#}'],{visible:false});

/*Optimization area as a polygon*/
var optAl = board.create('polygon',[k1,k2,k3,k4,k5],{borders: {visible: true},vertices: {visible: false}});


/*Points for drawing lines*/
var pa1 = board.create('point', '{#OptimiPa#}',{visible:false});
var pa2 = board.create('point', '{#OptPa2#}',{visible:false});

/*Lines*/
var suoraA = board.create('line',[pa1,pa2]);

})();
</jsxgraph>

What am I doing wrong here? The rest of the image appears just fine, but the line suoraA doesn't appear inside the figure.

EDIT: Fixed the code in accordance with @jkiiski's points, but the issue still persisted. Look below for a partial answer.


Solution

  • Alright, so the points made by @jkiiski were a part of the problem, but I have now found out what the issue might be. The variable OptimiPa2 is a list, and apparently STACK for some reason doesn't like accessing list-type variables directly using the '{#variable_name#}' syntax, even though that should work. What fixed the issue for me was to manually access and enter the list elements into a new list, that I then gave as an argument to the point "constructor":

    /*Pisteitä suoria varten*/
    var pa1 = board.create('point', ['{#OptimiPa[1]#}','{#OptimiPa[2]#}'],{visible:true});
    var pa2 = board.create('point', ['{#OptmiPa2[1]#}','{#OptmiPa2[2]#}'],{visible:true});
    
    /*Suoria*/
    var suoraA = board.create('line',[pa1,pa2]);
    

    This gave me the following image:

    Optimization area with the desired line.

    This is what I wanted to achieve, although I wish it was easier to enter lists as arguments to JSXGraph. Then again, I might just be doing it wrong.

    EDIT: Referencing lists the way @jkiiski suggested in the comments does the trick, though I swear that I tried it before and it didn't work.