]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/eja/eja_algebra.py
eja: fix random_instance() for JordanSpinEJA.
[sage.d.git] / mjo / eja / eja_algebra.py
index c822d14f6d0900a8b90c24d377fb31196003e0e8..4cbc88eb883d78bb5e85a9860b53330c9b980845 100644 (file)
@@ -2181,6 +2181,31 @@ class BilinearFormEJA(RationalBasisEuclideanJordanAlgebra):
     def _max_random_instance_size():
         return 5
 
+    @classmethod
+    def random_instance(cls, field=AA, **kwargs):
+        """
+        Return a random instance of this algebra.
+        """
+        n = ZZ.random_element(cls._max_random_instance_size() + 1)
+        if n == 0:
+            # Special case needed since we use (n-1) below.
+            B = matrix.identity(field, 0)
+            return cls(B, field, **kwargs)
+
+        B11 = matrix.identity(field,1)
+        M = matrix.random(field, n-1)
+        I = matrix.identity(field, n-1)
+        alpha = field.zero()
+        while alpha.is_zero():
+            alpha = field.random_element().abs()
+        B22 = M.transpose()*M + alpha*I
+
+        from sage.matrix.special import block_matrix
+        B = block_matrix(2,2, [ [B11,   ZZ(0) ],
+                                [ZZ(0), B22 ] ])
+
+        return cls(B, field, **kwargs)
+
     def inner_product(self, x, y):
         r"""
         Half of the trace inner product.
@@ -2200,16 +2225,11 @@ class BilinearFormEJA(RationalBasisEuclideanJordanAlgebra):
         paper::
 
             sage: set_random_seed()
-            sage: n = ZZ.random_element(2,5)
-            sage: M = matrix.random(QQ, max(0,n-1), algorithm='unimodular')
-            sage: B11 = matrix.identity(QQ,1)
-            sage: B22 = M.transpose()*M
-            sage: B = block_matrix(2,2,[ [B11,0  ],
-            ....:                        [0, B22 ] ])
-            sage: J = BilinearFormEJA(B)
+            sage: J = BilinearFormEJA.random_instance()
+            sage: n = J.dimension()
             sage: x = J.random_element()
             sage: y = J.random_element()
-            sage: x.inner_product(y) == (x*y).trace()/2
+            sage: (n == 1) or (x.inner_product(y) == (x*y).trace()/2)
             True
 
         """
@@ -2272,6 +2292,16 @@ class JordanSpinEJA(BilinearFormEJA):
         B = matrix.identity(field, n)
         super(JordanSpinEJA, self).__init__(B, field, **kwargs)
 
+    @classmethod
+    def random_instance(cls, field=AA, **kwargs):
+        """
+        Return a random instance of this type of algebra.
+
+        Needed here to override the implementation for ``BilinearFormEJA``.
+        """
+        n = ZZ.random_element(cls._max_random_instance_size() + 1)
+        return cls(n, field, **kwargs)
+
 
 class TrivialEJA(FiniteDimensionalEuclideanJordanAlgebra):
     """