]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/matrix_algebra.py
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(2, QQbar,RDF)
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(2,ZZ,ZZ).zero()
62 sage: MatrixAlgebra(0,ZZ,ZZ).zero()
66 if self
.nrows() == 0 or self
.ncols() == 0:
67 # Otherwise we get a crash or a blank space, depending on
68 # how hard we work for it. This is what MatrixSpace(...,
72 return table(self
.rows(), frame
=True)._repr
_()
77 Return one long list of this matrix's entries.
81 sage: from mjo.matrix_algebra import MatrixAlgebra
85 sage: A = MatrixAlgebra(2,ZZ,ZZ)
86 sage: A([[1,2],[3,4]]).list()
90 return sum( self
.rows(), [] )
93 def __getitem__(self
, indices
):
98 sage: from mjo.matrix_algebra import MatrixAlgebra
102 sage: M = MatrixAlgebra(2,ZZ,ZZ)([[1,2],[3,4]])
114 return self
.rows()[i
][j
]
118 Return the sum of this matrix's diagonal entries.
122 sage: from mjo.matrix_algebra import MatrixAlgebra
126 The trace (being a sum of entries) belongs to the same algebra
127 as those entries, and NOT the scalar ring::
129 sage: entries = MatrixSpace(ZZ,2)
131 sage: M = MatrixAlgebra(2, entries, scalars)
132 sage: I = entries.one()
133 sage: Z = entries.zero()
134 sage: M([[I,Z],[Z,I]]).trace()
139 zero
= self
.parent().entry_algebra().zero()
140 return sum( (self
[i
,i
] for i
in range(self
.nrows())), zero
)
142 def matrix_space(self
):
147 sage: from mjo.matrix_algebra import MatrixAlgebra
151 sage: set_random_seed()
152 sage: entries = QuaternionAlgebra(QQ,-1,-1)
153 sage: M = MatrixAlgebra(3, entries, QQ)
154 sage: M.random_element().matrix_space() == M
161 class MatrixAlgebra(CombinatorialFreeModule
):
163 An algebra of ``n``-by-``n`` matrices over an arbitrary scalar
164 ring whose entries come from a magmatic algebra that need not
165 be the same as the scalars.
167 The usual matrix spaces in SageMath don't support separate spaces
168 for the entries and the scalars; in particular they assume that
169 the entries come from a commutative and associative ring. This
170 is problematic in several interesting matrix algebras, like those
171 where the entries are quaternions or octonions.
175 sage: from mjo.matrix_algebra import MatrixAlgebra
179 The existence of a unit element is determined dynamically::
181 sage: MatrixAlgebra(2,ZZ,ZZ).one()
189 Element
= MatrixAlgebraElement
191 def __init__(self
, n
, entry_algebra
, scalars
, prefix
="A", **kwargs
):
193 category
= MagmaticAlgebras(scalars
).FiniteDimensional()
194 category
= category
.WithBasis()
196 if "Unital" in entry_algebra
.category().axioms():
197 category
= category
.Unital()
198 entry_one
= entry_algebra
.one()
199 self
.one
= lambda: self
.sum( (self
.monomial((i
,i
,entry_one
))
200 for i
in range(self
.nrows()) ) )
202 if "Associative" in entry_algebra
.category().axioms():
203 category
= category
.Associative()
207 # Since the scalar ring is real but the entries are not,
208 # sticking a "1" in each position doesn't give us a basis for
209 # the space. We actually need to stick each of e0, e1, ... (a
210 # basis for the entry algebra itself) into each position.
211 self
._entry
_algebra
= entry_algebra
213 # Needs to make the (overridden) method call when, for example,
214 # the entry algebra is the complex numbers and its gens() method
216 entry_basis
= self
.entry_algebra_gens()
218 basis_indices
= [(i
,j
,e
) for i
in range(n
)
220 for e
in entry_basis
]
222 super().__init
__(scalars
,
229 return ("Module of %d by %d matrices with entries in %s"
230 " over the scalar ring %s" %
233 self
.entry_algebra(),
236 def entry_algebra(self
):
238 Return the algebra that our elements' entries come from.
240 return self
._entry
_algebra
242 def entry_algebra_gens(self
):
244 Return a tuple of the generators of (that is, a basis for) the
245 entries of this matrix algebra.
247 This can be overridden in subclasses to work around the
248 inconsistency in the ``gens()`` methods of the various
251 return self
.entry_algebra().gens()
253 def _entry_algebra_element_to_vector(self
, entry
):
255 Return a vector representation (of length equal to the cardinality
256 of :meth:`entry_algebra_gens`) of the given ``entry``.
258 This can be overridden in subclasses to work around the fact that
259 real numbers, complex numbers, quaternions, et cetera, all require
260 different incantations to turn them into a vector.
262 It only makes sense to "guess" here in the superclass when no
263 subclass that overrides :meth:`entry_algebra_gens` exists. So
264 if you have a special subclass for your annoying entry algebra,
265 override this with the correct implementation there instead of
266 adding a bunch of awkward cases to this superclass method.
270 sage: from mjo.hurwitz import Octonions
271 sage: from mjo.matrix_algebra import MatrixAlgebra
277 sage: A = MatrixAlgebra(1, AA, QQ)
278 sage: A._entry_algebra_element_to_vector(AA(17))
283 sage: A = MatrixAlgebra(1, Octonions(), QQ)
284 sage: e = A.entry_algebra_gens()
285 sage: A._entry_algebra_element_to_vector(e[0])
286 (1, 0, 0, 0, 0, 0, 0, 0)
287 sage: A._entry_algebra_element_to_vector(e[1])
288 (0, 1, 0, 0, 0, 0, 0, 0)
289 sage: A._entry_algebra_element_to_vector(e[2])
290 (0, 0, 1, 0, 0, 0, 0, 0)
291 sage: A._entry_algebra_element_to_vector(e[3])
292 (0, 0, 0, 1, 0, 0, 0, 0)
293 sage: A._entry_algebra_element_to_vector(e[4])
294 (0, 0, 0, 0, 1, 0, 0, 0)
295 sage: A._entry_algebra_element_to_vector(e[5])
296 (0, 0, 0, 0, 0, 1, 0, 0)
297 sage: A._entry_algebra_element_to_vector(e[6])
298 (0, 0, 0, 0, 0, 0, 1, 0)
299 sage: A._entry_algebra_element_to_vector(e[7])
300 (0, 0, 0, 0, 0, 0, 0, 1)
304 sage: MS = MatrixSpace(QQ,2)
305 sage: A = MatrixAlgebra(1, MS, QQ)
306 sage: A._entry_algebra_element_to_vector(MS([[1,2],[3,4]]))
310 if hasattr(entry
, 'to_vector'):
311 return entry
.to_vector()
313 from sage
.modules
.free_module
import FreeModule
314 d
= len(self
.entry_algebra_gens())
315 V
= FreeModule(self
.entry_algebra().base_ring(), d
)
317 if hasattr(entry
, 'list'):
319 return V(entry
.list())
321 # This works in AA, and will crash if it doesn't know what to
322 # do, and that's fine because then I don't know what to do
332 def product_on_basis(self
, mon1
, mon2
):
337 sage: from mjo.hurwitz import Octonions
338 sage: from mjo.matrix_algebra import MatrixAlgebra
342 sage: O = Octonions(QQ)
346 sage: A = MatrixAlgebra(2,O,QQ)
347 sage: A.product_on_basis( (0,0,e[2]), (0,0,e[1]) )
358 # There's no reason to expect e1*e2 to itself be a monomial,
359 # so we have to do some manual conversion to get one.
360 p
= self
._entry
_algebra
_element
_to
_vector
(e1
*e2
)
362 # We have to convert alpha_g because a priori it lives in the
363 # base ring of the entry algebra.
365 return self
.sum_of_terms( (((i
,l
,g
), R(alpha_g
))
367 in zip(p
, self
.entry_algebra_gens()) ),
372 def from_list(self
, entries
):
374 Construct an element of this algebra from a list of lists of
379 sage: from mjo.hurwitz import ComplexMatrixAlgebra
383 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
384 sage: M = A.from_list([[0,I],[-I,0]])
392 (0, 0, 0, 1, 0, -1, 0, 0)
398 ncols
= len(entries
[0])
400 if (not all( len(r
) == ncols
for r
in entries
)) or (ncols
!= nrows
):
401 raise ValueError("list must be square")
404 if e_ij
in self
.entry_algebra():
405 # Don't re-create an element if it already lives where
410 # This branch works with e.g. QQbar, where no
411 # to/from_vector() methods are available.
412 return self
.entry_algebra()(e_ij
)
414 # We have to pass through vectors to convert from the
415 # given entry algebra to ours. Otherwise we can fail to
416 # convert an element of (for example) Octonions(QQ) to
418 return self
.entry_algebra().from_vector(e_ij
.to_vector())
420 def entry_to_element(i
,j
,entry
):
421 # Convert an entry at i,j to a matrix whose only non-zero
422 # entry is i,j and corresponds to the entry.
423 p
= self
._entry
_algebra
_element
_to
_vector
(entry
)
425 # We have to convert alpha_g because a priori it lives in the
426 # base ring of the entry algebra.
428 return self
.sum_of_terms( (((i
,j
,g
), R(alpha_g
))
430 in zip(p
, self
.entry_algebra_gens()) ),
433 return self
.sum( entry_to_element(i
,j
,entries
[i
][j
])
434 for j
in range(ncols
)
435 for i
in range(nrows
) )
438 def _element_constructor_(self
, elt
):
442 return self
.from_list(elt
)