|
1 | | -# test pd matrix types |
2 | 1 | using LinearAlgebra, PDMats, SparseArrays, SuiteSparse |
3 | 2 | using Test |
4 | 3 |
|
5 | | -for T in [Float64, Float32] |
6 | | - #test that all external constructors are accessible |
7 | | - m = Matrix{T}(I, 2, 2) |
8 | | - @test PDMat(m, cholesky(m)).mat == PDMat(Symmetric(m)).mat == PDMat(m).mat == PDMat(cholesky(m)).mat |
9 | | - d = ones(T,2) |
10 | | - @test PDiagMat(d,d).inv_diag == PDiagMat(d).inv_diag |
11 | | - x = one(T) |
12 | | - @test ScalMat(2,x,x).inv_value == ScalMat(2,x).inv_value |
13 | | - s = SparseMatrixCSC{T}(I, 2, 2) |
14 | | - @test PDSparseMat(s, cholesky(s)).mat == PDSparseMat(s).mat == PDSparseMat(cholesky(s)).mat |
15 | | - |
16 | | - #test the functionality |
17 | | - M = convert(Array{T,2}, [4. -2. -1.; -2. 5. -1.; -1. -1. 6.]) |
18 | | - V = convert(Array{T,1}, [1.5, 2.5, 2.0]) |
19 | | - X = convert(T,2.0) |
20 | | - |
21 | | - test_pdmat(PDMat(M), M, cmat_eq=true, verbose=1) #tests of PDMat |
22 | | - cholL = Cholesky(Matrix(transpose(cholesky(M).factors)), 'L', 0) |
23 | | - test_pdmat(PDMat(cholL), M, cmat_eq=true, verbose=1) #tests of PDMat |
24 | | - test_pdmat(PDiagMat(V), Matrix(Diagonal(V)), cmat_eq=true, verbose=1) #tests of PDiagMat |
25 | | - test_pdmat(ScalMat(3,x), x*Matrix{T}(I, 3, 3), cmat_eq=true, verbose=1) #tests of ScalMat |
26 | | - test_pdmat(PDSparseMat(sparse(M)), M, cmat_eq=true, verbose=1, t_eig=false) |
27 | | -end |
| 4 | +@testset "pd matrix types" begin |
| 5 | + for T in [Float64, Float32] |
| 6 | + @testset "test that all external constructors are accessible" begin |
| 7 | + m = Matrix{T}(I, 2, 2) |
| 8 | + @test PDMat(m, cholesky(m)).mat == PDMat(Symmetric(m)).mat == PDMat(m).mat == PDMat(cholesky(m)).mat |
| 9 | + d = ones(T,2) |
| 10 | + @test PDiagMat(d,d).inv_diag == PDiagMat(d).inv_diag |
| 11 | + x = one(T) |
| 12 | + @test ScalMat(2,x,x).inv_value == ScalMat(2,x).inv_value |
| 13 | + s = SparseMatrixCSC{T}(I, 2, 2) |
| 14 | + @test PDSparseMat(s, cholesky(s)).mat == PDSparseMat(s).mat == PDSparseMat(cholesky(s)).mat |
| 15 | + end |
| 16 | + |
| 17 | + @testset "test the functionality" begin |
| 18 | + M = convert(Array{T,2}, [4. -2. -1.; -2. 5. -1.; -1. -1. 6.]) |
| 19 | + V = convert(Array{T,1}, [1.5, 2.5, 2.0]) |
| 20 | + X = convert(T,2.0) |
| 21 | + |
| 22 | + @testset "PDMat from Matrix" begin |
| 23 | + test_pdmat(PDMat(M), M, cmat_eq=true, verbose=1) |
| 24 | + end |
| 25 | + @testset "PDMat from Cholesky" begin |
| 26 | + cholL = Cholesky(Matrix(transpose(cholesky(M).factors)), 'L', 0) |
| 27 | + test_pdmat(PDMat(cholL), M, cmat_eq=true, verbose=1) |
| 28 | + end |
| 29 | + @testset "PDiagMat" begin |
| 30 | + test_pdmat(PDiagMat(V), Matrix(Diagonal(V)), cmat_eq=true, verbose=1) |
| 31 | + end |
| 32 | + @testset "ScalMat" begin |
| 33 | + test_pdmat(ScalMat(3,X), X*Matrix{T}(I, 3, 3), cmat_eq=true, verbose=1) |
| 34 | + end |
| 35 | + @testset "PDSparseMat" begin |
| 36 | + test_pdmat(PDSparseMat(sparse(M)), M, cmat_eq=true, verbose=1, t_eig=false) |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + @testset "zero-dimensional matrices" begin |
| 42 | + Z = zeros(0, 0) |
| 43 | + test_pdmat(PDMat(Z), Z; t_eig=false) |
| 44 | + test_pdmat(PDiagMat(diag(Z)), Z; t_eig=false) |
| 45 | + end |
28 | 46 |
|
29 | | -m = Matrix{Float32}(I, 2, 2) |
30 | | -@test convert(PDMat{Float64}, PDMat(m)).mat == PDMat(convert(Array{Float64}, m)).mat |
31 | | -@test convert(AbstractArray{Float64}, PDMat(m)).mat == PDMat(convert(Array{Float64}, m)).mat |
32 | | -m = ones(Float32,2) |
33 | | -@test convert(PDiagMat{Float64}, PDiagMat(m)).diag == PDiagMat(convert(Array{Float64}, m)).diag |
34 | | -@test convert(AbstractArray{Float64}, PDiagMat(m)).diag == PDiagMat(convert(Array{Float64}, m)).diag |
35 | | -x = one(Float32); d = 4 |
36 | | -@test convert(ScalMat{Float64}, ScalMat(d, x)).value == ScalMat(d, convert(Float64, x)).value |
37 | | -@test convert(AbstractArray{Float64}, ScalMat(d, x)).value == ScalMat(d, convert(Float64, x)).value |
38 | | -s = SparseMatrixCSC{Float32}(I, 2, 2) |
39 | | -@test convert(PDSparseMat{Float64}, PDSparseMat(s)).mat == PDSparseMat(convert(SparseMatrixCSC{Float64}, s)).mat |
40 | | - |
41 | | -Z = zeros(0, 0) |
42 | | -test_pdmat(PDMat(Z), Z; t_eig=false) |
43 | | -test_pdmat(PDiagMat(diag(Z)), Z; t_eig=false) |
44 | | - |
45 | | -# no-op conversion with correct eltype (#101) |
46 | | -X = PDMat((Y->Y'Y)(randn(Float32, 4, 4))) |
47 | | -@test convert(AbstractArray{Float32}, X) === X |
48 | | -@test convert(AbstractArray{Float64}, X) !== X |
49 | | - |
50 | | -# type stability of whiten! and unwhiten! |
51 | | -a = PDMat([1 0.5; 0.5 1]) |
52 | | -@inferred whiten!(ones(2), a, ones(2)) |
53 | | -@inferred unwhiten!(ones(2), a, ones(2)) |
54 | | -@inferred whiten(a, ones(2)) |
55 | | -@inferred unwhiten(a, ones(2)) |
56 | | - |
57 | | -# convert Matrix type to the same Cholesky type (#117) |
58 | | -@test PDMat([1 0; 0 1]) == [1.0 0.0; 0.0 1.0] |
| 47 | + @testset "float type conversions" begin |
| 48 | + m = Matrix{Float32}(I, 2, 2) |
| 49 | + @test convert(PDMat{Float64}, PDMat(m)).mat == PDMat(convert(Array{Float64}, m)).mat |
| 50 | + @test convert(AbstractArray{Float64}, PDMat(m)).mat == PDMat(convert(Array{Float64}, m)).mat |
| 51 | + m = ones(Float32,2) |
| 52 | + @test convert(PDiagMat{Float64}, PDiagMat(m)).diag == PDiagMat(convert(Array{Float64}, m)).diag |
| 53 | + @test convert(AbstractArray{Float64}, PDiagMat(m)).diag == PDiagMat(convert(Array{Float64}, m)).diag |
| 54 | + x = one(Float32); d = 4 |
| 55 | + @test convert(ScalMat{Float64}, ScalMat(d, x)).value == ScalMat(d, convert(Float64, x)).value |
| 56 | + @test convert(AbstractArray{Float64}, ScalMat(d, x)).value == ScalMat(d, convert(Float64, x)).value |
| 57 | + s = SparseMatrixCSC{Float32}(I, 2, 2) |
| 58 | + @test convert(PDSparseMat{Float64}, PDSparseMat(s)).mat == PDSparseMat(convert(SparseMatrixCSC{Float64}, s)).mat |
| 59 | + end |
| 60 | + |
| 61 | + @testset "no-op conversion with correct eltype (#101)" begin |
| 62 | + X = PDMat((Y->Y'Y)(randn(Float32, 4, 4))) |
| 63 | + @test convert(AbstractArray{Float32}, X) === X |
| 64 | + @test convert(AbstractArray{Float64}, X) !== X |
| 65 | + end |
| 66 | + |
| 67 | + @testset "type stability of whiten! and unwhiten!" begin |
| 68 | + a = PDMat([1 0.5; 0.5 1]) |
| 69 | + @inferred whiten!(ones(2), a, ones(2)) |
| 70 | + @inferred unwhiten!(ones(2), a, ones(2)) |
| 71 | + @inferred whiten(a, ones(2)) |
| 72 | + @inferred unwhiten(a, ones(2)) |
| 73 | + end |
| 74 | + |
| 75 | + @testset "convert Matrix type to the same Cholesky type (#117)" begin |
| 76 | + @test PDMat([1 0; 0 1]) == [1.0 0.0; 0.0 1.0] |
| 77 | + end |
| 78 | +end |
0 commit comments