]>
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
7 Flatten a vector, matrix, or cartesian product of those things
12 sage: from mjo.eja.eja_utils import _all2list
13 sage: V1 = VectorSpace(QQ,2)
14 sage: V2 = MatrixSpace(QQ,2)
18 sage: y2 = V2([0,1,1,0])
19 sage: _all2list((x1,y1))
21 sage: _all2list((x2,y2))
23 sage: M = cartesian_product([V1,V2])
24 sage: _all2list(M((x1,y1)))
26 sage: _all2list(M((x2,y2)))
30 if hasattr(x
, 'list'):
34 # But what if it's a tuple or something else? This has to
35 # handle cartesian products of cartesian products, too; that's
37 return sum( map(_all2list
,x
), [] )
40 return vector(m
.base_ring(), m
.list())
43 return matrix(v
.base_ring(), sqrt(v
.degree()), v
.list())
45 def gram_schmidt(v
, inner_product
=None):
47 Perform Gram-Schmidt on the list ``v`` which are assumed to be
48 vectors over the same base ring. Returns a list of orthonormalized
49 vectors over the smallest extention ring containing the necessary
54 sage: from mjo.eja.eja_utils import gram_schmidt
58 The usual inner-product and norm are default::
60 sage: v1 = vector(QQ,(1,2,3))
61 sage: v2 = vector(QQ,(1,-1,6))
62 sage: v3 = vector(QQ,(2,1,-1))
64 sage: u = gram_schmidt(v)
65 sage: all( u_i.inner_product(u_i).sqrt() == 1 for u_i in u )
67 sage: bool(u[0].inner_product(u[1]) == 0)
69 sage: bool(u[0].inner_product(u[2]) == 0)
71 sage: bool(u[1].inner_product(u[2]) == 0)
75 But if you supply a custom inner product, the result is
76 orthonormal with respect to that (and not the usual inner
79 sage: v1 = vector(QQ,(1,2,3))
80 sage: v2 = vector(QQ,(1,-1,6))
81 sage: v3 = vector(QQ,(2,1,-1))
83 sage: B = matrix(QQ, [ [6, 4, 2],
86 sage: ip = lambda x,y: (B*x).inner_product(y)
87 sage: norm = lambda x: ip(x,x)
88 sage: u = gram_schmidt(v,ip)
89 sage: all( norm(u_i) == 1 for u_i in u )
91 sage: ip(u[0],u[1]).is_zero()
93 sage: ip(u[0],u[2]).is_zero()
95 sage: ip(u[1],u[2]).is_zero()
98 This Gram-Schmidt routine can be used on matrices as well, so long
99 as an appropriate inner-product is provided::
101 sage: E11 = matrix(QQ, [ [1,0],
103 sage: E12 = matrix(QQ, [ [0,1],
105 sage: E22 = matrix(QQ, [ [0,0],
107 sage: I = matrix.identity(QQ,2)
108 sage: trace_ip = lambda X,Y: (X*Y).trace()
109 sage: gram_schmidt([E11,E12,I,E22], inner_product=trace_ip)
111 [1 0] [ 0 1/2*sqrt(2)] [0 0]
112 [0 0], [1/2*sqrt(2) 0], [0 1]
115 It even works on Cartesian product spaces whose factors are vector
118 sage: V1 = VectorSpace(AA,2)
119 sage: V2 = MatrixSpace(AA,2)
120 sage: M = cartesian_product([V1,V2])
122 sage: x2 = V1([1,-1])
124 sage: y2 = V2([0,1,1,0])
125 sage: z1 = M((x1,y1))
126 sage: z2 = M((x2,y2))
128 ....: return a[0].inner_product(b[0]) + (a[1]*b[1]).trace()
129 sage: U = gram_schmidt([z1,z2], inner_product=ip)
139 Ensure that zero vectors don't get in the way::
141 sage: v1 = vector(QQ,(1,2,3))
142 sage: v2 = vector(QQ,(1,-1,6))
143 sage: v3 = vector(QQ,(0,0,0))
145 sage: len(gram_schmidt(v)) == 2
148 if inner_product
is None:
149 inner_product
= lambda x
,y
: x
.inner_product(y
)
150 norm
= lambda x
: inner_product(x
,x
).sqrt()
152 v
= list(v
) # make a copy, don't clobber the input
154 # Drop all zero vectors before we start.
155 v
= [ v_i
for v_i
in v
if not v_i
.is_zero() ]
163 # Define a scaling operation that can be used on tuples.
164 # Oh and our "zero" needs to belong to the right space.
165 scale
= lambda x
,alpha
: x
*alpha
166 zero
= v
[0].parent().zero()
167 if hasattr(v
[0], 'cartesian_factors'):
169 scale
= lambda x
,alpha
: P(tuple( x_i
*alpha
170 for x_i
in x
.cartesian_factors() ))
174 return scale(x
, (inner_product(x
,y
)/inner_product(x
,x
)))
176 # First orthogonalize...
177 for i
in range(1,len(v
)):
178 # Earlier vectors can be made into zero so we have to ignore them.
179 v
[i
] -= sum( (proj(v
[j
],v
[i
])
181 if not v
[j
].is_zero() ),
184 # And now drop all zero vectors again if they were "orthogonalized out."
185 v
= [ v_i
for v_i
in v
if not v_i
.is_zero() ]
187 # Just normalize. If the algebra is missing the roots, we can't add
188 # them here because then our subalgebra would have a bigger field
189 # than the superalgebra.
190 for i
in range(len(v
)):
191 v
[i
] = scale(v
[i
], ~
norm(v
[i
]))