]> gitweb.michael.orlitzky.com - sage.d.git/commitdiff
eja: further micro-optimize gram_schmidt().
authorMichael Orlitzky <michael@orlitzky.com>
Tue, 16 Mar 2021 01:19:27 +0000 (21:19 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Tue, 16 Mar 2021 01:19:27 +0000 (21:19 -0400)
mjo/eja/eja_utils.py

index 8334f516094fcbea657637da037f825c1cf4cbd2..a8abeff6be073b2b0aea3dd4f5af933c251666a5 100644 (file)
@@ -244,6 +244,7 @@ def gram_schmidt(v, inner_product=None):
         sage: v = [v1,v2,v3]
         sage: len(gram_schmidt(v)) == 2
         True
+
     """
     if len(v) == 0:
         # cool
@@ -254,14 +255,6 @@ def gram_schmidt(v, inner_product=None):
     if inner_product is None:
         inner_product = lambda x,y: x.inner_product(y)
 
-    def norm(x):
-        # Don't expand the given field; the inner-product's codomain
-        # is already correct. For example QQ(2).sqrt() returns sqrt(2)
-        # in SR, and that will give you weird errors about symbolics
-        # when what's really going wrong is that you're trying to
-        # orthonormalize in QQ.
-        return V.base_ring()(inner_product(x,x).sqrt())
-
     sc = lambda x,a: a*x
     if hasattr(V, 'cartesian_factors'):
         # Only use the slow implementation if necessary.
@@ -272,7 +265,12 @@ def gram_schmidt(v, inner_product=None):
         return sc(x, (inner_product(x,y)/inner_product(x,x)))
 
     def normalize(x):
-        return sc(x, ~norm(x))
+        # Don't extend the given field with the necessary
+        # square roots. This will probably throw weird
+        # errors about the symbolic ring if you e.g. try
+        # to use it on a set of rational vectors that isn't
+        # already orthonormalized.
+        return sc(x, ~inner_product(x,x).sqrt())
 
     v_out = [] # make a copy, don't clobber the input