I'm Mathematica newbie so please be gentle :) I have this, heavily 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. Also, matrices mat3, mat4, mat6 and mat7 consist of random simple trigonometric functions. Any kind of help is much appreciated. I have read about Map, Table, Do, Functional Programming... but I don't know how to apply it.
Code is part of Finite element-Finite strip calculation. I don't mind losing computing time during "real" calculation, but this part of code is just placing elements of matrices into other places with just few calculations. Matrices mat2, mat5, mat9, mat10 and mat11 need to be stored for later data processing.
Bonus question: is it possible to Compile this part of code?
Hope I gave optimal amount of data about my problem
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;
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]
]
]]
]
Comments
Post a Comment