# diff and contract

We may use the function diff to differentiate polynomials: the first argument is the variable to differentiate with respect to, and the second argument is the polynomial to be differentiated.
 i1 : R = QQ[a,b,t,x,y,z]; i2 : f = x^7 * y^11; i3 : diff(x,f) 6 11 o3 = 7x y o3 : R i4 : diff(y,f) 7 10 o4 = 11x y o4 : R
We indicate higher derivatives by simply multiplying the variables to differentiate by.
 i5 : diff(x^2,f) 5 11 o5 = 42x y o5 : R i6 : diff(x*y,f) 6 10 o6 = 77x y o6 : R i7 : diff(y^2,f) 7 9 o7 = 110x y o7 : R
The first argument can also be a sum, in which case the sum of the answers provided by each of its terms is returned.
 i8 : diff(x+y,f) 7 10 6 11 o8 = 11x y + 7x y o8 : R i9 : diff(x^2+x*y+y^2,f) 7 9 6 10 5 11 o9 = 110x y + 77x y + 42x y o9 : R
Remark: the operation diff is useful, but it's not a natural one: it's not invariant under linear coordinate changes; in effect, we've identified the a free module with its dual.

The second argument can be a matrix, in which case each of its entries gets differentiated.

 i10 : m = matrix {{x^3, x^4},{x^5,x^6}} o10 = | x3 x4 | | x5 x6 | 2 2 o10 : Matrix R <--- R i11 : diff(x,m) o11 = | 3x2 4x3 | | 5x4 6x5 | 2 2 o11 : Matrix R <--- R i12 : diff(x^2,m) o12 = | 6x 12x2 | | 20x3 30x4 | 2 2 o12 : Matrix R <--- R
The first argument can also be a matrix, in which case the matrices obtained from each of its entries, acting upon the second argument, are concatenated. Thus the shape of the first matrix plays the major role.
 i13 : diff(matrix {{x,x^2,x^3,x^4}}, m) o13 = | 3x2 4x3 6x 12x2 6 24x 0 24 | | 5x4 6x5 20x3 30x4 60x2 120x3 120x 360x2 | 2 8 o13 : Matrix R <--- R i14 : diff(matrix {{x,x^2},{x^3,x^4}}, m) o14 = | 3x2 4x3 6x 12x2 | | 5x4 6x5 20x3 30x4 | | 6 24x 0 24 | | 60x2 120x3 120x 360x2 | 4 4 o14 : Matrix R <--- R

Perhaps the most common usage of diff is when one argument has a single column and the other column has a single row. For example, the Jacobian matrix can be computed as follows.

 i15 : diff(matrix {{x},{y}}, matrix {{x^2, x*y, y^2}}) o15 = | 2x y 0 | | 0 x 2y | 2 3 o15 : Matrix R <--- R

We can also compute the Hessian matrix of a quadratic form using diff, as follows.
 i16 : v = matrix {{x,y}} o16 = | x y | 1 2 o16 : Matrix R <--- R i17 : diff(v ** transpose v, 3*x^2 + 5*x*y + 11*y^2) o17 = {1} | 6 5 | {1} | 5 22 | 2 2 o17 : Matrix R <--- R

As another example, we show how to compute the Wronskian of a polynomial f.
 i18 : f = x^3 + y^3 + z^3 - t*x*y*z 3 3 3 o18 = - t*x*y*z + x + y + z o18 : R i19 : v = matrix {{x,y,z}} o19 = | x y z | 1 3 o19 : Matrix R <--- R i20 : det diff(transpose v * v, f) 3 2 3 2 3 2 3 o20 = - 2t x*y*z - 6t x - 6t y - 6t z + 216x*y*z o20 : R

The function contract is the same as diff, except the multiplication by integers that occurs during differentiation is omitted.
 i21 : contract(x,m) o21 = | x2 x3 | | x4 x5 | 2 2 o21 : Matrix R <--- R i22 : contract(x^2,m) o22 = | x x2 | | x3 x4 | 2 2 o22 : Matrix R <--- R i23 : contract(matrix {{x,x^2,x^3,x^4}}, m) o23 = | x2 x3 x x2 1 x 0 1 | | x4 x5 x3 x4 x2 x3 x x2 | 2 8 o23 : Matrix R <--- R i24 : contract(matrix {{x,x^2},{x^3,x^4}}, m) o24 = | x2 x3 x x2 | | x4 x5 x3 x4 | | 1 x 0 1 | | x2 x3 x x2 | 4 4 o24 : Matrix R <--- R
One use is for picking out coefficients of homogeneous polynomials.
 i25 : f 3 3 3 o25 = - t*x*y*z + x + y + z o25 : R i26 : v3 = symmetricPower(3,matrix{{x,y,z}}) o26 = | x3 x2y x2z xy2 xyz xz2 y3 y2z yz2 z3 | 1 10 o26 : Matrix R <--- R i27 : contract(v3, f) o27 = | 1 0 0 0 -t 0 1 0 0 1 | 1 10 o27 : Matrix R <--- R

As an example, the Sylvester resultant between homogeneous polynomials f(x,y) and g(x,y) can be found in the following way.
 i28 : f = a * x^3 + b * x^2 * y + y^3 3 2 3 o28 = a*x + b*x y + y o28 : R i29 : g = b * x^3 + a * x * y^2 + y^3 3 2 3 o29 = b*x + a*x*y + y o29 : R
Multiply each of these by all quadrics, obtaining a set of elements in degree 5.
 i30 : n = matrix {{f,g}} ** symmetricPower(2,matrix {{x,y}}) o30 = | ax5+bx4y+x2y3 ax4y+bx3y2+xy4 ax3y2+bx2y3+y5 bx5+ax3y2+x2y3 ----------------------------------------------------------------------- bx4y+ax2y3+xy4 bx3y2+axy4+y5 | 1 6 o30 : Matrix R <--- R
Now create the matrix of coefficients by using contract against all monomials of degree 5 in x and y, and compute its determinant.
 i31 : M = contract(transpose symmetricPower(5,matrix {{x,y}}), n) o31 = {5} | a 0 0 b 0 0 | {5} | b a 0 0 b 0 | {5} | 0 b a a 0 b | {5} | 1 0 b 1 a 0 | {5} | 0 1 0 0 1 a | {5} | 0 0 1 0 0 1 | 6 6 o31 : Matrix R <--- R i32 : det M 5 2 3 3 2 2 3 4 3 2 2 3 o32 = - a - a b - a b - a b + 2a*b - b + a - 3a b + 3a*b - b o32 : R

The function diff' is the same as diff, except that the first argument is differentiated by the second; the shape of the first argument still plays the major role.
 i33 : diff'(m, matrix {{x,x^2,x^3,x^4}}) o33 = | 3x2 6x 6 0 4x3 12x2 24x 24 | | 5x4 20x3 60x2 120x 6x5 30x4 120x3 360x2 | 2 8 o33 : Matrix R <--- R i34 : diff'(m, matrix {{x,x^2},{x^3,x^4}}) o34 = | 3x2 6x 4x3 12x2 | | 6 0 24x 24 | | 5x4 20x3 6x5 30x4 | | 60x2 120x 120x3 360x2 | 4 4 o34 : Matrix R <--- R
The function contract' is the same as contract, except that the first argument is contracted by the second; the shape of the first argument still plays the major role.
 i35 : contract'(m, matrix {{x,x^2,x^3,x^4}}) o35 = | x2 x 1 0 x3 x2 x 1 | | x4 x3 x2 x x5 x4 x3 x2 | 2 8 o35 : Matrix R <--- R i36 : contract'(m, matrix {{x,x^2},{x^3,x^4}}) o36 = | x2 x x3 x2 | | 1 0 x 1 | | x4 x3 x5 x4 | | x2 x x3 x2 | 4 4 o36 : Matrix R <--- R

All four of these operators are engineered so that the result is a homogeneous matrix if the arguments are. The operations diff and contract are essentially partially defined division operations, so it should come as no surprise that the source and target of diff(m,n) are the same as those we would get from the tensor product transpose m^-1 ** n, if only m were invertible.