From ece9fa6ee0724db795df27797ca8db7e112176f2 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 28 Feb 2021 16:43:02 -0500 Subject: [PATCH] eja: massively speed up the trace inner-product in matrix algebras. --- mjo/eja/eja_algebra.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/mjo/eja/eja_algebra.py b/mjo/eja/eja_algebra.py index 5bb4cee..a765e97 100644 --- a/mjo/eja/eja_algebra.py +++ b/mjo/eja/eja_algebra.py @@ -1837,18 +1837,13 @@ class MatrixEJA: True """ - Xu = cls.real_unembed(X) - Yu = cls.real_unembed(Y) - tr = (Xu*Yu).trace() - - try: - # Works in QQ, AA, RDF, et cetera. - return tr.real() - except AttributeError: - # A quaternion doesn't have a real() method, but does - # have coefficient_tuple() method that returns the - # coefficients of 1, i, j, and k -- in that order. - return tr.coefficient_tuple()[0] + # This does in fact compute the real part of the trace. + # If we compute the trace of e.g. a complex matrix M, + # then we do so by adding up its diagonal entries -- + # call them z_1 through z_n. The real embedding of z_1 + # will be a 2-by-2 REAL matrix [a, b; -b, a] whose trace + # as a REAL matrix will be 2*a = 2*Re(z_1). And so forth. + return (X*Y).trace()/cls.dimension_over_reals() class RealMatrixEJA(MatrixEJA): -- 2.43.2