How to implement a timeout on simplify() ?

I want to use a timeout on the simplify() function. I think this functionality exists using the 'seconds' option. From MATLAB help: "Time limit for the simplification process, specified as the comma-separated pair consisting of 'Seconds' and a positive value that denotes the maximal time in seconds."
But for some reason this doesn't seem to be working. For example, when I have a rather long symbolic expression as given in the attached file (exceeded character limit for the question form), and try to run:
simplify(blah,'Seconds',5)
This just keeps running without timing out.
Am I doing something wrong? Is there some way to specify a timeout for the simplify() function?

7 Comments

Even just reading in the file is taking a long time :(
S = str2sym(fileread('New Text Document.txt'))
Ah, eval takes a reasonable time.
S = fileread('New Text Document.txt');
syms x1 x2
eval(S)
blah
blah = 
Imran Khan
Imran Khan on 3 Sep 2024
Edited: Imran Khan on 3 Sep 2024
@Walter Roberson, I'm writing a function where I won't know the incoming expression in advance. I want to try simplifying it for a "reasonable" amout of time, say 30 seconds before timing out, that's why I am looking for a way to timeout the simplify function (which I think the 'seconds' argument implements from my reading of the help section). The expression I attached is just an example of a lengthy expression test case I was using where it wasn't timing out as I was expecting.
EDIT: Sorry, I think I misunderstood you - eval helps load the expression in. But leaving the original comment in for context of what I'm doing.
Provided that you have the Parallel Computing Toolbox installed
You can use parfeval() to run the simplify() task, recording the future. pause() for 30 seconds. You must use a Processes parpool for this, not a Threads parpool.
Check the State property of the future to see if the future completed; if so then fetchOutputs() from the future; if not then cancel() the future.
@Walter Roberson, I had thought of doing that - I've used that method once before as a timeout for some other code I was writing, but that was only locally for myself. I'm hoping to share this code I'm writing so I shouldn't be making the assumption that a user would have the parallel toolbox license/installed.
Isn't 'Seconds' supposed to timeout simplify(), or is it meant for something else?
Seconds is meant for timeout.
In practice, it is impractical to check the timer at each low-level step. In practice, the code recurses through expressions. Potentially it might check the timer each time it recurses, or after each significant step. But the problem is your expressions are complicated, and even digging into the children a fair ways gets you expressions that are hard to simplify. You have sums of sines and cosines, and pattern matching needs to be done on each potential grouping of sine and cosine in each expression, because of possible trig reductions.
sin(A) + sin(B) + cos(C) + cos(D)
has to check {sin(A), sin(B)}, {sin(A), cos(C )}, {sin(A), cos(D)}, {cos(C }, cos(D)} for potential reductions. But you have whole series of them in each expression... Probably the program is trying to reduce the entire sum, taking a long time at it, and not checking the timer until after it is done.
@Walter Roberson, thanks for your detailed explanation of how the timer is likely working internally - it makes me think that this is an issue to escalate to support. That is, if something can be done about it.

Sign in to comment.

Answers (1)

Imran Khan
Imran Khan on 5 Sep 2024
Edited: Imran Khan on 5 Sep 2024
Contacted MATHWORKS support. This is a known issue under Windows 10. Fixed in MATLAB 2023Rb Release 6.

Categories

Products

Release

R2022a

Asked:

on 2 Sep 2024

Edited:

on 5 Sep 2024

Community Treasure Hunt

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

Start Hunting!