Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm looking to define a multi-line function that makes use of the "Part" command. A toy example:

f = Compile[{x, y}, (z = Sqrt[x^2 + y^2]; 
sol = NMinimize[zz^2, zz]; 
z + sol[[1]])-sol[[2]]]

The core idea is simply that I want to be able to take various parts of a list, produced by a previous command within the compiled function. When I attempt to do so, I get an error message like:

Compile::part: "Part specification sol[[1]] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function. "

The command works fine if I don't try to use Part (for example):

f = Compile[{x, y}, (z = Sqrt[x^2 + y^2]; 
sol = NMinimize[zz^2, zz]; 
z]

I've seen this "not a tensor" error before in compile, when defining arguments, and it appears to be a common (and answered) question. This does not seem to be.

Any light you could shed would be appreciated!

-Edit (Workaround)-

Silva's answer helped me come up with a workaround:

f = Compile[{x, y}, (z = Sqrt[x^2 + y^2]; 
sol2 = NArgMin[zz^2, zz]; 
sol1 = sol2^2; 
z + sol1 - sol2)]

Works with compile.

share|improve this question
6  
NMinimize returns a result like {-3.51391, {x -> 1.30084}}, which is not a tensor, so can not be assigned to any type of compiler registers. –  Silvia Dec 13 '13 at 14:01
3  
Also, NMinimize and NArgMin cannot be compiled. there are already heavily optimized so I'm not sure what the advantage would be, regardless. –  s0rce Dec 13 '13 at 16:03
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.