From 70fa00ef5bb0a1cd93567fb92dc224d721595252 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 15 Mar 2021 21:19:27 -0400 Subject: [PATCH] eja: further micro-optimize gram_schmidt(). --- mjo/eja/eja_utils.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) 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 -- 2.43.2