user1558881
user1558881

Reputation: 225

Maxima For Loop

I am trying to write a loop in maxima that will iterate i through values of 1 to 21, for each individual value of i the variable j is is iterated through values of 1 to 21. At every value of j the two are compared against each other and if i

for i:1 while i<=21 do(for j:1 while j<=21  do 
(if i<j then aa:realpart(c1[-j+i+21]))) and do aa1:makelist(aa,i,1,21,1);

Nothing even shows up on wxmaxima, it acts like it does not see this part of the code. Any thoughts?

Upvotes: 2

Views: 16634

Answers (2)

Stavros Macrakis
Stavros Macrakis

Reputation: 372

Let's look at your code:

for i:1 while i<=21 do(for j:1 while j<=21  do 
(if i<j then aa:realpart(c1[-j+i+21]))) and do aa1:makelist(aa,i,1,21,1);

Formatting this a little more clearly, it is:

for i:1 while i<=21 do
   (for j:1 while j<= 21 do
       (if i<j then aa:realpart(c1[-j+i+21])
       )
   ) 
and 
do aa1:makelist(aa,i,1,21,1);

Well, "and" is a boolean operator, and applying it to two loops isn't terribly meaningful (unless the loops return booleans), though Maxima lets you write it. Instead of (a and b) or (a and do b), you probably wanted to write (a,b), which executes a then b.

The nesting is probably not what you intended, either.

The second loop is "do aa1:...". In Maxima, "do xxx" is equivalent to "while true do xxx", i.e. an infinite loop. That would explain why this code never returned.

By the way, a simpler way of writing "for i:1 while i<=21 do..." is "for i thru 21 do...".

Hope this helps.

   -s

Upvotes: 2

Rupert Swarbrick
Rupert Swarbrick

Reputation: 2803

Someone just asked a rather similar question on the Maxima mailing list, following up on their previous one from two days before.

Now, terribly nice chap called Rupert Swarbrick replied constructively to both of those questions. He's so nice that he'll reply (yet) again. What it looks like you're trying to do is make a matrix, aa, such that aa[i,j] is zero on or below the diagonal and is equal to c1[-j+i+21] above the diagonal. Assuming that's the case, try the following:

genmatrix (lambda ([i,j], if i<j then realpart(c1[-j+i+21]) else 0), 21);

If you want something else, you'll have to ask a more specific question.

Upvotes: 3

Related Questions