Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Speed Up symbolic code

Subject: Speed Up symbolic code

From: kamuran turksoy

Date: 16 Aug, 2011 19:50:27

Message: 1 of 17

Hi everyone

I have a big code. In that code i have some for loop and and i call some other functions. I wrote one of my function as symbolic. Inside the function i use

syms x a1 a2 a3 a4 a5 a6 a7 ... a20
 and then i use these symbols as polynomials
1+a1*x+a12*x^2+a3*x^3...+a20*x^20
and then i use solve to find some coefficient in somewhere.

Anyway since i use symbolic toolbox and i also use for loop and in each loop i call function which includes symbolic functions, more than 20 times it makes my code running very very slow.

Is there any way to speed up?

Regards

Subject: Speed Up symbolic code

From: Christopher Creutzig

Date: 17 Aug, 2011 08:16:07

Message: 2 of 17

On 16.08.11 21:50, kamuran turksoy wrote:

> I have a big code. In that code i have some for loop and and i call some other functions. I wrote one of my function as symbolic. Inside the function i use
>
> syms x a1 a2 a3 a4 a5 a6 a7 ... a20
> and then i use these symbols as polynomials
> 1+a1*x+a12*x^2+a3*x^3...+a20*x^20
> and then i use solve to find some coefficient in somewhere.
>
> Anyway since i use symbolic toolbox and i also use for loop and in each loop i call function which includes symbolic functions, more than 20 times it makes my code running very very slow.

 The speed of such computations obviously depends heavily on what
exactly you are doing. But in general, finding symbolic solutions to
problems with 20 parameters will be slow, yes. So, without seeing what
you are actually doing, the only suggestion I have is to reduce the
number of parameters in your problem. Which may or may not be possible.


Christopher

Subject: Speed Up symbolic code

From: kamuran turksoy

Date: 17 Aug, 2011 18:19:10

Message: 3 of 17

Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <4E4B78C7.9010808@mathworks.com>...
> On 16.08.11 21:50, kamuran turksoy wrote:
>
> > I have a big code. In that code i have some for loop and and i call some other functions. I wrote one of my function as symbolic. Inside the function i use
> >
> > syms x a1 a2 a3 a4 a5 a6 a7 ... a20
> > and then i use these symbols as polynomials
> > 1+a1*x+a12*x^2+a3*x^3...+a20*x^20
> > and then i use solve to find some coefficient in somewhere.
> >
> > Anyway since i use symbolic toolbox and i also use for loop and in each loop i call function which includes symbolic functions, more than 20 times it makes my code running very very slow.
>
> The speed of such computations obviously depends heavily on what
> exactly you are doing. But in general, finding symbolic solutions to
> problems with 20 parameters will be slow, yes. So, without seeing what
> you are actually doing, the only suggestion I have is to reduce the
> number of parameters in your problem. Which may or may not be possible.
>
>
> Christopher

During the algorithm that i use i can not reduce number of parameters, i have to find a way to speed up because now it is very slow

Subject: Speed Up symbolic code

From: Alan Weiss

Date: 17 Aug, 2011 19:07:28

Message: 4 of 17

On 8/17/2011 2:19 PM, kamuran turksoy wrote:
> Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in
> message <4E4B78C7.9010808@mathworks.com>...
>> On 16.08.11 21:50, kamuran turksoy wrote:
>>
>> > I have a big code. In that code i have some for loop and and i call
>> some other functions. I wrote one of my function as symbolic. Inside
>> the function i use
>> > > syms x a1 a2 a3 a4 a5 a6 a7 ... a20
>> > and then i use these symbols as polynomials
>> > 1+a1*x+a12*x^2+a3*x^3...+a20*x^20
>> > and then i use solve to find some coefficient in somewhere. > >
>> Anyway since i use symbolic toolbox and i also use for loop and in
>> each loop i call function which includes symbolic functions, more than
>> 20 times it makes my code running very very slow.
>> The speed of such computations obviously depends heavily on what
>> exactly you are doing. But in general, finding symbolic solutions to
>> problems with 20 parameters will be slow, yes. So, without seeing what
>> you are actually doing, the only suggestion I have is to reduce the
>> number of parameters in your problem. Which may or may not be possible.
>>
>>
>> Christopher
>
> During the algorithm that i use i can not reduce number of parameters, i
> have to find a way to speed up because now it is very slow

Is it possible to perform a numeric, rather than symbolic calculation?
Perhaps you are looking for a root of your polynomial, and could use the
roots or fzero functions. Or perhaps you could separate out your
symbolic code and replace it with matlabFunction-generated code.

Unfortunately, without specific knowledge of your application, I cannot
give any specific advice.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: Speed Up symbolic code

From: kamuran turksoy

Date: 18 Aug, 2011 21:08:09

Message: 5 of 17

Alan Weiss <aweiss@mathworks.com> wrote in message <j2h3hg$e2i$1@newscl01ah.mathworks.com>...
> On 8/17/2011 2:19 PM, kamuran turksoy wrote:
> > Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in
> > message <4E4B78C7.9010808@mathworks.com>...
> >> On 16.08.11 21:50, kamuran turksoy wrote:
> >>
> >> > I have a big code. In that code i have some for loop and and i call
> >> some other functions. I wrote one of my function as symbolic. Inside
> >> the function i use
> >> > > syms x a1 a2 a3 a4 a5 a6 a7 ... a20
> >> > and then i use these symbols as polynomials
> >> > 1+a1*x+a12*x^2+a3*x^3...+a20*x^20
> >> > and then i use solve to find some coefficient in somewhere. > >
> >> Anyway since i use symbolic toolbox and i also use for loop and in
> >> each loop i call function which includes symbolic functions, more than
> >> 20 times it makes my code running very very slow.
> >> The speed of such computations obviously depends heavily on what
> >> exactly you are doing. But in general, finding symbolic solutions to
> >> problems with 20 parameters will be slow, yes. So, without seeing what
> >> you are actually doing, the only suggestion I have is to reduce the
> >> number of parameters in your problem. Which may or may not be possible.
> >>
> >>
> >> Christopher
> >
> > During the algorithm that i use i can not reduce number of parameters, i
> > have to find a way to speed up because now it is very slow
>
> Is it possible to perform a numeric, rather than symbolic calculation?
> Perhaps you are looking for a root of your polynomial, and could use the
> roots or fzero functions. Or perhaps you could separate out your
> symbolic code and replace it with matlabFunction-generated code.
>
> Unfortunately, without specific knowledge of your application, I cannot
> give any specific advice.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

Alan

You are right i am looking for roots of two polynomials.

C=A*B+D*E here C, A and D polynomials are know, but B and E polynomials are unknown. We only know orders of them, coefficients are symbolic. The aim it to find these coefficient or B and E polynomials.
B=1+a1*x+a12*x^2+a3*x^3...+a20*x^20
E=1+b1*x+b12*x^2+b3*x^3...+b20*x^20

The only why that i could solve this problem that i found is symbolic solution, i could not find any other way. but i mentioned before with symbolic it is very slow.

Subject: Speed Up symbolic code

From: Christopher Creutzig

Date: 19 Aug, 2011 07:49:44

Message: 6 of 17

On 18.08.11 23:08, kamuran turksoy wrote:

> You are right i am looking for roots of two polynomials.
>
> C=A*B+D*E here C, A and D polynomials are know, but B and E polynomials are unknown. We only know orders of them, coefficients are symbolic. The aim it to find these coefficient or B and E polynomials.
> B=1+a1*x+a12*x^2+a3*x^3...+a20*x^20
> E=1+b1*x+b12*x^2+b3*x^3...+b20*x^20
>
> The only why that i could solve this problem that i found is symbolic solution, i could not find any other way. but i mentioned before with symbolic it is very slow.

I don't really understand your problem description. If you are looking
for polynomials B and E satisfying your equation, then you are not
looking for roots of polynomials – and for performance, you should not
try using a generic solve call for this question; instead, use gcd with
three outputs:

>> syms a b x
>> A = x^4+a*x-2;
>> D = x^3-b*x^2+7;
>> C = (a-b)*x^4-3*x+2;
>> [g,s,t] = gcd(A,D,x);
>> B = simplify(s*C/g)

B =

((3*x - a*x^4 + b*x^4 - 2)*(- a^2*b*x + a^2*x^2 - a*b^4*x + a*b^3*x^2 +
21*a*b*x - 2*a*b - 14*a*x^2 + 2*a*x - 2*b^4 + 2*b^2*x^2 + 49*b^2 -
98*b*x + 42*b + 49*x^2 - 14*x + 4))/(7*a^3 + 7*a^2*b^3 - 147*a^2 +
14*a*b^2 - 4*a*b + 1029*a - 4*b^4 + 196*b^2 + 112*b - 2393)

>> E = simplify(t*C/g)

E =

-((3*x - a*x^4 + b*x^4 - 2)*(a^3 + a^2*b^3 + a^2*x^3 - 21*a^2 +
a*b^3*x^3 + 7*a*b^2*x + 2*a*b^2 + 7*a*b*x^2 - 14*a*x^3 + 2*a*x^2 + 147*a
+ 2*b^3*x^2 + 2*b^2*x^3 + 14*b^2 - 49*b*x^2 + 28*b*x + 4*b + 49*x^3 -
14*x^2 + 4*x - 343))/(7*a^3 + 7*a^2*b^3 - 147*a^2 + 14*a*b^2 - 4*a*b +
1029*a - 4*b^4 + 196*b^2 + 112*b - 2393)

>> simplify(B*A+D*E)

ans =

a*x^4 - 3*x - b*x^4 + 2



If C/g is not a polynomial, the equation has no solution.




Christopher

Subject: Speed Up symbolic code

From: kamuran turksoy

Date: 19 Aug, 2011 19:38:08

Message: 7 of 17

Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <4E4E1598.9010601@mathworks.com>...
> On 18.08.11 23:08, kamuran turksoy wrote:
>
> > You are right i am looking for roots of two polynomials.
> >
> > C=A*B+D*E here C, A and D polynomials are know, but B and E polynomials are unknown. We only know orders of them, coefficients are symbolic. The aim it to find these coefficient or B and E polynomials.
> > B=1+a1*x+a12*x^2+a3*x^3...+a20*x^20
> > E=1+b1*x+b12*x^2+b3*x^3...+b20*x^20
> >
> > The only why that i could solve this problem that i found is symbolic solution, i could not find any other way. but i mentioned before with symbolic it is very slow.
>
> I don't really understand your problem description. If you are looking
> for polynomials B and E satisfying your equation, then you are not
> looking for roots of polynomials – and for performance, you should not
> try using a generic solve call for this question; instead, use gcd with
> three outputs:
>
> >> syms a b x
> >> A = x^4+a*x-2;
> >> D = x^3-b*x^2+7;
> >> C = (a-b)*x^4-3*x+2;
> >> [g,s,t] = gcd(A,D,x);
> >> B = simplify(s*C/g)
>
> B =
>
> ((3*x - a*x^4 + b*x^4 - 2)*(- a^2*b*x + a^2*x^2 - a*b^4*x + a*b^3*x^2 +
> 21*a*b*x - 2*a*b - 14*a*x^2 + 2*a*x - 2*b^4 + 2*b^2*x^2 + 49*b^2 -
> 98*b*x + 42*b + 49*x^2 - 14*x + 4))/(7*a^3 + 7*a^2*b^3 - 147*a^2 +
> 14*a*b^2 - 4*a*b + 1029*a - 4*b^4 + 196*b^2 + 112*b - 2393)
>
> >> E = simplify(t*C/g)
>
> E =
>
> -((3*x - a*x^4 + b*x^4 - 2)*(a^3 + a^2*b^3 + a^2*x^3 - 21*a^2 +
> a*b^3*x^3 + 7*a*b^2*x + 2*a*b^2 + 7*a*b*x^2 - 14*a*x^3 + 2*a*x^2 + 147*a
> + 2*b^3*x^2 + 2*b^2*x^3 + 14*b^2 - 49*b*x^2 + 28*b*x + 4*b + 49*x^3 -
> 14*x^2 + 4*x - 343))/(7*a^3 + 7*a^2*b^3 - 147*a^2 + 14*a*b^2 - 4*a*b +
> 1029*a - 4*b^4 + 196*b^2 + 112*b - 2393)
>
> >> simplify(B*A+D*E)
>
> ans =
>
> a*x^4 - 3*x - b*x^4 + 2
>
>
>
> If C/g is not a polynomial, the equation has no solution.
>
>
>
>
> Christopher


Dear Christopher

I really appreciate for your solution. Actually i tried to use gcd before but i got some errors, but now i followed your solution and it works.

Thank you very much

Subject: Speed Up symbolic code

From: Daphne

Date: 24 Aug, 2011 05:08:27

Message: 8 of 17

I have a somewhat similar problem with symbolic calculations being really slow in MuPad. I'm using a code that was developed for Maple and am now working with 2010b where it's really slowed down. I would really appreciate any ideas on how to speed this up.
The entire function (a snipet here) sometimes takes 1 sec and sometimes 20 sec for the same calculation and slows down even more if I also carry large matrices with me.

The code basically takes a 6th order polynom defined by polyfit, makes it into a symbolic equation, differntiates it, then runs a few calculations ending with limits of the symbolic at a specific location.
Any ideas?


Some toy data that can be used with this
x = 1/100:1/100:10;
y = 4*x.^2 + 3*x.*(2-exp(x/10)) + 4*x.^3;

tic
p6 = polyfit(x,y,6);
f6 = polyval(p6, x);

syms t real;
eq6=p6*[t^6 t^5 t^4 t^3 t^2 t 1]';
tt=linspace(-2,2);
r=[t eq6];
rp=diff(r);
nrp=sqrt(dot(rp,rp));
T=rp/nrp;
Tp=diff(T);

ttt=x(1)+0.05:0.15:x(end);
for ind=1:length(ttt)
    y(ind)=double(limit(eq6,ttt(ind)));
    xTp(ind)=double(limit(Tp(1),ttt(ind)));
    yTp(ind)=double(limit(Tp(2),ttt(ind)));
    nTp_table(ind,:)=[sqrt( xTp(ind)^2+ yTp(ind)^2),ttt(ind)];
end
toc

Elapsed time is 47.103106 seconds.
(if run again immediately, this takes ~2 sec)

Preallocating the vectors in the loop doesn't really speed up and moving the
nTp_table out of the loop give "isequal" but further down the line of the
calculations gives slightly different results for some reason..

Thanks!
Daphne

Subject: Speed Up symbolic code

From: Christopher Creutzig

Date: 24 Aug, 2011 11:03:12

Message: 9 of 17

On 24.08.11 07:08, Daphne wrote:
> The code basically takes a 6th order polynom defined by polyfit, makes it into a symbolic equation, differntiates it, then runs a few calculations ending with limits of the symbolic at a specific location.
> Any ideas?

You get a little speedup by setting

Tp=simplify(diff(T));

But what is much more promising: I don't see any need for limit here
really, the expressions are smooth as is, you can simply evaluate at
your points.

for ind=1:length(ttt)
    y(ind)=double(subs(eq6,t,ttt(ind)));
    xTp(ind)=double(subs(Tp(1),t,ttt(ind)));
    yTp(ind)=double(subs(Tp(2),t,ttt(ind)));
    nTp_table(ind,:)=[sqrt( xTp(ind)^2+ yTp(ind)^2),ttt(ind)];
end

or, more aggressively,

eq6fn = matlabFunction(eq6);
Tp1fn = matlabFunction(Tp(1));
Tp2fn = matlabFunction(Tp(2));

ttt=x(1)+0.05:0.15:x(end);
y = eq6fn(ttt);
xTp = Tp1fn(ttt);
yTp = Tp2fn(ttt);
nTp_table = [sqrt(xTp.^2 + yTp.^2), ttt];


> Elapsed time is 47.103106 seconds.
> (if run again immediately, this takes ~2 sec)

That's a memoization effect in the symbolic engine.


Christopher

Subject: Speed Up symbolic code

From: Daphne

Date: 24 Aug, 2011 13:43:27

Message: 10 of 17


Wow, thanks!
This works great. I checked it on several instances of my data and consistently got for this snippet of code:
For my original calculation 3.2sec and above,
for the direct plug, 1.2 sec and above,
and for the "aggressive" approach, stable 0.15-0.16 sec

There were differences between the original calculation result and the two approaches, on the order of 10^-12-10^-15, so negligible.

Perfect!
Thanks again!

Daphne

Subject: Speed Up symbolic code

From: kamuran turksoy

Date: 24 Aug, 2011 18:08:14

Message: 11 of 17

"kamuran turksoy" <kamuranturksoy@gmail.com> wrote in message <j2me30$rar$1@newscl01ah.mathworks.com>...
> Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <4E4E1598.9010601@mathworks.com>...
> > On 18.08.11 23:08, kamuran turksoy wrote:
> >
> > > You are right i am looking for roots of two polynomials.
> > >
> > > C=A*B+D*E here C, A and D polynomials are know, but B and E polynomials are unknown. We only know orders of them, coefficients are symbolic. The aim it to find these coefficient or B and E polynomials.
> > > B=1+a1*x+a12*x^2+a3*x^3...+a20*x^20
> > > E=1+b1*x+b12*x^2+b3*x^3...+b20*x^20
> > >
> > > The only why that i could solve this problem that i found is symbolic solution, i could not find any other way. but i mentioned before with symbolic it is very slow.
> >
> > I don't really understand your problem description. If you are looking
> > for polynomials B and E satisfying your equation, then you are not
> > looking for roots of polynomials – and for performance, you should not
> > try using a generic solve call for this question; instead, use gcd with
> > three outputs:
> >
> > >> syms a b x
> > >> A = x^4+a*x-2;
> > >> D = x^3-b*x^2+7;
> > >> C = (a-b)*x^4-3*x+2;
> > >> [g,s,t] = gcd(A,D,x);
> > >> B = simplify(s*C/g)
> >
> > B =
> >
> > ((3*x - a*x^4 + b*x^4 - 2)*(- a^2*b*x + a^2*x^2 - a*b^4*x + a*b^3*x^2 +
> > 21*a*b*x - 2*a*b - 14*a*x^2 + 2*a*x - 2*b^4 + 2*b^2*x^2 + 49*b^2 -
> > 98*b*x + 42*b + 49*x^2 - 14*x + 4))/(7*a^3 + 7*a^2*b^3 - 147*a^2 +
> > 14*a*b^2 - 4*a*b + 1029*a - 4*b^4 + 196*b^2 + 112*b - 2393)
> >
> > >> E = simplify(t*C/g)
> >
> > E =
> >
> > -((3*x - a*x^4 + b*x^4 - 2)*(a^3 + a^2*b^3 + a^2*x^3 - 21*a^2 +
> > a*b^3*x^3 + 7*a*b^2*x + 2*a*b^2 + 7*a*b*x^2 - 14*a*x^3 + 2*a*x^2 + 147*a
> > + 2*b^3*x^2 + 2*b^2*x^3 + 14*b^2 - 49*b*x^2 + 28*b*x + 4*b + 49*x^3 -
> > 14*x^2 + 4*x - 343))/(7*a^3 + 7*a^2*b^3 - 147*a^2 + 14*a*b^2 - 4*a*b +
> > 1029*a - 4*b^4 + 196*b^2 + 112*b - 2393)
> >
> > >> simplify(B*A+D*E)
> >
> > ans =
> >
> > a*x^4 - 3*x - b*x^4 + 2
> >
> >
> >
> > If C/g is not a polynomial, the equation has no solution.
> >
> >
> >
> >
> > Christopher
>
>
> Dear Christopher
>
> I really appreciate for your solution. Actually i tried to use gcd before but i got some errors, but now i followed your solution and it works.
>
> Thank you very much

Actually the way with gcd works fine, but i figured out a problem. In my case for the equation that i have to solve;

C=A*B+D*E

Orders of unknown polynomials B and E are known. Order of E should be equal to order of A minus 1 and order of B should be equal to order of D minus 1

nE=nA-1;
nB=nD-1;
> > > B=1+a1*x+a12*x^2+a3*x^3...+a(nA-1)*x^(nA-1)
> > > E=1+b1*x+b12*x^2+b3*x^3...+b(nD-1)*x^(nD-1)

Is there any way to modify gcd for this case?

Regards

Subject: Speed Up symbolic code

From: Christopher Creutzig

Date: 25 Aug, 2011 07:02:23

Message: 12 of 17

On 24.08.11 20:08, kamuran turksoy wrote:

> Actually the way with gcd works fine, but i figured out a problem. In my case for the equation that i have to solve;
>
> C=A*B+D*E
>
> Orders of unknown polynomials B and E are known. Order of E should be equal to order of A minus 1 and order of B should be equal to order of D minus 1
>
> nE=nA-1;
> nB=nD-1;
>> > > B=1+a1*x+a12*x^2+a3*x^3...+a(nA-1)*x^(nA-1)
>> > > E=1+b1*x+b12*x^2+b3*x^3...+b(nD-1)*x^(nD-1)
>
> Is there any way to modify gcd for this case?

Not directly, but given any solution, say, E' and B', if such E and B
exist, then E = E' + abc*A, B = B' + xyz*D, with abc and xyz being
polynomials. I.e., in pseudocode, I'd assume something like this should
work:

while degree(E) is too large
  // if this is not an exact division, there is no solution:
  t = lterm(E)/lterm(A)
  E = E - t*A
  B = B + t*D
end

while degree(B) is too large
... same here ...
end

if degree(E) is too large
  there is no solution
end



Christopher

Subject: Speed Up symbolic code

From: kamuran turksoy

Date: 25 Aug, 2011 18:31:31

Message: 13 of 17

Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <4E55F37F.5000307@mathworks.com>...
> On 24.08.11 20:08, kamuran turksoy wrote:
>
> > Actually the way with gcd works fine, but i figured out a problem. In my case for the equation that i have to solve;
> >
> > C=A*B+D*E
> >
> > Orders of unknown polynomials B and E are known. Order of E should be equal to order of A minus 1 and order of B should be equal to order of D minus 1
> >
> > nE=nA-1;
> > nB=nD-1;
> >> > > B=1+a1*x+a12*x^2+a3*x^3...+a(nA-1)*x^(nA-1)
> >> > > E=1+b1*x+b12*x^2+b3*x^3...+b(nD-1)*x^(nD-1)
> >
> > Is there any way to modify gcd for this case?
>
> Not directly, but given any solution, say, E' and B', if such E and B
> exist, then E = E' + abc*A, B = B' + xyz*D, with abc and xyz being
> polynomials. I.e., in pseudocode, I'd assume something like this should
> work:
>
> while degree(E) is too large
> // if this is not an exact division, there is no solution:
> t = lterm(E)/lterm(A)
> E = E - t*A
> B = B + t*D
> end
>
> while degree(B) is too large
> ... same here ...
> end
>
> if degree(E) is too large
> there is no solution
> end
>
>
>
> Christopher

Christopher,

Sorry, but i could not understand totally, is it possible to explain in more details, also what is ''lterm'', i could not find any function as lterm.

Regards

Subject: Speed Up symbolic code

From: Christopher Creutzig

Date: 30 Aug, 2011 07:58:30

Message: 14 of 17

On 25.08.11 20:31, kamuran turksoy wrote:

>> Not directly, but given any solution, say, E' and B', if such E and B
>> exist, then E = E' + abc*A, B = B' + xyz*D, with abc and xyz being
>> polynomials. I.e., in pseudocode, I'd assume something like this should
>> work:
>>
>> while degree(E) is too large
>> // if this is not an exact division, there is no solution:
>> t = lterm(E)/lterm(A)
>> E = E - t*A
>> B = B + t*D
>> end
>>
>> while degree(B) is too large
>> ... same here ...
>> end
>>
>> if degree(E) is too large
>> there is no solution
>> end

> Sorry, but i could not understand totally, is it possible to explain in more details, also what is ''lterm'', i could not find any function as lterm.

I agree the above is a bit terse. What I meant to sketch was that if
C = A*B + D*E = A*B' + D*E' (where B' and E' are not derivatives or
transposes or anything like that, just different names), then the
equations E = E' + abc*A, B = B' + xyz*D must hold. So, if you want E
and B to have a certain max degree, that amounts to finding the proper
abc and xyz polynomials to cancel out terms (as in
coefficient*x^exponent) with a degree that is too high.

lterm is meant to be the leading term of a polynomial. From MATLAB, I'm
afraid the easiest way of getting it is

while double(feval(symengine, 'degree', E, x)) > myLimit
  t = simplify(feval(symengine, 'lterm', E, x) / ...
               feval(symengine, 'lterm', A, x));
... etc ...

(The symbolic toolbox interface is, at this point in time, not optimized
for polynomial operations. If you do a lot of those, it may be worth
checking out the MuPAD Notebook Interface and the MuPAD language
accessible from there.)



Christopher

Subject: Speed Up symbolic code

From: kamuran turksoy

Date: 1 Sep, 2011 20:53:10

Message: 15 of 17

Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <4E5C9826.7010105@mathworks.com>...
> On 25.08.11 20:31, kamuran turksoy wrote:
>
> >> Not directly, but given any solution, say, E' and B', if such E and B
> >> exist, then E = E' + abc*A, B = B' + xyz*D, with abc and xyz being
> >> polynomials. I.e., in pseudocode, I'd assume something like this should
> >> work:
> >>
> >> while degree(E) is too large
> >> // if this is not an exact division, there is no solution:
> >> t = lterm(E)/lterm(A)
> >> E = E - t*A
> >> B = B + t*D
> >> end
> >>
> >> while degree(B) is too large
> >> ... same here ...
> >> end
> >>
> >> if degree(E) is too large
> >> there is no solution
> >> end
>
> > Sorry, but i could not understand totally, is it possible to explain in more details, also what is ''lterm'', i could not find any function as lterm.
>
> I agree the above is a bit terse. What I meant to sketch was that if
> C = A*B + D*E = A*B' + D*E' (where B' and E' are not derivatives or
> transposes or anything like that, just different names), then the
> equations E = E' + abc*A, B = B' + xyz*D must hold. So, if you want E
> and B to have a certain max degree, that amounts to finding the proper
> abc and xyz polynomials to cancel out terms (as in
> coefficient*x^exponent) with a degree that is too high.
>
> lterm is meant to be the leading term of a polynomial. From MATLAB, I'm
> afraid the easiest way of getting it is
>
> while double(feval(symengine, 'degree', E, x)) > myLimit
> t = simplify(feval(symengine, 'lterm', E, x) / ...
> feval(symengine, 'lterm', A, x));
> ... etc ...
>
> (The symbolic toolbox interface is, at this point in time, not optimized
> for polynomial operations. If you do a lot of those, it may be worth
> checking out the MuPAD Notebook Interface and the MuPAD language
> accessible from there.)
>
>
>
> Christopher

I have tried your method, but i can not check it whether i have correct results or not, for example when i type A*B + D*E to check is it equal to C, it gives many parameters inside 'Iterm' so i don know that method works fine or not

Subject: Speed Up symbolic code

From: Christopher Creutzig

Date: 2 Sep, 2011 12:05:52

Message: 16 of 17

On 01.09.11 22:53, kamuran turksoy wrote:
> Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <4E5C9826.7010105@mathworks.com>...

>> lterm is meant to be the leading term of a polynomial. From MATLAB, I'm
>> afraid the easiest way of getting it is
>>
>> while double(feval(symengine, 'degree', E, x)) > myLimit
>> t = simplify(feval(symengine, 'lterm', E, x) / ...
>> feval(symengine, 'lterm', A, x));
>> ... etc ...
>>
>> (The symbolic toolbox interface is, at this point in time, not optimized
>> for polynomial operations. If you do a lot of those, it may be worth
>> checking out the MuPAD Notebook Interface and the MuPAD language
>> accessible from there.)

> I have tried your method, but i can not check it whether i have correct results or not, for example when i type A*B + D*E to check is it equal to C, it gives many parameters inside 'Iterm' so i don know that method works fine or not

Not Iterm, lterm. As in “*l*eading *term*”. (I'm surprised to hear that
the while loop would terminate if you replace lterm with Iterm.)


Christopher

Subject: Speed Up symbolic code

From: Evan

Date: 23 Oct, 2013 04:53:09

Message: 17 of 17

"kamuran turksoy" <kamuranturksoy@gmail.com> wrote in message <j2ehm3$3r0$1@newscl01ah.mathworks.com>...
> Hi everyone
>
> I have a big code. In that code i have some for loop and and i call some other functions. I wrote one of my function as symbolic. Inside the function i use
>
> syms x a1 a2 a3 a4 a5 a6 a7 ... a20
> and then i use these symbols as polynomials
> 1+a1*x+a12*x^2+a3*x^3...+a20*x^20
> and then i use solve to find some coefficient in somewhere.
>
> Anyway since i use symbolic toolbox and i also use for loop and in each loop i call function which includes symbolic functions, more than 20 times it makes my code running very very slow.
>
> Is there any way to speed up?
>
> Regards

I had exactly the same problem as you.
What I did was that I converted x to double. If you need to solve equation in each loop, I am afraid it might not help much, but if you do numeric calculations using the result of your solution, which matlab will store 20 or 30 decimals, then it helps a lot if you just use x = double(x).
This is because if x is the solution of a symbolic function, though x is numeric, it has to be operated through symbolic ways. It makes the whole program extremely slow.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us