Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

non optimized inner for loops

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128723] non optimized inner for loops
  • From: Sasa B <neprihvatljivo at gmail.com>
  • Date: Wed, 21 Nov 2012 19:47:14 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net

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]
    ]

   ]]
 ]



  • Prev by Date: Re: Animating a 3D Plot
  • Next by Date: Re: correlation function
  • Previous by thread: Text processing in Mathematica
  • Next by thread: Re: non optimized inner for loops