]>
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
10 if hasattr(x
, 'list'):
14 # But what if it's a tuple or something else? This has to
15 # handle cartesian products of cartesian products, too; that's
17 return sum( map(_all2list
,x
), [] )
20 return vector(m
.base_ring(), m
.list())
23 return matrix(v
.base_ring(), sqrt(v
.degree()), v
.list())
25 def gram_schmidt(v
, inner_product
=None):
27 Perform Gram-Schmidt on the list ``v`` which are assumed to be
28 vectors over the same base ring. Returns a list of orthonormalized
29 vectors over the smallest extention ring containing the necessary
34 sage: from mjo.eja.eja_utils import gram_schmidt
38 The usual inner-product and norm are default::
40 sage: v1 = vector(QQ,(1,2,3))
41 sage: v2 = vector(QQ,(1,-1,6))
42 sage: v3 = vector(QQ,(2,1,-1))
44 sage: u = gram_schmidt(v)
45 sage: all( u_i.inner_product(u_i).sqrt() == 1 for u_i in u )
47 sage: bool(u[0].inner_product(u[1]) == 0)
49 sage: bool(u[0].inner_product(u[2]) == 0)
51 sage: bool(u[1].inner_product(u[2]) == 0)
55 But if you supply a custom inner product, the result is
56 orthonormal with respect to that (and not the usual inner
59 sage: v1 = vector(QQ,(1,2,3))
60 sage: v2 = vector(QQ,(1,-1,6))
61 sage: v3 = vector(QQ,(2,1,-1))
63 sage: B = matrix(QQ, [ [6, 4, 2],
66 sage: ip = lambda x,y: (B*x).inner_product(y)
67 sage: norm = lambda x: ip(x,x)
68 sage: u = gram_schmidt(v,ip)
69 sage: all( norm(u_i) == 1 for u_i in u )
71 sage: ip(u[0],u[1]).is_zero()
73 sage: ip(u[0],u[2]).is_zero()
75 sage: ip(u[1],u[2]).is_zero()
78 This Gram-Schmidt routine can be used on matrices as well, so long
79 as an appropriate inner-product is provided::
81 sage: E11 = matrix(QQ, [ [1,0],
83 sage: E12 = matrix(QQ, [ [0,1],
85 sage: E22 = matrix(QQ, [ [0,0],
87 sage: I = matrix.identity(QQ,2)
88 sage: trace_ip = lambda X,Y: (X*Y).trace()
89 sage: gram_schmidt([E11,E12,I,E22], inner_product=trace_ip)
91 [1 0] [ 0 1/2*sqrt(2)] [0 0]
92 [0 0], [1/2*sqrt(2) 0], [0 1]
97 Ensure that zero vectors don't get in the way::
99 sage: v1 = vector(QQ,(1,2,3))
100 sage: v2 = vector(QQ,(1,-1,6))
101 sage: v3 = vector(QQ,(0,0,0))
103 sage: len(gram_schmidt(v)) == 2
107 if inner_product
is None:
108 inner_product
= lambda x
,y
: x
.inner_product(y
)
109 norm
= lambda x
: inner_product(x
,x
).sqrt()
111 v
= list(v
) # make a copy, don't clobber the input
113 # Drop all zero vectors before we start.
114 v
= [ v_i
for v_i
in v
if not v_i
.is_zero() ]
122 # Define a scaling operation that can be used on tuples.
123 # Oh and our "zero" needs to belong to the right space.
124 scale
= lambda x
,alpha
: x
*alpha
125 zero
= v
[0].parent().zero()
126 if hasattr(v
[0], 'cartesian_factors'):
128 scale
= lambda x
,alpha
: P(tuple( x_i
*alpha
129 for x_i
in x
.cartesian_factors() ))
133 return scale(x
, (inner_product(x
,y
)/inner_product(x
,x
)))
135 # First orthogonalize...
136 for i
in range(1,len(v
)):
137 # Earlier vectors can be made into zero so we have to ignore them.
138 v
[i
] -= sum( (proj(v
[j
],v
[i
])
140 if not v
[j
].is_zero() ),
143 # And now drop all zero vectors again if they were "orthogonalized out."
144 v
= [ v_i
for v_i
in v
if not v_i
.is_zero() ]
146 # Just normalize. If the algebra is missing the roots, we can't add
147 # them here because then our subalgebra would have a bigger field
148 # than the superalgebra.
149 for i
in range(len(v
)):
150 v
[i
] = scale(v
[i
], ~
norm(v
[i
]))