From: Cyril Branciard on
Hi all,

I am using the Symbolic Math Toolbox, and define polynoms as symbolic objects, eg:

syms a b X
P = a^2*b + a*b*X + 2*a*X + b*X^2

I would need to list all coefficients of such a multivariate polynomial, when considered as a polynomial in the variable X for instance.
That is, I would need a function that would act just like sym2poly: something like
sym2poly(P,X)
that would return
[ b, 2*a+a*b, a^2*b].

The problem is that sym2poly does not work for multivariate polynomials ("Error: Input has more than one symbolic variable").
Would anyone have a solution to my problem ?? Any idea ??

Thanks a lot in advance,
Cyril
From: Walter Roberson on
Cyril Branciard wrote:

> I am using the Symbolic Math Toolbox, and define polynoms as symbolic
> objects, eg:

> syms a b X
> P = a^2*b + a*b*X + 2*a*X + b*X^2
>
> I would need to list all coefficients of such a multivariate polynomial,
> when considered as a polynomial in the variable X for instance.

See coeffs()
From: Cyril on
Dear Walter, dear all,

Thanks for your reply.
The problem with coeffs() is that it does not organize the coefficient it returns, so I don't know which coefficient corresponds to which monomial. Also, it does not return the null coefficients...
eg:
> syms a b X
> P = a^2*b + a*b*X + 2*a*X + b*X^3;
> coeffs(P)
[ 1, 2, 1, 1]

I would need a function that would give me for instance the coefficients of P when considered as a polynom in the variable X: sthg like
> myfunction(P,X)
[ a^2*b, a*b+2*a, 0, b]

(In Mathematica, this would correspond to the function CoefficientList)

Any idea on how to do that with Matlab ?

Thanks again,
Cyril

Walter Roberson <roberson(a)hushmail.com> wrote in message <hmjbk0$9ft$1(a)canopus.cc.umanitoba.ca>...
> Cyril Branciard wrote:
>
> > I am using the Symbolic Math Toolbox, and define polynoms as symbolic
> > objects, eg:
>
> > syms a b X
> > P = a^2*b + a*b*X + 2*a*X + b*X^2
> >
> > I would need to list all coefficients of such a multivariate polynomial,
> > when considered as a polynomial in the variable X for instance.
>
> See coeffs()
From: Walter Roberson on
Cyril wrote:

> The problem with coeffs() is that it does not organize the coefficient
> it returns, so I don't know which coefficient corresponds to which
> monomial. Also, it does not return the null coefficients...
> eg:
>> syms a b X
>> P = a^2*b + a*b*X + 2*a*X + b*X^3;
>> coeffs(P)
> [ 1, 2, 1, 1]
>
> I would need a function that would give me for instance the coefficients
> of P when considered as a polynom in the variable X: sthg like
>> myfunction(P,X)
> [ a^2*b, a*b+2*a, 0, b]

In Maple, if it was known that P was at least order 2, I'd do it like this:

C :=
[seq(coeff(collect(P,X),X,N),N=0..max(map2(op,2,indets(P,identical(X)^integer))))];

The indets() call finds all the X to a power, the map2 steps over each member
of the resulting set and does op(2,value) on the value, thus extracting the
integer power; the max() finds the maximum of those integers, thus giving the
order of the polynomial. Then a loop from 0 to that maximum is done,
extracting the coeff() of X of each power; the [] wrapped about the whole
things puts it all together in a list.


Well... actually, in Maple, I'd probably just use

C := [coeffs(collect(P,X),X,'xp')];
after which C would contain a list of coefficients and xp would contain the
corresponding factor of X... i.e.,
C = [a^2*b, a*b+2*a, b]
xp = [1, X, X^3]

but then I don't recall that I've ever needed to put in specific placeholders
for the null powers.
From: Cyril on
Thanks a lot, I think you solved my problem! :-)

cheers,
Cyril

Walter Roberson <roberson(a)hushmail.com> wrote in message <hmn46j$is5$1(a)canopus.cc.umanitoba.ca>...
> Cyril wrote:
>
> > The problem with coeffs() is that it does not organize the coefficient
> > it returns, so I don't know which coefficient corresponds to which
> > monomial. Also, it does not return the null coefficients...
> > eg:
> >> syms a b X
> >> P = a^2*b + a*b*X + 2*a*X + b*X^3;
> >> coeffs(P)
> > [ 1, 2, 1, 1]
> >
> > I would need a function that would give me for instance the coefficients
> > of P when considered as a polynom in the variable X: sthg like
> >> myfunction(P,X)
> > [ a^2*b, a*b+2*a, 0, b]
>
> In Maple, if it was known that P was at least order 2, I'd do it like this:
>
> C :=
> [seq(coeff(collect(P,X),X,N),N=0..max(map2(op,2,indets(P,identical(X)^integer))))];
>
> The indets() call finds all the X to a power, the map2 steps over each member
> of the resulting set and does op(2,value) on the value, thus extracting the
> integer power; the max() finds the maximum of those integers, thus giving the
> order of the polynomial. Then a loop from 0 to that maximum is done,
> extracting the coeff() of X of each power; the [] wrapped about the whole
> things puts it all together in a list.
>
>
> Well... actually, in Maple, I'd probably just use
>
> C := [coeffs(collect(P,X),X,'xp')];
> after which C would contain a list of coefficients and xp would contain the
> corresponding factor of X... i.e.,
> C = [a^2*b, a*b+2*a, b]
> xp = [1, X, X^3]
>
> but then I don't recall that I've ever needed to put in specific placeholders
> for the null powers.