From 2f6fd1b3c7ef71f3a91b1cce9d9e92724a5c890b Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 10:50:01 +0530 Subject: [PATCH 01/20] Typed Matrix constructor and more stable tests --- Project.toml | 7 ++++++- src/pdiagmat.jl | 7 ++----- src/pdmat.jl | 4 ++-- src/pdsparsemat.jl | 4 ++-- test/pdmtypes.jl | 24 ++++++++++++++++++------ test/specialarrays.jl | 2 +- test/testutils.jl | 4 +++- 7 files changed, 34 insertions(+), 18 deletions(-) diff --git a/Project.toml b/Project.toml index d9b4481..f708168 100644 --- a/Project.toml +++ b/Project.toml @@ -8,12 +8,17 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" [compat] +BandedMatrices = "1" +Random = "1" +StaticArrays = "1" +Test = "1" julia = "1" [extras] BandedMatrices = "aae01518-5342-5314-be14-df237901396f" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["BandedMatrices", "StaticArrays", "Test"] +test = ["BandedMatrices", "StaticArrays", "Random", "Test"] diff --git a/src/pdiagmat.jl b/src/pdiagmat.jl index cb43a70..fad2b7e 100644 --- a/src/pdiagmat.jl +++ b/src/pdiagmat.jl @@ -29,6 +29,7 @@ Base.convert(::Type{AbstractPDMat{T}}, a::PDiagMat) where {T<:Real} = convert(PD Base.size(a::PDiagMat) = (a.dim, a.dim) Base.Matrix(a::PDiagMat) = Matrix(Diagonal(a.diag)) +Base.Matrix{T}(a::PDiagMat) where {T} = Matrix{T}(Diagonal(a.diag)) LinearAlgebra.diag(a::PDiagMat) = copy(a.diag) LinearAlgebra.cholesky(a::PDiagMat) = Cholesky(Diagonal(map(sqrt, a.diag)), 'U', 0) @@ -37,11 +38,7 @@ Base.broadcastable(a::PDiagMat) = Base.broadcastable(Diagonal(a.diag)) ### Inheriting from AbstractMatrix -function Base.getindex(a::PDiagMat, i::Integer) - ncol, nrow = fldmod1(i, a.dim) - ncol == nrow ? a.diag[nrow] : zero(eltype(a)) -end -Base.getindex(a::PDiagMat{T},i::Integer,j::Integer) where {T} = i == j ? a.diag[i] : zero(T) +Base.@propagate_inbounds Base.getindex(a::PDiagMat{T}, i::Int, j::Int) where {T} = i == j ? a.diag[i] : zero(T) ### Arithmetics diff --git a/src/pdmat.jl b/src/pdmat.jl index abaec69..fc3dca4 100644 --- a/src/pdmat.jl +++ b/src/pdmat.jl @@ -43,6 +43,7 @@ Base.convert(::Type{AbstractPDMat{T}}, a::PDMat) where {T<:Real} = convert(PDMat Base.size(a::PDMat) = (a.dim, a.dim) Base.Matrix(a::PDMat) = Matrix(a.mat) +Base.Matrix{T}(a::PDMat) where {T} = Matrix{T}(a.mat) LinearAlgebra.diag(a::PDMat) = diag(a.mat) LinearAlgebra.cholesky(a::PDMat) = a.chol @@ -51,8 +52,7 @@ Base.broadcastable(a::PDMat) = Base.broadcastable(a.mat) ### Inheriting from AbstractMatrix -Base.getindex(a::PDMat, i::Int) = getindex(a.mat, i) -Base.getindex(a::PDMat, I::Vararg{Int, N}) where {N} = getindex(a.mat, I...) +Base.@propagate_inbounds Base.getindex(a::PDMat, I::Vararg{Int, 2}) = getindex(a.mat, I...) ### Arithmetics diff --git a/src/pdsparsemat.jl b/src/pdsparsemat.jl index 032e0ed..b1b602e 100644 --- a/src/pdsparsemat.jl +++ b/src/pdsparsemat.jl @@ -46,13 +46,13 @@ Base.convert(::Type{AbstractPDMat{T}}, a::PDSparseMat) where {T<:Real} = convert Base.size(a::PDSparseMat) = (a.dim, a.dim) Base.Matrix(a::PDSparseMat) = Matrix(a.mat) +Base.Matrix{T}(a::PDSparseMat) where {T} = Matrix{T}(a.mat) LinearAlgebra.diag(a::PDSparseMat) = diag(a.mat) LinearAlgebra.cholesky(a::PDSparseMat) = a.chol ### Inheriting from AbstractMatrix -Base.getindex(a::PDSparseMat,i::Integer) = getindex(a.mat, i) -Base.getindex(a::PDSparseMat,I::Vararg{Int, N}) where {N} = getindex(a.mat, I...) +Base.getindex(a::PDSparseMat, I::Vararg{Int, 2}) = getindex(a.mat, I...) ### Arithmetics diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index c749cf8..99ce6ce 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -166,19 +166,28 @@ using Test @testset "AbstractPDMat constructors (#136)" begin x = rand(10, 10) - A = x' * x + I + A = Array(Hermitian(x' * x + I)) M = @inferred AbstractPDMat(A) @test M isa PDMat @test Matrix(M) ≈ A + Mat32 = Matrix{Float32}(M) + @test eltype(Mat32) == Float32 + @test Mat32 ≈ Float32.(A) M = @inferred AbstractPDMat(cholesky(A)) @test M isa PDMat @test Matrix(M) ≈ A + Mat32 = Matrix{Float32}(M) + @test eltype(Mat32) == Float32 + @test Mat32 ≈ Float32.(A) M = @inferred AbstractPDMat(Diagonal(A)) @test M isa PDiagMat @test Matrix(M) ≈ Diagonal(A) + Mat32 = Matrix{Float32}(M) + @test eltype(Mat32) == Float32 + @test Mat32 ≈ Float32.(Diagonal(A)) M = @inferred AbstractPDMat(Symmetric(Diagonal(A))) @test M isa PDiagMat @@ -191,6 +200,9 @@ using Test M = @inferred AbstractPDMat(sparse(A)) @test M isa PDSparseMat @test Matrix(M) ≈ A + Mat32 = Matrix{Float32}(M) + @test eltype(Mat32) == Float32 + @test Mat32 ≈ Float32.(A) if VERSION < v"1.6" # inference fails e.g. on Julia 1.0 @@ -205,7 +217,7 @@ using Test @testset "properties and fields" begin for dim in (1, 5, 10) x = rand(dim, dim) - M = PDMat(x' * x + I) + M = PDMat(Array(Hermitian(x' * x + I))) @test fieldnames(typeof(M)) == (:mat, :chol) @test propertynames(M) == (fieldnames(typeof(M))..., :dim) @test getproperty(M, :dim) === dim @@ -230,7 +242,7 @@ using Test if HAVE_CHOLMOD x = sprand(dim, dim, 0.2) - M = PDSparseMat(x' * x + I) + M = PDSparseMat(sparse(Hermitian(x' * x + I))) @test fieldnames(typeof(M)) == (:mat, :chol) @test propertynames(M) == (fieldnames(typeof(M))..., :dim) @test getproperty(M, :dim) === dim @@ -243,14 +255,14 @@ using Test @testset "Incorrect dimensions" begin x = rand(10, 10) - A = x * x' + I + A = Array(Hermitian(x * x' + I)) C = cholesky(A) @test_throws DimensionMismatch PDMat(A[:, 1:(end - 1)], C) @test_throws DimensionMismatch PDMat(A[1:(end - 1), 1:(end - 1)], C) if HAVE_CHOLMOD x = sprand(10, 10, 0.2) - A = x * x' + I + A = sparse(Hermitian(x * x' + I)) C = cholesky(A) @test_throws DimensionMismatch PDSparseMat(A[:, 1:(end - 1)], C) @test_throws DimensionMismatch PDSparseMat(A[1:(end - 1), 1:(end - 1)], C) @@ -261,7 +273,7 @@ using Test # This falls back to the generic method in Julia based on broadcasting dim = 4 x = rand(dim, dim) - A = PDMat(x' * x + I) + A = PDMat(Array(Hermitian(x' * x + I))) @test Base.broadcastable(A) == A.mat B = PDiagMat(rand(dim)) diff --git a/test/specialarrays.jl b/test/specialarrays.jl index be6cbe8..8e67d77 100644 --- a/test/specialarrays.jl +++ b/test/specialarrays.jl @@ -4,7 +4,7 @@ using StaticArrays @testset "Special matrix types" begin @testset "StaticArrays" begin # Full matrix - S = (x -> x * x' + I)(@SMatrix(randn(4, 7))) + S = (x -> SMatrix{4,4}(Hermitian(x * x' + I)))(@SMatrix(randn(4, 7))) PDS = PDMat(S) @test PDS isa PDMat{Float64, <:SMatrix{4, 4, Float64}} @test isbits(PDS) diff --git a/test/testutils.jl b/test/testutils.jl index 32bd5a9..e3a6e1e 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -4,7 +4,9 @@ # the implementation of a subtype of AbstractPDMat # -using PDMats, SuiteSparse, Test +using PDMats, SuiteSparse, Test, Random + +Random.seed!(10) const HAVE_CHOLMOD = isdefined(SuiteSparse, :CHOLMOD) const PDMatType = HAVE_CHOLMOD ? Union{PDMat, PDSparseMat, PDiagMat} : Union{PDMat, PDiagMat} From 2284177a5f585c7753217a0ca2c5fed382d9ed51 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 11:08:42 +0530 Subject: [PATCH 02/20] Fix compat bounds --- Project.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index f708168..3dd7f89 100644 --- a/Project.toml +++ b/Project.toml @@ -8,10 +8,10 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" [compat] -BandedMatrices = "1" -Random = "1" +BandedMatrices = "0.15, 1" +Random = "<0.0.1, 1" StaticArrays = "1" -Test = "1" +Test = "<0.0.1, 1" julia = "1" [extras] From 9182c12289d38f1e0100e09119bde1271c64a68e Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 11:15:22 +0530 Subject: [PATCH 03/20] Allow BandedMatrices v0.17 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 3dd7f89..583f021 100644 --- a/Project.toml +++ b/Project.toml @@ -8,7 +8,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" [compat] -BandedMatrices = "0.15, 1" +BandedMatrices = "0.15, 0.17, 1" Random = "<0.0.1, 1" StaticArrays = "1" Test = "<0.0.1, 1" From 441799cc309ac2a46802483b9206db6e113b5918 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:15:05 +0530 Subject: [PATCH 04/20] Remove untyped Matrix method --- src/pdiagmat.jl | 1 - src/pdmat.jl | 1 - src/pdsparsemat.jl | 1 - 3 files changed, 3 deletions(-) diff --git a/src/pdiagmat.jl b/src/pdiagmat.jl index fad2b7e..70ab2c5 100644 --- a/src/pdiagmat.jl +++ b/src/pdiagmat.jl @@ -28,7 +28,6 @@ Base.convert(::Type{AbstractPDMat{T}}, a::PDiagMat) where {T<:Real} = convert(PD ### Basics Base.size(a::PDiagMat) = (a.dim, a.dim) -Base.Matrix(a::PDiagMat) = Matrix(Diagonal(a.diag)) Base.Matrix{T}(a::PDiagMat) where {T} = Matrix{T}(Diagonal(a.diag)) LinearAlgebra.diag(a::PDiagMat) = copy(a.diag) LinearAlgebra.cholesky(a::PDiagMat) = Cholesky(Diagonal(map(sqrt, a.diag)), 'U', 0) diff --git a/src/pdmat.jl b/src/pdmat.jl index fc3dca4..903f143 100644 --- a/src/pdmat.jl +++ b/src/pdmat.jl @@ -42,7 +42,6 @@ Base.convert(::Type{AbstractPDMat{T}}, a::PDMat) where {T<:Real} = convert(PDMat ### Basics Base.size(a::PDMat) = (a.dim, a.dim) -Base.Matrix(a::PDMat) = Matrix(a.mat) Base.Matrix{T}(a::PDMat) where {T} = Matrix{T}(a.mat) LinearAlgebra.diag(a::PDMat) = diag(a.mat) LinearAlgebra.cholesky(a::PDMat) = a.chol diff --git a/src/pdsparsemat.jl b/src/pdsparsemat.jl index b1b602e..3f4cf15 100644 --- a/src/pdsparsemat.jl +++ b/src/pdsparsemat.jl @@ -45,7 +45,6 @@ Base.convert(::Type{AbstractPDMat{T}}, a::PDSparseMat) where {T<:Real} = convert ### Basics Base.size(a::PDSparseMat) = (a.dim, a.dim) -Base.Matrix(a::PDSparseMat) = Matrix(a.mat) Base.Matrix{T}(a::PDSparseMat) where {T} = Matrix{T}(a.mat) LinearAlgebra.diag(a::PDSparseMat) = diag(a.mat) LinearAlgebra.cholesky(a::PDSparseMat) = a.chol From 1381050227d04ccbbfbcdad8512338dca680d3b9 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:33:52 +0530 Subject: [PATCH 05/20] use `Symmetric` instead of `Hermitian` Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index 99ce6ce..7cf9c0e 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -166,7 +166,7 @@ using Test @testset "AbstractPDMat constructors (#136)" begin x = rand(10, 10) - A = Array(Hermitian(x' * x + I)) + A = Array(Symmetric(x' * x + I)) M = @inferred AbstractPDMat(A) @test M isa PDMat From ba676f84bedf65df6ace58c970cdbe63125e9a49 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:37:15 +0530 Subject: [PATCH 06/20] Inferred test 1 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index 7cf9c0e..b175e3a 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -171,7 +171,7 @@ using Test M = @inferred AbstractPDMat(A) @test M isa PDMat @test Matrix(M) ≈ A - Mat32 = Matrix{Float32}(M) + Mat32 = @inferred Matrix{Float32}(M) @test eltype(Mat32) == Float32 @test Mat32 ≈ Float32.(A) From f89dc5a7634bc71037471af6d572dfa23a7896bb Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:37:44 +0530 Subject: [PATCH 07/20] use `Symmetric` instead of `Hermitian` 2 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index b175e3a..58c4695 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -217,7 +217,7 @@ using Test @testset "properties and fields" begin for dim in (1, 5, 10) x = rand(dim, dim) - M = PDMat(Array(Hermitian(x' * x + I))) + M = PDMat(Array(Symmetric(x' * x + I))) @test fieldnames(typeof(M)) == (:mat, :chol) @test propertynames(M) == (fieldnames(typeof(M))..., :dim) @test getproperty(M, :dim) === dim From 68b360b4361dcd2713066acd460432d72de0d6e8 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:38:30 +0530 Subject: [PATCH 08/20] use `Symmetric` instead of `Hermitian` 3 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index 58c4695..8695199 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -255,7 +255,7 @@ using Test @testset "Incorrect dimensions" begin x = rand(10, 10) - A = Array(Hermitian(x * x' + I)) + A = Array(Symmetric(x * x' + I)) C = cholesky(A) @test_throws DimensionMismatch PDMat(A[:, 1:(end - 1)], C) @test_throws DimensionMismatch PDMat(A[1:(end - 1), 1:(end - 1)], C) From 9415bf91004a832c64898d4729edf9a5f960b948 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:38:47 +0530 Subject: [PATCH 09/20] use `Symmetric` instead of `Hermitian` 4 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index 8695199..703fccf 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -262,7 +262,7 @@ using Test if HAVE_CHOLMOD x = sprand(10, 10, 0.2) - A = sparse(Hermitian(x * x' + I)) + A = sparse(Symmetric(x * x' + I)) C = cholesky(A) @test_throws DimensionMismatch PDSparseMat(A[:, 1:(end - 1)], C) @test_throws DimensionMismatch PDSparseMat(A[1:(end - 1), 1:(end - 1)], C) From b53d12f7bce579e0c7327a7bd04e835971445b8e Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:39:02 +0530 Subject: [PATCH 10/20] use `Symmetric` instead of `Hermitian` 5 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index 703fccf..b1e061b 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -273,7 +273,7 @@ using Test # This falls back to the generic method in Julia based on broadcasting dim = 4 x = rand(dim, dim) - A = PDMat(Array(Hermitian(x' * x + I))) + A = PDMat(Array(Symmetric(x' * x + I))) @test Base.broadcastable(A) == A.mat B = PDiagMat(rand(dim)) From f203bff1c7c8651aea2a07dfc07f4d83568970e8 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:39:25 +0530 Subject: [PATCH 11/20] Inferred test 2 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index b1e061b..03941db 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -178,7 +178,7 @@ using Test M = @inferred AbstractPDMat(cholesky(A)) @test M isa PDMat @test Matrix(M) ≈ A - Mat32 = Matrix{Float32}(M) + Mat32 = @inferred Matrix{Float32}(M) @test eltype(Mat32) == Float32 @test Mat32 ≈ Float32.(A) From d67b2b55477b50044cefde79f1e1bfbe8b13951e Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:39:53 +0530 Subject: [PATCH 12/20] use `Symmetric` instead of `Hermitian` 5 Co-authored-by: David Widmann --- test/specialarrays.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/specialarrays.jl b/test/specialarrays.jl index 8e67d77..9292c2e 100644 --- a/test/specialarrays.jl +++ b/test/specialarrays.jl @@ -4,7 +4,7 @@ using StaticArrays @testset "Special matrix types" begin @testset "StaticArrays" begin # Full matrix - S = (x -> SMatrix{4,4}(Hermitian(x * x' + I)))(@SMatrix(randn(4, 7))) + S = (x -> SMatrix{4,4}(Symmetric(x * x' + I)))(@SMatrix(randn(4, 7))) PDS = PDMat(S) @test PDS isa PDMat{Float64, <:SMatrix{4, 4, Float64}} @test isbits(PDS) From 9acb5a05b0a61289c61dfa85505f896f326477d6 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:40:14 +0530 Subject: [PATCH 13/20] Inferred test 3 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index 03941db..62324a2 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -185,7 +185,7 @@ using Test M = @inferred AbstractPDMat(Diagonal(A)) @test M isa PDiagMat @test Matrix(M) ≈ Diagonal(A) - Mat32 = Matrix{Float32}(M) + Mat32 = @inferred Matrix{Float32}(M) @test eltype(Mat32) == Float32 @test Mat32 ≈ Float32.(Diagonal(A)) From 0a6856c5e29b79b67b8d7db168d973361323c909 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:40:52 +0530 Subject: [PATCH 14/20] Drop BandedMatrices v0.17 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 583f021..3dd7f89 100644 --- a/Project.toml +++ b/Project.toml @@ -8,7 +8,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" [compat] -BandedMatrices = "0.15, 0.17, 1" +BandedMatrices = "0.15, 1" Random = "<0.0.1, 1" StaticArrays = "1" Test = "<0.0.1, 1" From ab9783895e44c84f6f76ea2a49f87673b611ddf6 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:41:42 +0530 Subject: [PATCH 15/20] Test for Matrix{eltype} 1 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index 62324a2..b1d5eb2 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -179,7 +179,7 @@ using Test @test M isa PDMat @test Matrix(M) ≈ A Mat32 = @inferred Matrix{Float32}(M) - @test eltype(Mat32) == Float32 + @test Mat32 isa Matrix{Float32} @test Mat32 ≈ Float32.(A) M = @inferred AbstractPDMat(Diagonal(A)) From 4e0cf9710b3787f03a10c9dbf16501ca03d6f3ba Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:41:55 +0530 Subject: [PATCH 16/20] Test for Matrix{eltype} 2 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index b1d5eb2..3d272f2 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -186,7 +186,7 @@ using Test @test M isa PDiagMat @test Matrix(M) ≈ Diagonal(A) Mat32 = @inferred Matrix{Float32}(M) - @test eltype(Mat32) == Float32 + @test Mat32 isa Matrix{Float32} @test Mat32 ≈ Float32.(Diagonal(A)) M = @inferred AbstractPDMat(Symmetric(Diagonal(A))) From 2262eae037619dce33382326c93fe976790c48aa Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:42:36 +0530 Subject: [PATCH 17/20] Inferred test 4 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index 3d272f2..f46cf1f 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -200,7 +200,7 @@ using Test M = @inferred AbstractPDMat(sparse(A)) @test M isa PDSparseMat @test Matrix(M) ≈ A - Mat32 = Matrix{Float32}(M) + Mat32 = @inferred Matrix{Float32}(M) @test eltype(Mat32) == Float32 @test Mat32 ≈ Float32.(A) From 04506b6707a1e59c0f83c10881d4d97cd0bc90aa Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:42:54 +0530 Subject: [PATCH 18/20] Test for Matrix{eltype} 3 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index f46cf1f..a315d7f 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -201,7 +201,7 @@ using Test @test M isa PDSparseMat @test Matrix(M) ≈ A Mat32 = @inferred Matrix{Float32}(M) - @test eltype(Mat32) == Float32 + @test Mat32 isa Matrix{Float32} @test Mat32 ≈ Float32.(A) if VERSION < v"1.6" From c06753797d4fe45adba5fa2564bf1a0483815e79 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:44:57 +0530 Subject: [PATCH 19/20] use `Symmetric` instead of `Hermitian` 6 Co-authored-by: David Widmann --- test/pdmtypes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pdmtypes.jl b/test/pdmtypes.jl index a315d7f..a8f7530 100644 --- a/test/pdmtypes.jl +++ b/test/pdmtypes.jl @@ -242,7 +242,7 @@ using Test if HAVE_CHOLMOD x = sprand(dim, dim, 0.2) - M = PDSparseMat(sparse(Hermitian(x' * x + I))) + M = PDSparseMat(sparse(Symmetric(x' * x + I))) @test fieldnames(typeof(M)) == (:mat, :chol) @test propertynames(M) == (fieldnames(typeof(M))..., :dim) @test getproperty(M, :dim) === dim From 5710a1bf7de2ce5a25e04010742441e7bc14b056 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 8 Dec 2023 13:58:56 +0530 Subject: [PATCH 20/20] Restore linear indexing methods --- src/pdmat.jl | 4 ++++ src/pdsparsemat.jl | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pdmat.jl b/src/pdmat.jl index 903f143..d47153e 100644 --- a/src/pdmat.jl +++ b/src/pdmat.jl @@ -51,6 +51,10 @@ Base.broadcastable(a::PDMat) = Base.broadcastable(a.mat) ### Inheriting from AbstractMatrix +Base.IndexStyle(::Type{PDMat{T,S}}) where {T,S} = Base.IndexStyle(S) +# Linear Indexing +Base.@propagate_inbounds Base.getindex(a::PDMat, i::Int) = getindex(a.mat, i) +# Cartesian Indexing Base.@propagate_inbounds Base.getindex(a::PDMat, I::Vararg{Int, 2}) = getindex(a.mat, I...) ### Arithmetics diff --git a/src/pdsparsemat.jl b/src/pdsparsemat.jl index 3f4cf15..eb5f640 100644 --- a/src/pdsparsemat.jl +++ b/src/pdsparsemat.jl @@ -51,7 +51,11 @@ LinearAlgebra.cholesky(a::PDSparseMat) = a.chol ### Inheriting from AbstractMatrix -Base.getindex(a::PDSparseMat, I::Vararg{Int, 2}) = getindex(a.mat, I...) +Base.IndexStyle(::Type{PDSparseMat{T,S}}) where {T,S} = IndexStyle(S) +# Linear Indexing +Base.@propagate_inbounds Base.getindex(a::PDSparseMat, i::Int) = getindex(a.mat, i) +# Cartesian Indexing +Base.@propagate_inbounds Base.getindex(a::PDSparseMat, I::Vararg{Int, 2}) = getindex(a.mat, I...) ### Arithmetics