# Ticket #14403: trac_14403_symbolic_charpoly_v2.patch

File trac_14403_symbolic_charpoly_v2.patch, 4.3 KB (added by gagern, 9 years ago)
• ## sage/matrix/matrix_symbolic_dense.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 34ded95e582a49864154dc28c5589dfc4858fb3c
# Parent  0f8fd922eaed351e39f913f1317d319dcceb4c01
Trac 14403: fix charpoly for symbolic matrices

diff --git a/sage/matrix/matrix_symbolic_dense.pyx b/sage/matrix/matrix_symbolic_dense.pyx```
 a return z._sage_() def charpoly(self, var='x', algorithm=None): def charpoly(self, var=None, algorithm=None): """ Compute the characteristic polynomial of self, using maxima. 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() x1^2 + (-x - sqrt(x))*x1 + x^(3/2) - x sage: _.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) if cp is None: from sage.symbolic.ring import SR from sage.symbolic.ring import SR # 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 # We must not use a variable name already present in the matrix variables = [repr(v) for v in self.variables()] varname = 'x' varcounter = 0 while varname in variables: varcounter += 1 varname = 'x' + str(varcounter) varsymbol = SR(varname) self.cache(cache_key, cp) cp = self._maxima_(maxima).charpoly(varname)._sage_().expand() cp = [cp.coefficient(varsymbol, i) for i in range(self.nrows() + 1)] cp = SR[varname](cp) # Maxima has the definition det(matrix-xI) instead of # det(xI-matrix), which is what Sage uses elsewhere.  We # correct for the discrepancy. if self.nrows() % 2 == 1: cp = -cp self.cache(cache_key, cp) if var is not None: cp = cp.change_variable_name(var) return cp def fcp(self, var='x'):
• ## sage/symbolic/expression_conversions.py

`diff --git a/sage/symbolic/expression_conversions.py b/sage/symbolic/expression_conversions.py`
 a if ring is not None: base_ring = ring.base_ring() G = ring.variable_names_recursive() self.varnames = ring.variable_names_recursive() for v in ex.variables(): if repr(v) not in G and v not in base_ring: if repr(v) not in self.varnames and v not in base_ring: raise TypeError, "%s is not a variable of %s" %(v, ring) self.ring = ring self.base_ring = base_ring vars = ['x'] from sage.rings.all import PolynomialRing self.ring = PolynomialRing(self.base_ring, names=vars) self.varnames = self.ring.variable_names() else: raise TypeError, "either a ring or base ring must be specified" sage: p = PolynomialConverter(x+y, base_ring=RR) sage: p.arithmetic(x*y+y^2, operator.add) x*y + y^2 sage: p = PolynomialConverter(y^(3/2), ring=SR['x']) sage: p.arithmetic(y^(3/2), operator.pow) y^(3/2) sage: _.parent() Symbolic Ring """ if len(ex.variables()) == 0: if not any(repr(v) in self.varnames for v in ex.variables()): return self.base_ring(ex) elif operator == _operator.pow: from sage.rings.all import Integer