| 551 | |
| 552 | def condition(self, p='frob'): |
| 553 | r""" |
| 554 | Returns the condition number of a square nonsingular matrix. |
| 555 | |
| 556 | Roughly speaking, this is a measure of how sensitive |
| 557 | the matrix is to round-off errors in numerical computations. |
| 558 | The minimum possible value is 1.0, and larger numbers indicate |
| 559 | greater sensitivity. |
| 560 | |
| 561 | INPUT: |
| 562 | |
| 563 | - ``p`` - default: 'frob' - controls which norm is used |
| 564 | to compute the condition number, allowable values are |
| 565 | 'frob' (for the Frobenius norm), integers -2, -1, 1, 2, |
| 566 | positive and negative infinity. See output discussion |
| 567 | for specifics. |
| 568 | |
| 569 | OUTPUT: |
| 570 | |
| 571 | The condition number of a matrix is the product of a norm |
| 572 | of the matrix times the norm of the inverse of the matrix. |
| 573 | This requires that the matrix be square and invertible |
| 574 | (nonsingular, full rank). |
| 575 | |
| 576 | Returned value is a double precision floating point value |
| 577 | in ``RDF``, or ``Infinity``. Row and column sums described below are |
| 578 | sums of the absolute values of the entries, where the |
| 579 | absolute value of the complex number `a+bi` is `\sqrt{a^2+b^2}`. |
| 580 | Singular values are the "diagonal" entries of the "S" matrix in |
| 581 | the singular value decomposition. |
| 582 | |
| 583 | - ``p = 'frob'``: the default norm employed in computing |
| 584 | the condition number, the Frobenius norm, which for a |
| 585 | matrix `A=(a_{ij})` computes |
| 586 | |
| 587 | .. math:: |
| 588 | |
| 589 | \left(\sum_{i,j}\left\lvert{a_{i,j}}\right\rvert^2\right)^{1/2} |
| 590 | |
| 591 | - ``p = Infinity`` or ``p = oo``: the maximum row sum. |
| 592 | - ``p = -Infinity`` or ``p = -oo``: the minimum colum sum. |
| 593 | - ``p = 1``: the maximum column sum. |
| 594 | - ``p = -1``: the minimum column sum. |
| 595 | - ``p = 2``: the 2-norm, equal to the maximum singular value. |
| 596 | - ``p = -2``: the minimum singular value. |
| 597 | |
| 598 | ALGORITHM: |
| 599 | |
| 600 | Computation is performed by the ``cond()`` function of |
| 601 | the SciPy/NumPy library. |
| 602 | |
| 603 | EXAMPLES: |
| 604 | |
| 605 | First over the reals. :: |
| 606 | |
| 607 | sage: A = matrix(RDF, 4, [(1/4)*x^3 for x in range(16)]); A |
| 608 | [ 0.0 0.25 2.0 6.75] |
| 609 | [ 16.0 31.25 54.0 85.75] |
| 610 | [ 128.0 182.25 250.0 332.75] |
| 611 | [ 432.0 549.25 686.0 843.75] |
| 612 | sage: A.condition() |
| 613 | 9923.88955... |
| 614 | sage: A.condition(p='frob') |
| 615 | 9923.88955... |
| 616 | sage: A.condition(p=Infinity) |
| 617 | 22738.5 |
| 618 | sage: A.condition(p=-Infinity) |
| 619 | 17.5 |
| 620 | sage: A.condition(p=1) |
| 621 | 12139.21... |
| 622 | sage: A.condition(p=-1) |
| 623 | 550.0 |
| 624 | sage: A.condition(p=2) |
| 625 | 9897.8088... |
| 626 | sage: A.condition(p=-2) |
| 627 | 0.000101032462... |
| 628 | |
| 629 | And over the complex numbers. :: |
| 630 | |
| 631 | sage: B = matrix(CDF, 3, [x + x^2*I for x in range(9)]); B |
| 632 | [ 0 1.0 + 1.0*I 2.0 + 4.0*I] |
| 633 | [ 3.0 + 9.0*I 4.0 + 16.0*I 5.0 + 25.0*I] |
| 634 | [6.0 + 36.0*I 7.0 + 49.0*I 8.0 + 64.0*I] |
| 635 | sage: B.condition() |
| 636 | 203.851798... |
| 637 | sage: B.condition(p='frob') |
| 638 | 203.851798... |
| 639 | sage: B.condition(p=Infinity) |
| 640 | 369.55630... |
| 641 | sage: B.condition(p=-Infinity) |
| 642 | 5.46112969... |
| 643 | sage: B.condition(p=1) |
| 644 | 289.251481... |
| 645 | sage: B.condition(p=-1) |
| 646 | 20.4566639... |
| 647 | sage: B.condition(p=2) |
| 648 | 202.653543... |
| 649 | sage: B.condition(p=-2) |
| 650 | 0.00493453005... |
| 651 | |
| 652 | Hilbert matrices are famously ill-conditioned, while |
| 653 | an identity matrix can hit the minimum with the right norm. :: |
| 654 | |
| 655 | sage: A = matrix(RDF, 10, [1/(i+j+1) for i in range(10) for j in range(10)]) |
| 656 | sage: A.condition() |
| 657 | 1.63346888329e+13 |
| 658 | sage: id = identity_matrix(CDF, 10) |
| 659 | sage: id.condition(p=1) |
| 660 | 1.0 |
| 661 | |
| 662 | Return values are in `RDF`. :: |
| 663 | |
| 664 | sage: A = matrix(CDF, 2, range(1,5)) |
| 665 | sage: A.condition() in RDF |
| 666 | True |
| 667 | |
| 668 | Rectangular and singular matrices raise errors. :: |
| 669 | |
| 670 | sage: A = matrix(RDF, 2, 3, range(6)) |
| 671 | sage: A.condition() |
| 672 | Traceback (most recent call last): |
| 673 | ... |
| 674 | TypeError: matrix must be square, not 2 x 3 |
| 675 | |
| 676 | sage: A = matrix(QQ, 5, range(25)) |
| 677 | sage: A.is_singular() |
| 678 | True |
| 679 | sage: B = A.change_ring(CDF) |
| 680 | sage: B.condition() |
| 681 | Traceback (most recent call last): |
| 682 | ... |
| 683 | LinAlgError: Singular matrix |
| 684 | |
| 685 | Improper values of ``p`` are caught. :: |
| 686 | |
| 687 | sage: A = matrix(CDF, 2, range(1,5)) |
| 688 | sage: A.condition(p='bogus') |
| 689 | Traceback (most recent call last): |
| 690 | ... |
| 691 | ValueError: condition number 'p' must be +/- infinity, 'frob' or an integer, not bogus |
| 692 | sage: A.condition(p=632) |
| 693 | Traceback (most recent call last): |
| 694 | ... |
| 695 | ValueError: condition number integer values of 'p' must be -2, -1, 1 or 2, not 632 |
| 696 | """ |
| 697 | if not self.is_square(): |
| 698 | raise TypeError("matrix must be square, not %s x %s" % (self.nrows(), self.ncols())) |
| 699 | global numpy |
| 700 | if numpy is None: |
| 701 | import numpy |
| 702 | import sage.rings.infinity |
| 703 | import sage.rings.integer |
| 704 | import sage.rings.real_double |
| 705 | if p == sage.rings.infinity.Infinity: |
| 706 | p = numpy.inf |
| 707 | elif p == -sage.rings.infinity.Infinity: |
| 708 | p = -numpy.inf |
| 709 | elif p == 'frob': |
| 710 | p = 'fro' |
| 711 | else: |
| 712 | try: |
| 713 | p = sage.rings.integer.Integer(p) |
| 714 | except: |
| 715 | raise ValueError("condition number 'p' must be +/- infinity, 'frob' or an integer, not %s" % p) |
| 716 | if not p in [-2,-1,1,2]: |
| 717 | raise ValueError("condition number integer values of 'p' must be -2, -1, 1 or 2, not %s" % p) |
| 718 | # may raise a LinAlgError if matrix is singular |
| 719 | c = numpy.linalg.cond(self._matrix_numpy, p=p) |
| 720 | if c == numpy.inf: |
| 721 | return sage.rings.infinity.Infinity |
| 722 | else: |
| 723 | return sage.rings.real_double.RDF(c) |
| 724 | |
| 725 | def norm(self, p='frob'): |
| 726 | r""" |
| 727 | Returns the norm of the matrix. |
| 728 | |
| 729 | INPUT: |
| 730 | |
| 731 | - ``p`` - default: 'frob' - controls which norm is computed, |
| 732 | allowable values are 'frob' (for the Frobenius norm), |
| 733 | integers -2, -1, 1, 2, positive and negative infinity. |
| 734 | See output discussion for specifics. |
| 735 | |
| 736 | OUTPUT: |
| 737 | |
| 738 | Returned value is a double precision floating point value |
| 739 | in ``RDF``. Row and column sums described below are |
| 740 | sums of the absolute values of the entries, where the |
| 741 | absolute value of the complex number `a+bi` is `\sqrt{a^2+b^2}`. |
| 742 | Singular values are the "diagonal" entries of the "S" matrix in |
| 743 | the singular value decomposition. |
| 744 | |
| 745 | - ``p = 'frob'``: the default, the Frobenius norm, which for |
| 746 | a matrix `A=(a_{ij})` computes |
| 747 | |
| 748 | .. math:: |
| 749 | |
| 750 | \left(\sum_{i,j}\left\lvert{a_{i,j}}\right\rvert^2\right)^{1/2} |
| 751 | |
| 752 | - ``p = Infinity`` or ``p = oo``: the maximum row sum. |
| 753 | - ``p = -Infinity`` or ``p = -oo``: the minimum colum sum. |
| 754 | - ``p = 1``: the maximum column sum. |
| 755 | - ``p = -1``: the minimum column sum. |
| 756 | - ``p = 2``: the 2-norm, equal to the maximum singular value. |
| 757 | - ``p = -2``: the minimum singular value. |
| 758 | |
| 759 | ALGORITHM: |
| 760 | |
| 761 | Computation is performed by the ``norm()`` function of |
| 762 | the SciPy/NumPy library. |
| 763 | |
| 764 | EXAMPLES: |
| 765 | |
| 766 | First over the reals. :: |
| 767 | |
| 768 | sage: A = matrix(RDF, 3, range(-3, 6)); A |
| 769 | [-3.0 -2.0 -1.0] |
| 770 | [ 0.0 1.0 2.0] |
| 771 | [ 3.0 4.0 5.0] |
| 772 | sage: A.norm() |
| 773 | 8.30662386... |
| 774 | sage: A.norm(p='frob') |
| 775 | 8.30662386... |
| 776 | sage: A.norm(p=Infinity) |
| 777 | 12.0 |
| 778 | sage: A.norm(p=-Infinity) |
| 779 | 3.0 |
| 780 | sage: A.norm(p=1) |
| 781 | 8.0 |
| 782 | sage: A.norm(p=-1) |
| 783 | 6.0 |
| 784 | sage: A.norm(p=2) |
| 785 | 7.99575670... |
| 786 | sage: A.norm(p=-2) |
| 787 | 3.84592537...e-16 |
| 788 | |
| 789 | And over the complex numbers. :: |
| 790 | |
| 791 | sage: B = matrix(CDF, 2, [[1+I, 2+3*I],[3+4*I,3*I]]); B |
| 792 | [1.0 + 1.0*I 2.0 + 3.0*I] |
| 793 | [3.0 + 4.0*I 3.0*I] |
| 794 | sage: B.norm() |
| 795 | 7.0 |
| 796 | sage: B.norm(p='frob') |
| 797 | 7.0 |
| 798 | sage: B.norm(p=Infinity) |
| 799 | 8.0 |
| 800 | sage: B.norm(p=-Infinity) |
| 801 | 5.01976483... |
| 802 | sage: B.norm(p=1) |
| 803 | 6.60555127... |
| 804 | sage: B.norm(p=-1) |
| 805 | 6.41421356... |
| 806 | sage: B.norm(p=2) |
| 807 | 6.66189877... |
| 808 | sage: B.norm(p=-2) |
| 809 | 2.14921023... |
| 810 | |
| 811 | Since it is invariant under unitary multiplication, the |
| 812 | Frobenius norm is equal to the square root of the sum of |
| 813 | squares of the singular values. :: |
| 814 | |
| 815 | sage: A = matrix(RDF, 5, range(1,26)) |
| 816 | sage: f = A.norm(p='frob') |
| 817 | sage: U, S, V = A.SVD() |
| 818 | sage: s = sqrt(sum([S[i,i]^2 for i in range(5)])) |
| 819 | sage: abs(f-s) < 1.0e-12 |
| 820 | True |
| 821 | |
| 822 | Return values are in `RDF`. |
| 823 | |
| 824 | sage: A = matrix(CDF, 2, range(4)) |
| 825 | sage: A.norm() in RDF |
| 826 | True |
| 827 | |
| 828 | Improper values of ``p`` are caught. :: |
| 829 | |
| 830 | sage: A.norm(p='bogus') |
| 831 | Traceback (most recent call last): |
| 832 | ... |
| 833 | ValueError: matrix norm 'p' must be +/- infinity, 'frob' or an integer, not bogus |
| 834 | sage: A.norm(p=632) |
| 835 | Traceback (most recent call last): |
| 836 | ... |
| 837 | ValueError: matrix norm integer values of 'p' must be -2, -1, 1 or 2, not 632 |
| 838 | """ |
| 839 | global numpy |
| 840 | if numpy is None: |
| 841 | import numpy |
| 842 | import sage.rings.infinity |
| 843 | import sage.rings.integer |
| 844 | import sage.rings.real_double |
| 845 | if p == sage.rings.infinity.Infinity: |
| 846 | p = numpy.inf |
| 847 | elif p == -sage.rings.infinity.Infinity: |
| 848 | p = -numpy.inf |
| 849 | elif p == 'frob': |
| 850 | p = 'fro' |
| 851 | else: |
| 852 | try: |
| 853 | p = sage.rings.integer.Integer(p) |
| 854 | except: |
| 855 | raise ValueError("matrix norm 'p' must be +/- infinity, 'frob' or an integer, not %s" % p) |
| 856 | if not p in [-2,-1,1,2]: |
| 857 | raise ValueError("matrix norm integer values of 'p' must be -2, -1, 1 or 2, not %s" % p) |
| 858 | return sage.rings.real_double.RDF(numpy.linalg.norm(self._matrix_numpy, ord=p)) |
| 859 | |