1
$\begingroup$

Going through some old code I found this line:

SampleSierpinskiCarpet[n_] := 
 Module[{digitpairs = {{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 
  0}, {2, 1}, {2, 2}}}, 
  Map[N[#.(1/3^Range[26])] &, 
   Transpose[RandomChoice[digitpairs, {n, 26}], {1, 3, 2}], {2}]]

Could someone explain exactly whats going on here? There is a lot of syntax to unpack, and no extra comments to go along with it.

When plotted it produces a Sierpiński Carpet:

S = SampleSierpinskiCarpet[100000];
V = SimilarFunction[100000];
Row[ListPlot[#, PlotStyle -> PointSize[Tiny], AspectRatio -> 1, 
ImageSize -> 400] & /@ {S, V}]

Yields: enter image description here

edit: Thanks for the help everyone: The similar function looks like this

SampleViksecFractal[n_] :=
Module[{digitpairs = {{0, 0}, {0, 2}, {2, 0}, {2, 2}, {1, 1}}}, 
Map[N[#.(1/3^Range[26])] &, 
Transpose[RandomChoice[digitpairs, {n, 26}], {1, 3, 2}], {2}]]
$\endgroup$
  • 3
    $\begingroup$ Here's the rough idea: Essentially, each point of the carpet is described by a list of offsets (an element of digitcharacters). You choose 26 offsets, each for a subsequent level of the fractal, which are then multiplied by 1/3^n, where n is the level. For the rest, try to evaluate parts of the expression one by one to see how the output changes as include more and more of the original code $\endgroup$ – Lukas Lang Jul 2 '18 at 18:39
  • $\begingroup$ Do you have the definition of SimilarFunction as well? $\endgroup$ – MarcoB Jul 2 '18 at 19:45
  • $\begingroup$ When looking at code like this, I find it's easier to see what it does if I enforce formatting on the code. Try evaluating Needs[ "GeneralUtilities`"]; HoldPrettyForm[ SampleSierpinskiCarpet[n_] := <rest of definition> ] and you might find it easier to understand what it's doing. $\endgroup$ – Jason B. Jul 2 '18 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.