%mathpiper,def="SylvesterMatrix"

/* SylvesterMatrix */

Function("SylvesterMatrix",{poly1, poly2, var})
[
  Local(i,m,p,q,y,z,result);
  y:=Degree(poly1,var);
  z:=Degree(poly2,var);
  m:=y+z;
  p:={};
  q:={};
  result:=ZeroMatrix(m,m);

  For(i:=y,i>=0,i--)
    DestructiveAppend(p,Coef(poly1,var,i));
  For(i:=z,i>=0,i--)
    DestructiveAppend(q,Coef(poly2,var,i));

  For(i:=1,i<=z,i++)
  [
    Local(j,k);
        k:=1;
    For(j:=i,k<=Length(p),j++)
        [
          result[i][j]:=p[k];
          k++;
        ];
  ];

  For(i:=1,i<=y,i++)
  [
    Local(j,k);
        k:=1;
    For(j:=i,k<=Length(q),j++)
        [
          result[i+z][j]:=q[k];
          k++;
        ];
  ];
  result;
];

%/mathpiper



%mathpiper_docs,name="SylvesterMatrix",categories="User Functions;Matrices (Special)"
*CMD SylvesterMatrix --- calculate the Sylvester matrix of two polynomials
*STD
*CALL
	SylvesterMatrix(poly1,poly2,variable)

*PARMS

{poly1} -- polynomial

{poly2} -- polynomial

{variable} -- variable to express the matrix for

*DESC

The function {SylvesterMatrix} calculates the Sylvester matrix
for a pair of polynomials.

The Sylvester matrix is closely related to the resultant, which
is defined as the determinant of the Sylvester matrix. Two polynomials
share common roots only if the resultant is zero.

*E.G.

In> ex1:= x^2+2*x-a
Result: x^2+2*x-a;
In> ex2:= x^2+a*x-4
Result: x^2+a*x-4;
In> A:=SylvesterMatrix(ex1,ex2,x)
Result: {{1,2,-a,0},{0,1,2,-a},
	  {1,a,-4,0},{0,1,a,-4}};
In> B:=Determinant(A)
Result: 16-a^2*a- -8*a-4*a+a^2- -2*a^2-16-4*a;
In> Simplify(B)
Result: 3*a^2-a^3;

The above example shows that the two polynomials have common
zeros if $ a = 3 $.

*SEE Determinant, Simplify, Solve, PSolve
%/mathpiper_docs