Skip to content

Commit 49ac0cd

Browse files
committed
src/sage/matrix/special.py: optimize random_unitary_matrix()
Use the lower-level methods Matrix.set_row_to_multiple_of_row() and random.random() to implement random_unitary_matrix(). This should bring with it a small speed increase, and actually makes the code simpler. Thanks to John Cremona for the suggestion.
1 parent 1ceab98 commit 49ac0cd

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/sage/matrix/special.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,11 +3215,16 @@ def random_unitary_matrix(parent):
32153215
A = random_matrix(F,n)
32163216
S = A - A.conjugate_transpose()
32173217
U = (S-I).inverse()*(S+I)
3218-
D = identity_matrix(F,n)
3218+
3219+
# Scale the rows of U by plus/minus one with equal probability.
3220+
# This generates the equivalence class of U according to the
3221+
# Liebeck/Osborne paper.
3222+
from random import random
32193223
for i in range(n):
3220-
if ZZ.random_element(2).is_zero():
3221-
D[i,i] *= F(-1)
3222-
return D*U
3224+
if random() < 0.5:
3225+
U.set_row_to_multiple_of_row(i, i, -1)
3226+
3227+
return U
32233228

32243229

32253230
@matrix_method

0 commit comments

Comments
 (0)