I have some column vectors, and I put them in a row, so I have $[a \, b \, c]$. Now I want to get a matrix of the form $[a^2 \, b^2 \, c^2 \, ab \, ac \, bc]$. It is like expanding $(a+b+c)^2$, but applying it to matrix columns. Is there any trick in Octave/Matlab that would allow me to do it automatically? The example is easy. Imagine if I want to get $[a_1 \, a_2 \, \cdots a_n]$ to a higher degree. It would be a pain to do it manually.
Asked
Active
Viewed 1,100 times
1
-
Are these numeric or symbolic vectors? – horchler Sep 02 '14 at 15:37
1 Answers
3
Polynomial multiplication is equivalent to convolution. This means that if you have two numeric vectors, you can use the conv
function in Matlab:
v1 = [2 3 4];
v2 = [3 5 7];
w = conv(v1,v2)
which returns
w =
6 19 41 41 28
The vectors don't even need to be the same length. In your case, you can just pass the same vector in twice.
Unfortunately, the conv
function isn't overloaded for symbolic variables. Howver, you can take advantage of MuPAD's polynomial capabilities from within Matlab (I have no idea what Octave supports in this area). You can use poly
to create polynomials from coefficients. These objects can be multiplied together naturally. Then you can use coeff
to extract the symbolic coefficients. Here's an example:
syms a1 a2 b1 b2 c1 c2;
v1 = char([a1 b1 c1]);
p1 = feval(symengine,'poly',v1(9:end-2),'[x]');
v2 = char([a2 b2 c2]);
p2 = feval(symengine,'poly',v2(9:end-2),'[x]');
w = feval(symengine,'coeff',p1*p2)
which returns the symbolic vector:
w =
[ c1*c2, b1*c2 + b2*c1, a1*c2 + a2*c1 + b1*b2, a1*b2 + a2*b1, a1*a2]