Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #12472 > unrolled thread
| Started by | Dana DeLouis <dana01@me.com> |
|---|---|
| First post | 2012-11-26 04:38 +0000 |
| Last post | 2012-11-26 04:38 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: non optimized inner for loops Dana DeLouis <dana01@me.com> - 2012-11-26 04:38 +0000
| From | Dana DeLouis <dana01@me.com> |
|---|---|
| Date | 2012-11-26 04:38 +0000 |
| Subject | Re: non optimized inner for loops |
| Message-ID | <k8urni$st2$1@smc.vnet.net> |
Hi. I don't have any great solutions, but here's just an observation.
> For[k = 1, k < limit4 + 1, k++,
> zz = 1/2 (mat8[[i, j, k, 1]] + mat8[[i, j, k, 2]]);
In this loop, you are calculating the Mean for All k's, and the data in 1 & 2 (who's dimension is also 2, or ALL)
You could pull this out of the loop, and do it in 1 calculation:
zz = Mean /@ mat8[[i,j, All, All]]
> 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]]};
The above is hard to follow, so here's just a general idea.
For mat9, it appears the data on the right is constant during the loop for All k's.
In this small example, we change the value of the last index (ie your k) with the sum of the first two numbers
m=RandomInteger[{1,9},{3,2,2,3}]
{{{{2,3,7},{4,9,7}},{{4,7,1},{3,7,4}}},{{{3,2,8}, << removed for display >>
m[[All,All,All,-1]]=m[[All,All,All,1]]+m[[All,All,All,2]];
m
{{{{2,3,5},{4,9,13}},{{4,7,11},{3,7,10}}},{{{3,2,5}, << removed for display >>
The 5 is the sum 2+3, 13 is 4+9, etc.
This was done without a For loop.
See if any ideas above will help.
Good Luck
= = = = = = = = = =
HTH :>)
Dana DeLouis
Mac & Mathematica 8
= = = = = = = = = =
On Wednesday, November 21, 2012 8:03:03 PM UTC-5, Sasa B wrote:
> 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]
>
> ]
>
>
>
> ]]
>
> ]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web