From c4a42bd7c60a34353f481f86caeef3f1d1019754 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 2 Feb 2022 13:43:53 -0500 Subject: [PATCH] hurwitz.py: speed up conjugate() and conjugate_transpose(). ...by staying as close to the underlying monomial dict as possible. --- mjo/hurwitz.py | 66 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/mjo/hurwitz.py b/mjo/hurwitz.py index 614804f..07eace6 100644 --- a/mjo/hurwitz.py +++ b/mjo/hurwitz.py @@ -319,11 +319,21 @@ class HurwitzMatrixAlgebraElement(MatrixAlgebraElement): | -3*I | -4*I | +------+----------+ + :: + + sage: A = ComplexMatrixAlgebra(2, QQbar, QQ) + sage: M = A([ [ 1, 2], + ....: [ 3, 4] ]) + sage: M.conjugate() == M + True + sage: M.to_vector() + (1, 0, 2, 0, 3, 0, 4, 0) + """ - entries = [ [ self[i,j].conjugate() - for j in range(self.ncols())] - for i in range(self.nrows()) ] - return self.parent()._element_constructor_(entries) + d = self.monomial_coefficients() + A = self.parent() + new_terms = ( A._conjugate_term((k,v)) for (k,v) in d.items() ) + return self.parent().sum_of_terms(new_terms) def conjugate_transpose(self): r""" @@ -348,10 +358,11 @@ class HurwitzMatrixAlgebraElement(MatrixAlgebraElement): (0, -1, 0, -3, 0, -2, 0, -4) """ - entries = [ [ self[j,i].conjugate() - for j in range(self.ncols())] - for i in range(self.nrows()) ] - return self.parent()._element_constructor_(entries) + d = self.monomial_coefficients() + A = self.parent() + new_terms = ( A._conjugate_term( ((k[1],k[0],k[2]), v) ) + for (k,v) in d.items() ) + return self.parent().sum_of_terms(new_terms) def is_hermitian(self): r""" @@ -467,6 +478,45 @@ class HurwitzMatrixAlgebra(MatrixAlgebra): super().__init__(n, entry_algebra, scalars, **kwargs) + + @staticmethod + def _conjugate_term(t): + r""" + Conjugate the given ``(index, coefficient)`` term, returning + another such term. + + Given a term ``((i,j,e), c)``, it's straightforward to + conjugate the entry ``e``, but if ``e``-conjugate is ``-e``, + then the resulting ``((i,j,-e), c)`` is not a term, since + ``(i,j,-e)`` is not a monomial index! So when we build a sum + of these conjugates we can wind up with a nonsense object. + + This function handles the case where ``e``-conjugate is + ``-e``, but nothing more complicated. Thus it makes sense in + Hurwitz matrix algebras, but not more generally. + + SETUP:: + + sage: from mjo.hurwitz import ComplexMatrixAlgebra + + EXAMPLES:: + + sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ) + sage: M = A([ [ I, 1 + 2*I], + ....: [ 3*I, 4*I] ]) + sage: t = list(M.monomial_coefficients().items())[1] + sage: t + ((1, 0, I), 3) + sage: A._conjugate_term(t) + ((1, 0, I), -3) + + """ + if t[0][2].conjugate() == t[0][2]: + return t + else: + return (t[0], -t[1]) + + def entry_algebra_gens(self): r""" Return a tuple of the generators of (that is, a basis for) the -- 2.44.2