@@ -241,8 +241,11 @@ def random_matrix(ring, nrows, ncols=None, algorithm='randomize', implementation
241
241
242
242
- ``'unimodular'`` -- creates a matrix of determinant 1
243
243
244
- - ``'diagonalizable'`` -- creates a diagonalizable matrix whose
245
- eigenvectors, if computed by hand, will have only integer entries
244
+ - ``'diagonalizable'`` -- creates a diagonalizable matrix. if the
245
+ base ring is ``QQ`` creates a diagonalizable matrix whose eigenvectors,
246
+ if computed by hand, will have only integer entries. See the
247
+ documentation of :meth:`~sage.matrix.special.random_diagonalizable_matrix`
248
+ for more information
246
249
247
250
- ``implementation`` -- (``None`` or string or a matrix class) a possible
248
251
implementation. See the documentation of the constructor of
@@ -3054,15 +3057,16 @@ def random_diagonalizable_matrix(parent, eigenvalues=None, dimensions=None):
3054
3057
"""
3055
3058
Create a random matrix that diagonalizes nicely.
3056
3059
3057
- To be used as a teaching tool. Return matrices have only real
3058
- eigenvalues.
3060
+ To be used as a teaching tool. The eigenvalues will be elements of the
3061
+ base ring. If the base ring used is ``QQ`` then the returned matrix will
3062
+ have integer eigenvalues.
3059
3063
3060
3064
INPUT:
3061
3065
3062
3066
If eigenvalues and dimensions are not specified in a list,
3063
3067
they will be assigned randomly.
3064
3068
3065
- - ``parent`` -- the desired size of the square matrix
3069
+ - ``parent`` -- the desired parent of the square matrix (a matrix space)
3066
3070
3067
3071
- ``eigenvalues`` -- the list of desired eigenvalues (default=None)
3068
3072
@@ -3071,8 +3075,9 @@ def random_diagonalizable_matrix(parent, eigenvalues=None, dimensions=None):
3071
3075
3072
3076
OUTPUT:
3073
3077
3074
- A square, diagonalizable, matrix with only integer entries. The
3075
- eigenspaces of this matrix, if computed by hand, give basis
3078
+ A square, diagonalizable, matrix. Elements of the matrix are elements
3079
+ of the base ring. If the ring used is ``QQ`` then we have integer entries,
3080
+ and the eigenspaces of this matrix, if computed by hand, gives basis
3076
3081
vectors with only integer entries.
3077
3082
3078
3083
.. NOTE::
@@ -3118,15 +3123,28 @@ def random_diagonalizable_matrix(parent, eigenvalues=None, dimensions=None):
3118
3123
sage: all(e in eigenvalues for e in eigenvalues2)
3119
3124
True
3120
3125
3126
+ Matrices over finite fields are also supported::
3127
+
3128
+ sage: K = GF(3)
3129
+ sage: M = random_matrix(K, 3, 3, algorithm="diagonalizable")
3130
+ sage: M.parent()
3131
+ Full MatrixSpace of 3 by 3 dense matrices over Finite Field of size 3
3132
+ sage: M.is_diagonalizable()
3133
+ True
3134
+ sage: M # random
3135
+ [0 0 1]
3136
+ [2 1 1]
3137
+ [1 0 0]
3138
+
3121
3139
TESTS:
3122
3140
3123
- Eigenvalues must all be integers . ::
3141
+ Eigenvalues must all be elements of the ring . ::
3124
3142
3125
3143
sage: random_matrix(QQ, 3, algorithm='diagonalizable', # needs sage.symbolic
3126
3144
....: eigenvalues=[2+I, 2-I, 2], dimensions=[1,1,1])
3127
3145
Traceback (most recent call last):
3128
3146
...
3129
- TypeError: eigenvalues must be integers .
3147
+ TypeError: eigenvalues must be elements of the corresponding ring .
3130
3148
3131
3149
Diagonal matrices must be square. ::
3132
3150
@@ -3189,6 +3207,7 @@ def random_diagonalizable_matrix(parent, eigenvalues=None, dimensions=None):
3189
3207
from sage .misc .prandom import randint
3190
3208
3191
3209
size = parent .nrows ()
3210
+ ring = parent .base_ring ()
3192
3211
if parent .nrows () != parent .ncols ():
3193
3212
raise TypeError ("a diagonalizable matrix must be square." )
3194
3213
if eigenvalues is not None and dimensions is None :
@@ -3199,7 +3218,7 @@ def random_diagonalizable_matrix(parent, eigenvalues=None, dimensions=None):
3199
3218
values = []
3200
3219
# create a list with "size" number of entries
3201
3220
for eigen_index in range (size ):
3202
- eigenvalue = randint (- 10 , 10 )
3221
+ eigenvalue = ring ( randint (- 10 , 10 ) )
3203
3222
values .append (eigenvalue )
3204
3223
values .sort ()
3205
3224
dimensions = []
@@ -3214,8 +3233,8 @@ def random_diagonalizable_matrix(parent, eigenvalues=None, dimensions=None):
3214
3233
size_check = 0
3215
3234
for check in range (len (dimensions )):
3216
3235
size_check = size_check + dimensions [check ]
3217
- if not all (x in ZZ for x in eigenvalues ):
3218
- raise TypeError ("eigenvalues must be integers ." )
3236
+ if not all (x in ring for x in eigenvalues ):
3237
+ raise TypeError ("eigenvalues must be elements of the corresponding ring ." )
3219
3238
if size != size_check :
3220
3239
raise ValueError ("the size of the matrix must equal the sum of the dimensions." )
3221
3240
if min (dimensions ) < 1 :
@@ -3227,7 +3246,7 @@ def random_diagonalizable_matrix(parent, eigenvalues=None, dimensions=None):
3227
3246
dimensions = [x [0 ] for x in dimensions_sort ]
3228
3247
eigenvalues = [x [1 ] for x in dimensions_sort ]
3229
3248
# Create the matrix of eigenvalues on the diagonal. Use a lower limit and upper limit determined by the eigenvalue dimensions.
3230
- diagonal_matrix = matrix (QQ , size )
3249
+ diagonal_matrix = matrix (ring , size )
3231
3250
up_bound = 0
3232
3251
low_bound = 0
3233
3252
for row_index in range (len (dimensions )):
@@ -3237,7 +3256,7 @@ def random_diagonalizable_matrix(parent, eigenvalues=None, dimensions=None):
3237
3256
low_bound = low_bound + dimensions [row_index ]
3238
3257
# Create a matrix to hold each of the eigenvectors as its columns, begin with an identity matrix so that after row and column
3239
3258
# operations the resulting matrix will be unimodular.
3240
- eigenvector_matrix = matrix ( QQ , size , size , 1 )
3259
+ eigenvector_matrix = matrix . identity ( ring , size )
3241
3260
upper_limit = 0
3242
3261
lower_limit = 0
3243
3262
# run the routine over the necessary number of columns corresponding eigenvalue dimension.
0 commit comments