# Construction of Matrix Algebras and their Elements

## Construction of the Complete Matrix Algebra

### `MatrixAlgebra(S, n): Rng, RngIntElt -> AlgMat`

### `MatrixRing(S, n): Rng, RngIntElt -> AlgMat`

Given a positive integer $n$ and a ring $S$, create the complete matrix algebra $M_n(S)$, consisting of all $n\times n$ matrices with coefficients in the ring $S$.

## Construction of a Matrix

### `elt< R | L >: AlgMat, RngElt -> AlgMatElt`

Given a matrix algebra defined as a subalgebra of $M_n(S)$, create the element of $R$ defined by the list $L$ of $n^2$ elements from $S$.

### `R ! Q: AlgMat, [ RngElt ] -> AlgMatElt`

Given a matrix algebra $R$ defined as a subalgebra of $M_n(S)$ and a sequence $Q=[a_{11}, \ldots, a_{1n}, a_{21},\ldots, a_{2n}, \ldots, a_{n1},\ldots, a_{nn}]$ of $n^2$ elements of $S$, return the matrix $\begin{pmatrix}a_{11}&a_{12}&\ldots&a_{1n}\\ a_{21}&a_{22}&\ldots&a_{2n}\\ \vdots&\vdots&\ddots&\vdots\\ a_{n1}&a_{n2}&\ldots&a_{nn}\end{pmatrix}$

as an element of $R$. Note that the algebra $R$ must exist before an attempt is made to create matrices.

### `CambridgeMatrix(t, K, n, Q): RngIntElt, FldFin, RngIntElt, [ ] -> AlgMatElt`

This function creates a $n$ by $n$ matrix over the finite field $K$ of cardinality $q$ specified in a “Cambridge” format in the general matrix algebra of degree $n$ over $K$. The parameter $t$ specifies the type of the format. If $t$ is 1, then $q$ is assumed to be less than 10 and the sequence $Q$ must consist of $n$ strings which give the $n$ rows—each string must have length $n$ and contain the entries of that row (each entry is a digit in the range [0, $q - 1$]). If $t$ is 3 then $Q$ must consist of $n^2$ integers in the range [0, $q-1$] which give the entries in row-major order. In either format, if $q=p^e$, where $p$ is prime and $e>1$, then an entry $x$ is written as a vector using the base-$p$ representation of length $e$ of $x$ and the corresponding element in $K$ is used (see the Finite Fields chapter for details). This function is principally provided for the reading in of large matrices.

### `CompanionMatrix(p): RngUPolElt -> AlgMatElt`

Given a monic polynomial $p$ of degree $n$ over a ring $R$, create the companion matrix $C$ for $p$ as an element of $M_n(R)$. The minimal and characteristic polynomial of $C$ is then $p$.

### `DiagonalMatrix(R, Q): AlgMat, [ RngElt ] -> AlgMatElt`

If $R$ is a subalgebra of $M_n(S)$ and $Q$ is a sequence of $n$ elements of $S$, create the diagonal matrix $diag( Q[1], Q[2], \ldots, Q[n] )$.

### `MatrixUnit(R, i, j): AlgMat, RngIntElt, RngIntElt -> AlgMatElt`

Create the matrix unit $E(i, j)$ in the matrix algebra $R$, i.e. the matrix having the one of the coefficient ring of $R$ in position $(i, j)$ and zeros elsewhere.

### `Random(R): AlgMat -> AlgMatElt`

Create a random matrix of the matrix algebra $R$.

### `ScalarMatrix(R, t): AlgMat, RngElt -> AlgMatElt`

If $R$ is a subalgebra of $M_n(S)$ and $t$ is an element of the ring $S$, create the scalar matrix $t*I$ in $R$.

### `R ! 1: AlgMat, RngIntElt -> AlgMatElt`

Create the identity matrix $I_n$ of the matrix algebra $R$.

### `R ! 0: AlgMat, RngIntElt -> AlgMatElt`

Create the zero matrix of the matrix algebra $R$.

### `R ! t: AlgMat, RngIntElt -> AlgMatElt`

Create the scalar matrix $t * I$ of the matrix algebra $R$.

## Constructing a General Matrix Algebra

### `MatrixAlgebra<S, n | L>: Rng, RngIntElt, List -> AlgMat`

### `MatrixRing<S, n | L>: Rng, RngIntElt, List -> AlgMat`

Given a commutative ring $S$ and a positive integer $n$, create the $S$-algebra $R$ consisting of the $n \times n$ matrices over the ring $S$ generated by the elements defined in the list $L$. Let $F$ denote the algebra $M_n(S)$. Each term $L_i$ of the list $L$ must be an expression defining an object of one of the following types:

**(a)**
A sequence of $n^2$ elements of $S$ defining an element of $F$.

**(b)**
A set or sequence whose terms are sequences of type (a).

**(c)**
An element of $F$.

**(d)**
A set or sequence whose terms are elements of $F$.

**(e)**
The null list.

The generators stored for $R$ consist of the elements specified by terms $L_i$ together with the stored generators for subalgebras specified by terms of $L_i$. Repetitions of an element and occurrences of scalar matrices are removed.

### `Example: Creation (ex-0b78f0)`

We demonstrate the use of the matrix algebra constructor by creating an algebra of $3 \times 3$ lower-triangular matrices over the rational field.

```magma
> Q := RationalField();
> A := MatrixAlgebra< Q, 3 | [ 1/3,0,0, 3/2,3,0, -1/2,4,3],
>        [ 3,0,0, 1/2,-5,0, 8,-1/2,4] >;
> A:Maximal;
Matrix Algebra of degree 3 with 2 generators over Rational Field
Generators:
[ 1/3    0    0]
[ 3/2    3    0]
[-1/2    4    3]

[   3    0    0]
[ 1/2   -5    0]
[   8 -1/2    4]
> Dimension(A);
6

```

### `Example: Cambridge (ex-424fbd)`

We construct a 4 by 4 matrix over the finite field with 5 elements using the CambridgeMatrix function.

```magma
> K := FiniteField(5);
> x := CambridgeMatrix(1, K, 4, [ "1234", "0111", "4321", "1211" ]);
> x;
[1 2 3 4]
[0 1 1 1]
[4 3 2 1]
[1 2 1 1]

```

### `Algebra(R): AlgMatV -> AlgGen, Map`

Given a matrix algebra $R$, construct a structure-constant algebra $C$ isomorphic to $R$ together with the isomorphism from $R$ onto $C$.

## The Invariants of a Matrix Algebra

### `R . i: AlgMat, RngIntElt -> AlgMatElt`

The $i$-th defining generator for the matrix algebra $R$.

### `BaseRing(R): AlgMatV -> Rng`

### `CoefficientRing(R): AlgMatV -> Rng`

The coefficient ring $S$ for the matrix algebra $R$.

### `Degree(R): AlgMatV -> RngIntElt`

Given a matrix algebra $R$, return the degree $n$ of $R$.

### `Generators(R): AlgMat -> { AlgMatElt}`

The set consisting of the defining generators for the matrix algebra $R$.

### `Generic(R): AlgMat -> AlgMat`

The complete matrix algebra $M_n(S)$ in which the matrix algebra $R$ is naturally embedded.

### `BaseModule(R): AlgMatV -> ModTup`

If $R$ is a subring of the matrix algebra $M_n(S)$, then $R$ is considered to act on the free $S$-module of rank $n$, consisting of $n$-tuples over $S$. The function `BaseModule` returns this $S$-module.

### `NumberOfGenerators(R): AlgMat -> { AlgMatElt}`

### `Ngens(R): AlgMat -> { AlgMatElt}`

The number of defining generators for the matrix algebra $R$.

### `Parent(a): AlgMatElt -> AlgMat`

Given an element $a$ belonging to the matrix algebra $R$, return $R$, i.e. the parent structure for $a$.

### `Example: Invariants (ex-97af59)`

We illustrate the use of these functions by applying them to the algebra of $3 \times 3$ lower-triangular matrices over the rational field constructed above.

```magma
> Q := RationalField();
> A := MatrixAlgebra< Q, 3 | [ 1/3,0,0, 3/2,3,0, -1/2,4,3],
>        [ 3,0,0, 1/2,-5,0, 8,-1/2,4] >;
> CoefficientRing(A);
Rational Field
> Degree(A);
3
> Ngens(A);
2
> Generators(A);
{
    [ 1/3    0    0]
    [ 3/2    3    0]
    [-1/2    4    3],

    [   3    0    0]
    [ 1/2   -5    0]
    [   8 -1/2    4]
}
> Generic(A);
Full Matrix Algebra of degree 3 over Rational Field
> Dimension(A);
6

```
