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

File trac-10837-norms-condition-CDF-review-v2.patch, 5.3 KB (added by rbeezer, 11 years ago)

Combined reviewer patch

• ## sage/matrix/matrix_double_dense.pyx

# HG changeset patch
# User Martin Raum <Martin.Raum@matha.rwth-aachen.de>
# Date 1313383603 25200
# Node ID fa183071f5755e81761cc2722f6e98b4d2fb94dd
# Parent  fa64509fe343c4b4a01ac5d7a0ecd81d892961f5
#10837: Fix doctest failures due to rounding errors and fix the 0-norm for vectors.

diff --git a/sage/matrix/matrix_double_dense.pyx b/sage/matrix/matrix_double_dense.pyx
 a \left(\sum_{i,j}\left\lvert{a_{i,j}}\right\rvert^2\right)^{1/2} - p = 'sv': the quotient of the maximal and minimal singular value. - p = Infinity or p = oo: the maximum row sum. - p = -Infinity or p = -oo: the minimum column sum. - p = 1: the maximum column sum. sage: A = matrix(RDF, 10, [1/(i+j+1) for i in range(10) for j in range(10)]) sage: A.condition() 1.63346888329e+13 1.633...e+13 sage: id = identity_matrix(CDF, 10) sage: id.condition(p=1) 1.0 sage: A.condition() in RDF True Rectangular and singular matrices raise errors.  :: Rectangular and singular matrices raise errors if p is not 'sv'.  :: sage: A = matrix(RDF, 2, 3, range(6)) sage: A.condition() Traceback (most recent call last): ... TypeError: matrix must be square, not 2 x 3 TypeError: matrix must be square if p is not 'sv', not 2 x 3 sage: A.condition('sv') 7.34... sage: A = matrix(QQ, 5, range(25)) sage: A.is_singular() sage: A.condition(p='bogus') Traceback (most recent call last): ... ValueError: condition number 'p' must be +/- infinity, 'frob' or an integer, not bogus ValueError: condition number 'p' must be +/- infinity, 'frob', 'sv' or an integer, not bogus sage: A.condition(p=632) Traceback (most recent call last): ... sage: abs(c-d) < 1.0e-14 True """ if not self.is_square(): raise TypeError("matrix must be square, not %s x %s" % (self.nrows(), self.ncols())) if not self.is_square() and p != 'sv': raise TypeError("matrix must be square if p is not 'sv', not %s x %s" % (self.nrows(), self.ncols())) global numpy if numpy is None: import numpy p = -numpy.inf elif p == 'frob': p = 'fro' elif p == 'sv' : p = None else: try: p = sage.rings.integer.Integer(p) except: raise ValueError("condition number 'p' must be +/- infinity, 'frob' or an integer, not %s" % p) raise ValueError("condition number 'p' must be +/- infinity, 'frob', 'sv' or an integer, not %s" % p) if p not in [-2,-1,1,2]: raise ValueError("condition number integer values of 'p' must be -2, -1, 1 or 2, not %s" % p) # may raise a LinAlgError if matrix is singular 6.0 sage: A.norm(p=2) 7.99575670... sage: A.norm(p=-2) 3.84592537...e-16 sage: A.norm(p=-2) < 10^-15 True And over the complex numbers.  :: [  0.139 -0.3892 -0.2648  0.8713] [ 0.4361   0.359  0.7599  0.3217] [ -0.836  0.3945  0.1438  0.3533] sage: T = T.zero_at(1.0e-12).change_ring(RDF) sage: T.round(4) [-13.5698      0.0      0.0      0.0] [     0.0  -0.8508      0.0      0.0] [     0.0      0.0   7.7664      0.0] [     0.0      0.0      0.0  11.6542] sage: T = T.zero_at(10^-12) sage: all(abs(e) < 10^-4 for e in (T - diagonal_matrix(RDF, [-13.5698, -0.8508, 7.7664, 11.6542])).list()) True sage: (Q*Q.transpose()).zero_at(1.0e-12) [1.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0]
• ## sage/modules/vector_double_dense.pyx

diff --git a/sage/modules/vector_double_dense.pyx b/sage/modules/vector_double_dense.pyx
 a sage: v.norm(p=-oo) 0.0 sage: v.norm(p=0) 8 8.0 sage: v.norm(p=0.3) 4099.153615... sage: w.norm(p=2) 13.9283882... sage: w.norm(p=0) 2 2.0 sage: w.norm(p=4.2) 13.0555695... sage: w.norm(p=oo) raise ValueError("vector norm 'p' must be +/- infinity or a real number, not %s" % p) n = numpy.linalg.norm(self._vector_numpy, ord=p) # p = 0 returns integer *count* of non-zero entries if n.dtype == numpy.int64: return sage.rings.integer.Integer(n) else: return RDF(n) return RDF(n) #############################