From a2dabad45525791c258a91e2134abbf5f5591dbe Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 23 Aug 2019 20:02:28 -0400 Subject: [PATCH] eja: add random_elements() method to get multiple random elements at once. --- mjo/eja/eja_algebra.py | 43 +++++++++++++++++++++++++++--------------- mjo/eja/eja_element.py | 38 ++++++++++--------------------------- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/mjo/eja/eja_algebra.py b/mjo/eja/eja_algebra.py index 29ff1da..1ae3286 100644 --- a/mjo/eja/eja_algebra.py +++ b/mjo/eja/eja_algebra.py @@ -5,6 +5,8 @@ are used in optimization, and have some additional nice methods beyond what can be supported in a general Jordan Algebra. """ +from itertools import repeat + from sage.algebras.quatalg.quaternion_algebra import QuaternionAlgebra from sage.categories.magmatic_algebras import MagmaticAlgebras from sage.combinat.free_module import CombinatorialFreeModule @@ -51,8 +53,7 @@ class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule): sage: set_random_seed() sage: J = random_eja() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: x*y == y*x True @@ -446,9 +447,7 @@ class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule): sage: set_random_seed() sage: J = random_eja() - sage: x = J.random_element() - sage: y = J.random_element() - sage: z = J.random_element() + sage: x,y,z = J.random_elements(3) sage: (x*y).inner_product(z) == y.inner_product(x*z) True @@ -657,6 +656,25 @@ class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule): s = super(FiniteDimensionalEuclideanJordanAlgebra, self) return s.random_element() + def random_elements(self, count): + """ + Return ``count`` random elements as a tuple. + + SETUP:: + + sage: from mjo.eja.eja_algebra import JordanSpinEJA + + EXAMPLES:: + + sage: J = JordanSpinEJA(3) + sage: x,y,z = J.random_elements(3) + sage: all( [ x in J, y in J, z in J ]) + True + sage: len( J.random_elements(10) ) == 10 + True + + """ + return tuple( self.random_element() for idx in xrange(count) ) @classmethod def random_instance(cls, field=QQ, **kwargs): @@ -824,8 +842,7 @@ class RealCartesianProductEJA(FiniteDimensionalEuclideanJordanAlgebra): sage: set_random_seed() sage: J = RealCartesianProductEJA.random_instance() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: X = x.natural_representation() sage: Y = y.natural_representation() sage: x.inner_product(y) == J.natural_inner_product(X,Y) @@ -1032,8 +1049,7 @@ class RealSymmetricEJA(MatrixEuclideanJordanAlgebra): sage: set_random_seed() sage: J = RealSymmetricEJA.random_instance() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: actual = (x*y).natural_representation() sage: X = x.natural_representation() sage: Y = y.natural_representation() @@ -1275,8 +1291,7 @@ class ComplexHermitianEJA(ComplexMatrixEuclideanJordanAlgebra): sage: set_random_seed() sage: J = ComplexHermitianEJA.random_instance() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: actual = (x*y).natural_representation() sage: X = x.natural_representation() sage: Y = y.natural_representation() @@ -1525,8 +1540,7 @@ class QuaternionHermitianEJA(QuaternionMatrixEuclideanJordanAlgebra): sage: set_random_seed() sage: J = QuaternionHermitianEJA.random_instance() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: actual = (x*y).natural_representation() sage: X = x.natural_representation() sage: Y = y.natural_representation() @@ -1691,8 +1705,7 @@ class JordanSpinEJA(FiniteDimensionalEuclideanJordanAlgebra): sage: set_random_seed() sage: J = JordanSpinEJA.random_instance() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: X = x.natural_representation() sage: Y = y.natural_representation() sage: x.inner_product(y) == J.natural_inner_product(X,Y) diff --git a/mjo/eja/eja_element.py b/mjo/eja/eja_element.py index f26766d..8a17c25 100644 --- a/mjo/eja/eja_element.py +++ b/mjo/eja/eja_element.py @@ -243,8 +243,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): sage: set_random_seed() sage: J = random_eja() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: x.inner_product(y) in RR True @@ -280,9 +279,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): Test Lemma 1 from Chapter III of Koecher:: sage: set_random_seed() - sage: J = random_eja() - sage: u = J.random_element() - sage: v = J.random_element() + sage: u,v = random_eja().random_elements(2) sage: lhs = u.operator_commutes_with(u*v) sage: rhs = v.operator_commutes_with(u^2) sage: lhs == rhs @@ -292,9 +289,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): Chapter III, or from Baes (2.3):: sage: set_random_seed() - sage: J = random_eja() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = random_eja().random_elements(2) sage: Lx = x.operator() sage: Ly = y.operator() sage: Lxx = (x*x).operator() @@ -306,10 +301,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): Baes (2.4):: sage: set_random_seed() - sage: J = random_eja() - sage: x = J.random_element() - sage: y = J.random_element() - sage: z = J.random_element() + sage: x,y,z = random_eja().random_elements(3) sage: Lx = x.operator() sage: Ly = y.operator() sage: Lz = z.operator() @@ -323,10 +315,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): Baes (2.5):: sage: set_random_seed() - sage: J = random_eja() - sage: u = J.random_element() - sage: y = J.random_element() - sage: z = J.random_element() + sage: u,y,z = random_eja().random_elements(3) sage: Lu = u.operator() sage: Ly = y.operator() sage: Lz = z.operator() @@ -388,8 +377,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): sage: set_random_seed() sage: J = random_eja().random_element().subalgebra_generated_by() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: (x*y).det() == x.det()*y.det() True @@ -885,8 +873,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): sage: set_random_seed() sage: J = random_eja() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: x.operator()(y) == x*y True sage: y.operator()(x) == x*y @@ -936,8 +923,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): sage: set_random_seed() sage: J = random_eja() - sage: x = J.random_element() - sage: y = J.random_element() + sage: x,y = J.random_elements(2) sage: Lx = x.operator() sage: Lxx = (x*x).operator() sage: Qx = x.quadratic_representation() @@ -1039,9 +1025,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): sage: set_random_seed() sage: x0 = random_eja().random_element() sage: A = x0.subalgebra_generated_by() - sage: x = A.random_element() - sage: y = A.random_element() - sage: z = A.random_element() + sage: x,y,z = A.random_elements(3) sage: (x*y)*z == x*(y*z) True @@ -1181,9 +1165,7 @@ class FiniteDimensionalEuclideanJordanAlgebraElement(IndexedFreeModuleElement): sage: set_random_seed() sage: J = random_eja() - sage: x = J.random_element(); - sage: y = J.random_element() - sage: z = J.random_element() + sage: x,y,z = J.random_elements(3) sage: # commutative sage: x.trace_inner_product(y) == y.trace_inner_product(x) True -- 2.44.2