]>
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: entries = QuaternionAlgebra(QQ,-1,-1)
152 sage: M = MatrixAlgebra(3, entries, QQ)
153 sage: M.random_element().matrix_space() == M
160 class MatrixAlgebra(CombinatorialFreeModule
):
162 An algebra of ``n``-by-``n`` matrices over an arbitrary scalar
163 ring whose entries come from a magmatic algebra that need not
164 be the same as the scalars.
166 The usual matrix spaces in SageMath don't support separate spaces
167 for the entries and the scalars; in particular they assume that
168 the entries come from a commutative and associative ring. This
169 is problematic in several interesting matrix algebras, like those
170 where the entries are quaternions or octonions.
174 sage: from mjo.matrix_algebra import MatrixAlgebra
178 The existence of a unit element is determined dynamically::
180 sage: MatrixAlgebra(2,ZZ,ZZ).one()
188 Element
= MatrixAlgebraElement
190 def __init__(self
, n
, entry_algebra
, scalars
, prefix
="A", **kwargs
):
192 category
= MagmaticAlgebras(scalars
).FiniteDimensional()
193 category
= category
.WithBasis()
195 if "Unital" in entry_algebra
.category().axioms():
196 category
= category
.Unital()
197 entry_one
= entry_algebra
.one()
198 self
.one
= lambda: self
.sum( (self
.monomial((i
,i
,entry_one
))
199 for i
in range(self
.nrows()) ) )
201 if "Associative" in entry_algebra
.category().axioms():
202 category
= category
.Associative()
206 # Since the scalar ring is real but the entries are not,
207 # sticking a "1" in each position doesn't give us a basis for
208 # the space. We actually need to stick each of e0, e1, ... (a
209 # basis for the entry algebra itself) into each position.
210 self
._entry
_algebra
= entry_algebra
212 # Needs to make the (overridden) method call when, for example,
213 # the entry algebra is the complex numbers and its gens() method
215 entry_basis
= self
.entry_algebra_gens()
217 basis_indices
= [(i
,j
,e
) for i
in range(n
)
219 for e
in entry_basis
]
221 super().__init
__(scalars
,
228 return ("Module of %d by %d matrices with entries in %s"
229 " over the scalar ring %s" %
232 self
.entry_algebra(),
235 def entry_algebra(self
):
237 Return the algebra that our elements' entries come from.
239 return self
._entry
_algebra
241 def entry_algebra_gens(self
):
243 Return a tuple of the generators of (that is, a basis for) the
244 entries of this matrix algebra.
246 This can be overridden in subclasses to work around the
247 inconsistency in the ``gens()`` methods of the various
250 return self
.entry_algebra().gens()
252 def _entry_algebra_element_to_vector(self
, entry
):
254 Return a vector representation (of length equal to the cardinality
255 of :meth:`entry_algebra_gens`) of the given ``entry``.
257 This can be overridden in subclasses to work around the fact that
258 real numbers, complex numbers, quaternions, et cetera, all require
259 different incantations to turn them into a vector.
261 It only makes sense to "guess" here in the superclass when no
262 subclass that overrides :meth:`entry_algebra_gens` exists. So
263 if you have a special subclass for your annoying entry algebra,
264 override this with the correct implementation there instead of
265 adding a bunch of awkward cases to this superclass method.
269 sage: from mjo.hurwitz import Octonions
270 sage: from mjo.matrix_algebra import MatrixAlgebra
276 sage: A = MatrixAlgebra(1, AA, QQ)
277 sage: A._entry_algebra_element_to_vector(AA(17))
282 sage: A = MatrixAlgebra(1, Octonions(), QQ)
283 sage: e = A.entry_algebra_gens()
284 sage: A._entry_algebra_element_to_vector(e[0])
285 (1, 0, 0, 0, 0, 0, 0, 0)
286 sage: A._entry_algebra_element_to_vector(e[1])
287 (0, 1, 0, 0, 0, 0, 0, 0)
288 sage: A._entry_algebra_element_to_vector(e[2])
289 (0, 0, 1, 0, 0, 0, 0, 0)
290 sage: A._entry_algebra_element_to_vector(e[3])
291 (0, 0, 0, 1, 0, 0, 0, 0)
292 sage: A._entry_algebra_element_to_vector(e[4])
293 (0, 0, 0, 0, 1, 0, 0, 0)
294 sage: A._entry_algebra_element_to_vector(e[5])
295 (0, 0, 0, 0, 0, 1, 0, 0)
296 sage: A._entry_algebra_element_to_vector(e[6])
297 (0, 0, 0, 0, 0, 0, 1, 0)
298 sage: A._entry_algebra_element_to_vector(e[7])
299 (0, 0, 0, 0, 0, 0, 0, 1)
303 sage: MS = MatrixSpace(QQ,2)
304 sage: A = MatrixAlgebra(1, MS, QQ)
305 sage: A._entry_algebra_element_to_vector(MS([[1,2],[3,4]]))
309 if hasattr(entry
, 'to_vector'):
310 return entry
.to_vector()
312 from sage
.modules
.free_module
import FreeModule
313 d
= len(self
.entry_algebra_gens())
314 V
= FreeModule(self
.entry_algebra().base_ring(), d
)
316 if hasattr(entry
, 'list'):
318 return V(entry
.list())
320 # This works in AA, and will crash if it doesn't know what to
321 # do, and that's fine because then I don't know what to do
331 def product_on_basis(self
, mon1
, mon2
):
336 sage: from mjo.hurwitz import Octonions
337 sage: from mjo.matrix_algebra import MatrixAlgebra
341 sage: O = Octonions(QQ)
345 sage: A = MatrixAlgebra(2,O,QQ)
346 sage: A.product_on_basis( (0,0,e[2]), (0,0,e[1]) )
357 # There's no reason to expect e1*e2 to itself be a monomial,
358 # so we have to do some manual conversion to get one.
359 p
= self
._entry
_algebra
_element
_to
_vector
(e1
*e2
)
361 # We have to convert alpha_g because a priori it lives in the
362 # base ring of the entry algebra.
364 return self
.sum_of_terms( (((i
,l
,g
), R(alpha_g
))
366 in zip(p
, self
.entry_algebra_gens()) ),
371 def from_list(self
, entries
):
373 Construct an element of this algebra from a list of lists of
378 sage: from mjo.hurwitz import ComplexMatrixAlgebra
382 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
383 sage: M = A.from_list([[0,I],[-I,0]])
391 (0, 0, 0, 1, 0, -1, 0, 0)
397 ncols
= len(entries
[0])
399 if (not all( len(r
) == ncols
for r
in entries
)) or (ncols
!= nrows
):
400 raise ValueError("list must be square")
403 if e_ij
in self
.entry_algebra():
404 # Don't re-create an element if it already lives where
409 # This branch works with e.g. QQbar, where no
410 # to/from_vector() methods are available.
411 return self
.entry_algebra()(e_ij
)
413 # We have to pass through vectors to convert from the
414 # given entry algebra to ours. Otherwise we can fail to
415 # convert an element of (for example) Octonions(QQ) to
417 return self
.entry_algebra().from_vector(e_ij
.to_vector())
419 def entry_to_element(i
,j
,entry
):
420 # Convert an entry at i,j to a matrix whose only non-zero
421 # entry is i,j and corresponds to the entry.
422 p
= self
._entry
_algebra
_element
_to
_vector
(entry
)
424 # We have to convert alpha_g because a priori it lives in the
425 # base ring of the entry algebra.
427 return self
.sum_of_terms( (((i
,j
,g
), R(alpha_g
))
429 in zip(p
, self
.entry_algebra_gens()) ),
432 return self
.sum( entry_to_element(i
,j
,entries
[i
][j
])
433 for j
in range(ncols
)
434 for i
in range(nrows
) )
437 def _element_constructor_(self
, elt
):
441 return self
.from_list(elt
)