Multivariate Horner scheme implementation in MATLAB
Show older comments
Hello friends!
I have very long symbolic expressions (mainly polynomials or rational types) and I need to evaluate them many many times. Therefore,
in order to reduce the computational time I would like to re-write my expressions by their equivalent horner form. For instance, the expression
needs f=2*x^3-3*x^2+6*x-5 requires 8 multiplications and 3 additions while its following horner equivalent requires only 3 multiplications and 3 additions:
>> horner(f,x)
ans =
x*(x*(2*x - 3) + 6) - 5
Unfortunately, matlab only supports a univariate horner scheme. But, my expressions are multivariate and I need a multivariate Horner scheme
to simplify my long multivariate expressions. For instance, consider the following bi-variate expression:
syms x y
f = x^2+2*x*y+y^2;
If I go for a uni-variate Horner scheme then I get
>> horner(f,x)
ans =
x*(x + 2*y) + y^2
which requires less multiplications. This is, of course, an improvement. But, this is not the best improvement. A bi-variate Horner scheme
should give us (x+y)^2 as the best representation which needs only 2 arithmatics!
So, in sum, I need a matlab code which can implement a multivariate Horner scheme. I could find a python code but nothing in matlab. Unfortunately, I do not know python very well and really need a matlab code.
Any help is GREATLY appreciated!
Thanks in advance,
Babak
7 Comments
Torsten
on 6 Mar 2022
Use "matlabFunction" to convert your long expressions into function handles.
This will save much more time than using a horner scheme for evaluation.
Mohammad Shojaei Arani
on 6 Mar 2022
Mohammad Shojaei Arani
on 6 Mar 2022
Your claim of a "bivariate" horner method on that example is simply the result of factoring the polynomial.
syms x y
f = x^2+2*x*y+y^2;
prod(factor(f))
Anyway, Torsten is abolutely correct. You do not want to use symbolic polynomials IF you care about efficiency. Convert them to functions in MATLAB, that can be evaluated using double precision arithmetic. This will be far more efficient then any symbolic imporvements could possibly yield.
Mohammad Shojaei Arani
on 6 Mar 2022
Torsten
on 6 Mar 2022
A multivariate Horner scheme under MATLAB does not exist.
And the package under Python also will not be useful for your purpose because it can only handle very basic cases. But you have unstructured and very long expressions as you wrote.
So I think you will have to be satisfied with what MATLAB can offer, namely "matlabFunction". Or you can completely restructure your code to numerical, not symbolic computation (which I would recommend).
Mohammad Shojaei Arani
on 7 Mar 2022
Answers (0)
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!