HOW can one OUTPUT a DOUBLE-NESTED loop?

resolvent's picture

Here's a little test double loop

for i from 1 to 2 do

for j from 1 to 2 do

A[i,j]:=Binomial[i+j,i];

end do;

end do;

I would like to get ANYTHING to output. I get NOTHING, no matter WHAT variations I make. I can output when the loop is just a SINGLE nest. But, as soon as I embed that nest inside a second nest - NOTHING outputs.

 

Comments

printlevel

If you want to see the computations of inner loops, then you have to increase the value of printlevel.  In this case,

printlevel := 2:
for i to 2 do
   for j to 2 do
        f(i,j)
   end do;
end do;
                                    f(1, 1)
                                    f(1, 2)
                                    f(2, 1)
                                    f(2, 2)


acer's picture

HELP PAGE

Having TROUBLE with `for` or `do`? Then READ the HELP-PAGE for `for` or `do`.

The HELP-PAGE for `do` is accessible by entering ?do or ?for

It says that the printing from nested loops is controlled via printlevel, in the bullet point titled "Note about Nested Loops". It has a cross-reference to ?printlevel

acer

resolvent's picture

Searching for the correct command

Thank you for your help. I wouldn't know for which command to look under Help.

Now I have the more difficult problem of including multiple if and then statements within my recursion.  My recursion is

with(Physics):

c[i+1,j,k,l] : = (1/((i+1)*d[0]))* sum(iq*d[i+1-iq]*c[iq,j,k,l],iq=1..i) +

sum(c[i-1,j,kq,l]*binomial(i+j-kq-l,k),kq=0..k)

- sum(jq*c[i,jq,k,l]*e[j+1-jq],jq=1..j)

+sum(c[i,j-1,k,l]*binomial(i+j-k-lq,l),lq=0..l)

+KroneckerDelta[i,1]*KroneckerDelta[j,0]*KroneckerDelta[k,0]*KroneckerDelta[l,0]

+KroneckerDelta[i,0]*KroneckerDelta[j,1]*KronckerDelta[k,0]*KroneckerDelta[l,0];

with c[i,j,k,l] = 0 if any of i,j,k,l <0,

c[0,j,k,l] = 0 for all j,k,l if k>0

c[i,0,k,l] = 0 for i,k,l if l>0

and c[0,j,0,l] are assumed known for all j,l and c[i,0,k,0] are assumed known for all i,k and the arrays d[ ] and e[ ] consist of algebraically independent indeterminates.

Hence, I seek to express c[i,j,k,l] in terms of c[0,j',0,l'], c[i',0,k',0], d[0],d[1],...etc e[0],e[1],e[2]...etc

Doug Meade's picture

Other Possibilities: print and userinfo

Using printlevel gives you all of the output. If you want to see only selected results you should insert explicit print statements.

Another way to control output is to use the userinfo and infolevel commands. The userinfo command specifies the "level" required to have the output displayed and what to print. The infolevel command controls the setting of the "level" which is used to decide when to print the information in the userinfo. Unfortunately, to the best of my knowledge, userinfo only displays messages, not 2D output. But, you can mimic this functionality with flags that you set and check on  your own.

For example,

MyTest := (a,b) -> isprime(a) and isprime(b):
for i from 1 to 10 do
  for j from 1 to 10 do
    a[i,j] := i+j;
    if MyTest(i,j) then print( i, j, a[i,j] ) end if;
  od;
od;
                                   2, 2, 4
                                   2, 3, 5
                                   2, 5, 7
                                   2, 7, 9
                                   3, 2, 5
                                   3, 3, 6
                                   3, 5, 8
                                  3, 7, 10
                                   5, 2, 7
                                   5, 3, 8
                                  5, 5, 10
                                  5, 7, 12
                                   7, 2, 9
                                  7, 3, 10
                                  7, 5, 12
                                  7, 7, 14

I hope this is helpful.

Doug

---------------------------------------------------------------------
Douglas B. Meade  <><
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
Phone:  (803) 777-6183         URL:    http://www.math.sc.ed

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}