Expansion of a polynomial (Best way)
8 views (last 30 days)
Show older comments
Hi, I have a polynomial of the form : (1+x^a1)*(1+x^a2)*...*(1+x^an), and I want to expand it (compute its coefficients). Which is the best way (low computational complexity) of doing this in Matlab?
I hope it is clear. Thanks for your help.
Best regards. H.
3 Comments
Iain
on 29 Jan 2014
If you go to a sensible order in the polynomial (i.e. less than about 10), the code will likely be too fast to worry about.
Accepted Answer
Roger Stafford
on 30 Jan 2014
Let a be a row vector consisting of your exponents, a1, a2, ..., an. Then try this:
A = cumsum([0,a])+1;
a1 = a + 1;
C = [1,zeros(1,A(size(A,2))-1)];
for k = 1:size(a,2)
C(a1(k):A(k+1)) = C(a1(k):A(k+1)) + C(1:A(k));
end
The C row vector will have the coefficients of the powers of x for your product:
C(1)+C(2)*x+C(3)*x^2+C(4)*x^3+C(5)*x^4+...
2 Comments
More Answers (1)
Walter Roberson
on 29 Jan 2014
Edited: Walter Roberson
on 29 Jan 2014
I am not sure of the best way to express this at the moment. The elements are
(-1)^(k) * sum( x^(sum(a[p], p element of Q), Q is the set of distinct combinations of n objects taken k at a time)
so for example, for n = 4,
+ (+1) * (x^(a[1]+a[2]+a[3]+a[4]))
+ (-1) * (x^(a[1]+a[2]+a[3]) + x^(a[1]+a[2]+a[4]) + x^(a[1]+a[3]+a[4]) + x^(a[2]+a[3]+a[4]))
+ (+1) * (x^(a[1]+a[2]) + x^(a[1]+a[3]) + x^(a[1]+a[4]) + x^(a[2]+a[3]) + x^(a[2]+a[4]) + x^(a[3]+a[4])
+ (-1) * (x^a[1] + x^a[2] + x^a[3] + x^a[4])
+ (+1) * 1
and so the computational complexity would be that of producing all of the combinations. Some of the methods are noted at http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n
But if I were doing it myself, unless I had good reason otherwise, I would probably just toss it into a symbolic toolbox. For example in Maple,
sort(combine(expand(product(1-x^a[n], n = 1 .. 4))))
3 Comments
Walter Roberson
on 29 Jan 2014
2^n seems plausible to me, but perhaps someone found a better way.
You can see with a small amount of experimentation that this is what the elements do come out as. If it takes O(2^n) to calculate them all out, then it takes O(2^n), and any algorithm we might put forth as having lower complexity would, if valid, also be a way to lower the complexity of finding all combinations.
See Also
Categories
Find more on Polynomials 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!