Skip to content

Commit c3067e2

Browse files
authored
Fix DimensionMismatch in loadings (#229)
When the embedding has a number of zero eigenvalues, `λ` gets truncated. However, `U` does not, and thus `loadings` throws a `DimensionMismatch` error on master. This fixes that error. Alternatively, we could truncate `U` as well, but retaining the full `U` matrix makes it easier to obtain a full orthonormal basis, if desired.
1 parent e64e4f4 commit c3067e2

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/cmds.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ projection(M::MDS) = M.U
135135
"""
136136
eigvecs(M::MDS)
137137
138-
Get the MDS model `M` eigenvectors matrix.
138+
Get the MDS model `M` eigenvectors matrix.
139139
"""
140140
eigvecs(M::MDS) = projection(M)
141141

@@ -151,7 +151,7 @@ eigvals(M::MDS) = M.λ
151151
152152
Get the loading of the MDS model `M`.
153153
"""
154-
loadings(M::MDS) = sqrt.(M.λ)' .* M.U
154+
loadings(M::MDS) = sqrt.(M.λ)' .* M.U[:,eachindex(M.λ)]
155155

156156
## use
157157

test/cmds.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,12 @@ using StableRNGs
156156
M = fit(MetricMDS, Δ, maxoutdim=2, distances=true, initial=Z, metric=isotonic, tol=1e-6)
157157
@test stress(M) 0.234 atol=1e-2
158158

159+
# loadings when the Gramian has fewer positive eigenvalues than there are points
160+
X = randn(2, 100)
161+
D = [norm(x - y) for x in eachcol(X), y in eachcol(X)]
162+
M = fit(MDS, D; distances=true)
163+
@test length(eigvals(M)) < 99 # check that this test is checking what we want it to check
164+
Y = loadings(M)
165+
@test size(Y) == (100, length(eigvals(M)))
159166
end
160167

0 commit comments

Comments
 (0)