user10141041
user10141041

Reputation:

For loop in Maxima for matrix making

I want to make 3x3 M matrix using for loop. eq[] and Acc[] are arrays. Instead of getting 3x3 matrix , I am getting 1x9 Array which I don't want to get.

for f:1 thru 3 step 1 do(
    for r:1 thru 3 step 1 do(
    M[[r],[f]]:ratcoef(eq[r],Acc[f]))
);
listarray(M);
(%o22)  [3*l[1]^2*m[3],3*l[1]*l[2]*m[3]*cos(r[2](t)-r[1](t)),(3*l[1]*l[3]*m[3]*cos(r[3](t)-r[1](t)))/2,3*l[1]*l[2]*m[3]*cos(r[2](t)-r[1](t)),3*l[2]^2*m[3],(3*l[2]*l[3]*m[3]*cos(r[3](t)-r[2](t)))/2,(3*l[1]*l[3]*m[3]*cos(r[3](t)-r[1](t)))/2,(3*l[2]*l[3]*m[3]*cos(r[3](t)-r[2](t)))/2,(3*l[3]^2*m[3]+12*Theta[3])/4]

Somehow I have to mention a symbol to indicate that new row is starting, I tried to put ; but it ends the for loop immediately and causes problem. Any suggestions?

Upvotes: 1

Views: 1093

Answers (1)

Robert Dodier
Robert Dodier

Reputation: 17575

When you assign something to M[i, j] without declaring M previously, Maxima creates an array (called an "undeclared array" in Maxima terminology) attached to the symbol M as a property, not a value. Therefore when you input M in the interactive prompt, you see only M because it doesn't have a value. (Properties are items which are associated with a symbol, which are distinct from the value of the symbol.)

With that preamble, I will recommend that you assign a matrix value to M and then assign to the elements of the matrix. I don't have the definitions of eq and Acc so ratcoef doesn't do anything useful here.

(%i4) M : zeromatrix (3, 3);
                           [ 0  0  0 ]
                           [         ]
(%o4)                      [ 0  0  0 ]
                           [         ]
                           [ 0  0  0 ]

(%i6) for f:1 thru 3
        do for r:1 thru 3
          do M[r, f] : ratcoef(eq[r],Acc[f]);
(%o6)                         done

Note that the subscripts are just r and f, not [r] and [f]. In general [x] is a list of 1 element, namely x.

There are other ways to accomplish this, if it turns out this doesn't work so well for you.

Upvotes: 3

Related Questions