@@ -20,7 +20,7 @@ function test_pdmat(C::AbstractPDMat, Cmat::Matrix;
2020 t_logdet:: Bool = true , # whether to test logdet method
2121 t_eig:: Bool = true , # whether to test eigmax and eigmin
2222 t_mul:: Bool = true , # whether to test multiplication
23- t_rdiv :: Bool = true , # whether to test right division (solve)
23+ t_div :: Bool = true , # whether to test division
2424 t_quad:: Bool = true , # whether to test quad & invquad
2525 t_triprod:: Bool = true , # whether to test X_A_Xt, Xt_A_X, X_invA_Xt, and Xt_invA_X
2626 t_whiten:: Bool = true # whether to test whiten and unwhiten
@@ -45,7 +45,7 @@ function test_pdmat(C::AbstractPDMat, Cmat::Matrix;
4545 X = rand (eltype (C),d,n) .- convert (eltype (C),0.5 )
4646
4747 t_mul && pdtest_mul (C, Cmat, X, verbose)
48- t_rdiv && pdtest_rdiv (C, Imat, X, verbose)
48+ t_div && pdtest_div (C, Imat, X, verbose)
4949 t_quad && pdtest_quad (C, Cmat, Imat, X, verbose)
5050 t_triprod && pdtest_triprod (C, Cmat, Imat, X, verbose)
5151
@@ -174,42 +174,60 @@ end
174174
175175function pdtest_mul (C:: AbstractPDMat , Cmat:: Matrix , verbose:: Int )
176176 n = 5
177- X = rand (eltype (C),dim (C), n)
178-
179- _pdt (verbose, " multiply" )
180- @test C * X ≈ Cmat * X
181-
182- for i = 1 : n
183- xi = vec (copy (X[:,i]))
184- @test C * xi ≈ Cmat * xi
185- end
177+ X = rand (eltype (C), dim (C), n)
178+ pdtest_mul (C, Cmat, X, verbose)
186179end
187180
188181
189182function pdtest_mul (C:: AbstractPDMat , Cmat:: Matrix , X:: Matrix , verbose:: Int )
190183 _pdt (verbose, " multiply" )
184+ d, n = size (X)
185+ @assert d == dim (C)
186+ @assert size (Cmat) == size (C)
191187 @test C * X ≈ Cmat * X
192188
193- y = similar (C * X, size (C, 1 ) )
194- ymat = similar (Cmat * X, size (Cmat, 1 ) )
195- for i = 1 : size (X, 2 )
189+ y = similar (C * X, d )
190+ ymat = similar (Cmat * X, d )
191+ for i = 1 : n
196192 xi = vec (copy (X[:,i]))
197193 @test C * xi ≈ Cmat * xi
198194
199195 mul! (y, C, xi)
200196 mul! (ymat, Cmat, xi)
201197 @test y ≈ ymat
202198 end
199+
200+ # Dimension mismatches
201+ @test_throws DimensionMismatch C * rand (d + 1 )
202+ @test_throws DimensionMismatch C * rand (d + 1 , n)
203203end
204204
205205
206- function pdtest_rdiv (C:: AbstractPDMat , Imat:: Matrix , X:: Matrix , verbose:: Int )
207- _pdt (verbose, " rdivide" )
206+ function pdtest_div (C:: AbstractPDMat , Imat:: Matrix , X:: Matrix , verbose:: Int )
207+ _pdt (verbose, " divide" )
208+ d, n = size (X)
209+ @assert d == dim (C)
210+ @assert size (Imat) == size (C)
208211 @test C \ X ≈ Imat * X
212+ # Right division with Choleskyrequires https://github.com/JuliaLang/julia/pull/32594
213+ # CHOLMOD throws error since no method is found for
214+ # `rdiv!(::Matrix{Float64}, ::SuiteSparse.CHOLMOD.Factor{Float64})`
215+ check_rdiv = ! (C isa PDMat && VERSION < v " 1.3.0-DEV.562" ) && ! (C isa PDSparseMat && HAVE_CHOLMOD)
216+ check_rdiv && @test Matrix (X' ) / C ≈ (C \ X)'
209217
210- for i = 1 : size (X, 2 )
218+ for i = 1 : n
211219 xi = vec (copy (X[:,i]))
212220 @test C \ xi ≈ Imat * xi
221+ check_rdiv && @test Matrix (xi' ) / C ≈ (C \ xi)'
222+ end
223+
224+
225+ # Dimension mismatches
226+ @test_throws DimensionMismatch C \ rand (d + 1 )
227+ @test_throws DimensionMismatch C \ rand (d + 1 , n)
228+ if check_rdiv
229+ @test_throws DimensionMismatch rand (1 , d + 1 ) / C
230+ @test_throws DimensionMismatch rand (n, d + 1 ) / C
213231 end
214232end
215233
@@ -240,23 +258,29 @@ end
240258
241259
242260function pdtest_triprod (C:: AbstractPDMat , Cmat:: Matrix , Imat:: Matrix , X:: Matrix , verbose:: Int )
261+ d, n = size (X)
262+ @assert d == dim (C)
243263 Xt = copy (transpose (X))
244264
245265 _pdt (verbose, " X_A_Xt" )
246266 # default tolerance in isapprox is different on 0.4. rtol argument can be deleted
247267 # ≈ form used when 0.4 is no longer supported
248268 lhs, rhs = X_A_Xt (C, Xt), Xt * Cmat * X
249269 @test isapprox (lhs, rhs, rtol= sqrt (max (eps (real (float (eltype (lhs)))), eps (real (float (eltype (rhs)))))))
270+ @test_throws DimensionMismatch X_A_Xt (C, rand (n, d + 1 ))
250271
251272 _pdt (verbose, " Xt_A_X" )
252273 lhs, rhs = Xt_A_X (C, X), Xt * Cmat * X
253274 @test isapprox (lhs, rhs, rtol= sqrt (max (eps (real (float (eltype (lhs)))), eps (real (float (eltype (rhs)))))))
275+ @test_throws DimensionMismatch Xt_A_X (C, rand (d + 1 , n))
254276
255277 _pdt (verbose, " X_invA_Xt" )
256278 @test X_invA_Xt (C, Xt) ≈ Xt * Imat * X
279+ @test_throws DimensionMismatch X_invA_Xt (C, rand (n, d + 1 ))
257280
258281 _pdt (verbose, " Xt_invA_X" )
259282 @test Xt_invA_X (C, X) ≈ Xt * Imat * X
283+ @test_throws DimensionMismatch Xt_invA_X (C, rand (d + 1 , n))
260284end
261285
262286
0 commit comments