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 b  
    76407640            [-4*x    0]
    76417641            [   0 -4*x]
    76427642       
    7643         TESTS::
    7644        
     7643        TESTS:
     7644
     7645        Ensure correct handling of very small matrices::
     7646
    76457647            sage: A = matrix(ZZ, 0, 0)
    76467648            sage: A
    76477649            []
     
    76527654            [2]
    76537655            sage: A._adjoint()
    76547656            [1]
     7657
     7658        Ensure proper computation of the adjoint matrix even in the
     7659        presence of non-integral powers of the variable `x`
     7660        (:trac:`14403`)::
     7661
     7662            sage: x = var('x')
     7663            sage: Matrix([[sqrt(x),x],[1,0]]).adjoint()
     7664            [      0      -x]
     7665            [     -1 sqrt(x)]
    76557666       
    76567667        NOTES:
    76577668       
  • sage/matrix/matrix_symbolic_dense.pyx

    diff --git a/sage/matrix/matrix_symbolic_dense.pyx b/sage/matrix/matrix_symbolic_dense.pyx
    a b  
    104104    sage: M = matrix(SR, 2, 2, [cos(t), sin(t), -sin(t), cos(t)])
    105105    sage: M.det()
    106106    sin(t)^2 + cos(t)^2
     107    sage: M = matrix([[sqrt(x),0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]])
     108    sage: det(M)
     109    sqrt(x)
    107110
    108111Permanents::
    109112
     
    422425            sage: A._cache['charpoly']
    423426            x^2 - 3*x - 2
    424427
     428        Ensure the variable name of the polynomial does not conflict
     429        with variables used within the matrix (:trac:`14403`)::
     430
     431            sage: Matrix(SR, [[sqrt(x), x],[1,x]]).charpoly().list()
     432            [x^(3/2) - x, -x - sqrt(x), 1]
    425433        """
    426434        cache_key = 'charpoly'
    427435        cp = self.fetch(cache_key)
    428436        if cp is not None:
    429437            return cp.change_variable_name(var)
     438        from sage.symbolic.ring import SR
    430439
    431         from sage.symbolic.ring import SR
     440        # We must not use a variable name already present in the matrix
     441        vname = 'do_not_use_this_name_in_a_matrix_youll_compute_a_charpoly_of'
     442        vsym = SR(vname)
     443
     444        cp = self._maxima_(maxima).charpoly(vname)._sage_().expand()
     445        cp = [cp.coefficient(vsym, i) for i in range(self.nrows() + 1)]
     446        cp = SR[var](cp)
     447
    432448        # Maxima has the definition det(matrix-xI) instead of
    433449        # det(xI-matrix), which is what Sage uses elsewhere.  We
    434450        # correct for the discrepancy.
    435         cp = self._maxima_(maxima).charpoly(var)._sage_()
    436         cp = cp.expand().polynomial(None, ring=SR[var])
    437451        if self.nrows() % 2 == 1:
    438452            cp = -cp
    439453
  • sage/misc/functional.py

    diff --git a/sage/misc/functional.py b/sage/misc/functional.py
    a b  
    182182        T^10 + T^6 + T^5 + 4*T^4 + T^3 + 2*T^2 + 3*T + 3
    183183        sage: characteristic_polynomial(alpha, 'T')
    184184        T^10 + T^6 + T^5 + 4*T^4 + T^3 + 2*T^2 + 3*T + 3
     185
     186    Ensure the variable name of the polynomial does not conflict with
     187    variables used within the matrix, and that non-integral powers of
     188    variables don't confuse the computation (:trac:`14403`)::
     189
     190        sage: y = var('y')
     191        sage: a = matrix([[x,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])
     192        sage: characteristic_polynomial(a).list()
     193        [x, -3*x - 1, 3*x + 3, -x - 3, 1]
     194        sage: b = matrix([[y^(1/2),0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])
     195        sage: charpoly(b).list()
     196        [sqrt(y), -3*sqrt(y) - 1, 3*sqrt(y) + 3, -sqrt(y) - 3, 1]
    185197    """
    186198    try:
    187199        return x.charpoly(var)