-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from alyst/spec_matrices
Cleanup Special Matrices code
- Loading branch information
Showing
6 changed files
with
160 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
transpose_linear_indices(n, [m]) | ||
Put each linear index of the *n×m* matrix to the position of the | ||
corresponding element in the transposed matrix. | ||
## Example | ||
` | ||
1 4 | ||
2 5 => 1 2 3 | ||
3 6 4 5 6 | ||
` | ||
""" | ||
transpose_linear_indices(n::Integer, m::Integer = n) = | ||
repeat(1:n, inner = m) .+ repeat((0:(m-1)) * n, outer = n) | ||
|
||
""" | ||
CommutationMatrix(n::Integer) <: AbstractMatrix{Int} | ||
A *commutation matrix* *C* is a n²×n² matrix of 0s and 1s. | ||
If *vec(A)* is a vectorized form of a n×n matrix *A*, | ||
then ``C * vec(A) = vec(Aᵀ)``. | ||
""" | ||
struct CommutationMatrix <: AbstractMatrix{Int} | ||
n::Int | ||
n²::Int | ||
transpose_inds::Vector{Int} # maps the linear indices of n×n matrix *B* to the indices of matrix *B'* | ||
|
||
CommutationMatrix(n::Integer) = new(n, n^2, transpose_linear_indices(n)) | ||
end | ||
|
||
Base.size(A::CommutationMatrix) = (A.n², A.n²) | ||
Base.size(A::CommutationMatrix, dim::Integer) = | ||
1 <= dim <= 2 ? A.n² : throw(ArgumentError("invalid matrix dimension $dim")) | ||
Base.length(A::CommutationMatrix) = A.n²^2 | ||
Base.getindex(A::CommutationMatrix, i::Int, j::Int) = j == A.transpose_inds[i] ? 1 : 0 | ||
|
||
function Base.:(*)(A::CommutationMatrix, B::AbstractVector) | ||
size(A, 2) == size(B, 1) || throw( | ||
DimensionMismatch("A has $(size(A, 2)) columns, but B has $(size(B, 1)) elements"), | ||
) | ||
return B[A.transpose_inds] | ||
end | ||
|
||
function Base.:(*)(A::CommutationMatrix, B::AbstractMatrix) | ||
size(A, 2) == size(B, 1) || throw( | ||
DimensionMismatch("A has $(size(A, 2)) columns, but B has $(size(B, 1)) rows"), | ||
) | ||
return B[A.transpose_inds, :] | ||
end | ||
|
||
function Base.:(*)(A::CommutationMatrix, B::SparseMatrixCSC) | ||
size(A, 2) == size(B, 1) || throw( | ||
DimensionMismatch("A has $(size(A, 2)) columns, but B has $(size(B, 1)) rows"), | ||
) | ||
return SparseMatrixCSC( | ||
size(B, 1), | ||
size(B, 2), | ||
copy(B.colptr), | ||
A.transpose_inds[B.rowval], | ||
copy(B.nzval), | ||
) | ||
end | ||
|
||
function LinearAlgebra.lmul!(A::CommutationMatrix, B::SparseMatrixCSC) | ||
size(A, 2) == size(B, 1) || throw( | ||
DimensionMismatch("A has $(size(A, 2)) columns, but B has $(size(B, 1)) rows"), | ||
) | ||
|
||
@inbounds for (i, rowind) in enumerate(B.rowval) | ||
B.rowval[i] = A.transpose_inds[rowind] | ||
end | ||
return B | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using StructuralEquationModels, Test, Random, SparseArrays, LinearAlgebra | ||
using StructuralEquationModels: | ||
CommutationMatrix, transpose_linear_indices, duplication_matrix, elimination_matrix | ||
|
||
Random.seed!(73721) | ||
|
||
n = 4 | ||
m = 5 | ||
|
||
@testset "Commutation matrix" begin | ||
# transpose linear indices | ||
A = rand(n, m) | ||
@test reshape(A[transpose_linear_indices(n, m)], m, n) == A' | ||
# commutation matrix multiplication | ||
K = CommutationMatrix(n) | ||
# test K array interface methods | ||
@test size(K) == (n^2, n^2) | ||
@test size(K, 1) == n^2 | ||
@test length(K) == n^4 | ||
nn_linind = LinearIndices((n, n)) | ||
@test K[nn_linind[3, 2], nn_linind[2, 3]] == 1 | ||
@test K[nn_linind[3, 2], nn_linind[3, 2]] == 0 | ||
|
||
B = rand(n, n) | ||
@test_throws DimensionMismatch K * rand(n, m) | ||
@test K * vec(B) == vec(B') | ||
C = sprand(n, n, 0.5) | ||
@test K * vec(C) == vec(C') | ||
# lmul! | ||
D = sprand(n^2, n^2, 0.1) | ||
E = copy(D) | ||
F = Matrix(E) | ||
lmul!(K, D) | ||
@test D == K * E | ||
@test Matrix(D) == K * F | ||
end | ||
|
||
@testset "Duplication / elimination matrix" begin | ||
A = rand(m, m) | ||
A = A * A' | ||
|
||
# dupication | ||
D = duplication_matrix(m) | ||
@test D * A[tril(trues(size(A)))] == vec(A) | ||
|
||
# elimination | ||
E = elimination_matrix(m) | ||
@test E * vec(A) == A[tril(trues(size(A)))] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters