]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/eja/eja_utils.py
eja: make gram_schmidt work in Cartesian product algebras.
[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 _all2list(x):
6 r"""
7 Flatten a vector, matrix, or cartesian product of those things
8 into a long list.
9 """
10 if hasattr(x, 'list'):
11 # Easy case...
12 return x.list()
13 if hasattr(x, 'cartesian_factors'):
14 # If it's a formal cartesian product space element, then
15 # we also know what to do...
16 return sum(( x_i.list() for x_i in x ), [])
17 else:
18 # But what if it's a tuple or something else?
19 return sum( map(_all2list,x), [] )
20
21 def _mat2vec(m):
22 return vector(m.base_ring(), m.list())
23
24 def _vec2mat(v):
25 return matrix(v.base_ring(), sqrt(v.degree()), v.list())
26
27 def gram_schmidt(v, inner_product=None):
28 """
29 Perform Gram-Schmidt on the list ``v`` which are assumed to be
30 vectors over the same base ring. Returns a list of orthonormalized
31 vectors over the smallest extention ring containing the necessary
32 roots.
33
34 SETUP::
35
36 sage: from mjo.eja.eja_utils import gram_schmidt
37
38 EXAMPLES:
39
40 The usual inner-product and norm are default::
41
42 sage: v1 = vector(QQ,(1,2,3))
43 sage: v2 = vector(QQ,(1,-1,6))
44 sage: v3 = vector(QQ,(2,1,-1))
45 sage: v = [v1,v2,v3]
46 sage: u = gram_schmidt(v)
47 sage: all( u_i.inner_product(u_i).sqrt() == 1 for u_i in u )
48 True
49 sage: bool(u[0].inner_product(u[1]) == 0)
50 True
51 sage: bool(u[0].inner_product(u[2]) == 0)
52 True
53 sage: bool(u[1].inner_product(u[2]) == 0)
54 True
55
56
57 But if you supply a custom inner product, the result is
58 orthonormal with respect to that (and not the usual inner
59 product)::
60
61 sage: v1 = vector(QQ,(1,2,3))
62 sage: v2 = vector(QQ,(1,-1,6))
63 sage: v3 = vector(QQ,(2,1,-1))
64 sage: v = [v1,v2,v3]
65 sage: B = matrix(QQ, [ [6, 4, 2],
66 ....: [4, 5, 4],
67 ....: [2, 4, 9] ])
68 sage: ip = lambda x,y: (B*x).inner_product(y)
69 sage: norm = lambda x: ip(x,x)
70 sage: u = gram_schmidt(v,ip)
71 sage: all( norm(u_i) == 1 for u_i in u )
72 True
73 sage: ip(u[0],u[1]).is_zero()
74 True
75 sage: ip(u[0],u[2]).is_zero()
76 True
77 sage: ip(u[1],u[2]).is_zero()
78 True
79
80 This Gram-Schmidt routine can be used on matrices as well, so long
81 as an appropriate inner-product is provided::
82
83 sage: E11 = matrix(QQ, [ [1,0],
84 ....: [0,0] ])
85 sage: E12 = matrix(QQ, [ [0,1],
86 ....: [1,0] ])
87 sage: E22 = matrix(QQ, [ [0,0],
88 ....: [0,1] ])
89 sage: I = matrix.identity(QQ,2)
90 sage: trace_ip = lambda X,Y: (X*Y).trace()
91 sage: gram_schmidt([E11,E12,I,E22], inner_product=trace_ip)
92 [
93 [1 0] [ 0 1/2*sqrt(2)] [0 0]
94 [0 0], [1/2*sqrt(2) 0], [0 1]
95 ]
96
97 TESTS:
98
99 Ensure that zero vectors don't get in the way::
100
101 sage: v1 = vector(QQ,(1,2,3))
102 sage: v2 = vector(QQ,(1,-1,6))
103 sage: v3 = vector(QQ,(0,0,0))
104 sage: v = [v1,v2,v3]
105 sage: len(gram_schmidt(v)) == 2
106 True
107
108 """
109 if inner_product is None:
110 inner_product = lambda x,y: x.inner_product(y)
111 norm = lambda x: inner_product(x,x).sqrt()
112
113 v = list(v) # make a copy, don't clobber the input
114
115 # Drop all zero vectors before we start.
116 v = [ v_i for v_i in v if not v_i.is_zero() ]
117
118 if len(v) == 0:
119 # cool
120 return v
121
122 R = v[0].base_ring()
123
124 # Define a scaling operation that can be used on tuples.
125 # Oh and our "zero" needs to belong to the right space.
126 scale = lambda x,alpha: x*alpha
127 zero = v[0].parent().zero()
128 if hasattr(v[0], 'cartesian_factors'):
129 P = v[0].parent()
130 scale = lambda x,alpha: P(tuple( x_i*alpha
131 for x_i in x.cartesian_factors() ))
132
133
134 def proj(x,y):
135 return scale(x, (inner_product(x,y)/inner_product(x,x)))
136
137 # First orthogonalize...
138 for i in range(1,len(v)):
139 # Earlier vectors can be made into zero so we have to ignore them.
140 v[i] -= sum( (proj(v[j],v[i])
141 for j in range(i)
142 if not v[j].is_zero() ),
143 zero )
144
145 # And now drop all zero vectors again if they were "orthogonalized out."
146 v = [ v_i for v_i in v if not v_i.is_zero() ]
147
148 # Just normalize. If the algebra is missing the roots, we can't add
149 # them here because then our subalgebra would have a bigger field
150 # than the superalgebra.
151 for i in range(len(v)):
152 v[i] = scale(v[i], ~norm(v[i]))
153
154 return v