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