From 07f5168dc8e9434171ab89d7089330c561da4380 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 8 Nov 2020 08:43:37 -0500 Subject: [PATCH] eja: make random_element() more random. --- mjo/eja/TODO | 2 -- mjo/eja/eja_algebra.py | 52 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/mjo/eja/TODO b/mjo/eja/TODO index 32e78d4..985e50a 100644 --- a/mjo/eja/TODO +++ b/mjo/eja/TODO @@ -8,5 +8,3 @@ 5. Factor out the unit-norm basis (and operator symmetry) tests once all of the algebras pass. - -6. The EJA random element method only returns two summands by default. \ No newline at end of file diff --git a/mjo/eja/eja_algebra.py b/mjo/eja/eja_algebra.py index ed2e258..91088a2 100644 --- a/mjo/eja/eja_algebra.py +++ b/mjo/eja/eja_algebra.py @@ -672,10 +672,57 @@ class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule): return (J0, J5, J1) - def random_elements(self, count): + def random_element(self, thorough=False): + r""" + Return a random element of this algebra. + + Our algebra superclass method only returns a linear + combination of at most two basis elements. We instead + want the vector space "random element" method that + returns a more diverse selection. + + INPUT: + + - ``thorough`` -- (boolean; default False) whether or not we + should generate irrational coefficients for the random + element when our base ring is irrational; this slows the + algebra operations to a crawl, but any truly random method + should include them + + """ + # For a general base ring... maybe we can trust this to do the + # right thing? Unlikely, but. + V = self.vector_space() + v = V.random_element() + + if self.base_ring() is AA: + # The "random element" method of the algebraic reals is + # stupid at the moment, and only returns integers between + # -2 and 2, inclusive. Instead, we implement our own + # "random vector" method, and then coerce that into the + # algebra. We use the vector space degree here instead of + # the dimension because a subalgebra could (for example) be + # spanned by only two vectors, each with five coordinates. + # We need to generate all five coordinates. + if thorough: + v *= QQbar.random_element().real() + else: + v *= QQ.random_element() + + return self.from_vector(V.coordinate_vector(v)) + + def random_elements(self, count, thorough=False): """ Return ``count`` random elements as a tuple. + INPUT: + + - ``thorough`` -- (boolean; default False) whether or not we + should generate irrational coefficients for the random + elements when our base ring is irrational; this slows the + algebra operations to a crawl, but any truly random method + should include them + SETUP:: sage: from mjo.eja.eja_algebra import JordanSpinEJA @@ -690,7 +737,8 @@ class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule): True """ - return tuple( self.random_element() for idx in range(count) ) + return tuple( self.random_element(thorough) + for idx in range(count) ) @classmethod def random_instance(cls, field=AA, **kwargs): -- 2.44.2