]>
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 Scale the vector, matrix, or cartesian-product-of-those-things
10 This works around the inability to scale certain elements of
11 Cartesian product spaces, as reported in
13 https://trac.sagemath.org/ticket/31435
17 This will do the wrong thing if you feed it a tuple or list.
21 sage: from mjo.eja.eja_utils import _scale
25 sage: v = vector(QQ, (1,2,3))
28 sage: m = matrix(QQ, [[1,2],[3,4]])
29 sage: M = cartesian_product([m.parent(), m.parent()])
30 sage: _scale(M((m,m)), 2)
36 if hasattr(x
, 'cartesian_factors'):
38 return P(tuple( _scale(x_i
, alpha
)
39 for x_i
in x
.cartesian_factors() ))
46 Flatten a vector, matrix, or cartesian product of those things
51 sage: from mjo.eja.eja_utils import _all2list
52 sage: V1 = VectorSpace(QQ,2)
53 sage: V2 = MatrixSpace(QQ,2)
57 sage: y2 = V2([0,1,1,0])
58 sage: _all2list((x1,y1))
60 sage: _all2list((x2,y2))
62 sage: M = cartesian_product([V1,V2])
63 sage: _all2list(M((x1,y1)))
65 sage: _all2list(M((x2,y2)))
69 if hasattr(x
, 'list'):
73 # But what if it's a tuple or something else? This has to
74 # handle cartesian products of cartesian products, too; that's
76 return sum( map(_all2list
,x
), [] )
79 return vector(m
.base_ring(), m
.list())
82 return matrix(v
.base_ring(), sqrt(v
.degree()), v
.list())
84 def gram_schmidt(v
, inner_product
=None):
86 Perform Gram-Schmidt on the list ``v`` which are assumed to be
87 vectors over the same base ring. Returns a list of orthonormalized
88 vectors over the smallest extention ring containing the necessary
93 sage: from mjo.eja.eja_utils import gram_schmidt
97 The usual inner-product and norm are default::
99 sage: v1 = vector(QQ,(1,2,3))
100 sage: v2 = vector(QQ,(1,-1,6))
101 sage: v3 = vector(QQ,(2,1,-1))
103 sage: u = gram_schmidt(v)
104 sage: all( u_i.inner_product(u_i).sqrt() == 1 for u_i in u )
106 sage: bool(u[0].inner_product(u[1]) == 0)
108 sage: bool(u[0].inner_product(u[2]) == 0)
110 sage: bool(u[1].inner_product(u[2]) == 0)
114 But if you supply a custom inner product, the result is
115 orthonormal with respect to that (and not the usual inner
118 sage: v1 = vector(QQ,(1,2,3))
119 sage: v2 = vector(QQ,(1,-1,6))
120 sage: v3 = vector(QQ,(2,1,-1))
122 sage: B = matrix(QQ, [ [6, 4, 2],
125 sage: ip = lambda x,y: (B*x).inner_product(y)
126 sage: norm = lambda x: ip(x,x)
127 sage: u = gram_schmidt(v,ip)
128 sage: all( norm(u_i) == 1 for u_i in u )
130 sage: ip(u[0],u[1]).is_zero()
132 sage: ip(u[0],u[2]).is_zero()
134 sage: ip(u[1],u[2]).is_zero()
137 This Gram-Schmidt routine can be used on matrices as well, so long
138 as an appropriate inner-product is provided::
140 sage: E11 = matrix(QQ, [ [1,0],
142 sage: E12 = matrix(QQ, [ [0,1],
144 sage: E22 = matrix(QQ, [ [0,0],
146 sage: I = matrix.identity(QQ,2)
147 sage: trace_ip = lambda X,Y: (X*Y).trace()
148 sage: gram_schmidt([E11,E12,I,E22], inner_product=trace_ip)
150 [1 0] [ 0 1/2*sqrt(2)] [0 0]
151 [0 0], [1/2*sqrt(2) 0], [0 1]
154 It even works on Cartesian product spaces whose factors are vector
157 sage: V1 = VectorSpace(AA,2)
158 sage: V2 = MatrixSpace(AA,2)
159 sage: M = cartesian_product([V1,V2])
161 sage: x2 = V1([1,-1])
163 sage: y2 = V2([0,1,1,0])
164 sage: z1 = M((x1,y1))
165 sage: z2 = M((x2,y2))
167 ....: return a[0].inner_product(b[0]) + (a[1]*b[1]).trace()
168 sage: U = gram_schmidt([z1,z2], inner_product=ip)
178 Ensure that zero vectors don't get in the way::
180 sage: v1 = vector(QQ,(1,2,3))
181 sage: v2 = vector(QQ,(1,-1,6))
182 sage: v3 = vector(QQ,(0,0,0))
184 sage: len(gram_schmidt(v)) == 2
187 if inner_product
is None:
188 inner_product
= lambda x
,y
: x
.inner_product(y
)
189 norm
= lambda x
: inner_product(x
,x
).sqrt()
191 v
= list(v
) # make a copy, don't clobber the input
193 # Drop all zero vectors before we start.
194 v
= [ v_i
for v_i
in v
if not v_i
.is_zero() ]
202 # Our "zero" needs to belong to the right space for sum() to work.
203 zero
= v
[0].parent().zero()
206 if hasattr(v
[0], 'cartesian_factors'):
207 # Only use the slow implementation if necessary.
211 return sc(x
, (inner_product(x
,y
)/inner_product(x
,x
)))
213 # First orthogonalize...
214 for i
in range(1,len(v
)):
215 # Earlier vectors can be made into zero so we have to ignore them.
216 v
[i
] -= sum( (proj(v
[j
],v
[i
])
218 if not v
[j
].is_zero() ),
221 # And now drop all zero vectors again if they were "orthogonalized out."
222 v
= [ v_i
for v_i
in v
if not v_i
.is_zero() ]
224 # Just normalize. If the algebra is missing the roots, we can't add
225 # them here because then our subalgebra would have a bigger field
226 # than the superalgebra.
227 for i
in range(len(v
)):
228 v
[i
] = sc(v
[i
], ~
norm(v
[i
]))