1
$\begingroup$

As I get more familiar JavaScript & Typescript, I noticed the need in my Wolfram code to continually pass big arrays into my functions. I guess this leads me to two questions I haven't been able to figure out.

  1. Does this have any negative performance implications as the arrays of values get bigger and/or multiple noise samples need to be take for multiple rings at the same time? (Notice how I keep needing to passing the array and points into the function. I'm not sure if there is a performance hit for this configuration.)
  2. How can I refactor this code from a block into it's own function? Though rather straightforward with JS/TS, it's not clear to me exact how to rewrite the block into a main function that can be called.
sampleNoise[index_, n_, pts_, arr_] := Module[{noise, sample},
      sample = arr[[index]];
      noise = getvalue[sample, #] & /@  pts;
      If[Length@pts == Length@noise, MapThread[Times, {basicRing[n], noise}], Throw["Error: Sample Noise Length Mismatch"]]
      ]

Block[{array, val, pts, frames = 80, n = 200, size = 256, dispersion = 12},
    (* Global Var for block *)
    array = noisyArray[frames, size, dispersion]; (* 3D array of noisy values *)
    pts = translatedCoods[n, size]; (* circular pts to sample from along array zaxis *)
 
    (* Create frame data *)
    val = Table[Catch[sampleNoise[i, n, pts, array]], {i, frames}];
    val = val[[5 ;; -5]]; (* Removes top and bottom frames for consistency *)
 
    ListAnimate[graphicsEngine[val], 10] (* Adds graphial elements to data points / not animated in question *)
 ]

$\endgroup$
1
  • 3
    $\begingroup$ Not an answer but Times has the Listable attribute so no need for MapThread just basicRing[n] * noise is equivalent. $\endgroup$ Apr 26, 2021 at 2:19

1 Answer 1

1
$\begingroup$

large arrays as arguments

Mathematica in general uses the "copy on write" approach for handling data. That means as long as you do only read data from the array there is no performance problem. Only if you would try to alter the array or a copy of it within the function body a copy of the large array would be made which could result in performance problems.

create function

actually it would be as simple as prefixing your Block with the function signature, e.g.:

makeAnimation[]:=Block[...]

Typically you would of course want the function to use some arguments, which could mean to replace some of the block local variables with arguments:

makeAnimation[frames_, n_, size_, dispersion_]:=Block[{array, val, pts},...]

which you then could call like:

makeAnimation[80,200,256,12]

You might want to look up how you can set default values for some arguments or how you can use Options for your function to allow optional named arguments for your functions. This could look like:

Options[makeAnimation]={"Size"->256,"Dispersion"->12};
makeAnimation[frames_:80, n_:200,OptionsPattern[]]:=Block[{
    array, val, pts,size=OptionValue["Size"],dispersion=OptionValue["Dispersion"]
   },...]

You will find explanations and more examples for that in the documentation or on this site...

$\endgroup$
1
  • $\begingroup$ The copy-on-write can be avoided in some cases if one of the HoldXXX attributes is used, and all one does is (re)set values in the array. $\endgroup$ Apr 28, 2021 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.