I have list containing 2D points in various groupings, i.e. points, lists of points (figures), lists of lists of points (patterns) and finally a list of patterns. The list also contains other data, i.e. Colors and other decorators.
For (a simple!) example:
fig1 = {{0,0}, {1,0}, {0,1}}
fig2 = {{2,0}, {3,0}, {2,1}}
fig3 = {{4,0}, {5,0}, {4,1}}
li = {{fig1, Red}, {{fig2, fig3}, Blue}}
Now I would like to replace the points in the list with the result of a function applied to that point, for example a matrix multiplication. For the example we can use a simple function. f[p:pnt2D] := 2p
or multiplication with IdentityMatrix[2]
will do to illustrate.
I am looking for a typical functional Mathematica way to attack this problem. Extract all the points from the list, apply the function and replace with the outcome. In such a way that it works for all sorts of (huge) lists.
How to replace data in a list (tree) in a typical Mathematica way, preferably using clear code?
li /. p : {_?NumericQ, _?NumericQ} :> f@p
? $\endgroup$//.
, which is also probably the worst in performance. Since we have that cleared up, is my second sentence the answer you were looking for? $\endgroup$ReplaceAll
is ideal, the question even uses the word replace in the title :). $\endgroup$