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...
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 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 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)) ')'];
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)
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 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 which usually runs under GAMS or AMPL
Simply put, I'm not so sure these software are not well designed...
per isakson
per isakson on 29 May 2013
For The MathWorks' sake ask their tech support for help. Obviously, we cannot help you.

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.

Community Treasure Hunt

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

Start Hunting!