1 from sage
.misc
.table
import table
2 from sage
.categories
.magmatic_algebras
import MagmaticAlgebras
3 from sage
.misc
.cachefunc
import cached_method
4 from sage
.combinat
.free_module
import CombinatorialFreeModule
5 from sage
.modules
.with_basis
.indexed_element
import IndexedFreeModuleElement
7 class MatrixAlgebraElement(IndexedFreeModuleElement
):
9 return self
.parent().nrows()
17 sage: from mjo.matrix_algebra import MatrixAlgebra
21 sage: M = MatrixAlgebra(QQbar,RDF,2)
22 sage: A = M.monomial((0,0,1)) + 4*M.monomial((0,1,1))
33 zero
= self
.parent().entry_algebra().zero()
34 l
= [[zero
for j
in range(self
.ncols())] for i
in range(self
.nrows())]
35 for (k
,v
) in self
.monomial_coefficients().items():
42 Display this matrix as a table.
44 The SageMath Matrix class representation is not easily reusable,
45 but using a table fakes it.
49 sage: from mjo.matrix_algebra import MatrixAlgebra
53 sage: MatrixAlgebra(ZZ,ZZ,2).zero()
61 return table(self
.rows(), frame
=True)._repr
_()
66 Return one long list of this matrix's entries.
70 sage: from mjo.matrix_algebra import MatrixAlgebra
74 sage: A = MatrixAlgebra(ZZ,ZZ,2)
75 sage: A([[1,2],[3,4]]).list()
79 return sum( self
.rows(), [] )
82 def __getitem__(self
, indices
):
87 sage: from mjo.matrix_algebra import MatrixAlgebra
91 sage: M = MatrixAlgebra(ZZ,ZZ,2)([[1,2],[3,4]])
103 return self
.rows()[i
][j
]
107 Return the sum of this matrix's diagonal entries.
111 sage: from mjo.matrix_algebra import MatrixAlgebra
115 The trace (being a sum of entries) belongs to the same algebra
116 as those entries, and NOT the scalar ring::
118 sage: entries = MatrixSpace(ZZ,2)
120 sage: M = MatrixAlgebra(entries, scalars, 2)
121 sage: I = entries.one()
122 sage: Z = entries.zero()
123 sage: M([[I,Z],[Z,I]]).trace()
128 zero
= self
.parent().entry_algebra().zero()
129 return sum( (self
[i
,i
] for i
in range(self
.nrows())), zero
)
131 def matrix_space(self
):
136 sage: from mjo.matrix_algebra import MatrixAlgebra
140 sage: set_random_seed()
141 sage: entries = QuaternionAlgebra(QQ,-1,-1)
142 sage: M = MatrixAlgebra(entries, QQ, 3)
143 sage: M.random_element().matrix_space() == M
150 class MatrixAlgebra(CombinatorialFreeModule
):
152 An algebra of ``n``-by-``n`` matrices over an arbitrary scalar
153 ring whose entries come from a magmatic algebra that need not
154 be the same as the scalars.
156 The usual matrix spaces in SageMath don't support separate spaces
157 for the entries and the scalars; in particular they assume that
158 the entries come from a commutative and associative ring. This
159 is problematic in several interesting matrix algebras, like those
160 where the entries are quaternions or octonions.
164 sage: from mjo.matrix_algebra import MatrixAlgebra
168 The existence of a unit element is determined dynamically::
170 sage: MatrixAlgebra(ZZ,ZZ,2).one()
178 Element
= MatrixAlgebraElement
180 def __init__(self
, entry_algebra
, scalars
, n
, prefix
="A", **kwargs
):
182 category
= MagmaticAlgebras(scalars
).FiniteDimensional()
183 category
= category
.WithBasis()
185 if "Unital" in entry_algebra
.category().axioms():
186 category
= category
.Unital()
187 entry_one
= entry_algebra
.one()
188 self
.one
= lambda: sum( (self
.monomial((i
,i
,entry_one
))
189 for i
in range(self
.nrows()) ),
192 if "Associative" in entry_algebra
.category().axioms():
193 category
= category
.Associative()
197 # Since the scalar ring is real but the entries are not,
198 # sticking a "1" in each position doesn't give us a basis for
199 # the space. We actually need to stick each of e0, e1, ... (a
200 # basis for the entry algebra itself) into each position.
203 self
._entry
_algebra
= entry_algebra
204 entry_basis
= entry_algebra
.gens()
206 basis_indices
= [(i
,j
,e
) for i
in range(n
)
208 for e
in entry_algebra
.gens()]
210 super().__init
__(scalars
,
217 return ("Module of %d by %d matrices with entries in %s"
218 " over the scalar ring %s" %
221 self
.entry_algebra(),
224 def entry_algebra(self
):
226 Return the algebra that our elements' entries come from.
228 return self
._entry
_algebra
234 def product_on_basis(self
, mon1
, mon2
):
239 sage: from mjo.octonions import Octonions
240 sage: from mjo.matrix_algebra import MatrixAlgebra
244 sage: O = Octonions(QQ)
248 sage: A = MatrixAlgebra(O,QQ,2)
249 sage: A.product_on_basis( (0,0,e[2]), (0,0,e[1]) )
260 # If e1*e2 has a negative sign in front of it,
261 # then (i,l,e1*e2) won't be a monomial!
263 if (i
,l
,p
) in self
.indices():
264 return self
.monomial((i
,l
,p
))
266 return -self
.monomial((i
,l
,-p
))
270 def from_list(self
, entries
):
272 Construct an element of this algebra from a list of lists of
277 sage: from mjo.matrix_algebra import MatrixAlgebra
281 sage: A = MatrixAlgebra(QQbar, ZZ, 2)
282 sage: A.from_list([[0,I],[-I,0]])
293 ncols
= len(entries
[0])
295 if (not all( len(r
) == ncols
for r
in entries
)) or (ncols
!= nrows
):
296 raise ValueError("list must be square")
299 if e_ij
in self
.entry_algebra():
300 # Don't re-create an element if it already lives where
305 # This branch works with e.g. QQbar, where no
306 # to/from_vector() methods are available.
307 return self
.entry_algebra()(e_ij
)
309 # We have to pass through vectors to convert from the
310 # given entry algebra to ours. Otherwise we can fail to
311 # convert an element of (for example) Octonions(QQ) to
313 return self
.entry_algebra().from_vector(e_ij
.to_vector())
315 return sum( (self
.monomial( (i
,j
, convert(entries
[i
][j
])) )
316 for i
in range(nrows
)
317 for j
in range(ncols
) ),
320 def _element_constructor_(self
, elt
):
324 return self
.from_list(elt
)
327 class HurwitzMatrixAlgebraElement(MatrixAlgebraElement
):
328 def is_hermitian(self
):
333 sage: from mjo.matrix_algebra import HurwitzMatrixAlgebra
337 sage: A = HurwitzMatrixAlgebra(QQbar, ZZ, 2)
338 sage: M = A([ [ 0,I],
340 sage: M.is_hermitian()
344 return all( self
[i
,j
] == self
[j
,i
].conjugate()
345 for i
in range(self
.nrows())
346 for j
in range(self
.ncols()) )
349 class HurwitzMatrixAlgebra(MatrixAlgebra
):
350 Element
= HurwitzMatrixAlgebraElement