# Undeclared Identifier error in Compile

I've been trying to compile a couple of functions in Mathematica, and I keep coming across an error that I do not know how to resolve. Consider the following function:

FindCircleVerticalLineIntersections =
(*First, let's initialize points*)
(*This will not work any other way that I have tried*)
points = {{0.0, 0.0}};

(*First things first:
let's see if the vertical line is within the boundaries of the \
circle*)
(*If it is, let's see if it is right on the edge of the circle*)

(*If it is,
then the list of all intersection points will have just one \
entry*)
points = {{xIntercept, circlePos[[2]]}};
,
(*Otherwise, let's find both intersections*)
points = {{xIntercept, circlePos[[2]] + yDist}, {xIntercept,
circlePos[[2]] - yDist}};
];
,
(*Otherwise, we don't have any intersections,
let's just set points to an empty list*)
points = Delete[points, 1];
];

(*Finally, let's return points*)
points
]
, CompilationTarget -> "C"];


Now, this function works just as expected, and returns results well, but whenever I try to call it from any other compiled function, such as the following function:

f = Compile[{},
FindCircleVerticalLineIntersections[{0.0, 0.0}, 1.0, 0.0]
, CompilationTarget -> "C"];


I get the following undeclared identifier error:

CCompilerDriverCreateLibrary::cmperr: Compile error: C:\Users\Surge\AppData\Roaming\Mathematica\ApplicationData\CCompilerDriver\BuildFolder\combinatoric-5700\Working-combinatoric-5700-2808-88\compiledFunction87.c(85) : error C2065: 'P1' : undeclared identifier >>

I've looked at the human-readable C form of this program, and I don't even see an identifier called P1:

        No argument
2 Integer registers
2 Real registers
3 Tensor registers
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}

T(I1)0 = {0, 0}
I1 = 1
I0 = 3
Result = T(R2)1

1   T(R2)1 = MainEvaluate[ Hold[FindCircleVerticalLineIntersections][ \
T(I1)0, I1, I1]]
2   Return


Does anyone know what the problem is?

EDIT

Here are the missing functions, my apologies for the delay:

    (*The following function determines wither a point pointPos is within \
a circle of a given radius centered on the origin*)
PointIsWithinCircleWidthOnOrigin =
(*First,
let's make sure the radius that was passed in is positive*)

(*Next, let's see if the X-
componant of the point is within the boundary of the circle*)
(*This If structure to my knowledge is unnecessary,
but it makes the code look a bit cleaner in my opinion since we \
are explictly passing back a True or a False*)
True,
False
]
]
, CompilationTarget -> "C", RuntimeOptions -> "Speed"];

(*The following function determines whether some point pPos is within \
PointIsWithinCircleWidth =
Compile[{{cPos, _Real, 1}, {rad}, {pPos, _Real, 1}},
(*First,
let's shift the entire system so that we can be dealing with a \
circle centered at the origin*)
pos = pPos - cPos;

(*Next,
let's call PointIsWithinCircleWidthOnOrigin and return whatever \
that returns*)
]
, CompilationTarget -> "C", RuntimeOptions -> "Speed"];

(*This function determines whether a given point is on the left-most \
or right-most boundary of a circle on the origin given a radius*)
PointIsOnCircleWidthOnOrigin = Compile[{{radius}, {point, _Real, 1}},
(*Let's first make sure the radius is positive*)

(*Next, let's find the absolute value of the X-componant*)
posXComponant = Abs[point[[1]]];

True,
False
]
]
, CompilationTarget -> "C", RuntimeOptions -> "Speed"];

(*This function determines whether a point is on the left-most or \
right-most boundary of a circle on the origin given a radius*)
PointIsOnCircleWidth =
Compile[{{cPos, _Real, 1}, {radius}, {point, _Real, 1}},
Module[{pos},
pos = point - cPos;

]
, CompilationTarget -> "C", RuntimeOptions -> "Speed"];

• I upvoted your question because I think this probably should not happen. However, it is not really answerable without the definitions of PointIsWithinCircleWidth and PointIsOnCircleWidth given, so please include those in the question as well. Welcome to the site, by the way. It is a rarer thing than you would think for a brand-new user to post a (almost) fully formed and properly formatted question, and it is appreciated. – Oleksandr R. Jul 31 '15 at 0:00
• Have you looked at "InlineCompiledFunctions"? – Michael E2 Jul 31 '15 at 0:42
• I am sorry, but without those definitions, the issue is not reproducible and so the question is not answerable. Therefore I have to vote to close your question as it currently is. If it is closed before you are able to update it, please cast a vote to re-open after you have done so, and I'm sure other users will follow suit. – Oleksandr R. Aug 3 '15 at 17:17

When you call compiled functions inside another compiled function, you should consider to inline them. You can do this by wrapping a With statement around and using

CompilationOptions -> {"InlineCompiledFunctions" -> True}


as option to compile. I have cleaned your code, moving a lot of definitions of variable right where you declare it in Module. I have remove all the compilation targets and "Speed" runtime options and only used it in the very last FindCircleVerticalLineIntersections function. The function PointIsWithinCircleWidth contains an error because you declare pos and pRad in the Module but you don't give them values. I fixed this, but you need to check whether it is what you meant!

Import["http://goo.gl/NaH6rM"]["http://i.stack.imgur.com/uxbnd.png"]


or you copy&paste it from below. The final function should work now:

PointIsWithinCircleWidthOnOrigin =
Compile[{{radius, _Real, 0}, {pointPos, _Real, 1}},

PointIsWithinCircleWidth =
With[{PointIsWithinCircleWidthOnOrigin =
PointIsWithinCircleWidthOnOrigin},
Compile[{{cPos, _Real, 1}, {rad, _Real, 0}, {pPos, _Real, 1}},
],
CompilationOptions -> {"InlineCompiledFunctions" -> True}
]
];

PointIsOnCircleWidthOnOrigin =
];

PointIsOnCircleWidth =
With[{PointIsOnCircleWidthOnOrigin = PointIsOnCircleWidthOnOrigin},
Compile[{{cPos, _Real, 1}, {radius, _Real, 0}, {point, _Real, 1}},
Module[{pos = point - cPos},
CompilationOptions -> {"InlineCompiledFunctions" -> True}
]
];

FindCircleVerticalLineIntersections =
With[{
PointIsWithinCircleWidth = PointIsWithinCircleWidth,
PointIsOnCircleWidth = PointIsOnCircleWidth
},
Module[{points = {{0.0, 0.0}}, \[Theta], posRadius, yDist},
points = {{xIntercept, circlePos[[2]]}},
points = {{xIntercept, circlePos[[2]] + yDist}, {xIntercept,
circlePos[[2]] - yDist}}
],
points = Delete[points, 1]
];
points
],
CompilationOptions -> {"InlineCompiledFunctions" -> True},
CompilationTarget -> "C",
RuntimeOptions -> "Speed"
]
];

• What was the problem, though? I'm more interested in learning the inner workings of Mathematica than just getting code that works. – Surge Aug 25 '15 at 21:59
• Beside the error in PointIsWithinCircleWidth that I pointed out, you cannot simply call a compiled function from within another compiled function and expect that it is properly included (injected). The combination of With and the option "InlineCompiledFunctions" is important because otherwise when you call your compiled function, it calls back to Mathematica which is not what you want. You want a compiled function to be fast compiled code only without callbacks to the Mathematica Kernel. The real problem was that with compilation target "C" this produced not only slow code... – halirutan Aug 25 '15 at 23:37
• ...In your example, it gave a cryptic error. I couldn't even reproduce your error on my Linux machine. Here, the Kernel just crashed. Therefore, I can only tell you how to do it right and not, what exactly went wrong in your specific case. – halirutan Aug 25 '15 at 23:38
• Thank you very much for your reply. Is this documented anywhere? The documentation in the help page of Mathematica is woefully insufficient on these particular points discussed here. – Surge Sep 1 '15 at 1:02
• @Surge You mean the options to compile? Sure, see the Details and Options section of Compile and follow the links. In the doc of CompilationOptions` you'll find examples. – halirutan Sep 1 '15 at 13:34