Manipulating matrices

This page describes functions for modifying or rearranging matrices. Many operations are available both as in-place and non-mutating variants.

Entry-wise operations

AbstractAlgebra.map_entriesMethod
map_entries(f, a::MatElem{T}) where T <: NCRingElement

Return a new matrix obtained by applying f to each entry of the matrix a.

Examples

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

julia> M2 = map_entries(x -> x^2, M)
[1    4]
[9   16]
source
AbstractAlgebra.map_entries!Method
map_entries!(f, dst::MatElem{T}, src::MatElem{U}) where {T <: NCRingElement, U <: NCRingElement}

Apply f to each entry of src, store the result in the given matrix dst and return the modified matrix dst.

Examples

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

julia> N = zero_matrix(ZZ, 2, 2)
[0   0]
[0   0]

julia> map_entries!(x -> x^2, N, M)
[1    4]
[9   16]

julia> N
[1    4]
[9   16]
source
Base.mapMethod
map(f, a::MatrixElem{T}) where T <: NCRingElement

Return a new matrix obtained by applying f to each entry of the matrix a.

This is equivalent to map_entries(f, a), see map_entries.

source
Base.map!Method
map!(f, dst::MatrixElem{T}, src::MatrixElem{U}) where {T <: NCRingElement, U <: NCRingElement}

Apply f to each entry of src, store the result in the given matrix dst and return the modified matrix dst.

This is equivalent to map_entries!(f, dst, src), see map_entries!.

source

Elementary row and column operations

AbstractAlgebra.add_columnMethod
add_column(a::MatrixElem{T}, s::RingElement, i::Int, j::Int, rows = 1:nrows(a)) where T <: RingElement

Return a new matrix obtained from a by adding s times the i-th column to the j-th column.

By default, this operation changes all entries of the j-th column in the returned matrix. An optional final argument restricts the operation to entries in the specified rows.

Examples

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

julia> add_column(M, 2, 3, 1)
[ 7   2   3]
[10   3   4]
[14   5   5]

julia> M
[1   2   3]
[2   3   4]
[4   5   5]

julia> add_column(M, 2, 3, 1, 1:1)
[7   2   3]
[2   3   4]
[4   5   5]
source
AbstractAlgebra.add_column!Method
add_column!(a::MatrixElem{T}, s::RingElement, i::Int, j::Int, rows = 1:nrows(a)) where T <: RingElement

Add s times the i-th column to the j-th column of a and return the modified matrix a.

By default, this operation modifies all entries of the j-th column. An optional final argument restricts the operation to entries in the specified rows.

Examples

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

julia> add_column!(M, 2, 3, 1)
[ 7   2   3]
[10   3   4]
[14   5   5]

julia> M
[ 7   2   3]
[10   3   4]
[14   5   5]

julia> add_column!(M, 2, 3, 1, 1:1)
[13   2   3]
[10   3   4]
[14   5   5]
source
AbstractAlgebra.add_rowMethod
add_row(a::MatrixElem{T}, s::RingElement, i::Int, j::Int, cols = 1:ncols(a)) where T <: RingElement

Return a new matrix obtained from a by adding s times the i-th row to the j-th row.

By default, this operation changes all entries of the j-th row in the returned matrix. An optional final argument restricts the operation to entries in the specified columns.

source
AbstractAlgebra.add_row!Method
add_row!(a::MatrixElem{T}, s::RingElement, i::Int, j::Int, cols = 1:ncols(a)) where T <: RingElement

Add s times the i-th row to the j-th row of a and return the modified matrix a.

By default, this operation modifies all entries of the j-th row. An optional final argument restricts the operation to entries in the specified columns.

source
AbstractAlgebra.multiply_columnMethod
multiply_column(a::MatrixElem{T}, s::RingElement, i::Int, rows = 1:nrows(a)) where T <: RingElement

Return a new matrix obtained from a by multiplying the i-th column by s.

By default, this operation changes all entries of the i-th column in the returned matrix. An optional final argument restricts the operation to entries in the specified rows.

source
AbstractAlgebra.multiply_column!Method
multiply_column!(a::MatrixElem{T}, s::RingElement, i::Int, rows = 1:nrows(a)) where T <: RingElement

Multiply the i-th column of a by s and return the modified matrix a.

By default, this operation modifies all entries of the i-th column. An optional final argument restricts the operation to entries in the specified rows.

source
AbstractAlgebra.multiply_rowMethod
multiply_row(a::MatrixElem{T}, s::RingElement, i::Int, cols = 1:ncols(a)) where T <: RingElement

Return a new matrix obtained from a by multiplying the i-th row by s.

By default, this operation changes all entries of the i-th row in the returned matrix. An optional final argument restricts the operation to entries in the specified columns.

Examples

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

julia> multiply_row(M, 2, 3)
[1    2    3]
[2    3    4]
[8   10   10]

julia> M
[1   2   3]
[2   3   4]
[4   5   5]

julia> multiply_row(M, 2, 3, 2:2)
[1    2   3]
[2    3   4]
[4   10   5]
source
AbstractAlgebra.multiply_row!Method
multiply_row!(a::MatrixElem{T}, s::RingElement, i::Int, cols = 1:ncols(a)) where T <: RingElement

Multiply the i-th row of a by s and return the modified matrix a.

By default, this operation modifies all entries of the i-th row. An optional final argument restricts the operation to entries in the specified columns.

source

Row and column permutations

Base.:*Method
*(P::Perm, x::MatrixElem{T}) where T <: NCRingElement

Return a new matrix obtained by applying the permutation P to the 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> G = SymmetricGroup(3)
Full symmetric group over 3 elements

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> P = G([1, 3, 2])
(2,3)

julia> P*A
[t + 1       t             1]
[   -2   t + 2   t^2 + t + 1]
[  t^2       t             t]
source
Base.:*Method
*(x::MatrixElem{T}, P::Perm) where T <: NCRingElement

Return a new matrix obtained by applying the permutation P to the columns 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> G = SymmetricGroup(3)
Full symmetric group over 3 elements

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> P = G([1, 3, 2])
(2,3)

julia> A*P
[t + 1             1       t]
[  t^2             t       t]
[   -2   t^2 + t + 1   t + 2]
source
AbstractAlgebra.swap_rowsMethod
swap_rows(a::MatElem{T}, i::Int, j::Int) where T <: NCRingElement

Return a new matrix obtained from a by swapping the i-th and j-th rows.

The original matrix a remains unchanged.

Examples

julia> M = identity_matrix(ZZ, 3)
[1   0   0]
[0   1   0]
[0   0   1]

julia> swap_rows(M, 1, 2)
[0   1   0]
[1   0   0]
[0   0   1]

julia> M
[1   0   0]
[0   1   0]
[0   0   1]
source
AbstractAlgebra.swap_rows!Method
swap_rows!(a::MatElem{T}, i::Int, j::Int) where T <: NCRingElement

Swap the i-th and j-th rows of a in place and return the modified matrix a.

No bounds checking is performed; the indices i and j must be in range.

Examples

julia> M = identity_matrix(ZZ, 3)
[1   0   0]
[0   1   0]
[0   0   1]

julia> swap_rows!(M, 1, 2)
[0   1   0]
[1   0   0]
[0   0   1]

julia> M
[0   1   0]
[1   0   0]
[0   0   1]
source
AbstractAlgebra.swap_colsMethod
swap_cols(a::MatElem{T}, i::Int, j::Int) where T <: NCRingElement

Return a new matrix obtained from a by swapping the i-th and j-th columns.

source
AbstractAlgebra.swap_cols!Method
swap_cols!(a::MatElem{T}, i::Int, j::Int) where T <: NCRingElement

Swap the i-th and j-th columns of a in place and return the modified matrix a.

No bounds checking is performed; the indices i and j must be in range.

source
AbstractAlgebra.reverse_rows!Method
reverse_rows!(a::MatElem{T}) where T <: NCRingElement

Reverse the order of the rows of a in place and return the modified matrix a.

source
AbstractAlgebra.reverse_colsMethod
reverse_cols(a::MatElem{T}) where T <: NCRingElement

Return a new matrix obtained from a by reversing the order of its columns.

source
AbstractAlgebra.reverse_cols!Method
reverse_cols!(a::MatElem{T}) where T <: NCRingElement

Reverse the order of the columns of a in place and return the modified matrix a.

source

Transposition

Matrices can be transposed either by creating a new matrix or by modifying an existing square matrix in place.

Base.transposeMethod
transpose(x::MatElem)

Return a new matrix containing the transpose of x.

Examples

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

julia> A = matrix(R, [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> transpose(A)
[t + 1   t^2            -2]
[    t     t         t + 2]
[    1     t   t^2 + t + 1]
source
LinearAlgebra.transpose!Method
transpose!(x::MatElem)
transpose!(z::T, x::T) where T <: MatElem

Return the transpose of x, storing the result in a pre-existing matrix.

The unary version stores the result in x itself and requires x to be square; an error is raised otherwise.

The binary version stores the transpose of x in z and returns z. The matrix z must have size ncols(x) by nrows(x). No dimension checks are performed, and incorrect dimensions may result in undefined behaviour.

source