Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: non optimized inner for loops
Replies: 0

 sasa Posts: 7 Registered: 3/6/11
non optimized inner for loops
Posted: Nov 21, 2012 8:00 PM

Hi everyone. I have one, probably horrible non-optimized part of code,
which I would like to speed up. I have put all matrices to be RandomReal,
but in my code they take specific values.

All the best,
Aleksandar

limit1 = 10;
limit2 = 20;
limit3 = 10;
limit4 = 15;
mat1 = RandomReal[{-100, 100}, {limit3, 2}];
mat2 = RandomReal[{-100, 100}, {limit1, limit2, limit3, 2}];
mat3 = Table[
Sin[m*\[Pi]*y] + Cos[s*\[Pi]*y], {m, limit2}, {s, limit3}];
mat4 = Table[
Sin[m*s*\[Pi]*y] + Cos[s*\[Pi]*y], {m, limit2}, {s, limit3}];
mat5 = RandomReal[{-100, 100}, {limit1, limit2, limit3, 6}];
mat6 = Table[
Sin[m*m*\[Pi]*y] + Cos[s*\[Pi]*y], {m, limit2}, {s, limit3}];
mat7 = Table[
Sin[m*\[Pi]*y] + Cos[s*s*\[Pi]*y], {m, limit2}, {s, limit3}];
mat8 = RandomReal[{-100, 100}, {limit2, limit3, limit4, 2}];
mat9 = RandomReal[{-100, 100}, {limit1, limit2, limit3, limit4}];
mat10 = RandomReal[{-100, 100}, {limit1, limit2, limit3, limit4}];
mat11 = RandomReal[{-100, 100}, {limit1, limit2, limit3, limit4}];

For[n = 1, n < limit1 + 1, n++,
For[i = 1, i < limit2 + 1, i++,

For[j = 1, j < limit3 + 1, j++,

y = (mat1[[j, 1]] + mat1[[j, 2]])/2*d1;

mat2[[n, i, j, 1]] = mat3[[i, j]];

mat2[[n, i, j, 2]] = mat4[[i, j]];

mat5[[n, i, j, All]] = 1/2 (mat6[[i, j]] + mat7[[i, j]]);

Clear[y];

For[k = 1, k < limit4 + 1, k++,

zz = 1/2 (mat8[[i, j, k, 1]] + mat8[[i, j, k, 2]]);

mat9[[n, i, j, k]] = {mat5[[n, i, j, 1]] + zz*mat5[[n, i, j, 4]],
mat5[[n, i, j, 2]] + zz*mat5[[n, i, j, 5]],
mat5[[n, i, j, 3]] + zz*mat5[[n, i, j, 6]]};

mat10[[n, i, j, k]] = mat11[[n, i, j, k]].mat9[[n, i, j, k]];

Clear[zz]
]

]]
]