# 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
|
|
587 | 587 | |
588 | 588 | \left(\sum_{i,j}\left\lvert{a_{i,j}}\right\rvert^2\right)^{1/2} |
589 | 589 | |
| 590 | - ``p = 'sv'``: the quotient of the maximal and minimal singular value. |
590 | 591 | - ``p = Infinity`` or ``p = oo``: the maximum row sum. |
591 | 592 | - ``p = -Infinity`` or ``p = -oo``: the minimum column sum. |
592 | 593 | - ``p = 1``: the maximum column sum. |
… |
… |
|
653 | 654 | |
654 | 655 | sage: A = matrix(RDF, 10, [1/(i+j+1) for i in range(10) for j in range(10)]) |
655 | 656 | sage: A.condition() |
656 | | 1.63346888329e+13 |
| 657 | 1.633...e+13 |
657 | 658 | sage: id = identity_matrix(CDF, 10) |
658 | 659 | sage: id.condition(p=1) |
659 | 660 | 1.0 |
… |
… |
|
664 | 665 | sage: A.condition() in RDF |
665 | 666 | True |
666 | 667 | |
667 | | Rectangular and singular matrices raise errors. :: |
| 668 | Rectangular and singular matrices raise errors if p is not 'sv'. :: |
668 | 669 | |
669 | 670 | sage: A = matrix(RDF, 2, 3, range(6)) |
670 | 671 | sage: A.condition() |
671 | 672 | Traceback (most recent call last): |
672 | 673 | ... |
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... |
674 | 678 | |
675 | 679 | sage: A = matrix(QQ, 5, range(25)) |
676 | 680 | sage: A.is_singular() |
… |
… |
|
687 | 691 | sage: A.condition(p='bogus') |
688 | 692 | Traceback (most recent call last): |
689 | 693 | ... |
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 |
691 | 695 | sage: A.condition(p=632) |
692 | 696 | Traceback (most recent call last): |
693 | 697 | ... |
… |
… |
|
708 | 712 | sage: abs(c-d) < 1.0e-14 |
709 | 713 | True |
710 | 714 | """ |
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())) |
713 | 717 | global numpy |
714 | 718 | if numpy is None: |
715 | 719 | import numpy |
… |
… |
|
722 | 726 | p = -numpy.inf |
723 | 727 | elif p == 'frob': |
724 | 728 | p = 'fro' |
| 729 | elif p == 'sv' : |
| 730 | p = None |
725 | 731 | else: |
726 | 732 | try: |
727 | 733 | p = sage.rings.integer.Integer(p) |
728 | 734 | 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) |
730 | 736 | if p not in [-2,-1,1,2]: |
731 | 737 | raise ValueError("condition number integer values of 'p' must be -2, -1, 1 or 2, not %s" % p) |
732 | 738 | # may raise a LinAlgError if matrix is singular |
diff -r 0f68eda0c749 sage/modules/vector_double_dense.pyx
a
|
b
|
|
626 | 626 | sage: v.norm(p=-oo) |
627 | 627 | 0.0 |
628 | 628 | sage: v.norm(p=0) |
629 | | 8 |
| 629 | 8.0 |
630 | 630 | sage: v.norm(p=0.3) |
631 | 631 | 4099.153615... |
632 | 632 | |
… |
… |
|
638 | 638 | sage: w.norm(p=2) |
639 | 639 | 13.9283882... |
640 | 640 | sage: w.norm(p=0) |
641 | | 2 |
| 641 | 2.0 |
642 | 642 | sage: w.norm(p=4.2) |
643 | 643 | 13.0555695... |
644 | 644 | sage: w.norm(p=oo) |
… |
… |
|
688 | 688 | raise ValueError("vector norm 'p' must be +/- infinity or a real number, not %s" % p) |
689 | 689 | n = numpy.linalg.norm(self._vector_numpy, ord=p) |
690 | 690 | # 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) |
695 | 692 | |
696 | 693 | |
697 | 694 | ############################# |