Search code examples
arraysfor-loopmaxima

Arrays from nested for loops in maxima


Using MAxima I want to create 11 arrays in maxima. I am trying something like this:

for n:1 step 1 while n<=11 do( for j:1 while j<=21 do( if i<j then aa[n][i,j]:i+j+n));

This compiles fine but I can not use it how I would like. Say for example I want value 2,2 in the 5th array, I try the following but it does not work:

aa[5][2,2];

Any help is appreciated, Ben


Solution

  • Your code fragment is missing any loop over i or other assignment to i.

    You might consider using 'genmatrix' to construct a matrix, and then a loop over n to generate several matrices. E.g.:

    foo : lambda ([i, j], if i < j then i + j + n else 0);
    for n:1 thru 11 do aa[n] : genmatrix (foo, 21, 21);
    

    Then I get

    aa[5][2, 2];
     => 0
    aa[5][2, 3];
     => 10
    grind (aa[10]);
     => matrix([0,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32],
       [0,0,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33],
       [0,0,0,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34],
       [0,0,0,0,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35],
       [0,0,0,0,0,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36],
       [0,0,0,0,0,0,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37],
       [0,0,0,0,0,0,0,25,26,27,28,29,30,31,32,33,34,35,36,37,38],
       [0,0,0,0,0,0,0,0,27,28,29,30,31,32,33,34,35,36,37,38,39],
       [0,0,0,0,0,0,0,0,0,29,30,31,32,33,34,35,36,37,38,39,40],
       [0,0,0,0,0,0,0,0,0,0,31,32,33,34,35,36,37,38,39,40,41],
       [0,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,38,39,40,41,42],
       [0,0,0,0,0,0,0,0,0,0,0,0,35,36,37,38,39,40,41,42,43],
       [0,0,0,0,0,0,0,0,0,0,0,0,0,37,38,39,40,41,42,43,44],
       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,39,40,41,42,43,44,45],
       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,42,43,44,45,46],
       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,44,45,46,47],
       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,45,46,47,48],
       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,47,48,49],
       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,49,50],
       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,51],
       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])$