]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/eja/eja_utils.py
c25b81921e1be4f0d6a77580227cb8692e21605f
1 from sage
.functions
.other
import sqrt
2 from sage
.matrix
.constructor
import matrix
3 from sage
.modules
.free_module_element
import vector
7 Scale the vector, matrix, or cartesian-product-of-those-things
10 if hasattr(x
, 'cartesian_factors'):
12 return P(tuple( _scale(x_i
, alpha
)
13 for x_i
in x
.cartesian_factors() ))
19 Flatten a vector, matrix, or cartesian product of those things
24 sage: from mjo.eja.eja_utils import _all2list
25 sage: V1 = VectorSpace(QQ,2)
26 sage: V2 = MatrixSpace(QQ,2)
30 sage: y2 = V2([0,1,1,0])
31 sage: _all2list((x1,y1))
33 sage: _all2list((x2,y2))
35 sage: M = cartesian_product([V1,V2])
36 sage: _all2list(M((x1,y1)))
38 sage: _all2list(M((x2,y2)))
42 if hasattr(x
, 'list'):
46 # But what if it's a tuple or something else? This has to
47 # handle cartesian products of cartesian products, too; that's
49 return sum( map(_all2list
,x
), [] )
52 return vector(m
.base_ring(), m
.list())
55 return matrix(v
.base_ring(), sqrt(v
.degree()), v
.list())
57 def gram_schmidt(v
, inner_product
=None):
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
66 sage: from mjo.eja.eja_utils import gram_schmidt
70 The usual inner-product and norm are default::
72 sage: v1 = vector(QQ,(1,2,3))
73 sage: v2 = vector(QQ,(1,-1,6))
74 sage: v3 = vector(QQ,(2,1,-1))
76 sage: u = gram_schmidt(v)
77 sage: all( u_i.inner_product(u_i).sqrt() == 1 for u_i in u )
79 sage: bool(u[0].inner_product(u[1]) == 0)
81 sage: bool(u[0].inner_product(u[2]) == 0)
83 sage: bool(u[1].inner_product(u[2]) == 0)
87 But if you supply a custom inner product, the result is
88 orthonormal with respect to that (and not the usual inner
91 sage: v1 = vector(QQ,(1,2,3))
92 sage: v2 = vector(QQ,(1,-1,6))
93 sage: v3 = vector(QQ,(2,1,-1))
95 sage: B = matrix(QQ, [ [6, 4, 2],
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 )
103 sage: ip(u[0],u[1]).is_zero()
105 sage: ip(u[0],u[2]).is_zero()
107 sage: ip(u[1],u[2]).is_zero()
110 This Gram-Schmidt routine can be used on matrices as well, so long
111 as an appropriate inner-product is provided::
113 sage: E11 = matrix(QQ, [ [1,0],
115 sage: E12 = matrix(QQ, [ [0,1],
117 sage: E22 = matrix(QQ, [ [0,0],
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)
123 [1 0] [ 0 1/2*sqrt(2)] [0 0]
124 [0 0], [1/2*sqrt(2) 0], [0 1]
127 It even works on Cartesian product spaces whose factors are vector
130 sage: V1 = VectorSpace(AA,2)
131 sage: V2 = MatrixSpace(AA,2)
132 sage: M = cartesian_product([V1,V2])
134 sage: x2 = V1([1,-1])
136 sage: y2 = V2([0,1,1,0])
137 sage: z1 = M((x1,y1))
138 sage: z2 = M((x2,y2))
140 ....: return a[0].inner_product(b[0]) + (a[1]*b[1]).trace()
141 sage: U = gram_schmidt([z1,z2], inner_product=ip)
151 Ensure that zero vectors don't get in the way::
153 sage: v1 = vector(QQ,(1,2,3))
154 sage: v2 = vector(QQ,(1,-1,6))
155 sage: v3 = vector(QQ,(0,0,0))
157 sage: len(gram_schmidt(v)) == 2
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()
164 v
= list(v
) # make a copy, don't clobber the input
166 # Drop all zero vectors before we start.
167 v
= [ v_i
for v_i
in v
if not v_i
.is_zero() ]
175 # Our "zero" needs to belong to the right space for sum() to work.
176 zero
= v
[0].parent().zero()
179 if hasattr(v
[0], 'cartesian_factors'):
180 # Only use the slow implementation if necessary.
184 return sc(x
, (inner_product(x
,y
)/inner_product(x
,x
)))
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
])
191 if not v
[j
].is_zero() ),
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() ]
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
]))