]>
gitweb.michael.orlitzky.com - sage.d.git/blob - 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
5 def _change_ring(x
, R
):
7 Change the ring of a vector, matrix, or a cartesian product of
12 sage: from mjo.eja.eja_utils import _change_ring
16 sage: v = vector(QQ, (1,2,3))
17 sage: m = matrix(QQ, [[1,2],[3,4]])
18 sage: _change_ring(v, RDF)
20 sage: _change_ring(m, RDF)
23 sage: _change_ring((v,m), RDF)
26 (1.0, 2.0, 3.0), [3.0 4.0]
28 sage: V1 = cartesian_product([v.parent(), v.parent()])
29 sage: V = cartesian_product([v.parent(), V1])
31 ((1, 2, 3), ((1, 2, 3), (1, 2, 3)))
32 sage: _change_ring(V((v, (v, v))), RDF)
33 ((1.0, 2.0, 3.0), ((1.0, 2.0, 3.0), (1.0, 2.0, 3.0)))
37 return x
.change_ring(R
)
38 except AttributeError:
40 from sage
.categories
.sets_cat
import cartesian_product
41 if hasattr(x
, 'element_class'):
42 # x is a parent and we're in a recursive call.
43 return cartesian_product( [_change_ring(x_i
, R
)
44 for x_i
in x
.cartesian_factors()] )
46 # x is an element, and we want to change the ring
49 Q
= cartesian_product( [_change_ring(P_i
, R
)
50 for P_i
in P
.cartesian_factors()] )
52 except AttributeError:
54 return x
.__class
__( _change_ring(x_i
, R
) for x_i
in x
)
58 Scale the vector, matrix, or cartesian-product-of-those-things
61 This works around the inability to scale certain elements of
62 Cartesian product spaces, as reported in
64 https://trac.sagemath.org/ticket/31435
68 This will do the wrong thing if you feed it a tuple or list.
72 sage: from mjo.eja.eja_utils import _scale
76 sage: v = vector(QQ, (1,2,3))
79 sage: m = matrix(QQ, [[1,2],[3,4]])
80 sage: M = cartesian_product([m.parent(), m.parent()])
81 sage: _scale(M((m,m)), 2)
87 if hasattr(x
, 'cartesian_factors'):
89 return P(tuple( _scale(x_i
, alpha
)
90 for x_i
in x
.cartesian_factors() ))
97 Flatten a vector, matrix, or cartesian product of those things
102 sage: from mjo.eja.eja_utils import _all2list
103 sage: V1 = VectorSpace(QQ,2)
104 sage: V2 = MatrixSpace(QQ,2)
106 sage: x2 = V1([1,-1])
108 sage: y2 = V2([0,1,1,0])
109 sage: _all2list((x1,y1))
111 sage: _all2list((x2,y2))
113 sage: M = cartesian_product([V1,V2])
114 sage: _all2list(M((x1,y1)))
116 sage: _all2list(M((x2,y2)))
120 if hasattr(x
, 'list'):
124 # But what if it's a tuple or something else? This has to
125 # handle cartesian products of cartesian products, too; that's
126 # why it's recursive.
127 return sum( map(_all2list
,x
), [] )
130 return vector(m
.base_ring(), m
.list())
133 return matrix(v
.base_ring(), sqrt(v
.degree()), v
.list())
135 def gram_schmidt(v
, inner_product
=None):
137 Perform Gram-Schmidt on the list ``v`` which are assumed to be
138 vectors over the same base ring. Returns a list of orthonormalized
139 vectors over the smallest extention ring containing the necessary
144 sage: from mjo.eja.eja_utils import gram_schmidt
148 The usual inner-product and norm are default::
150 sage: v1 = vector(QQ,(1,2,3))
151 sage: v2 = vector(QQ,(1,-1,6))
152 sage: v3 = vector(QQ,(2,1,-1))
154 sage: u = gram_schmidt(v)
155 sage: all( u_i.inner_product(u_i).sqrt() == 1 for u_i in u )
157 sage: bool(u[0].inner_product(u[1]) == 0)
159 sage: bool(u[0].inner_product(u[2]) == 0)
161 sage: bool(u[1].inner_product(u[2]) == 0)
165 But if you supply a custom inner product, the result is
166 orthonormal with respect to that (and not the usual inner
169 sage: v1 = vector(QQ,(1,2,3))
170 sage: v2 = vector(QQ,(1,-1,6))
171 sage: v3 = vector(QQ,(2,1,-1))
173 sage: B = matrix(QQ, [ [6, 4, 2],
176 sage: ip = lambda x,y: (B*x).inner_product(y)
177 sage: norm = lambda x: ip(x,x)
178 sage: u = gram_schmidt(v,ip)
179 sage: all( norm(u_i) == 1 for u_i in u )
181 sage: ip(u[0],u[1]).is_zero()
183 sage: ip(u[0],u[2]).is_zero()
185 sage: ip(u[1],u[2]).is_zero()
188 This Gram-Schmidt routine can be used on matrices as well, so long
189 as an appropriate inner-product is provided::
191 sage: E11 = matrix(QQ, [ [1,0],
193 sage: E12 = matrix(QQ, [ [0,1],
195 sage: E22 = matrix(QQ, [ [0,0],
197 sage: I = matrix.identity(QQ,2)
198 sage: trace_ip = lambda X,Y: (X*Y).trace()
199 sage: gram_schmidt([E11,E12,I,E22], inner_product=trace_ip)
201 [1 0] [ 0 1/2*sqrt(2)] [0 0]
202 [0 0], [1/2*sqrt(2) 0], [0 1]
205 It even works on Cartesian product spaces whose factors are vector
208 sage: V1 = VectorSpace(AA,2)
209 sage: V2 = MatrixSpace(AA,2)
210 sage: M = cartesian_product([V1,V2])
212 sage: x2 = V1([1,-1])
214 sage: y2 = V2([0,1,1,0])
215 sage: z1 = M((x1,y1))
216 sage: z2 = M((x2,y2))
218 ....: return a[0].inner_product(b[0]) + (a[1]*b[1]).trace()
219 sage: U = gram_schmidt([z1,z2], inner_product=ip)
229 Ensure that zero vectors don't get in the way::
231 sage: v1 = vector(QQ,(1,2,3))
232 sage: v2 = vector(QQ,(1,-1,6))
233 sage: v3 = vector(QQ,(0,0,0))
235 sage: len(gram_schmidt(v)) == 2
238 if inner_product
is None:
239 inner_product
= lambda x
,y
: x
.inner_product(y
)
240 norm
= lambda x
: inner_product(x
,x
).sqrt()
242 v
= list(v
) # make a copy, don't clobber the input
244 # Drop all zero vectors before we start.
245 v
= [ v_i
for v_i
in v
if not v_i
.is_zero() ]
253 # Our "zero" needs to belong to the right space for sum() to work.
254 zero
= v
[0].parent().zero()
257 if hasattr(v
[0], 'cartesian_factors'):
258 # Only use the slow implementation if necessary.
262 return sc(x
, (inner_product(x
,y
)/inner_product(x
,x
)))
264 # First orthogonalize...
265 for i
in range(1,len(v
)):
266 # Earlier vectors can be made into zero so we have to ignore them.
267 v
[i
] -= sum( (proj(v
[j
],v
[i
])
269 if not v
[j
].is_zero() ),
272 # And now drop all zero vectors again if they were "orthogonalized out."
273 v
= [ v_i
for v_i
in v
if not v_i
.is_zero() ]
275 # Just normalize. If the algebra is missing the roots, we can't add
276 # them here because then our subalgebra would have a bigger field
277 # than the superalgebra.
278 for i
in range(len(v
)):
279 v
[i
] = sc(v
[i
], ~
norm(v
[i
]))