My question is related to computing what is called "invariant measure" for a particular well known fractal - the Sierpinski triangle.
We have an array m of four two by two matrices, say
m = {{{1.22951,-0.102459},{0.058548,1.1856}},{{2.34016,2.08943},{-2.31509,1.50439}},
{{1.52091,2.98751},{2.58555,-2.0641}},{{-11857.1,6214.29},{3214.29,-1678.57}}};
and an array a of four vectors, say:
a = {{-0.127561,-0.238217},{-0.322608,0.247723},{-0.601847,-0.165997},{1304.29,-353.571}};
We also have an array c of four coefficients (related to probabilities and determinants of the matrices), for instance:
c = {0.817674, 1.95388, 2.22763, 180.714};
Then we have four functions of a two-dimensional vector x, for i=1,2,3,4 defined as
w[i_,x_] := m[[i]].x + a[[i]]
The recursive formula I am trying to code is
f[ n_, x_ ] := Sum[c[[i]]*f[ n-1, w[i,x] ],{i,1,4}]
with f[ 0 ,x ] = 1 over the unit square 0 < x[[1]] < 1, 0 < x[[2]] < 1, and zero elsewhere:
f[0, x_] := If[x[[1]] > 0 && x[[1]] < 1 && x[[2]] > 0 && x[[2]] < 1, 1, 0]
I would like to plot functions f[ n, x ] for, say, n=1 up to n=10. It takes a lot of recursions, so I would like to find the optimal code that can be compiled for a fast execution. I tried to compile, but all my efforts failed, probably because I do not really understand what can be compiled and how. Without compiling it takes Mathematica 22 seconds to compute just one value::
Timing[f[10, {0.5, 0.5}]]
{22.667, 0.869726}
Can it be done faster with Mathematica? If so, how? Any help will be appreciated
w2[x_] := Dot[m, x] + a ; f2[0, x_] := If[x[[1]] > 0 && x[[1]] < 1 && x[[2]] > 0 && x[[2]] < 1, 1, 0] ; f2[n_, x_] := Dot[c, f2[n - 1, #] & /@ w2[x]]
. $\endgroup$