I'm trying to evaluate a series of expressions, which, given a set of parameters, may be complex. Instead of throwing a CompiledFunction::cfn
, I'm trying to catch it from within and reset it to some default value. Example:
cf = With[{NOTREAL = -1}, Compile[{{x, _Real}}, {Sqrt[x], Sqrt[-x]}]]
cf[100]
I want the return to be {10, -1}
without invoking uncompiled function or MainEvaluate
. If I used "RuntimeOptions"
per Catching error in middle of evaluation of compiled function, the error would still be thrown and the evaluation wil be aborted. Is it possible to tell Mathematica to not evaluate when it sees a non-numeric value coming up and hand it over to some handler? And in reality Sqrt[x]
will be replaced with some complicated function. Reduce
can't figure out when it'll be real.
If[Abs[Im[x]]>1. 10^-12,{Sqrt[x],-1.},{Sqrt[x],Sqrt[-x]}]
? $\endgroup$Im/@
before posting but the same error is thrown, but directly applyingIm
to each result prevents it. Might have to do with how type inference works. $\endgroup$