Ticket #9803: trac_9803-random-matrix-constructor-v1.patch

File trac_9803-random-matrix-constructor-v1.patch, 29.3 KB (added by rbeezer, 3 years ago)
  • sage/groups/perm_gps/partn_ref/refinement_binary.pyx

    # HG changeset patch
    # User Rob Beezer <beezer@ups.edu>
    # Date 1282860848 25200
    # Node ID 0bc3f6a4dab97bf7fbc995cb7592894e6af65385
    # Parent  422d1a1eefa543d00a46a9bca9cc8376e1202a8f
    #9803: expand random_matrix() constructor
    
    diff -r 422d1a1eefa5 -r 0bc3f6a4dab9 sage/groups/perm_gps/partn_ref/refinement_binary.pyx
    a b  
    11091109        nwords = randint(1, min(n-1,nwords_max) ) 
    11101110        S = Permutations(n) 
    11111111 
    1112         M = random_matrix(GF(2), k, n, False, p).row_space().basis_matrix() 
    1113         M_n = random_matrix(GF(2), nwords, n, False, p) 
     1112        M = random_matrix(GF(2), k, n, sparse=False, density=p).row_space().basis_matrix() 
     1113        M_n = random_matrix(GF(2), nwords, n, sparse=False, density=p) 
    11141114        B = LinearBinaryCodeStruct( M ) 
    11151115        B_n = NonlinearBinaryCodeStruct( M_n ) 
    11161116        B.run() 
  • sage/groups/perm_gps/partn_ref/refinement_matrices.pyx

    diff -r 422d1a1eefa5 -r 0bc3f6a4dab9 sage/groups/perm_gps/partn_ref/refinement_matrices.pyx
    a b  
    351351        ncols = randint(1, ncols_max) 
    352352        nsymbols = next_prime(randint(1, nsymbols_max)) 
    353353        S = Permutations(ncols) 
    354         MM = random_matrix(GF(nsymbols), nrows, ncols, False, p) 
     354        MM = random_matrix(GF(nsymbols), nrows, ncols, sparse=False, density=p) 
    355355        M = MatrixStruct( MM ) 
    356356        M.run() 
    357357 
  • sage/matrix/constructor.py

    diff -r 422d1a1eefa5 -r 0bc3f6a4dab9 sage/matrix/constructor.py
    a b  
    796796Matrix = matrix 
    797797 
    798798 
    799 def random_matrix(R, nrows, ncols=None, sparse=False, density=1, \ 
    800                   *args, **kwds): 
    801     """ 
    802     Return a random matrix with entries in the ring ``R``. 
    803      
     799def random_matrix(ring, nrows, ncols=None, algorithm='randomize', *args, **kwds): 
     800    r""" 
     801    Return a random matrix with entries in a specified ring, and possibly with additional properties. 
     802 
    804803    INPUT: 
    805      
    806     -  ``R`` - Ring 
    807      
     804 
     805    -  ``ring`` - base ring for entries of the matrix 
     806 
    808807    -  ``nrows`` - Integer; number of rows 
    809      
     808 
    810809    -  ``ncols`` - (default: ``None``); number of columns; if ``None``  
    811810       defaults to ``nrows`` 
    812      
    813     -  ``sparse`` - (default: ``False``); whether or not matrix is sparse 
    814      
    815     -  ``density`` - Integer (default: 1) 
    816      
    817     -  ``*args, **kwds`` - passed on to randomize function 
    818      
    819     EXAMPLES:: 
    820      
    821         sage: A = random_matrix(ZZ,50,x=2^16)    # entries are up to 2^16 i size 
    822         sage: A 
    823         50 x 50 dense matrix over Integer Ring (type 'print A.str()' to see all of the entries) 
     811 
     812    -  ``algorithm`` - (default: ``randomize``); determines what properties 
     813       the matrix will have.  See examples below for possible additional 
     814       arguments. 
     815 
     816       -  ``randomize`` - randomize the elements of the matrix, possibly 
     817          controlling the density of non-zero entries. 
     818 
     819       -  ``echelon_form`` - creates a matrix in echelon form 
     820 
     821       -  ``echelonizable`` - creates a matrix that has a predictable echelon form 
     822 
     823    -  ``*args, **kwds`` - arguments and keywords to describe additional properties. 
     824       See more detailed documentation below. 
     825 
     826    .. note:: 
     827 
     828        When constructing matrices with random entries and no additional properties 
     829        (i.e. when ``algorithm='randomize'``), most of the randomness 
     830        is controlled by the ``random_element`` method for elements of the 
     831        base ring of the matrix, so the documentation of that method may be 
     832        relevant or useful.  Also, the default is to not create zero entries, 
     833        unless the ``density`` keyword is set to something strictly less 
     834        than one. 
     835 
     836    EXAMPLES: 
     837 
     838    Random integer matrices.  With no arguments, the majority of the entries   
     839    are -1 and 1, never zero, and rarely "large." :: 
     840 
     841        sage: random_matrix(ZZ, 5, 5) 
     842        [ -8   2   1  -1   2] 
     843        [  1 -95  -1  -2 -12] 
     844        [  1  -1   1  -1  -2] 
     845        [ -1   4  -4  -6   5] 
     846        [ -2   1  -4  -6   1] 
     847 
     848    The ``distribution`` keyword  set to ``uniform`` will limit values 
     849    between -2 and 2, and never zero. :: 
     850 
     851        sage: random_matrix(ZZ, 5, 5, distribution='uniform') 
     852        [ 1  1  2 -1 -1] 
     853        [-1 -1  1  1 -2] 
     854        [ 1  2  1 -1  2] 
     855        [-2  1 -2  1  2] 
     856        [-2 -2  1 -1  2] 
     857 
     858    The ``x`` and ``y`` keywords can be used to distribute entries uniformly. 
     859    When both are used ``x`` is the minimum and ``y`` is one greater than the the maximum. 
     860    But still entries are never zero, even if the range contains zero. :: 
     861 
     862        sage: random_matrix(ZZ, 4, 8, x=70, y=100) 
     863        [89 86 77 86 89 85 92 95] 
     864        [94 72 89 78 80 89 82 94] 
     865        [72 90 92 72 82 94 92 70] 
     866        [78 76 93 81 73 76 75 94] 
     867 
     868        sage: random_matrix(ZZ, 3, 7, x=-5, y=5) 
     869        [-5 -2  1 -2 -2  2 -3] 
     870        [-4 -2 -1 -5  3 -2  3] 
     871        [ 3 -3 -3 -5 -3 -3 -1] 
     872 
     873    If only ``x`` is given, then it is used as the upper bound of a range starting at 0. :: 
     874 
     875        sage: random_matrix(ZZ, 5, 5, x=25) 
     876        [11 19 16 17 15] 
     877        [ 7 24  3 17 24] 
     878        [ 9  4 23 10 24] 
     879        [19 17 12 10 14] 
     880        [ 8  4  8 19 14] 
     881 
     882    To allow, and control, zero entries use the ``density`` keyword at a value 
     883    strictly below the default of 1.0, even if distributing entries across an 
     884    interval that does not contain zero already.  Note that for a square matrix it 
     885    is only necessary to set a single dimension. :: 
     886 
     887        sage: random_matrix(ZZ, 5, x=-10, y=10, density=0.75) 
     888        [  1   0   0   6   0] 
     889        [  0  -9   0   0  -8] 
     890        [ -5   0  -1 -10   0] 
     891        [  0  -9   0   7   0] 
     892        [  0  -2  -8   0   0] 
     893 
     894        sage: random_matrix(ZZ, 5, x=20, y=30, density=0.75) 
     895        [ 0  0  0 26 24] 
     896        [ 0 22 26  0 24] 
     897        [ 0 24  0 22 21] 
     898        [ 0  0  0 28 29] 
     899        [20  0  0 28  0] 
     900 
     901    It is possible to construct sparse matrices, where it may now be advisable 
     902    (but not required) to control the density of nonzero entries. :: 
     903 
     904        sage: A=random_matrix(ZZ, 5, 5) 
     905        sage: A.is_sparse() 
     906        False 
     907        sage: A=random_matrix(ZZ, 5, 5, sparse=True) 
     908        sage: A.is_sparse() 
     909        True 
     910 
     911        sage: random_matrix(ZZ, 5, 5, density=0.3, sparse=True) 
     912        [ -4   0 240   0   1] 
     913        [  0   0 -16   0   0] 
     914        [  0   0   0   0   1] 
     915        [  0   0   0   0   0] 
     916        [  0  28   0   0   0] 
     917 
     918    For algorithm testing you might want to control the number of bits, 
     919    say 10,000 entries, each limited to 16 bits.  :: 
     920 
     921        sage: A = random_matrix(ZZ, 100, 100, x=2^16); A 
     922        100 x 100 dense matrix over Integer Ring (type 'print A.str()' to see all of the entries) 
     923 
     924    Random rational matrices.  Now ``num_bound`` and ``den_bound`` control the 
     925    generation of random elements, by specifying limits on the absolute value of 
     926    numerators and denominators (respectively).  Entries will be positive and 
     927    negative (map the absolute value function through the entries to get all 
     928    positive values), and zeros are avoided unless the density is set.  If either 
     929    the numerator or denominator bound (or both) is not used, then the values 
     930    default to the distribution for `ZZ` described above that is most frequently 
     931    positive or negative one. :: 
     932 
     933        sage: random_matrix(QQ, 2, 8, num_bound=20, den_bound=4) 
     934        [ -1/2    16    -5   -20   -11  -7/3   1/2     6] 
     935        [ -9/2 -11/4  -1/2   1/4     8    10    -6   7/4] 
     936 
     937        sage: random_matrix(QQ, 4, density = 0.5, sparse=True) 
     938        [   0    0    0    0] 
     939        [   0   -1    1  -13] 
     940        [  -2    0  1/4    0] 
     941        [  -1    0    1 -1/3] 
     942 
     943        sage: A = random_matrix(QQ, 3, 10, num_bound = 99, den_bound = 99) 
     944        sage: positives = map(abs, A.list()) 
     945        sage: matrix(QQ, 3, 10, positives) 
     946        [ 7/24  12/5  13/8  8/25   1/3 61/14 92/45  4/85  3/38 95/16] 
     947        [82/71   1/5 41/16 55/76    19 28/41 52/51  14/3    43 76/13] 
     948        [ 8/77 13/38 37/21 17/15 60/61 85/44 75/97 31/55     4  49/8] 
     949 
     950        sage: random_matrix(QQ, 4, 10, den_bound = 10) 
     951        [-1/9    2  2/3    2  1/8   -2   -2    2 -1/2    2] 
     952        [   1 -2/3  1/6 -1/3 -2/9  2/5  1/9  1/6 1/10    1] 
     953        [  -1 -1/2  1/3  1/4 -1/6  1/8  1/8 -1/9  1/5  1/7] 
     954        [ 2/7    2 -1/6 -2/5  1/9 -1/4 -1/5 -1/4 -2/7 -1/3] 
     955 
     956    Random matrices over other rings.  Several classes of matrices have specialized 
     957    ``randomize()`` methods.  You can locate these with the Sage command:: 
     958 
     959        search_def('randomize') 
     960 
     961    The default implementation of :meth:`~sage.matrix.matrix2.randomize` relies 
     962    on the ``random_element()`` method for the base ring.  The ``density`` and 
     963    ``sparse`` keywords behave as described above. :: 
     964 
     965        sage: K.<a>=FiniteField(3^2) 
     966        sage: random_matrix(K, 2, 5) 
     967        [    2*a 2*a + 1   a + 2 2*a + 2   a + 2] 
     968        [  a + 1   a + 1       2 2*a + 2   a + 1] 
     969 
     970        sage: random_matrix(RR, 3, 4, density=0.66) 
     971        [  0.000000000000000   0.000000000000000   0.000000000000000   0.000000000000000] 
     972        [  0.000000000000000   0.000000000000000   0.000000000000000   0.839885947013929] 
     973        [0.00970128228876743  -0.711107146185354   0.000000000000000   0.582088854951784] 
     974 
     975        sage: A = random_matrix(ComplexField(32), 3, density=0.8, sparse=True); A 
     976        [ 0.595498794 + 0.570429906*I -0.306520063 - 0.574051516*I                            0] 
     977        [-0.307727175 - 0.941724613*I -0.127237880 - 0.756685386*I                            0] 
     978        [                           0 -0.843041497 - 0.885579092*I -0.247313397 + 0.624939327*I] 
     979        sage: A.is_sparse() 
     980        True 
     981 
     982    Random matrices in echelon form.  The ``algorithm='echelon_form'`` keyword, 
     983    along with a requested number of non-zero rows (``num_pivots``) will return 
     984    a random matrix in echelon form.  When the base ring is ``QQ`` the result has integer 
     985    entries.  Other exact rings may be also specified. :: 
     986 
     987        sage: A=random_matrix(QQ, 4, 8, algorithm='echelon_form', num_pivots=3); A # random 
     988        [ 1 -5  0 -2  0  1  1 -2] 
     989        [ 0  0  1 -5  0 -3 -1  0] 
     990        [ 0  0  0  0  1  2 -2  1] 
     991        [ 0  0  0  0  0  0  0  0] 
     992        sage: A.base_ring() 
     993        Rational Field 
     994        sage: (A.nrows(), A.ncols()) 
     995        (4, 8) 
     996        sage: A in sage.matrix.matrix_space.MatrixSpace(ZZ, 4, 8) 
     997        True 
     998        sage: A.rank() 
     999        3 
     1000        sage: A==A.rref() 
     1001        True 
     1002 
     1003    For more, see the documentation of the :func:`~sage.matrix.constructor.random_rref_matrix` 
     1004    function.  In the notebook or at the Sage command-line, first execute the following to make 
     1005    this further documentation available:: 
     1006 
     1007        from sage.matrix.constructor import random_rref_matrix 
     1008 
     1009    Random matrices with predictable echelon forms.  The ``algorithm='echelonizable'`` 
     1010    keyword, along with a requested rank (``rank``) and optional size control 
     1011    (``upper_bound``) will return a random matrix in echelon form.  When the 
     1012    base ring is ``ZZ`` or ``QQ`` the result has integer entries, whose magnitudes 
     1013    can be limited by the value of ``upper_bound``, and the echelon form of the 
     1014    matrix also has integer entries.  Other exact rings may be also 
     1015    specified, but there is no notion of controlling the size.  Square matrices 
     1016    of full rank generated by this function always have determinant one, and 
     1017    can be constructed with the upcoming ``unimodular`` keyword. :: 
     1018 
     1019        sage: A=random_matrix(QQ, 4, 8, algorithm='echelonizable', rank=3, upper_bound=60); A # random 
     1020        sage: A.base_ring() 
     1021        Rational Field 
     1022        sage: (A.nrows(), A.ncols()) 
     1023        (4, 8) 
     1024        sage: A in sage.matrix.matrix_space.MatrixSpace(ZZ, 4, 8) 
     1025        True 
     1026        sage: A.rank() 
     1027        3 
     1028        sage: all([abs(x)<60 for x in A.list()]) 
     1029        True 
     1030        sage: A.rref() in sage.matrix.matrix_space.MatrixSpace(ZZ, 4, 8) 
     1031        True 
     1032 
     1033    For more, see the documentation of the :func:`~sage.matrix.constructor.random_echelonizable_matrix` 
     1034    function.  In the notebook or at the Sage command-line, first execute the following to make 
     1035    this further documentation available:: 
     1036 
     1037        from sage.matrix.constructor import random_echelonizable_matrix 
     1038 
     1039    TESTS: 
     1040 
     1041    We return an error for a bogus value of ``algorithm``:: 
     1042 
     1043        sage: random_matrix(ZZ, 5, algorithm = 'bogus') 
     1044        Traceback (most recent call last): 
     1045        ... 
     1046        ValueError: random matrix algorithm "bogus" is not recognized 
     1047 
     1048    AUTHOR: 
     1049 
     1050    - William Stein (2007-02-06) 
     1051 
     1052    - Rob Beezer (2010-08-25) Documentation, code to allow additional types of output 
    8241053    """ 
    8251054    if ncols is None: 
    8261055        ncols = nrows 
    827     A = copy(matrix_space.MatrixSpace(R, nrows, ncols, sparse=sparse).zero_matrix()) 
    828     if density is None: 
    829         A.randomize(density=float(1), nonzero=False, *args, **kwds) 
     1056    sparse = kwds.pop('sparse', False) 
     1057    # Construct the parent of the desired matrix 
     1058    parent = matrix_space.MatrixSpace(ring, nrows, ncols, sparse=sparse) 
     1059    if algorithm == 'randomize': 
     1060        density = kwds.pop('density', 1) 
     1061        # zero matrix is immutable, copy is mutable 
     1062        A = copy(parent.zero_matrix()) 
     1063        if density is None: 
     1064            A.randomize(density=float(1), nonzero=False, *args, **kwds) 
     1065        else: 
     1066            A.randomize(density=density, nonzero=True, *args, **kwds) 
     1067        return A 
     1068    elif algorithm == 'echelon_form': 
     1069        return random_rref_matrix(parent, *args, **kwds) 
     1070    elif algorithm == 'echelonizable': 
     1071        return random_echelonizable_matrix(parent, *args, **kwds) 
    8301072    else: 
    831         A.randomize(density=density, nonzero=True, *args, **kwds) 
    832     return A 
     1073        raise ValueError('random matrix algorithm "%s" is not recognized' % algorithm) 
     1074 
    8331075 
    8341076def diagonal_matrix(arg0=None, arg1=None, arg2=None, sparse=None): 
    8351077    """ 
     
    12351477        block[i,i+1]=1 
    12361478    return block 
    12371479 
    1238 def random_rref_matrix(num_row, num_col, num_pivots,ring=QQ): 
     1480def random_rref_matrix(parent, num_pivots): 
    12391481    r""" 
    1240     Generate a matrix already in reduced row-echelon form with the desired row dimension, column dimension, and rank over the designated ring. 
     1482    Generate a matrix in reduced row-echelon form with a specified number of non-zero rows. 
    12411483 
    12421484    INPUT: 
    12431485 
    1244     - ``num_row`` - The number of rows desired for the return matrix. 
     1486    - ``parent`` - A matrix space specifying the base ring, dimensions and 
     1487      representation (dense/sparse) for the result.  The base ring must be exact. 
    12451488 
    1246     - ``num_col`` - The number of columns desired for the return matrix. 
    1247  
    1248     - ``num_pivots`` - The desired rank of the return matrix. 
    1249  
    1250     - ``ring`` - The desired ring for the return matrix to be built over. 
     1489    - ``num_pivots`` - The number of non-zero rows in the result, i.e. the rank. 
    12511490 
    12521491    OUTPUT: 
    12531492 
    1254     A matrix in reduced row echelon form with dimensions ``num_row`` x ``num_col`` and a rank of 
    1255     ``num_pivot``. 
     1493    A matrix in reduced row echelon form with ``num_pivots`` non-zero rows. If the 
     1494    base ring is `ZZ` or `QQ` then the entries are all integers. 
     1495 
     1496    .. note:: 
     1497 
     1498        It is easiest to use this function via a call to the 
     1499        :func:`~sage.matrix.constructor.random_matrix` 
     1500        function with the ``algorithm='echelon_form'`` keyword.  We provide 
     1501        one example accessing this function directly, while the remainder will 
     1502        use this more general function. 
    12561503 
    12571504    EXAMPLES: 
    12581505 
    1259     Matrices generated are in reduced row-echelon form with the default ring of QQ and specified rank. :: 
     1506    Matrices generated are in reduced row-echelon form with specified rank. If the 
     1507    base ring is `QQ` the result has only integer entries.  :: 
    12601508 
    12611509        sage: from sage.matrix.constructor import random_rref_matrix 
    1262         sage: A=random_rref_matrix(5,6,4); A # random 
     1510        sage: matrix_space = sage.matrix.matrix_space.MatrixSpace(QQ, 5, 6) 
     1511        sage: A=random_rref_matrix(matrix_space, num_pivots=4); A # random 
    12631512        [ 1  0  0 -6  0 -3] 
    12641513        [ 0  1  0  2  0  3] 
    12651514        [ 0  0  1 -4  0 -2] 
    12661515        [ 0  0  0  0  1  3] 
    12671516        [ 0  0  0  0  0  0] 
     1517        sage: A.base_ring() 
     1518        Rational Field 
     1519        sage: (A.nrows(), A.ncols()) 
     1520        (5, 6) 
     1521        sage: A in sage.matrix.matrix_space.MatrixSpace(ZZ, 5, 6) 
     1522        True 
    12681523        sage: A.rank() 
    12691524        4 
    1270         sage: A.base_ring() 
    1271         Rational Field 
    12721525        sage: A==A.rref() 
    12731526        True 
    12741527 
    1275     Matrices can be generated over other fields. :: 
     1528    Matrices can be generated over other exact rings. :: 
    12761529 
    1277         sage: B=random_rref_matrix(4,4,3,GF(7)); B # random 
     1530        sage: B=random_matrix(FiniteField(7), 4, 4, algorithm='echelon_form', num_pivots=3); B # random 
    12781531        [1 0 0 0] 
    12791532        [0 1 0 6] 
    12801533        [0 0 1 4] 
    12811534        [0 0 0 0] 
     1535        sage: B.rank() == 3 
     1536        True 
    12821537        sage: B.base_ring() 
    12831538        Finite Field of size 7 
    12841539        sage: B==B.rref() 
     
    12861541 
    12871542    TESTS: 
    12881543 
    1289     Row dimension input for a matrix must be whole valued. :: 
     1544    Rank of a matrix must be an integer. :: 
    12901545 
    1291        sage: random_rref_matrix(66.9,19,12) 
    1292        Traceback (most recent call last): 
    1293        ... 
    1294        TypeError: inputs for matrix dimensions and rank must be integers. 
    1295  
    1296     Column dimension input for a matrix must be whole valued. :: 
    1297  
    1298        sage: random_rref_matrix(31,74.2,6) 
    1299        Traceback (most recent call last): 
    1300        ... 
    1301        TypeError: inputs for matrix dimensions and rank must be integers. 
    1302  
    1303     Rank input of a matrix must be whole valued. :: 
    1304  
    1305         sage: random_rref_matrix(120,56,61/2) 
     1546        sage: random_matrix(QQ, 120, 56, algorithm='echelon_form', num_pivots=61/2) 
    13061547        Traceback (most recent call last): 
    13071548        ... 
    1308         TypeError: inputs for matrix dimensions and rank must be integers. 
     1549        TypeError: the number of pivots must be an integer. 
    13091550 
    13101551    Matrices must be generated over exact fields. :: 
    13111552 
    1312        sage: random_rref_matrix(40,88,39,RR) 
    1313        Traceback (most recent call last): 
    1314        ... 
    1315        TypeError: input ring must be exact. 
     1553        sage: random_matrix(RR, 40, 88, algorithm='echelon_form', num_pivots=39) 
     1554        Traceback (most recent call last): 
     1555        ... 
     1556        TypeError: the base ring must be exact. 
    13161557 
    13171558    Matrices must have the number of pivot columns be less than or equal to the number of rows. :: 
    13181559 
    1319         sage: C=random_rref_matrix(6,4,7); C 
     1560        sage: C=random_matrix(ZZ, 6,4, algorithm='echelon_form', num_pivots=7); C 
    13201561        Traceback (most recent call last): 
    13211562        ... 
    1322         ValueError: number of pivot column cannot exceed the number of rows or columns. 
     1563        ValueError: number of pivots cannot exceed the number of rows or columns. 
    13231564 
    13241565    Matrices must have the number of pivot columns be less than or equal to the number of columns. :: 
    13251566 
    1326         sage: D=random_rref_matrix(1,3,5); D 
     1567        sage: D=random_matrix(QQ, 1,3, algorithm='echelon_form', num_pivots=5); D 
    13271568        Traceback (most recent call last): 
    13281569        ... 
    1329         ValueError: number of pivot column cannot exceed the number of rows or columns. 
     1570        ValueError: number of pivots cannot exceed the number of rows or columns. 
    13301571 
    13311572    Matrices must have the number of pivot columns be greater than zero. :: 
    13321573 
    1333         sage: random_rref_matrix(5,4,0) 
     1574        sage: random_matrix(QQ, 5, 4, algorithm='echelon_form', num_pivots=0) 
    13341575        Traceback (most recent call last): 
    13351576        ... 
    1336         ValueError: matrices must have dimensions and rank greater than zero. 
     1577        ValueError: the number of pivots must be greater than zero. 
    13371578 
    13381579    AUTHOR: 
    13391580 
    13401581    Billy Wonderly (2010-07) 
    1341     """  
     1582    """ 
    13421583 
    13431584    import sage.gsl.probability_distribution as pd 
    13441585    from sage.misc.prandom import randint 
    13451586 
    13461587    try: 
    1347         num_row=ZZ(num_row) 
    1348         num_col=ZZ(num_col) 
    13491588        num_pivots=ZZ(num_pivots) 
    13501589    except TypeError: 
    1351         raise TypeError("inputs for matrix dimensions and rank must be integers.") 
     1590        raise TypeError("the number of pivots must be an integer.") 
     1591    if num_pivots<=0: 
     1592        raise ValueError("the number of pivots must be greater than zero.") 
     1593    ring = parent.base_ring() 
    13521594    if not ring.is_exact(): 
    1353         raise TypeError("input ring must be exact.") 
     1595        raise TypeError("the base ring must be exact.") 
     1596    num_row = parent.nrows() 
     1597    num_col = parent.ncols() 
    13541598    if num_pivots>num_row or num_pivots>num_col: 
    1355         raise ValueError("number of pivot column cannot exceed the number of rows or columns.") 
    1356     if num_pivots<=0: 
    1357         raise ValueError("matrices must have dimensions and rank greater than zero.") 
     1599        raise ValueError("number of pivots cannot exceed the number of rows or columns.") 
    13581600    else: 
    13591601        one=ring.one() 
    13601602        # Create a matrix of the desired size to be modified and then returned. 
    1361         return_matrix=matrix(ring,num_row,num_col)  
     1603        return_matrix=copy(parent.zero_matrix()) 
    13621604        pivots=[0] #Force first column to be a pivot. 
    13631605        # Probability distribution for the placement of leading one's. 
    13641606        pivot_generator=pd.RealDistribution("beta",[1.6,4.3])  
     
    14001642                    return_matrix[rest_entries,rest_non_pivot_column]=ring.random_element() 
    14011643    return return_matrix 
    14021644 
    1403 def random_echelonizable_matrix(rows,columns,rank,upper_bound=None,ring=QQ): 
     1645def random_echelonizable_matrix(parent, rank, upper_bound=None): 
    14041646    r""" 
    14051647    Generate a matrix of a desired size and rank, over a desired ring, whose reduced  
    14061648    row-echelon form has only integral values. 
    14071649 
    14081650    INPUT: 
    14091651 
    1410     - ``rows`` - The row dimension of the desired matrix, taking only integers greater than zero. 
     1652    - ``parent`` - A matrix space specifying the base ring, dimensions and 
     1653      representation (dense/sparse) for the result.  The base ring must be exact. 
    14111654 
    1412     - ``columns`` - The column dimension of the desired matrix, taking only integers greater than zero. 
     1655    - ``rank`` - Rank of result, i.e the number of non-zero rows in the 
     1656      reduced row echelon form. 
    14131657 
    1414     - ``rank`` - The desired rank, or number of leading ones, for the return matrix. 
    1415  
    1416     - ``upper_bound`` - If designated, size control of the matrix entries is desired, ``upper_bound`` is 1 + the maximum value entries can achieve.   
     1658    - ``upper_bound`` - If designated, size control of the matrix entries is desired. 
     1659      Set ``upper_bound`` to 1 more than the maximum value entries can achieve. 
    14171660      If None, no size control occurs. (default: None) 
    14181661 
    1419     - ``ring`` - The desired ring for the matrix to be generated over (default: QQ). 
    1420  
    1421  
    14221662    OUTPUT: 
    14231663 
    14241664    A matrix not in reduced row-echelon form with the desired dimensions and properties. 
    14251665 
     1666    .. note:: 
     1667 
     1668        It is easiest to use this function via a call to the 
     1669        :func:`~sage.matrix.constructor.random_matrix` 
     1670        function with the ``algorithm='echelonizable'`` keyword.  We provide 
     1671        one example accessing this function directly, while the remainder will 
     1672        use this more general function. 
     1673 
    14261674    EXAMPLES: 
    14271675 
    1428     Generated matrices have the desired dimensions, rank and entry size. The matrix in reduced row-echelon form has only integer entries. :: 
     1676    Generated matrices have the desired dimensions, rank and entry size. The 
     1677    matrix in reduced row-echelon form has only integer entries. :: 
    14291678 
    14301679        sage: from sage.matrix.constructor import random_echelonizable_matrix 
    1431         sage: A=random_echelonizable_matrix(5,6,4,upper_bound=40,ring=QQ); A # random 
     1680        sage: matrix_space = sage.matrix.matrix_space.MatrixSpace(QQ, 5, 6) 
     1681        sage: A=random_echelonizable_matrix(matrix_space, rank=4, upper_bound=40); A # random 
    14321682        [  1  -1   1  -3  -4   6] 
    14331683        [  5  -4   0   8   4  19] 
    14341684        [ -3   3  -2   4   7 -16] 
     
    14411691        sage: A.rref()==A.rref().change_ring(ZZ) 
    14421692        True 
    14431693 
    1444     An example with default settings (ring=QQ, no entry size control). :: 
     1694    An example with default settings (i.e. no entry size control). :: 
    14451695 
    1446         sage: C=random_echelonizable_matrix(6,7,5); C # random 
     1696        sage: C=random_matrix(QQ, 6, 7, algorithm='echelonizable', rank=5); C # random 
    14471697        [  1   0   5  -2 -26 -16   0] 
    14481698        [ -3   1 -19   6  97  61   1] 
    14491699        [  0   4 -15  -1  71  50   3] 
     
    14571707 
    14581708    A matrix without size control may have very large entry sizes. :: 
    14591709 
    1460         sage: D=random_echelonizable_matrix(7,8,6,ring=ZZ); D # random 
     1710        sage: D=random_matrix(ZZ, 7, 8, algorithm='echelonizable', rank=6); D # random 
    14611711        [    9   -53  -255    45 -1519  4043  9819  3324] 
    14621712        [    3   -14   -64     8  -369   972  2350   810] 
    14631713        [    2   -14   -65     9  -377  1000  2420   829] 
     
    14661716        [   -5    21    92   -13   548 -1432 -3466 -1183] 
    14671717        [    1    -9   -42     7  -254   670  1624   547] 
    14681718 
    1469     Matrices can be generated over a different ring. :: 
     1719    Matrices can be generated over any exact ring. :: 
    14701720 
    14711721        sage: F.<a>=GF(2^3) 
    1472         sage: B=random_echelonizable_matrix(4,5,4,None,F); B # random 
     1722        sage: B=random_matrix(F, 4, 5, algorithm='echelonizable', rank=4, upper_bound=None); B # random 
    14731723        [      a + 1 a^2 + a + 1         a^2           0           1] 
    14741724        [          1           a       a + 1         a^2     a^2 + a] 
    14751725        [          a         a^2 a^2 + a + 1       a + 1           1] 
     
    14791729 
    14801730    Square matrices over ZZ or QQ with full rank are unimodular. :: 
    14811731 
    1482         sage: E=random_echelonizable_matrix(7,7,7); E # random 
     1732        sage: E=random_matrix(QQ, 7, 7, algorithm='echelonizable', rank=7); E # random 
    14831733        [  1  -1   5  12 -24 -41  47] 
    14841734        [  0   1  -1   3   0 -11  40] 
    14851735        [  1  -1   6   6 -19 -20 -11] 
     
    14921742 
    14931743    TESTS: 
    14941744 
    1495     Matrices must have a row dimension greater than zero. :: 
     1745    Matrices must have a rank greater than zero. 
     1746    (For zero rank, just create a zero matrix.) :: 
    14961747 
    1497         sage: random_echelonizable_matrix(0,5,3) 
     1748        sage: random_matrix(QQ, 3, 4, algorithm='echelonizable', rank=0) 
    14981749        Traceback (most recent call last): 
    14991750        ... 
    1500         ValueError: matrices must have dimensions and rank greater than zero. 
     1751        ValueError: matrices must have rank greater than zero. 
    15011752 
    1502     Matrices must have a column dimension greater than zero. :: 
    1503      
    1504         sage: random_echelonizable_matrix(9,0,4) 
     1753    The base ring must be exact. :: 
     1754 
     1755        sage: random_matrix(RR, 3, 3, algorithm='echelonizable', rank=2) 
    15051756        Traceback (most recent call last): 
    15061757        ... 
    1507         ValueError: matrices must have dimensions and rank greater than zero. 
    1508  
    1509     Matrices must have a rank greater than zero. :: 
    1510  
    1511         sage: random_echelonizable_matrix(3,4,0) 
    1512         Traceback (most recent call last): 
    1513         ... 
    1514         ValueError: matrices must have dimensions and rank greater than zero. 
     1758        TypeError: the base ring must be exact. 
    15151759 
    15161760    AUTHOR: 
    15171761 
     
    15201764 
    15211765    from sage.misc.prandom import randint 
    15221766 
    1523     if rows<=0 or columns<=0 or rank<=0: 
    1524         raise ValueError("matrices must have dimensions and rank greater than zero.") 
     1767    ring = parent.base_ring() 
     1768    rows = parent.nrows() 
     1769    columns = parent.nrows() 
     1770    if rank<=0: 
     1771        raise ValueError("matrices must have rank greater than zero.") 
     1772    matrix = random_rref_matrix(parent, rank) 
    15251773    # Entries of matrices over the ZZ or QQ can get large, entry size is regulated by finding the largest 
    15261774    # entry of the resultant matrix after addition of scalar multiple of a row. 
    15271775    if ring==QQ or ring==ZZ: 
    1528         matrix=random_rref_matrix(rows,columns,rank,ring) 
    1529         matrix_copy=copy(matrix) 
    15301776        # If upper_bound is not set, don't control entry size. 
    15311777        if upper_bound==None: 
    15321778            upper_bound=50 
     
    15731819    # If the matrix generated over a different ring, random elements from the designated ring are used as and 
    15741820    # the routine is run similarly to the size unchecked version for rationals and integers. 
    15751821    else: 
    1576         matrix=random_rref_matrix(rows,columns,rank,ring) 
    1577         matrix_copy=copy(matrix) 
    15781822        for pivots in range(rank-1,-1,-1): 
    15791823            row_index=0 
    15801824            while row_index<rows: 
  • sage/matrix/matrix_modn_dense.pyx

    diff -r 422d1a1eefa5 -r 0bc3f6a4dab9 sage/matrix/matrix_modn_dense.pyx
    a b  
    553553        """ 
    554554        TESTS:: 
    555555         
    556             sage: a = random_matrix(GF(11), 5, 5, range(25)) 
     556            sage: a = random_matrix(GF(11), 5, 5) 
    557557            sage: a.set_immutable() 
    558558            sage: hash(a) #random 
    559559            216 
  • sage/misc/lazy_import.py

    diff -r 422d1a1eefa5 -r 0bc3f6a4dab9 sage/misc/lazy_import.py
    a b  
    123123            sage: from sage.misc.lazy_import import LazyImport 
    124124            sage: rm = LazyImport('sage.all', 'random_matrix') 
    125125            sage: rm._sage_argspec_() 
    126             (['R', 'nrows', 'ncols', 'sparse', 'density'], 
    127              'args', 
    128              'kwds', 
    129              (None, False, 1)) 
     126            (['ring', 'nrows', 'ncols', 'algorithm'], 'args', 'kwds', (None, 'randomize')) 
    130127        """ 
    131128        return sageinspect.sage_getargspec(self._get_object()) 
    132129