Ticket #13678: 13678matrixmethods_vb.patch
File 13678matrixmethods_vb.patch, 10.3 KB (added by , 9 years ago) 


sage/matrix/constructor.py
# HG changeset patch # User Robert Bradshaw <robertwb@math.washington.edu> # Date 1351720134 25200 # Node ID d7dba156b1c8ffe5460fa4c59fb7614ff1053b16 # Parent 4b00f434bdd1bcfcb00c1cb8738ccaf866b4aa91 Add identity, random, etc. methods to matrix constructor object. diff git a/sage/matrix/constructor.py b/sage/matrix/constructor.py
a b 31 31 32 32 import sage.categories.pushout 33 33 34 def matrix (*args, **kwds):34 def matrix_method(*function, **kwds): 35 35 """ 36 Create a matrix. 37 38 INPUT: The matrix command takes the entries of a matrix, optionally 36 Allows a function to be tabcompleted on the global matrix 37 constructor object. 38 39 INPUT: 40 41  ``*function``  a single argument. The function that is being 42 decorated. 43 44  ``**kwds``  a single optional keyword argument 45 ``name=<string>``. The name of the corresponding method in the 46 global matrix constructor object. If not given, it is derived 47 from the function name. 48 49 EXAMPLES:: 50 51 sage: from sage.matrix.constructor import matrix_method 52 sage: def foo_matrix(n): return matrix.diagonal(range(n)) 53 sage: matrix_method(foo_matrix) 54 <function foo_matrix at ...> 55 sage: matrix.foo(5) 56 [0 0 0 0 0] 57 [0 1 0 0 0] 58 [0 0 2 0 0] 59 [0 0 0 3 0] 60 [0 0 0 0 4] 61 sage: matrix_method(foo_matrix, name='bar') 62 <function foo_matrix at ...> 63 sage: matrix.bar(3) 64 [0 0 0] 65 [0 1 0] 66 [0 0 2] 67 """ 68 name = kwds.get('name', None) 69 if function: 70 func = function[0] 71 if name is None: 72 name = func.__name__.replace('matrix', '').strip('_') 73 setattr(matrix, name, func) 74 return func 75 else: 76 return lambda func:matrix_method(func, name=name) 77 78 79 def _matrix_constructor(*args, **kwds): 80 """ 81 Create a matrix. 82 83 This is the class of the ``matrix`` callable object:: 84 85 sage: matrix([[1,2],[3,4]]) 86 [1 2] 87 [3 4] 88 89 It also contains methods to create special types of matrices, see 90 ``matrix.[tab]`` for more options. For example:: 91 92 sage: matrix.identity(2) 93 [1 0] 94 [0 1] 95 96 INPUT: 97 98 The matrix command takes the entries of a matrix, optionally 39 99 preceded by a ring and the dimensions of the matrix, and returns a 40 100 matrix. 41 101 … … 668 728 return matrix_space.MatrixSpace(ring, nrows, ncols, sparse=sparse)(entries) 669 729 670 730 731 class MatrixFactory(object): 732 """ 733 The class of the ``matrix`` object. 734 735 See :func:`_matrix_constructor` for the implementation of the call 736 interface. 737 738 EXAMPLES:: 739 740 sage: from sage.misc.sageinspect import sage_getdoc, sage_getsource 741 sage: sage_getdoc(matrix) # used in output of matrix? 742 ' Create a matrix.\n\n This object implements the "matrix" ...' 743 sage: sage_getsource(matrix) # used in output of matrix?? 744 'class MatrixFactory(object):...' 745 """ 746 747 __doc__ = _matrix_constructor.__doc__ 748 749 def __call__(self, *args, **kwds): 750 """ 751 This method is called by ``matrix()`` 752 """ 753 return _matrix_constructor(*args, **kwds) 754 755 matrix = MatrixFactory() 756 Matrix = matrix 757 671 758 672 759 def prepare(w): 673 760 """ … … 799 886 return 0 800 887 return max([0] + [ij[1] for ij in d.keys()]) + 1 801 888 802 Matrix = matrix 803 889 890 @matrix_method 804 891 def column_matrix(*args, **kwds): 805 892 r""" 806 893 Constructs a matrix, and then swaps rows for columns and columns for rows. 807 894 808 895 .. note:: 809 896 810 Linear algebra in Sage favors rows over columns. So, generally, 811 when creating a matrix, input vectors and lists are treated as rows. 812 This function is a convenience that turns around this convention 813 when creating a matrix. If you are not familiar with the usual 814 :func:`matrix` constructor, you might want to consider it first. 897 Linear algebra in Sage favors rows over columns. So, 898 generally, when creating a matrix, input vectors and lists are 899 treated as rows. This function is a convenience that turns 900 around this convention when creating a matrix. If you are not 901 familiar with the usual :class:`matrix <MatrixFactory>` 902 constructor, you might want to consider it first. 815 903 816 904 INPUT: 817 905 818 Inputs are almost exactly the same as for the : func:`matrix`819 constructor, which are documented there. But see examples below820 for how dimensions are handled.906 Inputs are almost exactly the same as for the :class:`matrix 907 <MatrixFactory>` constructor, which are documented there. But see 908 examples below for how dimensions are handled. 821 909 822 910 OUTPUT: 823 911 824 Output is exactly the transpose of what the : func:`matrix`825 constructor would return. In other words, the ``matrix``826 constructor builds a matrix and then this function exchanges827 rows for columns, and columns for rows.912 Output is exactly the transpose of what the :class:`matrix 913 <MatrixFactory>` constructor would return. In other words, the 914 ``matrix`` constructor builds a matrix and then this function 915 exchanges rows for columns, and columns for rows. 828 916 829 917 EXAMPLES: 830 918 831 The most compelling use of this function is when you have 832 a collection of lists or vectors that you would like to833 become the columns of a matrix. In almost any other834 situation, the :func:`matrix` constructor can probably do835 thejob just as easily, or easier. ::919 The most compelling use of this function is when you have a 920 collection of lists or vectors that you would like to become the 921 columns of a matrix. In almost any other situation, the 922 :class:`matrix <MatrixFactory>` constructor can probably do the 923 job just as easily, or easier. :: 836 924 837 925 sage: col_1 = [1,2,3] 838 926 sage: col_2 = [4,5,6] … … 886 974 return matrix(*args, **kwds).transpose() 887 975 888 976 977 @matrix_method 889 978 def random_matrix(ring, nrows, ncols=None, algorithm='randomize', *args, **kwds): 890 979 r""" 891 980 Return a random matrix with entries in a specified ring, and possibly with additional properties. … … 1262 1351 raise ValueError('random matrix algorithm "%s" is not recognized' % algorithm) 1263 1352 1264 1353 1354 @matrix_method 1265 1355 def diagonal_matrix(arg0=None, arg1=None, arg2=None, sparse=True): 1266 1356 r""" 1267 1357 Return a square matrix with specified diagonal entries, and zeros elsewhere. … … 1481 1571 return matrix(ring, nrows, nrows, w, sparse=sparse) 1482 1572 1483 1573 1574 @matrix_method 1484 1575 def identity_matrix(ring, n=0, sparse=False): 1485 1576 r""" 1486 1577 Return the `n \times n` identity matrix over the given … … 1517 1608 return matrix_space.MatrixSpace(ring, n, n, sparse)(1) 1518 1609 1519 1610 1611 @matrix_method 1520 1612 def zero_matrix(ring, nrows, ncols=None, sparse=False): 1521 1613 r""" 1522 1614 Return the `nrows \times ncols` zero matrix over the given … … 1552 1644 ring = rings.ZZ 1553 1645 return matrix_space.MatrixSpace(ring, nrows, ncols, sparse)(0) 1554 1646 1647 1648 @matrix_method 1555 1649 def ones_matrix(ring, nrows=None, ncols=None, sparse=False): 1556 1650 r""" 1557 1651 Return a matrix with all entries equal to 1. … … 1643 1737 one = ring(1) 1644 1738 return matrix_space.MatrixSpace(ring, nrows, ncols, sparse).matrix([one]*nents) 1645 1739 1740 1741 @matrix_method 1646 1742 def elementary_matrix(arg0, arg1=None, **kwds): 1647 1743 r""" 1648 1744 Creates a square matrix that corresponds to a row operation or a column operation. … … 2247 2343 # If we got this far, then everything fits 2248 2344 return (row_heights, zero_widths, total_width) 2249 2345 2346 2347 @matrix_method 2250 2348 def block_matrix(*args, **kwds): 2251 2349 r""" 2252 2350 Returns a larger matrix made by concatenating submatrices … … 2620 2718 2621 2719 return big 2622 2720 2721 2722 @matrix_method 2623 2723 def block_diagonal_matrix(*sub_matrices, **kwds): 2624 2724 """ 2625 2725 Create a block matrix whose diagonal block entries are given by … … 2655 2755 entries[n*i+i] = sub_matrices[i] 2656 2756 return block_matrix(n, n, entries, **kwds) 2657 2757 2758 2759 @matrix_method 2658 2760 def jordan_block(eigenvalue, size, sparse=False): 2659 2761 r""" 2660 2762 Returns the Jordan block for the given eigenvalue with given size. … … 2697 2799 block[i,i+1]=1 2698 2800 return block 2699 2801 2802 2803 @matrix_method 2700 2804 def companion_matrix(poly, format='right'): 2701 2805 r""" 2702 2806 Create a companion matrix from a monic polynomial. … … 2893 2997 raise TypeError("unable to find common ring for coefficients from polynomial") 2894 2998 return M 2895 2999 3000 @matrix_method 2896 3001 def random_rref_matrix(parent, num_pivots): 2897 3002 r""" 2898 3003 Generate a matrix in reduced rowechelon form with a specified number of nonzero rows. … … 3058 3163 return_matrix[rest_entries,rest_non_pivot_column]=ring.random_element() 3059 3164 return return_matrix 3060 3165 3166 @matrix_method 3061 3167 def random_echelonizable_matrix(parent, rank, upper_bound=None): 3062 3168 r""" 3063 3169 Generate a matrix of a desired size and rank, over a desired ring, whose reduced … … 3246 3352 matrix.add_multiple_of_row(0,randint(1,rows1),ring.random_element()) 3247 3353 return matrix 3248 3354 3355 @matrix_method 3249 3356 def random_subspaces_matrix(parent, rank=None): 3250 3357 r""" 3251 3358 Create a matrix of the designated size and rank whose right and … … 3427 3534 # K matrix to the identity matrix. 3428 3535 return J.inverse()*B 3429 3536 3537 @matrix_method 3430 3538 def random_unimodular_matrix(parent, upper_bound=None): 3431 3539 """ 3432 3540 Generate a random unimodular (determinant 1) matrix of a desired size over a desired ring. … … 3526 3634 return random_matrix(ring, size,algorithm='echelonizable',rank=size, upper_bound=upper_bound) 3527 3635 3528 3636 3637 @matrix_method 3529 3638 def random_diagonalizable_matrix(parent,eigenvalues=None,dimensions=None): 3530 3639 """ 3531 3640 Create a random matrix that diagonalizes nicely. … … 3772 3881 eigenvector_matrix.add_multiple_of_row(upper_row,row,randint(4,4)) 3773 3882 return eigenvector_matrix*diagonal_matrix*(eigenvector_matrix.inverse()) 3774 3883 3884 3885 @matrix_method 3775 3886 def vector_on_axis_rotation_matrix(v, i, ring=None): 3776 3887 r""" 3777 3888 Return a rotation matrix `M` such that `det(M)=1` sending the vector … … 3845 3956 m = rot * m 3846 3957 return m 3847 3958 3959 3960 @matrix_method 3848 3961 def ith_to_zero_rotation_matrix(v, i, ring=None): 3849 3962 r""" 3850 3963 Return a rotation matrix that sends the ith coordinates of the