From 4bca351aed81d3ee621b530e7e802122b08bd2a6 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 22 Nov 2020 10:50:19 -0500 Subject: [PATCH] eja: cache the unit element immediately where it is known. --- mjo/eja/eja_algebra.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/mjo/eja/eja_algebra.py b/mjo/eja/eja_algebra.py index ec692e4..e436bf1 100644 --- a/mjo/eja/eja_algebra.py +++ b/mjo/eja/eja_algebra.py @@ -588,6 +588,16 @@ class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule): sage: actual == expected True + Ensure that the cached unit element (often precomputed by + hand) agrees with the computed one:: + + sage: set_random_seed() + sage: J = random_eja() + sage: cached = J.one() + sage: J.one.clear_cache() + sage: J.one() == cached + True + """ # We can brute-force compute the matrices of the operators # that correspond to the basis elements of this algebra. @@ -1108,7 +1118,8 @@ class MatrixEuclideanJordanAlgebra(FiniteDimensionalEuclideanJordanAlgebra): # time to ensure that it isn't a generator expression. basis = tuple(basis) - if len(basis) > 1 and normalize_basis: + algebra_dim = len(basis) + if algebra_dim > 1 and normalize_basis: # We'll need sqrt(2) to normalize the basis, and this # winds up in the multiplication table, so the whole # algebra needs to be over the field extension. @@ -1129,6 +1140,14 @@ class MatrixEuclideanJordanAlgebra(FiniteDimensionalEuclideanJordanAlgebra): natural_basis=basis, **kwargs) + if algebra_dim == 0: + self.one.set_cache(self.zero()) + else: + n = basis[0].nrows() + # The identity wrt (A,B) -> (AB + BA)/2 is independent of the + # details of this algebra. + self.one.set_cache(self(matrix.identity(field,n))) + @cached_method def _charpoly_coefficients(self): @@ -2051,6 +2070,7 @@ class HadamardEJA(RationalBasisEuclideanJordanAlgebra): check_axioms=False, **kwargs) self.rank.set_cache(n) + self.one.set_cache( sum(self.gens()) ) @staticmethod def _max_random_instance_size(): @@ -2191,6 +2211,11 @@ class BilinearFormEJA(RationalBasisEuclideanJordanAlgebra): **kwargs) self.rank.set_cache(min(n,2)) + if n == 0: + self.one.set_cache( self.zero() ) + else: + self.one.set_cache( self.monomial(0) ) + @staticmethod def _max_random_instance_size(): return 5 @@ -2354,6 +2379,7 @@ class TrivialEJA(FiniteDimensionalEuclideanJordanAlgebra): # The rank is zero using my definition, namely the dimension of the # largest subalgebra generated by any element. self.rank.set_cache(0) + self.one.set_cache( self.zero() ) @classmethod def random_instance(cls, field=AA, **kwargs): -- 2.43.2