Skip to content

Commit 2984203

Browse files
Maximilian-Stefan-Ernstalyst
authored andcommitted
add tests for commutation/dublication/elimination matrices
1 parent fd212c0 commit 2984203

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/additional_functions/helper.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function get_observed(rowind, data, semobserved; args = (), kwargs = NamedTuple(
4141
return observed_vec
4242
end
4343

44-
skipmissing_mean(mat::AbstractMatrix) =
44+
skipmissing_mean(mat::AbstractMatrix) =
4545
[mean(skipmissing(coldata)) for coldata in eachcol(mat)]
4646

4747
function F_one_person(imp_mean, meandiff, inverse, data, logdet)

test/unit_tests/matrix_helpers.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using StructuralEquationModels, Test, Random, SparseArrays, LinearAlgebra
2+
using StructuralEquationModels:
3+
CommutationMatrix, transpose_linear_indices, duplication_matrix, elimination_matrix
4+
5+
Random.seed!(73721)
6+
7+
n = 4
8+
m = 5
9+
10+
@testset "Commutation matrix" begin
11+
# transpose linear indices
12+
A = rand(n, m)
13+
@test reshape(A[transpose_linear_indices(n, m)], m, n) == A'
14+
# commutation matrix multiplication
15+
K = CommutationMatrix(n)
16+
B = rand(n, n)
17+
@test K * vec(B) == vec(B')
18+
C = sprand(n, n, 0.5)
19+
@test K * vec(C) == vec(C')
20+
# lmul!
21+
D = sprand(n^2, n^2, 0.1)
22+
E = copy(D)
23+
lmul!(K, D)
24+
@test D == K * E
25+
end
26+
27+
@testset "Duplication / elimination matrix" begin
28+
A = rand(m, m)
29+
A = A * A'
30+
# dupication
31+
D = duplication_matrix(m)
32+
@test D * A[tril(trues(size(A)))] == vec(A)
33+
# elimination
34+
D = elimination_matrix(m)
35+
@test D * vec(A) == A[tril(trues(size(A)))]
36+
end

test/unit_tests/unit_tests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ end
77
@safetestset "SemObs" begin
88
include("data_input_formats.jl")
99
end
10+
11+
@safetestset "Matrix algebra helper functions" begin
12+
include("matrix_helpers.jl")
13+
end

0 commit comments

Comments
 (0)