I have some code that sums planar waves and outputs a graphical result, as per the mock up below :
waveZoomFactor = 1;
waveHorizontalScaling = 1;
waveVerticalScaling = 1;
imageSize = 100;
createWaves =
Compile[{{x, _Real}, {y, _Real}, {sym, _Real}, {phaseShift, _Real}},
Table[Cos[
waveZoomFactor*(waveHorizontalScaling*x *Cos[2*Pi*(k/sym)] +
waveVerticalScaling*y *Sin[2*Pi (k/sym)]) + phaseShift], {k,
0, sym - 1}]
, CompilationTarget -> "C", RuntimeAttributes -> {Listable},
Parallelization -> True, RuntimeOptions -> "Speed"];
waves[phaseShift_] :=
ParallelTable[Total[createWaves[x, y, 5 , phaseShift]],
{x, -imageSize , imageSize , 0.2 }, {y, -imageSize , imageSize , 0.2 }];
CreatePattern := Table[Image[waves[t]], {t, 0, 2*Pi, 1}]
CreatePattern
I can't figure out why the code is so incredibly slow, in particular for larger 'imageSize'. I am a bit rusty with Mathematica so I am wondering if I overlooked something obvious or something dumb is going on. Thanks in advance.
Sum
inside thecreateWaves
function instead of returning list withTable
, since you're going to sum it anyway withTotal
? $\endgroup$