proc iml; /* Using SAS to construct a Christmas Tree from an iterated function system, and adding ornaments and a star. Rick Wicklin 12/14/2012 To construct an iterated function system in SAS, see http://blogs.sas.com/content/iml/2012/12/12/iterated-function-systems-and-barnsleys-fern-in-sas/ */ /* 1. Each row is a 2x2 linear transforamtion */ /* Christmas tree */ L = {0.03 0 0 0.1, 0.85 0.00 0.00 0.85, 0.8 0.00 0.00 0.8, 0.2 -0.08 0.15 0.22, -0.2 0.08 0.15 0.22, 0.25 -0.1 0.12 0.25, -0.2 0.1 0.12 0.2}; /* ... and each row is a translation vector */ B = {0 0, 0 1.5, 0 1.5, 0 0.85, 0 0.85, 0 0.3, 0 0.4 }; prob = { 0.02 0.6 0.1 0.07 0.07 0.07 0.07}; /* For convenience, transpose the L and B matrices */ L = L`; B = B`; /* 3. iterate the discrete stochastic map */ N = 1e5; /* number of iterations */ x = j(2,N); k = j(N,1); x[,1] = {0, 2}; /* initial point */ call randseed(1); call randgen(k, "Table", prob); do i = 2 to N; x[,i] = shape(L[,k[i]], 2)*x[,i-1] + B[,k[i]]; end; /* 4. plot the iteration history */ y = x`; create IFS from y[c={"x" "y"}]; append from y; close IFS; /* just for fun, create ornaments and colors */ idx = ceil(N*ranuni(j(500,1))); x1 = x[1,idx]`; jdx = loc(abs(x1)>0.04); idx = idx[jdx]; x1 = x[1,idx]`; y1 = x[2,idx]` - 0.1; group = ceil(5*ranuni(j(nrow(idx),1))); create Ornaments var {x1 y1 group}; append; close Ornaments; quit; /* basic IFS Christmas Tree */ ods graphics / width=200px height=400px; proc sgplot data=IFS; title "SAS Christmas Tree"; scatter x=x y=y / markerattrs=(size=1 color=ForestGreen); yaxis display=none; xaxis display=none; run; /* Add ornaments and star */ data Star; x2=0; y2=10; output; run; data All; merge IFS Ornaments Star; if group=. then group=1; run; data Attrs; length Value MarkerColor \$20; ID = "Ornaments"; Value = 1; MarkerColor = "Red "; output; Value = 2; MarkerColor = "Blue "; output; Value = 3; MarkerColor = "Purple "; output; Value = 4; MarkerColor = "Gold "; output; Value = 5; MarkerColor = "Chartreuse"; output; run; *ods graphics / width=400px height=800px; proc sgplot data=All noautolegend dattrmap=Attrs; title "SAS Christmas Tree"; scatter x=x y=y / markerattrs=(size=1 color=ForestGreen); scatter x=x1 y=y1 / transparency=0.33 attrid=Ornaments markerattrs=(size=8 symbol=CircleFilled) group=group; scatter x=x2 y=y2 / markerattrs=(color=Gold size=15 symbol=StarFilled); yaxis display=none; xaxis display=none; run;