# How do I write nested for-loops?

Looking through the reference.wolfram, I couldn't see an example of how to write a multiple line "for" loop in Mathematica. I need to nest many for-loops in such a way I can do many things in the innermost for-loop. How does one do this in Mathematica?

Here is an example (which you can may be use to demonstrate) of the kind of thing I want to do on Mathematica. In the following, a,b,c are integers such that $1 \leq a,b,c \leq (k-1)$ and w is the m^th of the k^th roots of unity.i.e $w = exp( (2 \pi I m)/k)$ . Now for a fixed value of $k$, I am running through all values of a,b,c and $1 \leq m \leq (k-1)$. And for each set I am evaluating the roots of the polynomial, $p(x) = x^4 - 6x^2 -x(w^{a-c} + w^{c-a} + w^b + w^{-b} + w^{b-c} + w^{c-b} + w^a + w^{-a}) +(3 -w^c - w^{-c} - w^{a+b-c} - w^{-a-b+c} - w^{a-b} - w^{-a+b} )$

To check if all the roots of it are in the interval $[-2\sqrt{2},2\sqrt{2}]$. If yes, then I am printing out the value of a,b and c.

Is such a kind of nested for-loop doable in Mathematica?

The following code I believe runs on Sage/Python. Since this is a quartic equation, I would think that Mathematica has a way of calculating the exact roots in terms of square-roots and then doing the comparison. Also since this polynomial evaluates the eigenvalues of a Hermitian matrix, all roots should be positive.

k=6;
var('x')
for a in range(1,k):
for b in range(1,k):
for c in range(1,k):
q = 1;
for m in range (1,k):
w = exp((2*pi*I*m )/k)
p(x) =
x^4 - 6*x^2 - x*(w^(a-c) +
w^(c-a) + w^b + w^(-b) + w^(b-c) + w^(c-b) + w^a + w^(-a)) +
(3 -w^c - w^(-c) - w^(a+b-c) - w^(-a-b+c) - w^(a-b) - w^(-a+b))
g(x)=real_part(p(x)).simplify()
U = (max([s.rhs() for s in g.solve(x)])).simplify_full()
L = (min([s.rhs() for s in g.solve(x)])).simplify_full()
if (U > 2*sqrt(2)):
q=0
if (L < -2*sqrt(2)):
q=0
if (q == 1):
print "a=",a,"b=",b,"c=",c

• You can nest any amount of Do or For calls... so, yes, doable. Please take a look at the documentation for details. Jan 21 '15 at 17:27
• @YvesKlett Can you show an example of how to write multiple lines in a "call" on Mathematica? All the examples on reference.wolfram are single line examples and its not clear how something like the above can be achieved via that! Jan 21 '15 at 17:31
• @user6818 Just use ; to separate commands, as usual. For[i=0, i < 10, i++, Print[i]; Print[i^2]]. Pay attention to the difference between , and ;. But please do not use For if you are a beginner in Mathematica ... take a look at Do instead and try to use functional constructs such as Table, Map, etc. whenever possible. Jan 21 '15 at 18:28
• @user6818 "lines" have no meaning in Mathematica. Line breaks don't matter at all, just like in C, Java, etc. The documentation page of For has several examples with multipe commands in the body of For. So does the documentation page of Do. Whether or not you write them on a single line or you break them across multiple ones makes no difference. Jan 21 '15 at 18:30
• Did you even try anything? For[i = 1, i <= 3, i++, For[j = i^2, j <= i^2 + 3, j++, Print["{i,j}=", {i, j}] ] ] Jan 22 '15 at 0:11

It's better to stay away from loops in Mathematica:

k = 6;
l = Tuples[ConstantArray[Range[k - 1], 4]];
p[{a_, b_, c_, m_}] := Module[{w = Exp[(2*Pi*I*m)/k]},
x^4 - 6*x^2 - x*(w^(a - c) + w^(c - a) + w^b + w^(-b) + w^(b - c) + w^(c - b) +
w^a + w^(-a)) + (3 - w^c - w^(-c) - w^(a + b - c) - w^(-a - b + c) - w^(a - b) - w^(-a + b))]
sols = Solve[p@# == 0, x] & /@ l;
compares = And @@@ (Thread[Less[Abs@x /. #, 2 Sqrt@2]] & /@ sols);
TableForm[Pick[l, compares], TableHeadings -> {None, {"a", "b", "c", "m"}}] • You have way too much forbearance. (Why are you even for bears in the first place?) Jan 22 '15 at 0:14
• @DanielLichtblau I may answer ... jokes.cc.com/funny-sports-jokes/m2rtxg/the-bear-hunter Jan 22 '15 at 1:27
• Heh. Somehow strangely reminds me of myself on MSE. Jan 22 '15 at 15:52