Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]; 

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

closed as off-topic by blochwave, m_goldberg, ilian, Michael E2, Sjoerd C. de Vries Sep 26 '15 at 22:36

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – blochwave, m_goldberg, ilian, Michael E2, Sjoerd C. de Vries
If this question can be reworded to fit the rules in the help center, please edit the question.

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
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