Consider such an iteration function
SeedRandom[1];
n = 2.5;
func[{A_, B_, C_}, t_] := With[{M = (A + B)/2, P = A + (B - A) 0.3, Q = A + (B - A) 0.7},
{{C, A, M + t (P - M)}, {B, C, M + t (Q - M)}}];
init = RandomReal[1, {3, 2}];
iter = Join @@ Table[func[p, If[Length@# <= 2^(n - 1), 1, Mod[n, 1]]], {p, #}] &;
ans1 = Nest[iter, {init}, Ceiling@n]
The above code can work normally, but it's not flexible enough, because it depends on the length of the previous iteration list. In this case, it's simple, the general term formula is $2^{n-1}$. When func
is changed, it's not necessarily a simple exponential function. I came up with this
list = NestList[Join @@ Table[func[i, 1], {i, #}] &, {init}, Ceiling@n];
iter2 = Join @@ Table[func[i, If[Length@# <= Length@list[[Floor@n]], 1, Mod[n, 1]]], {i, #}] &;
ans2 = Nest[iter2, {init}, Ceiling@n]
ans2 == ans1
I feel like this requires extra calculation, is there a better way?
My goal is to implement a fractal, when the number of iterations is not an integer, there is a transition effect. Change {n,0,5}
to {n,0,5,1}
to see the difference.
Manipulate[
Module[{func,init,iter,ans1},
func[{A_,B_,C_},t_]:=With[{M=(A+B)/2,P=A+(B-A) 0.3,Q=A+(B-A) 0.7},{{C,A,M+t (P-M)},{B,C,M+t (Q-M)}}];
SeedRandom[5];
init=RandomReal[1,{3,2}];
iter=Join@@Table[func[p,If[Length@#<=2^(n-1),1,Mod[n,1]]],{p,#}]&;
ans1=Nest[iter,{init},Ceiling@n];
Graphics[{Polygon/@ans1}]
],{n,0,5}]
<= 2^(n-1)
to<=Length@list[[Floor@n]]
(without explaining why). Is the goal simply to predict the length of the elements oflist
? $\endgroup$C
is a protected system symbol that serves a particular and familiar purpose. Using it for other purposes can lead to confusion. $\endgroup$it's not flexible enough, because it depends on the length of the previous iteration list
Yes, it very explicitly depends on the length of the previous result. That's an inherent part of the semantics of what you've given us. How could you possibly remove that and get the same result? $\endgroup$