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 1109 1109 nwords = randint(1, min(n-1,nwords_max) ) 1110 1110 S = Permutations(n) 1111 1111 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) 1114 1114 B = LinearBinaryCodeStruct( M ) 1115 1115 B_n = NonlinearBinaryCodeStruct( M_n ) 1116 1116 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 351 351 ncols = randint(1, ncols_max) 352 352 nsymbols = next_prime(randint(1, nsymbols_max)) 353 353 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) 355 355 M = MatrixStruct( MM ) 356 356 M.run() 357 357 -
sage/matrix/constructor.py
diff -r 422d1a1eefa5 -r 0bc3f6a4dab9 sage/matrix/constructor.py
a b 796 796 Matrix = matrix 797 797 798 798 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 799 def 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 804 803 INPUT: 805 806 - `` R`` - Ring807 804 805 - ``ring`` - base ring for entries of the matrix 806 808 807 - ``nrows`` - Integer; number of rows 809 808 810 809 - ``ncols`` - (default: ``None``); number of columns; if ``None`` 811 810 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 824 1053 """ 825 1054 if ncols is None: 826 1055 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) 830 1072 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 833 1075 834 1076 def diagonal_matrix(arg0=None, arg1=None, arg2=None, sparse=None): 835 1077 """ … … 1235 1477 block[i,i+1]=1 1236 1478 return block 1237 1479 1238 def random_rref_matrix( num_row, num_col, num_pivots,ring=QQ):1480 def random_rref_matrix(parent, num_pivots): 1239 1481 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. 1241 1483 1242 1484 INPUT: 1243 1485 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. 1245 1488 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. 1251 1490 1252 1491 OUTPUT: 1253 1492 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. 1256 1503 1257 1504 EXAMPLES: 1258 1505 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. :: 1260 1508 1261 1509 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 1263 1512 [ 1 0 0 -6 0 -3] 1264 1513 [ 0 1 0 2 0 3] 1265 1514 [ 0 0 1 -4 0 -2] 1266 1515 [ 0 0 0 0 1 3] 1267 1516 [ 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 1268 1523 sage: A.rank() 1269 1524 4 1270 sage: A.base_ring()1271 Rational Field1272 1525 sage: A==A.rref() 1273 1526 True 1274 1527 1275 Matrices can be generated over other fields. ::1528 Matrices can be generated over other exact rings. :: 1276 1529 1277 sage: B=random_ rref_matrix(4,4,3,GF(7)); B # random1530 sage: B=random_matrix(FiniteField(7), 4, 4, algorithm='echelon_form', num_pivots=3); B # random 1278 1531 [1 0 0 0] 1279 1532 [0 1 0 6] 1280 1533 [0 0 1 4] 1281 1534 [0 0 0 0] 1535 sage: B.rank() == 3 1536 True 1282 1537 sage: B.base_ring() 1283 1538 Finite Field of size 7 1284 1539 sage: B==B.rref() … … 1286 1541 1287 1542 TESTS: 1288 1543 1289 R ow dimension input for a matrix must be whole valued. ::1544 Rank of a matrix must be an integer. :: 1290 1545 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) 1306 1547 Traceback (most recent call last): 1307 1548 ... 1308 TypeError: inputs for matrix dimensions and rank must be integers.1549 TypeError: the number of pivots must be an integer. 1309 1550 1310 1551 Matrices must be generated over exact fields. :: 1311 1552 1312 sage: random_rref_matrix(40,88,39,RR)1313 Traceback (most recent call last):1314 ...1315 TypeError: inputring 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. 1316 1557 1317 1558 Matrices must have the number of pivot columns be less than or equal to the number of rows. :: 1318 1559 1319 sage: C=random_ rref_matrix(6,4,7); C1560 sage: C=random_matrix(ZZ, 6,4, algorithm='echelon_form', num_pivots=7); C 1320 1561 Traceback (most recent call last): 1321 1562 ... 1322 ValueError: number of pivot columncannot exceed the number of rows or columns.1563 ValueError: number of pivots cannot exceed the number of rows or columns. 1323 1564 1324 1565 Matrices must have the number of pivot columns be less than or equal to the number of columns. :: 1325 1566 1326 sage: D=random_ rref_matrix(1,3,5); D1567 sage: D=random_matrix(QQ, 1,3, algorithm='echelon_form', num_pivots=5); D 1327 1568 Traceback (most recent call last): 1328 1569 ... 1329 ValueError: number of pivot columncannot exceed the number of rows or columns.1570 ValueError: number of pivots cannot exceed the number of rows or columns. 1330 1571 1331 1572 Matrices must have the number of pivot columns be greater than zero. :: 1332 1573 1333 sage: random_ rref_matrix(5,4,0)1574 sage: random_matrix(QQ, 5, 4, algorithm='echelon_form', num_pivots=0) 1334 1575 Traceback (most recent call last): 1335 1576 ... 1336 ValueError: matrices must have dimensions and rankgreater than zero.1577 ValueError: the number of pivots must be greater than zero. 1337 1578 1338 1579 AUTHOR: 1339 1580 1340 1581 Billy Wonderly (2010-07) 1341 """ 1582 """ 1342 1583 1343 1584 import sage.gsl.probability_distribution as pd 1344 1585 from sage.misc.prandom import randint 1345 1586 1346 1587 try: 1347 num_row=ZZ(num_row)1348 num_col=ZZ(num_col)1349 1588 num_pivots=ZZ(num_pivots) 1350 1589 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() 1352 1594 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() 1354 1598 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.") 1358 1600 else: 1359 1601 one=ring.one() 1360 1602 # 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()) 1362 1604 pivots=[0] #Force first column to be a pivot. 1363 1605 # Probability distribution for the placement of leading one's. 1364 1606 pivot_generator=pd.RealDistribution("beta",[1.6,4.3]) … … 1400 1642 return_matrix[rest_entries,rest_non_pivot_column]=ring.random_element() 1401 1643 return return_matrix 1402 1644 1403 def random_echelonizable_matrix( rows,columns,rank,upper_bound=None,ring=QQ):1645 def random_echelonizable_matrix(parent, rank, upper_bound=None): 1404 1646 r""" 1405 1647 Generate a matrix of a desired size and rank, over a desired ring, whose reduced 1406 1648 row-echelon form has only integral values. 1407 1649 1408 1650 INPUT: 1409 1651 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. 1411 1654 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. 1413 1657 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. 1417 1660 If None, no size control occurs. (default: None) 1418 1661 1419 - ``ring`` - The desired ring for the matrix to be generated over (default: QQ).1420 1421 1422 1662 OUTPUT: 1423 1663 1424 1664 A matrix not in reduced row-echelon form with the desired dimensions and properties. 1425 1665 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 1426 1674 EXAMPLES: 1427 1675 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. :: 1429 1678 1430 1679 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 1432 1682 [ 1 -1 1 -3 -4 6] 1433 1683 [ 5 -4 0 8 4 19] 1434 1684 [ -3 3 -2 4 7 -16] … … 1441 1691 sage: A.rref()==A.rref().change_ring(ZZ) 1442 1692 True 1443 1693 1444 An example with default settings ( ring=QQ,no entry size control). ::1694 An example with default settings (i.e. no entry size control). :: 1445 1695 1446 sage: C=random_ echelonizable_matrix(6,7,5); C # random1696 sage: C=random_matrix(QQ, 6, 7, algorithm='echelonizable', rank=5); C # random 1447 1697 [ 1 0 5 -2 -26 -16 0] 1448 1698 [ -3 1 -19 6 97 61 1] 1449 1699 [ 0 4 -15 -1 71 50 3] … … 1457 1707 1458 1708 A matrix without size control may have very large entry sizes. :: 1459 1709 1460 sage: D=random_ echelonizable_matrix(7,8,6,ring=ZZ); D # random1710 sage: D=random_matrix(ZZ, 7, 8, algorithm='echelonizable', rank=6); D # random 1461 1711 [ 9 -53 -255 45 -1519 4043 9819 3324] 1462 1712 [ 3 -14 -64 8 -369 972 2350 810] 1463 1713 [ 2 -14 -65 9 -377 1000 2420 829] … … 1466 1716 [ -5 21 92 -13 548 -1432 -3466 -1183] 1467 1717 [ 1 -9 -42 7 -254 670 1624 547] 1468 1718 1469 Matrices can be generated over a different ring. ::1719 Matrices can be generated over any exact ring. :: 1470 1720 1471 1721 sage: F.<a>=GF(2^3) 1472 sage: B=random_ echelonizable_matrix(4,5,4,None,F); B # random1722 sage: B=random_matrix(F, 4, 5, algorithm='echelonizable', rank=4, upper_bound=None); B # random 1473 1723 [ a + 1 a^2 + a + 1 a^2 0 1] 1474 1724 [ 1 a a + 1 a^2 a^2 + a] 1475 1725 [ a a^2 a^2 + a + 1 a + 1 1] … … 1479 1729 1480 1730 Square matrices over ZZ or QQ with full rank are unimodular. :: 1481 1731 1482 sage: E=random_ echelonizable_matrix(7,7,7); E # random1732 sage: E=random_matrix(QQ, 7, 7, algorithm='echelonizable', rank=7); E # random 1483 1733 [ 1 -1 5 12 -24 -41 47] 1484 1734 [ 0 1 -1 3 0 -11 40] 1485 1735 [ 1 -1 6 6 -19 -20 -11] … … 1492 1742 1493 1743 TESTS: 1494 1744 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.) :: 1496 1747 1497 sage: random_ echelonizable_matrix(0,5,3)1748 sage: random_matrix(QQ, 3, 4, algorithm='echelonizable', rank=0) 1498 1749 Traceback (most recent call last): 1499 1750 ... 1500 ValueError: matrices must have dimensions andrank greater than zero.1751 ValueError: matrices must have rank greater than zero. 1501 1752 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) 1505 1756 Traceback (most recent call last): 1506 1757 ... 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. 1515 1759 1516 1760 AUTHOR: 1517 1761 … … 1520 1764 1521 1765 from sage.misc.prandom import randint 1522 1766 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) 1525 1773 # Entries of matrices over the ZZ or QQ can get large, entry size is regulated by finding the largest 1526 1774 # entry of the resultant matrix after addition of scalar multiple of a row. 1527 1775 if ring==QQ or ring==ZZ: 1528 matrix=random_rref_matrix(rows,columns,rank,ring)1529 matrix_copy=copy(matrix)1530 1776 # If upper_bound is not set, don't control entry size. 1531 1777 if upper_bound==None: 1532 1778 upper_bound=50 … … 1573 1819 # If the matrix generated over a different ring, random elements from the designated ring are used as and 1574 1820 # the routine is run similarly to the size unchecked version for rationals and integers. 1575 1821 else: 1576 matrix=random_rref_matrix(rows,columns,rank,ring)1577 matrix_copy=copy(matrix)1578 1822 for pivots in range(rank-1,-1,-1): 1579 1823 row_index=0 1580 1824 while row_index<rows: -
sage/matrix/matrix_modn_dense.pyx
diff -r 422d1a1eefa5 -r 0bc3f6a4dab9 sage/matrix/matrix_modn_dense.pyx
a b 553 553 """ 554 554 TESTS:: 555 555 556 sage: a = random_matrix(GF(11), 5, 5 , range(25))556 sage: a = random_matrix(GF(11), 5, 5) 557 557 sage: a.set_immutable() 558 558 sage: hash(a) #random 559 559 216 -
sage/misc/lazy_import.py
diff -r 422d1a1eefa5 -r 0bc3f6a4dab9 sage/misc/lazy_import.py
a b 123 123 sage: from sage.misc.lazy_import import LazyImport 124 124 sage: rm = LazyImport('sage.all', 'random_matrix') 125 125 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')) 130 127 """ 131 128 return sageinspect.sage_getargspec(self._get_object()) 132 129
