Matrix properties

This page collects functions which compute values, invariants, or associated objects from matrices, rather than testing whether matrices satisfy certain conditions. Examples include determinants, ranks, inverses, nullspaces, polynomials, and related constructions.

Basic properties

AbstractAlgebra.number_of_rowsMethod
number_of_rows(a::MatElem)

Return the number of rows of the given matrix.

Examples

julia> M = matrix(ZZ, [1 2 3; 4 5 6])
[1   2   3]
[4   5   6]

julia> number_of_rows(M)
2
source
AbstractAlgebra.number_of_columnsMethod
number_of_columns(a::MatElem)

Return the number of columns of the given matrix.

Examples

julia> M = matrix(ZZ, [1 2 3; 4 5 6])
[1   2   3]
[4   5   6]

julia> number_of_columns(M)
3
source
Base.lengthMethod
length(a::MatrixElem{T}) where T <: NCRingElement

Return the number of entries in the given matrix.

Examples

julia> M = matrix(ZZ, [1 2 3; 4 5 6])
[1   2   3]
[4   5   6]

julia> length(M)
6
source

Trace, determinant and rank

LinearAlgebra.trMethod
tr(x::MatElem{T}) where T <: NCRingElement

Return the trace of the matrix $x$, i.e. the sum of its diagonal elements. The matrix is required to be square.

Examples

julia> R, t = polynomial_ring(QQ, :t)
(Univariate polynomial ring in t over rationals, t)

julia> S = matrix_space(R, 3, 3)
Matrix space of 3 rows and 3 columns
  over univariate polynomial ring in t over rationals

julia> A = S([t + 1 t R(1); t^2 t t; R(-2) t + 2 t^2 + t + 1])
[t + 1       t             1]
[  t^2       t             t]
[   -2   t + 2   t^2 + t + 1]

julia> b = tr(A)
t^2 + 3*t + 2
source
LinearAlgebra.detMethod
det(M::MatElem{T}) where {T <: RingElement}

Return the determinant of the given matrix $M$. The matrix is required to be square.

Examples

julia> R, x = polynomial_ring(QQ, :x)
(Univariate polynomial ring in x over rationals, x)

julia> A = R[x 1; 1 x^2];

julia> det(A)
x^3 - 1
source
LinearAlgebra.rankMethod
rank(M::MatElem{T}) where {T <: RingElement}

Return the rank of the given matrix $M$.

Examples

julia> A = QQ[1 2 3; 2 4 6; 1 1 1];

julia> rank(A)
2
source

Inverses

Base.invMethod
inv(M::MatElem{T}) where {T <: RingElement}

Given an invertible $n \times n$ matrix $M$ over a ring, return the $n \times n$ matrix $X$ such that $MX = I_n$, where $I_n$ is the $n \times n$ identity matrix.

If $M$ is not invertible over the base ring, an exception is raised.

Examples

julia> M = matrix(QQ, 3, 3, [1 2 3; 4 5 6; 0 0 1])
[1//1   2//1   3//1]
[4//1   5//1   6//1]
[0//1   0//1   1//1]

julia> inv(M)
[-5//3    2//3    1//1]
[ 4//3   -1//3   -2//1]
[ 0//1    0//1    1//1]
source
AbstractAlgebra.pseudo_invMethod
pseudo_inv(M::MatElem{T}) where {T <: RingElement}

Given a non-singular $n \times n$ matrix $M$ over a ring, return a tuple $X, d$ consisting of an $n \times n$ matrix $X$ and a denominator $d$ such that $MX = dI_n$, where $I_n$ is the $n \times n$ identity matrix.

The denominator $d$ is the determinant of $M$ up to sign.

If $M$ is not invertible over the base ring, an exception is raised.

Examples

julia> M = matrix(QQ, 3, 3, [1 2 3;4 5 6;0 0 1])
[1//1   2//1   3//1]
[4//1   5//1   6//1]
[0//1   0//1   1//1]

julia> pseudo_inv(M)
([5 -2 -3; -4 1 6; 0 0 -3], -3//1)
source

Characteristic and minimal polynomials

AbstractAlgebra.charpolyMethod
charpoly(Y::MatElem{T}) where {T <: RingElement}
charpoly(S::PolyRing{T}, Y::MatElem{T}) where {T <: RingElement}

Return the characteristic polynomial $p$ of the square matrix $Y$. If a polynomial ring $S$ over the same base ring as $Y$ is supplied, the resulting polynomial is an element of it.

Examples

julia> R, = residue_ring(ZZ, 7);

julia> S = matrix_space(R, 4, 4)
Matrix space of 4 rows and 4 columns
  over residue ring of integers modulo 7

julia> T, y = polynomial_ring(R, :y)
(Univariate polynomial ring in y over R, y)

julia> M = S([R(1) R(2) R(4) R(3); R(2) R(5) R(1) R(0);
              R(6) R(1) R(3) R(2); R(1) R(1) R(3) R(5)])
[1   2   4   3]
[2   5   1   0]
[6   1   3   2]
[1   1   3   5]

julia> A = charpoly(T, M)
y^4 + 2*y^2 + 6*y + 2

julia> A = charpoly(M)
x^4 + 2*x^2 + 6*x + 2
source
AbstractAlgebra.minpolyMethod
minpoly(M::MatElem{T}) where {T <: RingElement}
minpoly(S::PolyRing{T}, M::MatElem{T}) where {T <: RingElement}

Return the minimal polynomial $p$ of the square matrix $M$. If a polynomial ring $S$ over the same base ring as $Y$ is supplied, the resulting polynomial is an element of it.

Examples

julia> R = GF(13)
Finite field F_13

julia> S, y = polynomial_ring(R, :y)
(Univariate polynomial ring in y over R, y)

julia> M = R[7 6 1;
             7 7 5;
             8 12 5]
[7    6   1]
[7    7   5]
[8   12   5]

julia> A = minpoly(S, M)
y^2 + 10*y

julia> A = minpoly(M)
x^2 + 10*x
source

Powers

AbstractAlgebra.powersMethod
powers(a::Union{NCRingElement, MatElem}, d::Int)

Return an array $M$ of "powers" of a where $M[i + 1] = a^i$ for $i = 0..d$.

Examples

julia> M = ZZ[1 2 3; 2 3 4; 4 5 5]
[1   2   3]
[2   3   4]
[4   5   5]

julia> A = powers(M, 4)
5-element Vector{AbstractAlgebra.Generic.MatSpaceElem{BigInt}}:
 [1 0 0; 0 1 0; 0 0 1]
 [1 2 3; 2 3 4; 4 5 5]
 [17 23 26; 24 33 38; 34 48 57]
 [167 233 273; 242 337 394; 358 497 579]
 [1725 2398 2798; 2492 3465 4044; 3668 5102 5957]
source

Gram matrices

AbstractAlgebra.gramMethod
gram(x::MatElem)

Return the Gram matrix of $x$, i.e. if $x$ is an $r \times c$ matrix, return the $r \times r$ matrix whose $(i, j)$-th entry is the dot product of the $i$-th and $j$-th rows of $x$.

Examples

julia> R, t = polynomial_ring(QQ, :t)
(Univariate polynomial ring in t over rationals, t)

julia> S = matrix_space(R, 3, 3)
Matrix space of 3 rows and 3 columns
  over univariate polynomial ring in t over rationals

julia> A = S([t + 1 t R(1); t^2 t t; R(-2) t + 2 t^2 + t + 1])
[t + 1       t             1]
[  t^2       t             t]
[   -2   t + 2   t^2 + t + 1]

julia> B = gram(A)
[2*t^2 + 2*t + 2   t^3 + 2*t^2 + t                   2*t^2 + t - 1]
[t^3 + 2*t^2 + t       t^4 + 2*t^2                       t^3 + 3*t]
[  2*t^2 + t - 1         t^3 + 3*t   t^4 + 2*t^3 + 4*t^2 + 6*t + 9]
source

Content

AbstractAlgebra.contentMethod
content(x::MatrixElem{T}) where T <: RingElement

Return the greatest common divisor of all entries of the matrix $x$, assuming such a greatest common divisor exists.

Examples

julia> R, t = polynomial_ring(QQ, :t)
(Univariate polynomial ring in t over rationals, t)

julia> S = matrix_space(R, 3, 3)
Matrix space of 3 rows and 3 columns
  over univariate polynomial ring in t over rationals

julia> A = S([t + 1 t R(1); t^2 t t; R(-2) t + 2 t^2 + t + 1])
[t + 1       t             1]
[  t^2       t             t]
[   -2   t + 2   t^2 + t + 1]

julia> b = content(A)
1
source

Minors and exterior powers

AbstractAlgebra.minorsMethod
minors(A::MatElem, k::Int)

Return an array consisting of all $k$-minors of the given matrix $A$, i.e. the determinants of all $k \times k$ submatrices of $A$.

Examples

julia> A = ZZ[1 2 3; 4 5 6]
[1   2   3]
[4   5   6]

julia> minors(A, 2)
3-element Vector{BigInt}:
 -3
 -6
 -3
source
AbstractAlgebra.minors_with_positionMethod
minors_with_position(A::MatElem, k::Int)

Return an array consisting of all $k$-minors of $A$, together with the row and column indices defining the corresponding submatrices.

Examples

julia> A = ZZ[1 2 3; 4 5 6]
[1   2   3]
[4   5   6]

julia> minors_with_position(A, 2)
3-element Vector{Tuple{BigInt, Vector{Int64}, Vector{Int64}}}:
 (-3, [1, 2], [1, 2])
 (-6, [1, 2], [1, 3])
 (-3, [1, 2], [2, 3])
source
AbstractAlgebra.minors_iteratorMethod
minors_iterator(A::MatElem, k::Int)

Return an iterator computing all $k$-minors of $A$, i.e. the determinants of all $k \times k$ submatrices of $A$.

Examples

julia> A = ZZ[1 2 3; 4 5 6]
[1   2   3]
[4   5   6]

julia> first(minors_iterator(A, 2))
-3

julia> collect(minors_iterator(A, 2))
3-element Vector{BigInt}:
 -3
 -6
 -3
source
AbstractAlgebra.minors_iterator_with_positionMethod
minors_iterator_with_position(A::MatElem, k::Int)

Return an iterator computing all $k$-minors of $A$, together with the row and column indices defining the corresponding submatrices.

Examples

julia> A = ZZ[1 2 3; 4 5 6]
[1   2   3]
[4   5   6]

julia> first(minors_iterator_with_position(A, 2))
(-3, [1, 2], [1, 2])
source
AbstractAlgebra.exterior_powerMethod
exterior_power(A::MatElem, k::Int) -> MatElem

Return the matrix of the induced map on the k-th exterior power. Its entries are the determinants of the $k \times k$ submatrices of $A$.

Examples

julia> A = matrix(ZZ, 3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]);

julia> exterior_power(A, 2)
[-3    -6   -3]
[-6   -12   -6]
[-3    -6   -3]
source

Pfaffians

AbstractAlgebra.pfaffianMethod
pfaffian(M::MatElem)

Return the Pfaffian of the skew-symmetric matrix $M$.

Examples

julia> R, x = polynomial_ring(QQ, ["x$i" for i in 1:6]);

julia> M = R[0 x[1] x[2] x[3]; -x[1] 0 x[4] x[5]; -x[2] -x[4] 0 x[6]; -x[3] -x[5] -x[6] 0]
[  0    x1    x2   x3]
[-x1     0    x4   x5]
[-x2   -x4     0   x6]
[-x3   -x5   -x6    0]

julia> pfaffian(M)
x1*x6 - x2*x5 + x3*x4
source
AbstractAlgebra.pfaffiansMethod
pfaffians(M::MatElem, k::Int)

Return a vector consisting of the Pfaffians of all $k \times k$ principal submatrices of the skew-symmetric matrix $M$.

Examples

julia> R, x = polynomial_ring(QQ, ["x$i" for i in 1:6]);

julia> M = R[0 x[1] x[2] x[3]; -x[1] 0 x[4] x[5]; -x[2] -x[4] 0 x[6]; -x[3] -x[5] -x[6] 0]
[  0    x1    x2   x3]
[-x1     0    x4   x5]
[-x2   -x4     0   x6]
[-x3   -x5   -x6    0]

julia> pfaffians(M, 2)
6-element Vector{AbstractAlgebra.Generic.MPoly{Rational{BigInt}}}:
 x1
 x2
 x4
 x3
 x5
 x6
source