Possible Duplicate:
Alternatives to procedural loops and iterating over lists in Mathematica
I am new to Mathematica and not familiar with functional programming. In particular, I have no idea on how to replace the usual for loops in C++. Here is an example of the way I am writing code now:
InitCoords[] := {
VDiv[gap, region, initCell];
n = 0;
For[nz = 0, nz < initCell[3], nz++,
For [ny = 0, ny < initCell[2], ny++,
For[nx = 0, nx < initCell[1], nx++,
VSet[c, nx + 0.25, ny + 0.25, nz + 0.25];
VMul[c, c, gap];
VSAdd[c, c, -0.5, region];
For[j = 0, j < 4, j++,
Do[r[n][i] = c[i], {i, 1, NDIM}];
If[j != 3,
If[j != 0, r[n][1] += 0.5*gap[1];];
If[j != 1, r[n][2] += 0.5*gap[2];];
If[j != 2, r[n][3] += 0.5*gap[3];];
];
++ n;
];
];
];
];
}
I know that this is a very bad way to code in Mathematica. What are the alternatives then?
Another question. Is there is a Mathematica convention for defining a subroutine with no input? I want to partition my big program into smaller parts. Currently, I am doing it by writing a function with no input.
VDiv,VSet,VMul, andVSAdddo? – rcollyer Dec 16 '12 at 06:44VSet[v_, sx_, sy_, sz_] := { v[1] = sx; v[2] = sy; v[3] = sz; }
VMul[v1_, v2_, v3_] := { v1[1] = v2[1]v3[1]; v1[2] = v2[2]v3[2]; v1[3] = v2[3]*v3[3]; }
VSAdd[v1_, v2_, s3_, v3_] := { v1[1] = v2[1] + s3v3[1]; v1[2] = v2[2] + s3v3[2]; v1[3] = v2[3] + s3*v3[3]; }
(sorry it seems that I don't know the comment formatting)
– Gosere Dec 16 '12 at 09:32DownValues(likec[i]) andSubValues(liker[n][1]) to mimic the array notation in C, and I'm pretty sure that's not what you intended. – Simon Woods Dec 16 '12 at 13:50