# Ticket #14403: trac_14403_symbolic_charpoly_v3.patch

File trac_14403_symbolic_charpoly_v3.patch, 3.7 KB (added by gagern, 9 years ago)
• ## sage/matrix/matrix2.pyx

```# HG changeset patch
# User Martin von Gagern <Martin.vGagern@gmx.net>
# Date 1374571714 -7200
#      Tue Jul 23 11:28:34 2013 +0200
# Node ID 3148a2f30359aacd4466dd665b699193ec9e168b
# Parent  0f8fd922eaed351e39f913f1317d319dcceb4c01
Trac 14403: fix charpoly for symbolic matrices

diff --git a/sage/matrix/matrix2.pyx b/sage/matrix/matrix2.pyx```
 a [-4*x    0] [   0 -4*x] TESTS:: TESTS: Ensure correct handling of very small matrices:: sage: A = matrix(ZZ, 0, 0) sage: A [] [2] sage: A._adjoint() [1] Ensure proper computation of the adjoint matrix even in the presence of non-integral powers of the variable `x` (:trac:`14403`):: sage: x = var('x') sage: Matrix([[sqrt(x),x],[1,0]]).adjoint() [      0      -x] [     -1 sqrt(x)] NOTES:
• ## sage/matrix/matrix_symbolic_dense.pyx

`diff --git a/sage/matrix/matrix_symbolic_dense.pyx b/sage/matrix/matrix_symbolic_dense.pyx`
 a sage: M = matrix(SR, 2, 2, [cos(t), sin(t), -sin(t), cos(t)]) sage: M.det() sin(t)^2 + cos(t)^2 sage: M = matrix([[sqrt(x),0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]]) sage: det(M) sqrt(x) Permanents:: sage: A._cache['charpoly'] x^2 - 3*x - 2 Ensure the variable name of the polynomial does not conflict with variables used within the matrix (:trac:`14403`):: sage: Matrix(SR, [[sqrt(x), x],[1,x]]).charpoly().list() [x^(3/2) - x, -x - sqrt(x), 1] """ cache_key = 'charpoly' cp = self.fetch(cache_key) if cp is not None: return cp.change_variable_name(var) from sage.symbolic.ring import SR from sage.symbolic.ring import SR # We must not use a variable name already present in the matrix vname = 'do_not_use_this_name_in_a_matrix_youll_compute_a_charpoly_of' vsym = SR(vname) cp = self._maxima_(maxima).charpoly(vname)._sage_().expand() cp = [cp.coefficient(vsym, i) for i in range(self.nrows() + 1)] cp = SR[var](cp) # Maxima has the definition det(matrix-xI) instead of # det(xI-matrix), which is what Sage uses elsewhere.  We # correct for the discrepancy. cp = self._maxima_(maxima).charpoly(var)._sage_() cp = cp.expand().polynomial(None, ring=SR[var]) if self.nrows() % 2 == 1: cp = -cp
• ## sage/misc/functional.py

`diff --git a/sage/misc/functional.py b/sage/misc/functional.py`
 a T^10 + T^6 + T^5 + 4*T^4 + T^3 + 2*T^2 + 3*T + 3 sage: characteristic_polynomial(alpha, 'T') T^10 + T^6 + T^5 + 4*T^4 + T^3 + 2*T^2 + 3*T + 3 Ensure the variable name of the polynomial does not conflict with variables used within the matrix, and that non-integral powers of variables don't confuse the computation (:trac:`14403`):: sage: y = var('y') sage: a = matrix([[x,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]) sage: characteristic_polynomial(a).list() [x, -3*x - 1, 3*x + 3, -x - 3, 1] sage: b = matrix([[y^(1/2),0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]) sage: charpoly(b).list() [sqrt(y), -3*sqrt(y) - 1, 3*sqrt(y) + 3, -sqrt(y) - 3, 1] """ try: return x.charpoly(var)