I am looking for a good way to manipulate multivariate polynomials in Matlab. The purpose of this question is very global. Currently, I am manipulating some matrices of polynomials in MATLAB (with currently 2 variables). To simplify this manipulation I split each matrix into four new matrices:
C
E
(see below)X
: MX
Y
: MY
Thus you can evaluate polynomial matrix by this way C./h^E.*X.^MX.*Y.^MY
. For some reason, sometimes each component of the polynomial matrix can be a sum of some monomials. In this case, I use some nD-arrays (and sum(.,3)
).
For my work, I need also to defined the derivatives of the polynomial matrices with respect to X
or Y
. Using the previous formulation, the derivatives can be easily obtained by subtracting 1 to the associated matrix MX
or MY
and by multiplying C
by the right matrix MX
or MY
.
Currently this approach works fine for lower degrees but I need also to multiplying some polynomial matrices and this is the big problem of this approach. To deal with this problem I write manually the full matrix product (compute using Mathematica).
I want to extend my code for higher degrees and to manipulate more easily the polynomial matrices. So if you have any idea to do this.
I can use any toolbox in Matlab but at the end I need to have the matrices MX
, MY
, E
and C
(I need this separated matrices for doing some specific computations). I tried to use the Symbolic Toolbox
but it seems to be very difficult to extract these four matrices when the polynomial matrix is complicated.
Example:
H=[
1 0 Y/h 10*Y^2/h^2 5X*Y/h^2 0
0 1 -X/h X/h 50*X^2/h^2 60*X*Y/h^2
]
C=[
1 0 1 10 5 0
0 1 -1 1 50 60
]
E=[
0 0 1 2 2 0
0 1 1 1 2 2
]
MX=[
0 0 0 0 1 0
0 0 1 1 2 1
]
MY=[
0 0 1 2 1 0
0 0 0 0 0 1
]
Problem: Compute H*D'
and extract C
, E
, MX
and MY
(with H
define above) and
D=[
Y/h Y^2/h^2 X/h
X/h Y/h X*Y/h
]
Finally I have found one solution. Unfortunately (or not) I use the Symbolic Toolbox
. In my case I consider 7 symbolic variables and the variables of the polynomial are X
and Y
. The matlab variable poly
is a struct
which include the field p
. p
is a symfun
matrix of polynomial.
For the output arguments, C
, E
, MX
and MY
are respectively included in polyOut.mult
, polyOut.exph
, polyOut.expoX
and polyOut.expoY
.
%% Build the polynomial matrices
function polyOut=buildPolyMat(poly)
syms X Y real
syms c11 c12 c21 c22 c33 real
P=poly.p(X,Y);
sizP=size(P);
polyOut.mult=sym(zeros(sizP));
polyOut.expoX=zeros(sizP);
polyOut.expoY=zeros(sizP);
%for each term of the polynomial matrix
for it=1:prod(sizP)
%polynomial
pT=P(it);
%read coeff and exponents
[C,M]=coeffs(pT,[X,Y]);
if ~isempty(C)
%subscripts in matrix
[sC,sR]=ind2sub(sizP,it);
%exponents
for iE=1:numel(C)
eX=feval(symengine,'degree',M(iE),X);
eY=feval(symengine,'degree',M(iE),Y);
polyOut.mult(sC,sR,iE)=C(iE);
polyOut.expoX(sC,sR,iE)=eX;
polyOut.expoY(sC,sR,iE)=eY;
end
end
end
polyOut.exph=poly.eH*ones(size(polyOut.mult));
%clean matrices with zeros multiplier
Ind=(polyOut.mult==0);
polyOut.expoX(Ind)=0;
polyOut.expoY(Ind)=0;
polyOut.exph(Ind)=0;
end