Evaluating very long symbolic expressions

I have a very long symbolic expression, about 48 MB when written in text(containing spaces).
It has 23 different symbols.
I need to put 23 the variables in 'double' form, only needing to substitute the whole symbolics to doubles.
I am aware that this is absurd, but I believe this is the only way to solve my problem.
My approach on this was to:
1st, Put the expression as a function
as
function bar = foo([x1,x2,...,x23])
bar = (some very long symbolic equation with *,+,/,^,sines,cosines,sqrt())
2nd, Evaluate the expression with subs()
both of which successfuly computes.
However, the expression needs to be calculated 'a lot' because I intend to use it as a objective function for optimization.
It takes 2~3 minutes to calculate one condition for one 23 inputs, both in a function or using subs().
It needs to be calculated more than 100 times for 1 iteration of the optimization, so this would take 300+ mins for one iteration.
Is there any recommendation for making this process a bit more faster?
I believe I am using enough computing power(perhaps I am not using MATLAB to make full use of my computer?).
simplify() on the process of getting to the long expression does not help much, nor as subexpr().
I have little experience in Python and C++, so any guides or leads outside of MATLAB would be helpful too.
This image is the small fraction of the expression, about 50,000,000 characters long.

4 Comments

Which MATLAB release are you using?
Genearally speaking, sometimes expressions like that can run faster if you use matlabFunction() with 'File' and with 'optimize' turned on (which is default with you use a 'File' option for matlabFunction())
However, the optimization process can take a long time in itself (days, sometimes). And, unfortunately, there have been a fair number of releases in which 'optimize' was broken and resulted in errors or completely wrong outputs.
It is generally more efficient to use smaller symbolic expressions and matlabFunction() them, and combine the results, instead of calculating everything in pure symbolic form.
Thank you for a very quick reply! (I was expecting to wait a few days for it!)
I am currently using R2021a, so I guess most of functionality would be available.
It is possible break the code into several pieces, but it still is about 15 MB long, so I thought it would have not much difference with going for the 48MB code.
I will try the matlabFunction(), but I guess if it takes too long, it would be better to set 'Optimization' as false?
I suspect that the optimization process is at least quadratic (possibly cubic) in the number of terms in the expression. When I look at your sample, I would not be surprised if optimizaton was very slow.
There are sometimes ways to make it faster, such as by finding highest order powers for each symbol and doing substitutions, such as
subs(EXPR, {b^3, b^2}, {b_3, b_2})
though with those expressions, you are probably going to be wanting to do regexp replacements rather than using subs() . Those combinations of 111 and 112 can be optimized at the text level for example... are you showing us outputs in which partial replacements have already been done?
Hi JW,
could you expound a bit on why, since you are going to convert everything to doubles, you could not do a numerical approach to begin with?

Sign in to comment.

Answers (0)

Categories

Tags

Asked:

on 3 Jan 2022

Community Treasure Hunt

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

Start Hunting!