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(waveHorizontalScalingx Cos[2Pi(k/sym)] +
waveVerticalScalingy Sin[2Pi (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.


Suminside thecreateWavesfunction instead of returning list withTable, since you're going to sum it anyway withTotal? – flinty Dec 29 '22 at 16:39