Ticket #10837: trac-10837-norms-condition-CDF-review.patch

File trac-10837-norms-condition-CDF-review.patch, 4.2 KB (added by mraum, 12 years ago)
  • sage/matrix/matrix_double_dense.pyx

    # HG changeset patch
    # Parent 0f68eda0c74927c0761cd9ac93b962cd20d03bde
    #10837: Fix doctest failures due to rounding errors and fix the 0-norm for vectors.
    
    diff -r 0f68eda0c749 sage/matrix/matrix_double_dense.pyx
    a b  
    587587
    588588                \left(\sum_{i,j}\left\lvert{a_{i,j}}\right\rvert^2\right)^{1/2}
    589589
     590        - ``p = 'sv'``: the quotient of the maximal and minimal singular value.
    590591        - ``p = Infinity`` or ``p = oo``: the maximum row sum.
    591592        - ``p = -Infinity`` or ``p = -oo``: the minimum column sum.
    592593        - ``p = 1``: the maximum column sum.
     
    653654
    654655            sage: A = matrix(RDF, 10, [1/(i+j+1) for i in range(10) for j in range(10)])
    655656            sage: A.condition()
    656             1.63346888329e+13
     657            1.633...e+13
    657658            sage: id = identity_matrix(CDF, 10)
    658659            sage: id.condition(p=1)
    659660            1.0
     
    664665            sage: A.condition() in RDF
    665666            True
    666667
    667         Rectangular and singular matrices raise errors.  ::
     668        Rectangular and singular matrices raise errors if p is not 'sv'.  ::
    668669
    669670            sage: A = matrix(RDF, 2, 3, range(6))
    670671            sage: A.condition()
    671672            Traceback (most recent call last):
    672673            ...
    673             TypeError: matrix must be square, not 2 x 3
     674            TypeError: matrix must be square if p is not 'sv', not 2 x 3
     675
     676            sage: A.condition('sv')
     677            7.34...
    674678
    675679            sage: A = matrix(QQ, 5, range(25))
    676680            sage: A.is_singular()
     
    687691            sage: A.condition(p='bogus')
    688692            Traceback (most recent call last):
    689693            ...
    690             ValueError: condition number 'p' must be +/- infinity, 'frob' or an integer, not bogus
     694            ValueError: condition number 'p' must be +/- infinity, 'frob', 'sv' or an integer, not bogus
    691695            sage: A.condition(p=632)
    692696            Traceback (most recent call last):
    693697            ...
     
    708712            sage: abs(c-d) < 1.0e-14
    709713            True
    710714        """
    711         if not self.is_square():
    712             raise TypeError("matrix must be square, not %s x %s" % (self.nrows(), self.ncols()))
     715        if not self.is_square() and p != 'sv':
     716            raise TypeError("matrix must be square if p is not 'sv', not %s x %s" % (self.nrows(), self.ncols()))
    713717        global numpy
    714718        if numpy is None:
    715719            import numpy
     
    722726            p = -numpy.inf
    723727        elif p == 'frob':
    724728            p = 'fro'
     729        elif p == 'sv' :
     730            p = None
    725731        else:
    726732            try:
    727733                p = sage.rings.integer.Integer(p)
    728734            except:
    729                 raise ValueError("condition number 'p' must be +/- infinity, 'frob' or an integer, not %s" % p)
     735                raise ValueError("condition number 'p' must be +/- infinity, 'frob', 'sv' or an integer, not %s" % p)
    730736            if p not in [-2,-1,1,2]:
    731737                raise ValueError("condition number integer values of 'p' must be -2, -1, 1 or 2, not %s" % p)
    732738        # may raise a LinAlgError if matrix is singular
  • sage/modules/vector_double_dense.pyx

    diff -r 0f68eda0c749 sage/modules/vector_double_dense.pyx
    a b  
    626626            sage: v.norm(p=-oo)
    627627            0.0
    628628            sage: v.norm(p=0)
    629             8
     629            8.0
    630630            sage: v.norm(p=0.3)
    631631            4099.153615...
    632632
     
    638638            sage: w.norm(p=2)
    639639            13.9283882...
    640640            sage: w.norm(p=0)
    641             2
     641            2.0
    642642            sage: w.norm(p=4.2)
    643643            13.0555695...
    644644            sage: w.norm(p=oo)
     
    688688                raise ValueError("vector norm 'p' must be +/- infinity or a real number, not %s" % p)
    689689        n = numpy.linalg.norm(self._vector_numpy, ord=p)
    690690        # p = 0 returns integer *count* of non-zero entries
    691         if n.dtype == numpy.int64:
    692             return sage.rings.integer.Integer(n)
    693         else:
    694             return RDF(n)
     691        return RDF(n)
    695692
    696693
    697694    #############################