Info

This question is closed. Reopen it to edit or answer.

Nesting parentheses issue: any workaround?

1 view (last 30 days)
Greetings. I need to write a function based on a polynomial expression, but MatLab seems to dislike nested parentheses with depth greater than 32.
Since this function would serve as a parameter to a mixed integer nonlinear solver, i cannot use temporary variables since the solver would not recognize them ( i've already tried this approach ). Is there any way to "force" matlab, so that it wouldn't complain?
For instance eval, str2fun, or even m-files return the same error, that is matlab complaining because of nesting depth limit.
Thanks for your attention.
==========================================
Addendum: The question is nontrivial. I thank for your help but you are off target, sorry to point out that.
Hope this may help: let's say you have a variable x that can be expressed as
x(t+1) = x(t)*(polynomial expression here)
when you develop this function you obtain
x(t+1) = x(t)*(x(t-1)*(x(t-2)*(...)))+( remaining polynomial elements)
at this point however I DO NOT have to solve this function. I have to pass this function as a constraint to a solver. In other words I have to define something like
my_func = @(x)( x(t)*(x(t-1)*(x(t-2)*(...)))+( remaining polynomial elements))
where my_func is an argument of another function, thus I don't have to solve the function itself.
However matlab fails when the evaluating my_func. Also, since the solver actually parse the whole expression, i cannot define a sub function, since the solver would not know how to parse it.
Thanks again...
  10 Comments
Jan
Jan on 29 May 2013
Edited: Jan on 29 May 2013
Perhaps your question is still not clear. And remember, that it is the nature of a forum, that the contributors offer their thoughts and suggestions based on the snippets of code presented by the OPs. Therefore I cannot see something absurd here. This is your first question in this forum. Perhaps you are not familiar with the customs here.
You write "matlab fails when the evaluating my_func". This observation is surely correct. But what is your question then?
Daniel Shub
Daniel Shub on 29 May 2013
I am closing this question. It is clear that without a substantial edit to the question which provides the link to the solver (and ideally its documentation) as well as the inputs, expected output, and error messages such that a meaningful answer will not be possible. Further, NotOfYourBusinessOrMaybeItIs, I am a little concerned about your behavior. TMW does not employee people to answer questions on Answers, we are all volunteers. If you don't like the help you are getting here, please consider using TMW official technical assistance channels or a paid service. Please feel free to edit the question to provide the level of detail necessary for us to answer the question.

Answers (8)

Jan
Jan on 28 May 2013
It is not only Matlab, but I dislike 32 nested parenthesis also. It is impossible to debug such monsters efficiently.
I cannot imagine a situation, where this is required. Using temporary array should be sufficient also and it does not waste computing time:
a = ((1 + 2) * 3);
% versus:
b = 1 + 2;
a = b * 3;

Daniel Shub
Daniel Shub on 28 May 2013
Based on some of your comments to the original question and answers, it seems the "solver" is a custom function that you (or someone else) wrote and not a standard MATLAB function or one available on the FEX. This solver requires an argument "handle_fun" that is in a very particular form such that it can parse the argument as a polynomial. You are then trying to use this function with an extremely large polynomial that exceeds the limit of MATLAB nesting.
There is no way to increase the maximum MATLAB nesting depth (although one could file an enhancement request). Assuming that when your polynomial is represented in the allowable form that minimizes the nesting depth you still exceed the maximum, you cannot use the solver as is. One possibility would be to modify the solver to take the argument in a different and more efficient form. If the code for the solver is not available, then you will need a different solver.
I would guess that any solver that requires arguments in such a crazy format likely is not well designed for the problem at hand.

Walter Roberson
Walter Roberson on 27 May 2013
Perhaps use polyval ?
  1 Comment
Walter Roberson
Walter Roberson on 28 May 2013
Remember you can define utility functions. For example,
Q = @(x) roots([5*x exp(-tan(x)]);
P = @(x) (17 + 35 * sin(Q(x));
fzero(P, [-1 1])

Kwen
Kwen on 27 May 2013
Is there any way to use less parenthesis? 32 sets seems like a large amount-maybe look into how MATLAB uses BEDMAS to simplify.

Teja Muppirala
Teja Muppirala on 28 May 2013
I am in agreement with Jan. I cannot recall a single instance where I needed to use 32 parenthesis for anything. If you really need to do something like that, do it recursively, or with some other method.
In your case, I think it can be done quite simply
%%1. Your current method. (I think you meant STR2FUNC not STR2FUN)
t_previous = num2str(1);
time_horizon = 5;
local_vec = [4 -3 2 9 6]; % Just random numbers
for i =1:time_horizon
t_previous = [ '(' t_previous ')*(1 - x(' num2str(i) ')+' num2str(local_vec(i)) ')'];
end
constr = str2func( [ '@(x)(' t_previous ')' ])
%%2. The exact same thing in one line of readable, scalable code
constr2 = @(x) prod(1 - x + local_vec)
%%3. Confirm they are the same
for n = 1:10
x = randn(1,time_horizon)
constr(x)
constr2(x)
end
  6 Comments
Jan
Jan on 29 May 2013
If the algebraic methods to simplify the code are applied, the code looks more aesthetic and it can be processed by Matlab due to the reduction of parenthesis. Of course the 2nd part would be more important for you, but the strategy remains the same.
The Optimization Toolbox can work with function handles as well as with anonymous functions. Both get input arguments and if you have a specific problem with the function handles in optimization functions, please post the corresponding code and the error messages or what ever describes the problems clearly.
"Sadly enough"? Is this still friendly? Are you trying to blame me for not getting the answer you like?
Walter Roberson
Walter Roberson on 29 May 2013
Okay, then... "there is".
Unfortunately for you, your "only two answers are possible" does not allow for statement of what the workaround is.

Jan
Jan on 28 May 2013
Matlab 5.3 accepts more parenthesis as far as I remember. But there have been other drawbacks, e.g. no function handles. Anyway, modern Matlab interpreters do not accept deeply nested parenthesis. You have to find another solution. And when you insist on using the above monster, you have to solve it with another program.

NotOfYourBusiness OrMaybeItIs
The solver I use is SCIP http://scip.zib.de/ which usually runs under GAMS http://www.gams.com/ or AMPL http://www.ampl.com/
Simply put, I'm not so sure these software are not well designed...
  4 Comments
Jan
Jan on 29 May 2013
You want Matlab to perform something, which it is not designed for. When it cannot handle 32 nested parenthesis, you have to reduce the number. There is no magic flag, which increases the number of accepted parenthesis to a larger number.
You have pointed out already, that I'm missing the point. But what is your point, or better: what is your question? At least this is not a useful explanation: "Only the number of iteration used when generating the string is a problem" (I'm not sure what this means) and "matlab fails when the evaluating my_func" (ok, but this is a limitation of the design of Matlab - the forum cannot create a new version for your needs).
It could be possible, it is at least worth to be considered, that you do not get my point. I have the impression that you get emotional. Perhaps it helps you to know, that I'm here to solve Matlab problems. And if the problems are a little abstract, my answers are also. Ok?
Perhaps it would have been a good idea to ask the forum for the design of your program. But now it looks like you have driven into a blind end and think that is the fault of Matlab or the dull contributors in the forum, which cannot or do not give the answer you like.
per isakson
per isakson on 29 May 2013
For The MathWorks' sake ask their tech support for help. Obviously, we cannot help you.

Jan
Jan on 29 May 2013
Edited: Jan on 29 May 2013
To summarize my comments, which might be better in the answers section:
  1. Modern Matlab versions cannot handle more than 32 nested parenthesis. (You have found out this fact already)
  2. There is no workaround like a magic flag.
  3. A promising workaround is a redesign of your program. The created function can get nicer and simultaneously contain less nested expressions (and from my point of view, this means the same).
  4. You could try to let a computer algebra program perform the simplifications of the string before you apply STR2FUNC.
  5. If the solver does not accept function handles, perhaps the solver is not sufficient for what you want to do. But when it accepts anonymous functions, function handles are expected to work also.

This question is closed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!