# Matrices and Modules

Since $S_p^\nu$ and $S_u^\nu$ are Euclidean, we can work with canonical forms and modules over these rings. We consider $S_p^\nu$ first.

## Matrices

The row-echelon form of a matrix over $S_p^\nu$ has pivot entries which are polynomials of the form

$$
u^d+\sum_{j=0}^{d-1} b_ju^j\qquad{\rm where}\qquad v(b_j)+\nu j >\nu d
$$

for $j$ in the sum, so that the Weierstrass degree is $d$ and the Gauss valuation is $d\nu$.

The Hermite form additionally assures that the other entries in the columns with pivots are polynomials of smaller degree, via taking Quotrem. This can be made unique via suitable choices, and so provides a way of testing equality of modules.

The Smith form is computed by creating a matrix of pivots corresponding to a Hermite on the matrix and then a Hermite on the transpose. One also demands recursive divisibility of the resulting diagonal terms.

Over $S_u^\nu$ the picture is the same, except that pivot entries of the echelon form are all `CanonicalElement`s, with similar changes thereon. E.g., the Hermite form will have elements of smaller valuation above the pivots.

### `SpMatrixSpace(S, r, c): SpRng, RngIntElt, RngIntElt -> SpMatRng`

### `SuMatrixSpace(S, r, c): SuRng, RngIntElt, RngIntElt -> SuMatRng`

Given an $S_p^\nu$ of $S^\nu_u$ ring and a (nonnegative) number of rows and columns, construct the matrix space.

### `IdentityMatrix(S, n): SpRng, RngIntElt -> SpMat`

### `IdentityMatrix(S): SpMatRng -> SpMat`

### `ZeroMatrix(S): SpMatRng -> SpMat`

### `ZeroMatrix(S, n): SpRng, RngIntElt -> SpMat`

### `ZeroMatrix(S, r, c): SpRng, RngIntElt, RngIntElt -> SpMat`

### `IdentityMatrix(S, n): SuRng, RngIntElt -> SuMat`

### `IdentityMatrix(S): SuMatRng -> SuMat`

### `ZeroMatrix(S): SuMatRng -> SuMat`

### `ZeroMatrix(S, n): SuRng, RngIntElt -> SuMat`

### `ZeroMatrix(S, r, c): SuRng, RngIntElt, RngIntElt -> SuMat`

Different ways of obtaining the zero matrix or identity matrix (if applicable) of a matrix ring.

### `SpMatrix(A): SeqEnum -> SpMat`

### `SpMatrix(r, c, A): RngIntElt, RngIntElt, SeqEnum -> SpMat`

### `SpMatrix(v): SeqEnum -> SpMat`

### `SpMatrix(A): SeqEnum -> SpMat`

### `SuMatrix(r, c, A): RngIntElt, RngIntElt, SeqEnum -> SuMat`

### `SuMatrix(v): SeqEnum -> SuMat`

Various ways of obtaining an `SpMat` or `SuMat`. The first takes a sequence of sequences of `SpElement`s or `SuElement`s, the second takes a row and column count and a sequence of `SpElement`s or `SuElement`s, and the third takes a sequence of `SpVec`s or `SuVec`s.

### `GaussValuations(M): SpMat -> SeqEnum`

### `GaussValuations(M): SuMat -> SeqEnum`

Returns a sequence of sequences of the Gauss valuations of the elements of a matrix. Since these can be `ExtendedReals`, the valuations themselves are put in an array rather than a matrix.

### `WeierstrassDegrees(M): SpMat -> SeqEnum`

### `WeierstrassDegrees(M): SuMat -> SeqEnum`

Returns a sequence of sequences of the Weierstrass degrees of the elements of a matrix. Since these can be `ExtendedReals`, the valuations themselves are put in an array rather than a matrix.

### `LeadingTerms(M): SpMat -> Mtrx`

### `LeadingTerms(M): SuMat -> Mtrx`

Returns a matrix (defined over the underlying power or Laurent series ring) which has the leading term of each element.

### `WeierstrassTerms(M): SpMat -> Mtrx`

### `WeierstrassTerms(M): SuMat -> Mtrx`

Returns a matrix (defined over the underlying power or Laurent series ring) which has the Weierstrass term of each element.

### `IsWeaklyZero(M): SpMat -> BoolElt`

### `IsWeaklyZero(M): SuMat -> BoolElt`

Returns whether every element in the matrix is nearly zero.

### `EchelonForm(M): SpMat -> SpMat, SpMat`

### `EchelonForm(M): SuMat -> SuMat, SuMat`

```magma
Transform: BoolElt                    Default: false
```

Returns the (row) echelon form $E$, and (if desired) the transform $T$ such that $E=TM$.

### `HermiteForm(M): SpMat -> SpMat, SpMat`

### `HermiteForm(M): SuMat -> SuMat, SuMat`

```magma
Transform: BoolElt                    Default: false
```

Returns the Hermite form $H$, and (if desired) the transform $T$ such that $H=TM$.

### `SmithForm(M): SpMat -> SpMat, SpMat, SpMat`

### `SmithForm(M): SuMat -> SuMat, SuMat, SuMat`

Returns the Smith form $S$, and (if desired) the transforms $P,Q$ such that $S=PMQ$.

### `Kernel(M): SpMat -> SpSpc`

### `Kernel(M): SuMat -> SuSpc`

### `Image(M): SpMat -> SpSpc`

### `Image(M): SuMat -> SuSpc`

Return the kernel or image of an $S_p^\nu$ or $S_u^\nu$ matrix as a module space. The `Image` also has a `Transform` parameter that gives the inclusion.

### `Example: Sp Matrices (ex-8c19ad)`

Here are some basic examples of operations with matrices over $S^\nu_p$.

```magma
> S<u> := SpRing (pAdicField (3, 20), 1/2); // slope 1/2
> M := SpMatrix (2, 3, [ S | u^2/3,u/3^2,0, u^3,3^2*u,3*u ]);
> E, T := EchelonForm (M : Transform); assert E eq T*M;
> LeadingTerms (E);
[(3^-1 + O(3^19))*$.1^2  (3^-2 + O(3^18))*$.1  O($.1^20)]
[O($.1^20)               (3^2 + O(3^18))*$.1   (3 + O(3^21))*$.1]
> WeierstrassTerms (E); // not the same
[(3^-1 + O(3^19))*$.1^2  (3^-2 + O(3^18))*$.1    O($.1^20)]
[O($.1^20)              -(3^-1 + O(3^19))*$.1^2  (3 + O(3^21))*$.1]

```

```magma
> S<u> := SpRing (pAdicField (5, 10), 3/2); // slope 3/2
> M := SpMatrix (3,5,[S|5^3,u,u^2/5,0,0, 0,0,25,u^3/25,3*5, 0,0,0,0,5^2]);
> H, T := HermiteForm (M : Transform); assert H eq T*M;
> WeierstrassTerms(H);
[5^3 + O(5^13)  $.1        O($.1^20)     -(5^-5 + O(5^5))*$.1^5  O($.1^20)]
[O($.1^20)      O($.1^20)  5^2 + O(5^9)  (5^-2 + O(5^8))*$.1^3   O($.1^20)]
[O($.1^20)      O($.1^20)  O($.1^20)     O($.1^20)               5^2 + O(5^12)]
> X, P, Q := SmithForm (M : Transform); assert P*M*Q eq X;
> WeierstrassTerms(X); // note that 5^3 divides 5^2 in this ring!
[5^3 + O(5^13)  O($.1^20)     O($.1^20)      O($.1^20)  O($.1^20)]
[O($.1^20)      5^2 + O(5^9)  O($.1^20)      O($.1^20)  O($.1^20)]
[O($.1^20)      O($.1^20)     5^2 + O(5^12)  O($.1^20)  O($.1^20)]

```

### `Example: Su Matrices (ex-970ed4)`

Here are similar examples over $S^\nu_u$.

```magma
> S<u> := SuRing (pAdicField (5, 15), 2/3); // slope 2/3
> M := SuMatrix (2, 3, [ S | 5^2/u,0,u^2, u,5*u^2,5^3/u^2 ]);
> E, T := EchelonForm (M : Transform); assert E eq T*M;
> LeadingTerms (E);
[$.1        (5 + O(5^16))*$.1^2   (5^3 + O(5^18))*$.1^-2]
[O($.1^20)  5^3 + O(5^15)         (5^5 + O(5^20))*$.1^-4]
> WeierstrassTerms (E); // not the same, in lower right
[$.1        (5 + O(5^16))*$.1^2  (5^3 + O(5^18))*$.1^-2]
[O($.1^20)  5^3 + O(5^15)        -$.1^2]

```

```magma
> A := [S | 25/u,u,u^2/5,0,0, 0,0,25,125/u^2,50*u, 0,0,0,0,5*u^2];
> M := SuMatrix (3, 5, A);
> H, T := HermiteForm (M : Transform); assert H eq T*M;
> W := WeierstrassTerms(H); W;
[$.1^2 (5^-2 + O(5^13))*$.1^4 (5^-3 + O(5^12))*$.1^5 O($.1^20) O($.1^20)]
[O($.1^20) O($.1^20) 5^2 + O(5^17) (5^3 + O(5^18))*$.1^-2 O($.1^20)]
[O($.1^20) O($.1^20) O($.1^20) O($.1^20) (5 + O(5^16))*$.1^2]
> assert CanonicalElement (S, 4/3) eq S!W[1,1];
> assert CanonicalElement (S, 2) eq S!W[2,3];
> assert CanonicalElement (S, 7/3) eq S!W[3,5];
> X, P, Q := SmithForm (M : Transform); assert X eq P*M*Q;
> WeierstrassTerms(X);
[(5^-1 + O(5^14))*$.1^2 O($.1^20) O($.1^20) O($.1^20) O($.1^20)]
[O($.1^20) (5 + O(5^16))*$.1 O($.1^20) O($.1^20) O($.1^20)]
[O($.1^20) O($.1^20) (5 + O(5^16))*$.1^2 O($.1^20) O($.1^20)]

```

## Modules

These are implemented as “vectors” over $S^\nu_p$ or $S^\nu_u$.

### `SpSpace(R, n): SpRng, RngIntElt -> SpSpc`

### `SuSpace(R, n): SuRng, RngIntElt -> SuSpc`

Given an $S^\nu_p$ or $S^\nu_u$ ring and a nonnegative integer, construct the module of that degree.

### `SpSpace(M): SpMat -> SpSpc`

### `SuSpace(M): SuMat -> SuSpc`

Given an $S^\nu_p$ or $S^\nu_u$ matrix, construct the associated module space. This always computes the Hermite form to use as the basis.

### `SpSpace(v): SeqEnum -> SpSpc`

### `SuSpace(v): SeqEnum -> SuSpc`

Given a sequence of `SpVec`s or `SuVec`s, construct the module space generated by them. Again this always computes the Hermite form for the basis.

### `Ambient(S): SpSpc -> SpSpc`

### `Ambient(S): SuSpc -> SuSpc`

Given a module space, return the (full) ambient space of dimension equal to the degree.

### `Parent(v): SpVec -> SpSpc`

### `Parent(v): SuVec -> SuSpc`

The Parent of an `SpVec` or `SuVec` is always its `Ambient` space.

### `ZeroVector(S): SpSpc -> SpVec`

### `ZeroVector(S): SuSpc -> SuVec`

Return the zero vector of a module space.

### `SpVector(e): SeqEnum -> SpVec`

### `SuVector(e): SeqEnum -> SuVec`

Given a sequence of $S^\nu_p$ or $S^\nu_u$ elements, construct the vector of them.

### `Basis(S): SpSpc -> SeqEnum`

### `Basis(S): SuSpc -> SeqEnum`

Given a module space, return a basis as a sequence of vectors.

### `BasisMatrix(S): SpSpc -> SpMat`

### `BasisMatrix(S): SuSpc -> SuMat`

Given a module space, return a basis as a matrix.

### `Dimension(S): SpSpc -> RngIntElt`

### `Dimension(S): SuSpc -> RngIntElt`

### `Degree(S): SpSpc -> RngIntElt`

### `Degree(S): SuSpc -> RngIntElt`

The dimension and degree of a module space.

### `LeadingTerms(v): SpVec -> ModTupRngElt`

### `LeadingTerms(v): SuVec -> ModTupRngElt`

The leading term of each element in an `SpVec` or `SuVec`.

### `WeierstrassTerms(v): SpVec -> ModTupRngElt`

### `WeierstrassTerms(v): SuVec -> ModTupRngElt`

The Weierstrass term of each element in an `SpVec` or `SuVec`.

### `GaussValuations(v): SpVec -> SeqEnum`

### `GaussValuations(v): SuVec -> SeqEnum`

Returns a sequence of sequences of the Gauss valuations of the elements of a matrix. Since these can be `ExtendedReals`, the valuations themselves are put in an array rather than a vector.

### `WeierstrassDegrees(v): SpVec -> SeqEnum`

### `WeierstrassDegrees(v): SuVec -> SeqEnum`

Returns a sequence of sequences of the Weierstrass degrees of the elements of a vector. Since these can be `ExtendedReals`, the valuations themselves are put in an array rather than a vector.

### `IsWeaklyZero(v): SpVec -> BoolElt`

### `IsWeaklyZero(v): SuVec -> BoolElt`

Return whether an `SpVec` or `SuVec` is weakly zero.

### `M1 + M2: SpSpc, SpSpc -> SpSpc`

### `M1 + M2: SuSpc, SuSpc -> SuSpc`

### `DirectSum(M1, M2): SpSpc, SpSpc -> SpSpc`

### `DirectSum(M1, M2): SuSpc, SuSpc -> SuSpc`

### `M1 meet M2: SpSpc, SpSpc -> SpSpc`

### `M1 meet M2: SuSpc, SuSpc -> SuSpc`

The sum and intersection of two module spaces. The use of `DirectSum` requires the summands to be disjoint.

### `S * M: SpSpc, SpMat -> SpSpc`

### `S * M: SuSpc, SuMat -> SuSpc`

The transformation of a module space by a matrix. The basis of the resulting space will be given in Hermite form.

### `IsConsistent(M, v): SpMat, SpVec -> BoolElt, SpVec`

### `IsConsistent(M, v): SuMat, SuVec -> BoolElt, SuVec`

### `IsConsistent(M, e): SpMat, SeqEnum -> BoolElt, SeqEnum`

### `IsConsistent(M, e): SuMat, SeqEnum -> BoolElt, SeqEnum`

### `IsConsistent(M, W): SpMat, SpMat -> BoolElt, SpMat, SpMat`

### `IsConsistent(M, W): SuMat, SpMat -> BoolElt, SuMat, SuMat`

### `v in M: SpVec, SpMat -> BoolElt, SpVec`

### `v in M: SuVec, SpMat -> BoolElt, SuVec`

Determines whether $xM=v$ is solvable, and it so returns such an $x$. The second version takes an a sequence of vectors, and the third takes a matrix (and also includes the kernel as a third return value).

### `IsSubspace(A, B): SpSpc, SpSpc -> BoolElt, SpMat`

### `IsSubspace(A, B): SuSpc, SuSpc -> BoolElt, SuMat`

Determines whether $A$ is a subspace of $B$, and if so gives an inclusion map. Equality can be determined via symmetric inclusion, or alternatively via the Hermite form (which should be unique) of the basis matrix.

### `Example: Sp Mod Basics (ex-3f4950)`

Here are some basic examples of usage for modules.

```magma
> S<u> := SpRing (pAdicField (5, 25), 1/2); // slope 1/2
> A := SpSpace (S, 3);
> A.1;
[ 1 + O(5^25) + O(u^20), O(u^20), O(u^20) ]
> M := SpMatrix (2, 3, [ S | 5*u,5,u^2, 1/5,u^3,u ]);
> B := SpSpace (M); assert Dimension(B) eq 2;
> LeadingTerms(BasisMatrix(B));
[5^-1 + O(5^24) O($.1^20)    $.1]
[O($.1^20)      -5 + O(5^26) (24 + O(5^25))*$.1^2]
> B eq SpSpace (VerticalJoin (M, M)); // also gives inclusion
true
> A meet B eq B;
true
> Image(M) eq B; // by definition
true
> IsConsistent (M, A.1);
false
> IsConsistent (M, B.1);
true
> SpSpace([B.1]) + SpSpace([B.2]) eq B;
true
> SpSpace([B.1]) + SpSpace([u * B.2]) eq B; // u does not invert
false
> SpSpace([1/5 * B.1]) + SpSpace([5 * B.2]) eq B;
true
> sub<B|[B.1+B.2]> + sub<B|[B.2-B.1]> eq sub<B|[B.1,B.2]>;
true

```

### `Example: Sp Kernel (ex-76e436)`

Here is an example over $S^\nu_p$ with a nontrivial kernel.

```magma
> S<u> := SpRing (pAdicField (7, 30), 1); // slope 1
> M := SpMatrix(3,5,[S|1,0,u,u^2,u,  u^2/7,0,1,0,0, 0,0,1,0,u^4/7^2]);
> ROWS := Rows(M);
> V := u * ROWS[1] + u^2/7 * ROWS[3]; // create a kernel
> MAT := SpMatrix(ROWS cat [Universe(ROWS)|V]); // 4 x 5
> Dimension (SpSpace (MAT)); // 3
3
> K := Kernel (MAT); K;
SpSpace of degree 4, dimension 1 over Sp-slope ring of slope 1 given
 by Power series ring in $.1 over pAdicField(7, 30)
Basis given by:
[ (-1 + O(7^30))*u + O(u^21), O(u^21),
  (-7^-1 + O(7^29))*u^2 + O(u^21), 1 + O(7^30) + O(u^21) ]
> KK := Kernel(BasisMatrix(K)); KK;
SpSpace of degree 1, dimension 0 over Sp-slope ring of slope 1 given
 by Power series ring in $.1 over pAdicField(7, 30)
> assert ZeroVector(Ambient(KK)) in KK;

```

### `Example: Su Module (ex-9b9567)`

Here is an example over $S^\nu_u$.

```magma
> S<u> := SuRing (pAdicField (7, 20), 5/4); // slope 5/4
> A := SuSpace (S, 3);
> B := SuSpace ([A.1, A.3]); // dimension 2
> SuSpace([B.1]) + SuSpace([7^5/u^4 * B.2]) eq B; // slope 5/4
true
> Basis(sub<A|[A.1+A.3]>)[1] in B;
true
> M := SuMatrix (2, 3, [S | 7^2/u,7,5*7*u, 7,u^3,u^2 ]);
> V := SuSpace(M);
> I := V meet B; assert Dimension(I) eq 1;
> v := Basis(I)[1]; assert v in I;
> WeierstrassTerms(v);
(7 + O(7^21) O($.1^20) $.1^2)
> WeierstrassDegrees(v);
[ 0, -Infinity, 2 ]
> I + B eq B; assert v in B;
true
> J := VerticalJoin (M, M);
> SetSeed(1); // ensure example works
> JJ := (SuMatrixSpace(S,4,4)!RandomSLnZ(4,8,16)) * J;
> assert Dimension(Kernel(JJ)) eq 2;
> assert Dimension(Image(JJ)) eq 2;

```

Note that the final commands, with the dimension of the kernel/image of the given space, can sometimes fail for various reasons involving precision loss.
