I have to reassign values in a big array many times and it turned out to be the most time consuming in my program. The best way I could figure out is this
nn=1024;nnn=1024/2-1;t=9.4122*10^-6;mass=RandomComplex[{1,I},{nn,nn}];
mass = ParallelTable[mass[[k, k1]] E^(I*t*(Mod[k - 1, nn, -nnn]^2 +
Mod[k1 - 1, nn, -nnn]^2)), {k, 1, nn}, {k1, 1, nn}];
I wonder if there is a faster functional way to change the values in an array with a function depending on their positions.
I tried to use Compile for this function
f = Compile[{{xp, _Complex, 2}}, ParallelTable[xp[[k,k1]]
E^(I*t*(Mod[k - 1, nn, -nnn]^2 + Mod[k1 - 1, nn, -nnn]^2)), {k, 1, nn}, {k1, 1, nn}],
{{k, _Integer}, {k1, _Integer}, {nn, _Integer}, {nnn, _Integer}, {Mod[_, _, _], _Integer}}]
But I get the answer that Compiled expression should be a mashine-size Complex number. Please tell me what is wrong.
mass=ParallelTable[...]statement repeatedly? If so, why on earth would you recalculate the same factors repeatedly? Simply calculate those once (i.e. remove themass[[k, k1]]piece, give result a name), and at each iteration to change mass it becomesmass=a_name*mass... – ciao Jan 19 '17 at 09:16