As the result of a symbolic integration, I have a very long array with symbolic variables u[i]
which I would like to substitute for real number values. Moreover, I would like to do this using a compiled function for efficiency.
For simplicity say we have the array
aLong := {u[1]/u[2], u[1]*u[2]}
I want to compile a function whose job is to evaluate this expression.
However, if I try:
f=Compile[{{w,_Real,1}},aLong/.{u[1]->w[[1]],u[2]->w[[2]]}]
f[{1,2}]
it gives errors:
CompiledFunction::cfse: Compiled expression {0.5,2.} should be a machine-size real number.
CompiledFunction::cfex: Could not complete external evaluation at instruction 1; proceeding with uncompiled evaluation.
If instead I try Evaluate inside of the Compile, then I get the following error:
f = Compile[{{w, _Real, 1}}, Evaluate[aLong /. {u[1] -> w[[1]], u[2] -> w[[2]]}]];
f[{1, 1}]
Part::partd: Part specification w[[1]] is longer than depth of object.
Part::partd: Part specification w[[2]] is longer than depth of object.
Any suggestions that can help me fix this? (there must be something very basic I have not understood) Thank you!