Super simple question, but I'm lost here.
f = Compile[{{a, _Real}, {b, _Real}, {c, _Real}},
x /. NSolve[a x^2 + b x + c == 0, {x}]]
f[1., -4., -5.]
gives the error
CompiledFunction::cfse: Compiled expression {-1.,5.} should be a machine-size real number.
But
g = Compile[{{a, _Real}, {b, _Real}, {c, _Real}},
x /. NSolve[a x^2 + b x + c == 0, {x}][[1]]]
g[1., -4., -5.]
works fine.
How do I type the result so that it knows it's a list. I haven't had any problems getting Compile to return a list before, and
h = Compile[{{a, _Real}, {b, _Real}, {c, _Real}},
sol = NSolve[a x^2 + b x + c == 0, {x}]; {x /. sol[[1]],
x /. sol[[2]]}]
h[1., -4., -5.]
gets around the issue, but there's got to be a better way than that.
This gives the same error, even though the output should be a real number. Somehow the list can't exist within the compiled function. I don't understand.
k = Compile[{{a, _Real}, {b, _Real}, {c, _Real}},
listofSolutions = x /. NSolve[a x^2 + b x + c == 0, {x}];
listofSolutions[[1]]]
k[1., -4., -5.]
Edited to add: I know NSolve isn't compilable; I'm trying to get the speed up from the typing of a, b, and c, among other things in my actual code.
ReplaceAllandNSolveare not compilable. Just get rid ofCompile. – Michael E2 Jul 09 '20 at 19:00NSolve, though, it will be negligible....Okay, I guess I can imagine a use-case that this would be reasonable. You shouldBlock/Module/localizex, though, to protect the code whenxhas a global value. (And thanks for the accept. :) – Michael E2 Jul 09 '20 at 19:25CompilePrintto inspect what the compiler is doing? (Needs@"CompiledFunctionTools`"; CompilePrint[f]) – Michael E2 Jul 09 '20 at 19:39