]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/eja/eja_utils.py
eja: start fixing Cartesian products of Cartesian products.
[sage.d.git] / mjo / eja / eja_utils.py
1 from sage.functions.other import sqrt
2 from sage.matrix.constructor import matrix
3 from sage.modules.free_module_element import vector
4
5 def _scale(x, alpha):
6 r"""
7 Scale the vector, matrix, or cartesian-product-of-those-things
8 ``x`` by ``alpha``.
9 """
10 if hasattr(x, 'cartesian_factors'):
11 P = x.parent()
12 return P(tuple( _scale(x_i, alpha)
13 for x_i in x.cartesian_factors() ))
14 else:
15 return x*alpha
16
17 def _all2list(x):
18 r"""
19 Flatten a vector, matrix, or cartesian product of those things
20 into a long list.
21
22 EXAMPLES::
23
24 sage: from mjo.eja.eja_utils import _all2list
25 sage: V1 = VectorSpace(QQ,2)
26 sage: V2 = MatrixSpace(QQ,2)
27 sage: x1 = V1([1,1])
28 sage: x2 = V1([1,-1])
29 sage: y1 = V2.one()
30 sage: y2 = V2([0,1,1,0])
31 sage: _all2list((x1,y1))
32 [1, 1, 1, 0, 0, 1]
33 sage: _all2list((x2,y2))
34 [1, -1, 0, 1, 1, 0]
35 sage: M = cartesian_product([V1,V2])
36 sage: _all2list(M((x1,y1)))
37 [1, 1, 1, 0, 0, 1]
38 sage: _all2list(M((x2,y2)))
39 [1, -1, 0, 1, 1, 0]
40
41 """
42 if hasattr(x, 'list'):
43 # Easy case...
44 return x.list()
45 else:
46 # But what if it's a tuple or something else? This has to
47 # handle cartesian products of cartesian products, too; that's
48 # why it's recursive.
49 return sum( map(_all2list,x), [] )
50
51 def _mat2vec(m):
52 return vector(m.base_ring(), m.list())
53
54 def _vec2mat(v):
55 return matrix(v.base_ring(), sqrt(v.degree()), v.list())
56
57 def gram_schmidt(v, inner_product=None):
58 """
59 Perform Gram-Schmidt on the list ``v`` which are assumed to be
60 vectors over the same base ring. Returns a list of orthonormalized
61 vectors over the smallest extention ring containing the necessary
62 roots.
63
64 SETUP::
65
66 sage: from mjo.eja.eja_utils import gram_schmidt
67
68 EXAMPLES:
69
70 The usual inner-product and norm are default::
71
72 sage: v1 = vector(QQ,(1,2,3))
73 sage: v2 = vector(QQ,(1,-1,6))
74 sage: v3 = vector(QQ,(2,1,-1))
75 sage: v = [v1,v2,v3]
76 sage: u = gram_schmidt(v)
77 sage: all( u_i.inner_product(u_i).sqrt() == 1 for u_i in u )
78 True
79 sage: bool(u[0].inner_product(u[1]) == 0)
80 True
81 sage: bool(u[0].inner_product(u[2]) == 0)
82 True
83 sage: bool(u[1].inner_product(u[2]) == 0)
84 True
85
86
87 But if you supply a custom inner product, the result is
88 orthonormal with respect to that (and not the usual inner
89 product)::
90
91 sage: v1 = vector(QQ,(1,2,3))
92 sage: v2 = vector(QQ,(1,-1,6))
93 sage: v3 = vector(QQ,(2,1,-1))
94 sage: v = [v1,v2,v3]
95 sage: B = matrix(QQ, [ [6, 4, 2],
96 ....: [4, 5, 4],
97 ....: [2, 4, 9] ])
98 sage: ip = lambda x,y: (B*x).inner_product(y)
99 sage: norm = lambda x: ip(x,x)
100 sage: u = gram_schmidt(v,ip)
101 sage: all( norm(u_i) == 1 for u_i in u )
102 True
103 sage: ip(u[0],u[1]).is_zero()
104 True
105 sage: ip(u[0],u[2]).is_zero()
106 True
107 sage: ip(u[1],u[2]).is_zero()
108 True
109
110 This Gram-Schmidt routine can be used on matrices as well, so long
111 as an appropriate inner-product is provided::
112
113 sage: E11 = matrix(QQ, [ [1,0],
114 ....: [0,0] ])
115 sage: E12 = matrix(QQ, [ [0,1],
116 ....: [1,0] ])
117 sage: E22 = matrix(QQ, [ [0,0],
118 ....: [0,1] ])
119 sage: I = matrix.identity(QQ,2)
120 sage: trace_ip = lambda X,Y: (X*Y).trace()
121 sage: gram_schmidt([E11,E12,I,E22], inner_product=trace_ip)
122 [
123 [1 0] [ 0 1/2*sqrt(2)] [0 0]
124 [0 0], [1/2*sqrt(2) 0], [0 1]
125 ]
126
127 It even works on Cartesian product spaces whose factors are vector
128 or matrix spaces::
129
130 sage: V1 = VectorSpace(AA,2)
131 sage: V2 = MatrixSpace(AA,2)
132 sage: M = cartesian_product([V1,V2])
133 sage: x1 = V1([1,1])
134 sage: x2 = V1([1,-1])
135 sage: y1 = V2.one()
136 sage: y2 = V2([0,1,1,0])
137 sage: z1 = M((x1,y1))
138 sage: z2 = M((x2,y2))
139 sage: def ip(a,b):
140 ....: return a[0].inner_product(b[0]) + (a[1]*b[1]).trace()
141 sage: U = gram_schmidt([z1,z2], inner_product=ip)
142 sage: ip(U[0],U[1])
143 0
144 sage: ip(U[0],U[0])
145 1
146 sage: ip(U[1],U[1])
147 1
148
149 TESTS:
150
151 Ensure that zero vectors don't get in the way::
152
153 sage: v1 = vector(QQ,(1,2,3))
154 sage: v2 = vector(QQ,(1,-1,6))
155 sage: v3 = vector(QQ,(0,0,0))
156 sage: v = [v1,v2,v3]
157 sage: len(gram_schmidt(v)) == 2
158 True
159 """
160 if inner_product is None:
161 inner_product = lambda x,y: x.inner_product(y)
162 norm = lambda x: inner_product(x,x).sqrt()
163
164 v = list(v) # make a copy, don't clobber the input
165
166 # Drop all zero vectors before we start.
167 v = [ v_i for v_i in v if not v_i.is_zero() ]
168
169 if len(v) == 0:
170 # cool
171 return v
172
173 R = v[0].base_ring()
174
175 # Our "zero" needs to belong to the right space for sum() to work.
176 zero = v[0].parent().zero()
177
178 sc = lambda x,a: a*x
179 if hasattr(v[0], 'cartesian_factors'):
180 # Only use the slow implementation if necessary.
181 sc = _scale
182
183 def proj(x,y):
184 return sc(x, (inner_product(x,y)/inner_product(x,x)))
185
186 # First orthogonalize...
187 for i in range(1,len(v)):
188 # Earlier vectors can be made into zero so we have to ignore them.
189 v[i] -= sum( (proj(v[j],v[i])
190 for j in range(i)
191 if not v[j].is_zero() ),
192 zero )
193
194 # And now drop all zero vectors again if they were "orthogonalized out."
195 v = [ v_i for v_i in v if not v_i.is_zero() ]
196
197 # Just normalize. If the algebra is missing the roots, we can't add
198 # them here because then our subalgebra would have a bigger field
199 # than the superalgebra.
200 for i in range(len(v)):
201 v[i] = sc(v[i], ~norm(v[i]))
202
203 return v