# 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
|
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 |
… |
… |
|
797 | 803 | 6.0 |
798 | 804 | sage: A.norm(p=2) |
799 | 805 | 7.99575670... |
800 | | sage: A.norm(p=-2) |
801 | | 3.84592537...e-16 |
| 806 | sage: A.norm(p=-2) < 10^-15 |
| 807 | True |
802 | 808 | |
803 | 809 | And over the complex numbers. :: |
804 | 810 | |
… |
… |
|
1971 | 1977 | [ 0.139 -0.3892 -0.2648 0.8713] |
1972 | 1978 | [ 0.4361 0.359 0.7599 0.3217] |
1973 | 1979 | [ -0.836 0.3945 0.1438 0.3533] |
1974 | | sage: T = T.zero_at(1.0e-12).change_ring(RDF) |
1975 | | sage: T.round(4) |
1976 | | [-13.5698 0.0 0.0 0.0] |
1977 | | [ 0.0 -0.8508 0.0 0.0] |
1978 | | [ 0.0 0.0 7.7664 0.0] |
1979 | | [ 0.0 0.0 0.0 11.6542] |
| 1980 | sage: T = T.zero_at(10^-12) |
| 1981 | sage: all(abs(e) < 10^-4 for e in (T - diagonal_matrix(RDF, [-13.5698, -0.8508, 7.7664, 11.6542])).list()) |
| 1982 | True |
1980 | 1983 | sage: (Q*Q.transpose()).zero_at(1.0e-12) |
1981 | 1984 | [1.0 0.0 0.0 0.0] |
1982 | 1985 | [0.0 1.0 0.0 0.0] |
diff --git a/sage/modules/vector_double_dense.pyx b/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 | ############################# |