From: Michael Orlitzky Date: Tue, 16 Mar 2021 01:19:27 +0000 (-0400) Subject: eja: further micro-optimize gram_schmidt(). X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=70fa00ef5bb0a1cd93567fb92dc224d721595252;p=sage.d.git eja: further micro-optimize gram_schmidt(). --- diff --git a/mjo/eja/eja_utils.py b/mjo/eja/eja_utils.py index 8334f51..a8abeff 100644 --- a/mjo/eja/eja_utils.py +++ b/mjo/eja/eja_utils.py @@ -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