- Why do we embed these? Basically, because all of numerical linear
- algebra assumes that you're working with vectors consisting of `n`
- entries from a field and scalars from the same field. There's no way
- to tell SageMath that (for example) the vectors contain complex
- numbers, while the scalar field is real.
-
- SETUP::
-
- sage: from mjo.eja.eja_algebra import ComplexHermitianEJA
-
- TESTS::
-
- sage: set_random_seed()
- sage: n = ZZ.random_element(1,5)
- sage: B = ComplexHermitianEJA._denormalized_basis(n,ZZ)
- sage: all( M.is_symmetric() for M in B)
- True
-
- """
- R = PolynomialRing(ZZ, 'z')
- z = R.gen()
- F = ZZ.extension(z**2 + 1, 'I')
- I = F.gen(1)
-
- # This is like the symmetric case, but we need to be careful:
- #
- # * We want conjugate-symmetry, not just symmetry.
- # * The diagonal will (as a result) be real.
- #
- S = []
- Eij = matrix.zero(F,n)
- for i in range(n):
- for j in range(i+1):
- # "build" E_ij
- Eij[i,j] = 1
- if i == j:
- Sij = cls.real_embed(Eij)
- S.append(Sij)
- else:
- # The second one has a minus because it's conjugated.
- Eij[j,i] = 1 # Eij = Eij + Eij.transpose()
- Sij_real = cls.real_embed(Eij)
- S.append(Sij_real)
- # Eij = I*Eij - I*Eij.transpose()
- Eij[i,j] = I
- Eij[j,i] = -I
- Sij_imag = cls.real_embed(Eij)
- S.append(Sij_imag)
- Eij[j,i] = 0
- # "erase" E_ij
- Eij[i,j] = 0
-
- # Since we embedded the entries, we can drop back to the
- # desired real "field" instead of the extension "F".
- return tuple( s.change_ring(field) for s in S )
-
-
- def __init__(self, n, field=AA, **kwargs):
- # We know this is a valid EJA, but will double-check
- # if the user passes check_axioms=True.
- if "check_axioms" not in kwargs: kwargs["check_axioms"] = False
-
- associative = False
- if n <= 1:
- associative = True
-
- super().__init__(self._denormalized_basis(n,field),
- self.jordan_product,
- self.trace_inner_product,
- field=field,
- associative=associative,
- **kwargs)
- # TODO: this could be factored out somehow, but is left here
- # because the MatrixEJA is not presently a subclass of the
- # FDEJA class that defines rank() and one().
- self.rank.set_cache(n)
- idV = matrix.identity(ZZ, self.dimension_over_reals()*n)
- self.one.set_cache(self(idV))
-
- @staticmethod
- def _max_random_instance_size():
- return 3 # Dimension 9
-
- @classmethod
- def random_instance(cls, **kwargs):
- """
- Return a random instance of this type of algebra.
- """
- n = ZZ.random_element(cls._max_random_instance_size() + 1)
- return cls(n, **kwargs)
-
-class QuaternionMatrixEJA(RealEmbeddedMatrixEJA):
-
- # A manual dictionary-cache for the quaternion_extension() method,
- # since apparently @classmethods can't also be @cached_methods.
- _quaternion_extension = {}
-
- @classmethod
- def quaternion_extension(cls,field):
- r"""
- The quaternion field that we embed/unembed, as an extension
- of the given ``field``.
- """
- if field in cls._quaternion_extension:
- return cls._quaternion_extension[field]
-
- Q = QuaternionAlgebra(field,-1,-1)
-
- cls._quaternion_extension[field] = Q
- return Q
-
- @staticmethod
- def dimension_over_reals():
- return 4
-
- @classmethod
- def real_embed(cls,M):
- """
- Embed the n-by-n quaternion matrix ``M`` into the space of real
- matrices of size 4n-by-4n by first sending each quaternion entry `z
- = a + bi + cj + dk` to the block-complex matrix ``[[a + bi,
- c+di],[-c + di, a-bi]]`, and then embedding those into a real
- matrix.
-
- SETUP::
-
- sage: from mjo.eja.eja_algebra import QuaternionMatrixEJA
-
- EXAMPLES::
-
- sage: Q = QuaternionAlgebra(QQ,-1,-1)
- sage: i,j,k = Q.gens()
- sage: x = 1 + 2*i + 3*j + 4*k
- sage: M = matrix(Q, 1, [[x]])
- sage: QuaternionMatrixEJA.real_embed(M)
- [ 1 2 3 4]
- [-2 1 -4 3]
- [-3 4 1 -2]
- [-4 -3 2 1]
-
- Embedding is a homomorphism (isomorphism, in fact)::
-
- sage: set_random_seed()
- sage: n = ZZ.random_element(2)
- sage: Q = QuaternionAlgebra(QQ,-1,-1)
- sage: X = random_matrix(Q, n)
- sage: Y = random_matrix(Q, n)
- sage: Xe = QuaternionMatrixEJA.real_embed(X)
- sage: Ye = QuaternionMatrixEJA.real_embed(Y)
- sage: XYe = QuaternionMatrixEJA.real_embed(X*Y)
- sage: Xe*Ye == XYe
- True
-
- """
- super().real_embed(M)
- quaternions = M.base_ring()
- n = M.nrows()
-
- F = QuadraticField(-1, 'I')
- i = F.gen()
-
- blocks = []
- for z in M.list():
- t = z.coefficient_tuple()
- a = t[0]
- b = t[1]
- c = t[2]
- d = t[3]
- cplxM = matrix(F, 2, [[ a + b*i, c + d*i],
- [-c + d*i, a - b*i]])
- realM = ComplexMatrixEJA.real_embed(cplxM)
- blocks.append(realM)
-
- # We should have real entries by now, so use the realest field
- # we've got for the return value.
- return matrix.block(quaternions.base_ring(), n, blocks)
-
-
-
- @classmethod
- def real_unembed(cls,M):
- """
- The inverse of _embed_quaternion_matrix().
-
- SETUP::
-
- sage: from mjo.eja.eja_algebra import QuaternionMatrixEJA
-
- EXAMPLES::
-
- sage: M = matrix(QQ, [[ 1, 2, 3, 4],
- ....: [-2, 1, -4, 3],
- ....: [-3, 4, 1, -2],
- ....: [-4, -3, 2, 1]])
- sage: QuaternionMatrixEJA.real_unembed(M)
- [1 + 2*i + 3*j + 4*k]
-
- TESTS:
-
- Unembedding is the inverse of embedding::
-
- sage: set_random_seed()
- sage: Q = QuaternionAlgebra(QQ, -1, -1)
- sage: M = random_matrix(Q, 3)
- sage: Me = QuaternionMatrixEJA.real_embed(M)
- sage: QuaternionMatrixEJA.real_unembed(Me) == M
- True