Clear["`*"];
cf = Compile[{{A, _Real, 2}, {n, _Integer}},
With[{a = A[[1]], b = A[[2]], c = A[[3]]},
If[n < 1, {{a, b, c}},
Join[cf[{a, (a + b)/2, (a + c)/2}, n - 1],
cf[{(b + a)/2, b, (b + c)/2}, n - 1],
cf[{(c + a)/2, (c + b)/2, c}, n - 1]
]
];
]
];
cf[{{0, 0}, {1, 0}, {.5, .8}} // N, 3]
This code I wrote can't be compiled, Mathematica returns
CompiledFunction::cfex: Could not complete external evaluation at instruction 19; proceeding with uncompiled evaluation. >>
How can I correctly compile it?
The uncompiled version:
Clear["`*"];
f[{a_, b_, c_}, n_] :=
If[n < 1, {{a, b, c}},
Join[f[{a, (a + c)/2, (a + b)/2}, n - 1],
f[{(b + a)/2, b, (b + c)/2}, n - 1],
f[{(c + a)/2, (c + b)/2, c}, n - 1]]
];
n = 3;
data = f[{{0, 0}, {1, 0}, {.5, .8}} // N, n];
ListLinePlot[data /. {a_, b_, c_} :> {a, b, c, a}]
;
2. if we fix that, it returns a rank 3 tensor which is fed back as input after a transformation. The input is expected to be a rank 2 tensor however. I'd suggest to first write an uncompiled version and fix the bugs. $\endgroup$ – Szabolcs Sep 10 '13 at 18:31