]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/random.py
2 Generate random things.
5 from sage
.matrix
.constructor
import matrix
6 from sage
.rings
.real_lazy
import ComplexLazyField
7 from sage
.rings
.all
import ZZ
9 def random_unitary_matrix(F
,n
):
11 Generate a random unitary matrix of size ``n`` with entries
16 - ``F`` -- a field; specifically, a subfield of the complex
17 numbers having characteristic zero.
19 - ``n`` -- integer; the size of the random matrix you want.
23 A random ``n``-by-``n`` unitary matrix with entries in ``F``. A
24 ``ValueError`` is raised if ``F`` is not an appropriate field.
28 - Hans Liebeck and Anthony Osborne. The Generation of All Rational
29 Orthogonal Matrices. The American Mathematical Monthly, Vol. 98,
30 No. 2. (February, 1991), pp. 131-133.
34 sage: from mjo.random import random_unitary_matrix
38 sage: n = ZZ.random_element(10)
39 sage: U = random_unitary_matrix(QQ,n)
42 sage: U.base_ring() is QQ
47 sage: n = ZZ.random_element(10)
48 sage: K = QuadraticField(-1,'i')
49 sage: U = random_unitary_matrix(K,n)
52 sage: U.base_ring() is K
56 raise ValueError("F must be a field")
57 elif not F
.is_subring(ComplexLazyField()):
58 raise ValueError("F must be a subfield of the complex numbers")
59 elif not F
.characteristic().is_zero():
60 raise ValueError("F must have characteristic zero")
62 I
= matrix
.identity(F
,n
)
63 A
= matrix
.random(F
,n
)
64 S
= A
- A
.conjugate_transpose()
65 U
= (S
-I
).inverse()*(S
+I
)
66 D
= matrix
.identity(F
,n
)
68 if ZZ
.random_element(2).is_zero():