-
Notifications
You must be signed in to change notification settings - Fork 6
Cleanup Special Matrices code #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Maximilian-Stefan-Ernst
merged 7 commits into
StructuralEquationModels:devel
from
alyst:spec_matrices
Apr 24, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
03dac06
CommutationMatrix type
alyst 47a1757
simplify elimination_matrix()
alyst fd212c0
simplify duplication_matrix()
2984203
add tests for commutation/dublication/elimination matrices
Maximilian-Stefan-Ernst b6db6b1
small unit test fixes
alyst 3abe92e
commutation_matrix * vec method
alyst c1e7a69
more comm_matrix tests
alyst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.