Compile fun continues today, and I'm trying to get the function below to compile. First the function:
NewC[ymax_, xmax_] := Block[{x1, a2, a0, b0, a1, b1, c1, b2, c2, a3, b3, c3, C0, xb = 0, xe = 100, ve},
a3 = (xmax + ymax - 100)/(2*(xe - xmax)*(xe - xmax));
x1 = (200 - xmax - 2*ymax + 2*a3*xe*(xe - xmax))/(2*a3*(xe - xmax) + 1);
a2 = -(2*a3*(xe - x1) + 1)/(2*(x1 - xmax));
b2 = -2*a2*xmax;
b3 = -1 - 2*a3*xe;
a0 = -(xb + ymax)/((xmax - xb)*(xmax - xb));
b0 = -2*a0*xmax;
C0 = ymax + a0*xmax*xmax;
a1 = (100 - xe - ymax)/((xe - xmax)*(xe - xmax));
b1 = -2*a1*xmax;
c1 = ymax + a1*xmax*xmax;
c2 = ymax + a2*xmax*xmax;
c3 = a3*xe*xe + 100;
ve2[ym_, tnlr_] := If[ym <= ((100 - xmax) + (100 - xe))/2,
If[tnlr <= xmax, a0*tnlr*tnlr + b0*tnlr + xb, a1*tnlr*tnlr + b1*tnlr + c1],
If[tnlr <= x1, a2*tnlr*tnlr + b2*tnlr + c2, a3*tnlr*tnlr + b3*tnlr + c3]];
ve = Chop[Map[ve2[ymax, #[[1]]] &, $range] // N]
]
So pretty straight forward, except for the embedded function. So I externalize the important parts and global variable, but have no idea how to handle the embedded function.
Here's what I tried:
NewC = With[{$range = $range, xb = 0, xe = 100},
Compile[{{ymax, _Real, 1}, {xmax, _Real, 1}},
Block[{x1, a2, a0, b0, a1, b1, c1, b2, c2, a3, b3, c3, C0, ve,ve2},
a3 = (xmax + ymax - 100)/(2*(xe - xmax)*(xe - xmax));
x1 = (200 - xmax - 2*ymax +
2*a3*xe*(xe - xmax))/(2*a3*(xe - xmax) + 1);
a2 = -(2*a3*(xe - x1) + 1)/(2*(x1 - xmax));
b2 = -2*a2*xmax;
b3 = -1 - 2*a3*xe;
a0 = -(xb + ymax)/((xmax - xb)*(xmax - xb));
b0 = -2*a0*xmax;
C0 = ymax + a0*xmax*xmax;
a1 = (100 - xe - ymax)/((xe - xmax)*(xe - xmax));
b1 = -2*a1*xmax;
c1 = ymax + a1*xmax*xmax;
c2 = ymax + a2*xmax*xmax;
c3 = a3*xe*xe + 100;
ve2[ym_, tnlr_] := If[ym <= ((100 - xmax) + (100 - xe))/2,
If[tnlr <= xmax, a0*tnlr*tnlr + b0*tnlr + xb, a1*tnlr*tnlr + b1*tnlr + c1],
If[tnlr <= x1, a2*tnlr*tnlr + b2*tnlr + c2,a3*tnlr*tnlr + b3*tnlr + c3]];
ve = Chop[Map[ve2[ymax, #[[1]]] &, $range] // N]
],
CompilationTarget -> "C"]];
Certainly not the winning ticket. So are there principles to follow when this type of function must be modified? Thanks!
$range
and does it ever change? Should it be an input to the function or a global variable? $\endgroup$Map
ing#[[1]]
onto$range
so I assumed that it was a matrix, not a vector (or it was a vector of vectors). Also, I just noticedCurve2
vsve2
. Is this a typo and are these two the same? I assumed that they are in the answer. $\endgroup$