]>
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()
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(2,ZZ,ZZ)
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(2,ZZ,ZZ)([[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(2, entries, scalars)
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(3, entries, QQ)
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(2,ZZ,ZZ).one()
178 Element
= MatrixAlgebraElement
180 def __init__(self
, n
, entry_algebra
, scalars
, 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: self
.sum( (self
.monomial((i
,i
,entry_one
))
189 for i
in range(self
.nrows()) ) )
191 if "Associative" in entry_algebra
.category().axioms():
192 category
= category
.Associative()
196 # Since the scalar ring is real but the entries are not,
197 # sticking a "1" in each position doesn't give us a basis for
198 # the space. We actually need to stick each of e0, e1, ... (a
199 # basis for the entry algebra itself) into each position.
200 self
._entry
_algebra
= entry_algebra
202 # Needs to make the (overridden) method call when, for example,
203 # the entry algebra is the complex numbers and its gens() method
205 entry_basis
= self
.entry_algebra_gens()
207 basis_indices
= [(i
,j
,e
) for i
in range(n
)
209 for e
in entry_basis
]
211 super().__init
__(scalars
,
218 return ("Module of %d by %d matrices with entries in %s"
219 " over the scalar ring %s" %
222 self
.entry_algebra(),
225 def entry_algebra(self
):
227 Return the algebra that our elements' entries come from.
229 return self
._entry
_algebra
231 def entry_algebra_gens(self
):
233 Return a tuple of the generators of (that is, a basis for) the
234 entries of this matrix algebra.
236 This can be overridden in subclasses to work around the
237 inconsistency in the ``gens()`` methods of the various
240 return self
.entry_algebra().gens()
242 def _entry_algebra_element_to_vector(self
, entry
):
244 Return a vector representation (of length equal to the cardinality
245 of :meth:`entry_algebra_gens`) of the given ``entry``.
247 This can be overridden in subclasses to work around the fact that
248 real numbers, complex numbers, quaternions, et cetera, all require
249 different incantations to turn them into a vector.
251 It only makes sense to "guess" here in the superclass when no
252 subclass that overrides :meth:`entry_algebra_gens` exists. So
253 if you have a special subclass for your annoying entry algebra,
254 override this with the correct implementation there instead of
255 adding a bunch of awkward cases to this superclass method.
259 sage: from mjo.hurwitz import Octonions
260 sage: from mjo.matrix_algebra import MatrixAlgebra
266 sage: A = MatrixAlgebra(1, AA, QQ)
267 sage: A._entry_algebra_element_to_vector(AA(17))
272 sage: A = MatrixAlgebra(1, Octonions(), QQ)
273 sage: e = A.entry_algebra_gens()
274 sage: A._entry_algebra_element_to_vector(e[0])
275 (1, 0, 0, 0, 0, 0, 0, 0)
276 sage: A._entry_algebra_element_to_vector(e[1])
277 (0, 1, 0, 0, 0, 0, 0, 0)
278 sage: A._entry_algebra_element_to_vector(e[2])
279 (0, 0, 1, 0, 0, 0, 0, 0)
280 sage: A._entry_algebra_element_to_vector(e[3])
281 (0, 0, 0, 1, 0, 0, 0, 0)
282 sage: A._entry_algebra_element_to_vector(e[4])
283 (0, 0, 0, 0, 1, 0, 0, 0)
284 sage: A._entry_algebra_element_to_vector(e[5])
285 (0, 0, 0, 0, 0, 1, 0, 0)
286 sage: A._entry_algebra_element_to_vector(e[6])
287 (0, 0, 0, 0, 0, 0, 1, 0)
288 sage: A._entry_algebra_element_to_vector(e[7])
289 (0, 0, 0, 0, 0, 0, 0, 1)
293 sage: MS = MatrixSpace(QQ,2)
294 sage: A = MatrixAlgebra(1, MS, QQ)
295 sage: A._entry_algebra_element_to_vector(MS([[1,2],[3,4]]))
299 if hasattr(entry
, 'to_vector'):
300 return entry
.to_vector()
302 from sage
.modules
.free_module
import FreeModule
303 d
= len(self
.entry_algebra_gens())
304 V
= FreeModule(self
.entry_algebra().base_ring(), d
)
306 if hasattr(entry
, 'list'):
308 return V(entry
.list())
310 # This works in AA, and will crash if it doesn't know what to
311 # do, and that's fine because then I don't know what to do
321 def product_on_basis(self
, mon1
, mon2
):
326 sage: from mjo.hurwitz import Octonions
327 sage: from mjo.matrix_algebra import MatrixAlgebra
331 sage: O = Octonions(QQ)
335 sage: A = MatrixAlgebra(2,O,QQ)
336 sage: A.product_on_basis( (0,0,e[2]), (0,0,e[1]) )
347 # There's no reason to expect e1*e2 to itself be a monomial,
348 # so we have to do some manual conversion to get one.
349 p
= self
._entry
_algebra
_element
_to
_vector
(e1
*e2
)
351 # We have to convert alpha_g because a priori it lives in the
352 # base ring of the entry algebra.
354 return self
.sum_of_terms( (((i
,l
,g
), R(alpha_g
))
356 in zip(p
, self
.entry_algebra_gens()) ),
361 def from_list(self
, entries
):
363 Construct an element of this algebra from a list of lists of
368 sage: from mjo.hurwitz import ComplexMatrixAlgebra
372 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
373 sage: M = A.from_list([[0,I],[-I,0]])
381 (0, 0, 0, 1, 0, -1, 0, 0)
387 ncols
= len(entries
[0])
389 if (not all( len(r
) == ncols
for r
in entries
)) or (ncols
!= nrows
):
390 raise ValueError("list must be square")
393 if e_ij
in self
.entry_algebra():
394 # Don't re-create an element if it already lives where
399 # This branch works with e.g. QQbar, where no
400 # to/from_vector() methods are available.
401 return self
.entry_algebra()(e_ij
)
403 # We have to pass through vectors to convert from the
404 # given entry algebra to ours. Otherwise we can fail to
405 # convert an element of (for example) Octonions(QQ) to
407 return self
.entry_algebra().from_vector(e_ij
.to_vector())
409 def entry_to_element(i
,j
,entry
):
410 # Convert an entry at i,j to a matrix whose only non-zero
411 # entry is i,j and corresponds to the entry.
412 p
= self
._entry
_algebra
_element
_to
_vector
(entry
)
414 # We have to convert alpha_g because a priori it lives in the
415 # base ring of the entry algebra.
417 return self
.sum_of_terms( (((i
,j
,g
), R(alpha_g
))
419 in zip(p
, self
.entry_algebra_gens()) ),
422 return self
.sum( entry_to_element(i
,j
,entries
[i
][j
])
423 for j
in range(ncols
)
424 for i
in range(nrows
) )
427 def _element_constructor_(self
, elt
):
431 return self
.from_list(elt
)