From 455d7714a92d9b3bd173149c5346bac2d2d18021 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Wed, 12 Jun 2024 23:56:37 +0200 Subject: [PATCH 1/3] Fix method ambiguity of `*` with `PDiagMat` --- Project.toml | 2 +- src/pdiagmat.jl | 14 +++++++------- test/testutils.jl | 3 +++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index 50b4806..7f8676b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "PDMats" uuid = "90014a1f-27ba-587c-ab20-58faa44d9150" -version = "0.11.31" +version = "0.11.32" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/pdiagmat.jl b/src/pdiagmat.jl index 70ab2c5..7960d24 100644 --- a/src/pdiagmat.jl +++ b/src/pdiagmat.jl @@ -52,14 +52,14 @@ function pdadd!(r::Matrix, a::Matrix, b::PDiagMat, c) end *(a::PDiagMat, c::Real) = PDiagMat(a.diag * c) -function *(a::PDiagMat, x::AbstractVector) - @check_argdims a.dim == length(x) - return a.diag .* x -end -function *(a::PDiagMat, x::AbstractMatrix) - @check_argdims a.dim == size(x, 1) - return a.diag .* x +*(a::PDiagMat, x::AbstractVector) = Diagonal(a.diag) * x +*(a::PDiagMat, x::AbstractMatrix) = Diagonal(a.diag) * x +if VERSION < v"1.11.0-DEV.1216" + # Fix method ambiguity with `*(::AbstractMatrix, ::Diagonal)` in LinearAlgebra + # Removed in https://github.com/JuliaLang/julia/pull/52464 + *(a::PDiagMat, x::Diagonal) = Diagonal(a.diag) * x end + function \(a::PDiagMat, x::AbstractVecOrMat) @check_argdims a.dim == size(x, 1) return x ./ a.diag diff --git a/test/testutils.jl b/test/testutils.jl index e3a6e1e..ac0400d 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -221,6 +221,9 @@ function pdtest_mul(C, Cmat::Matrix, X::Matrix, verbose::Int) @assert size(Cmat) == size(C) @test C * X ≈ Cmat * X + # Special matrix types (see #176) + @test C * Diagonal(X) ≈ Cmat * Diagonal(X) + y = similar(C * X, d) ymat = similar(Cmat * X, d) for i = 1:n From 568d724c725eeeab4e825ece4493e9b4d981e2dd Mon Sep 17 00:00:00 2001 From: David Widmann Date: Thu, 13 Jun 2024 00:15:15 +0200 Subject: [PATCH 2/3] Additional fixes for `ScalMat`, `PDMat`, and `PDSparseMat` --- src/pdmat.jl | 6 ++++++ src/pdsparsemat.jl | 6 ++++++ src/scalmat.jl | 9 +++++++++ 3 files changed, 21 insertions(+) diff --git a/src/pdmat.jl b/src/pdmat.jl index 7f4fcf0..11acefd 100644 --- a/src/pdmat.jl +++ b/src/pdmat.jl @@ -67,6 +67,12 @@ end *(a::PDMat, c::Real) = PDMat(a.mat * c) *(a::PDMat, x::AbstractVector) = a.mat * x *(a::PDMat, x::AbstractMatrix) = a.mat * x +if VERSION < v"1.11.0-DEV.1216" + # Fix method ambiguity with `*(::AbstractMatrix, ::Diagonal)` in LinearAlgebra + # Removed in https://github.com/JuliaLang/julia/pull/52464 + *(a::PDMat, x::Diagonal) = a.mat * x +end + \(a::PDMat, x::AbstractVecOrMat) = a.chol \ x function /(x::AbstractVecOrMat, a::PDMat) # /(::AbstractVector, ::Cholesky) is not defined diff --git a/src/pdsparsemat.jl b/src/pdsparsemat.jl index eb5f640..ed302e7 100644 --- a/src/pdsparsemat.jl +++ b/src/pdsparsemat.jl @@ -68,6 +68,12 @@ end *(a::PDSparseMat, c::Real) = PDSparseMat(a.mat * c) *(a::PDSparseMat, x::AbstractMatrix) = a.mat * x # defining these seperately to avoid *(a::PDSparseMat, x::AbstractVector) = a.mat * x # ambiguity errors +if VERSION < v"1.11.0-DEV.1216" + # Fix method ambiguity with `*(::AbstractMatrix, ::Diagonal)` in LinearAlgebra + # Removed in https://github.com/JuliaLang/julia/pull/52464 + *(a::PDSparseMat, x::Diagonal) = a.mat * x +end + \(a::PDSparseMat{T}, x::AbstractVecOrMat{T}) where {T<:Real} = convert(Array{T},a.chol \ convert(Array{Float64},x)) #to avoid limitations in sparse factorization library CHOLMOD, see e.g., julia issue #14076 /(x::AbstractVecOrMat{T}, a::PDSparseMat{T}) where {T<:Real} = convert(Array{T},convert(Array{Float64},x) / a.chol ) diff --git a/src/scalmat.jl b/src/scalmat.jl index feabe8a..c566939 100644 --- a/src/scalmat.jl +++ b/src/scalmat.jl @@ -48,6 +48,15 @@ function *(a::ScalMat, x::AbstractMatrix) @check_argdims a.dim == size(x, 1) return a.value * x end +if VERSION < v"1.11.0-DEV.1216" + # Fix method ambiguity with `*(::AbstractMatrix, ::Diagonal)` in LinearAlgebra + # Removed in https://github.com/JuliaLang/julia/pull/52464 + function *(a::ScalMat, x::Diagonal) + @check_argdims a.dim == size(x, 1) + return a.value * x + end +end + function \(a::ScalMat, x::AbstractVecOrMat) @check_argdims a.dim == size(x, 1) return x / a.value From b2dffb0836f65d3170e869d0b59e7d0eb5c481c9 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Thu, 13 Jun 2024 00:21:40 +0200 Subject: [PATCH 3/3] Modify test to account for inconsistencies in how Julia handles empty matrices --- test/testutils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testutils.jl b/test/testutils.jl index ac0400d..840ec78 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -236,8 +236,8 @@ function pdtest_mul(C, Cmat::Matrix, X::Matrix, verbose::Int) end # Dimension mismatches - @test_throws DimensionMismatch C * rand(d + 1) - @test_throws DimensionMismatch C * rand(d + 1, n) + @test_throws DimensionMismatch C * rand(d + 2) + @test_throws DimensionMismatch C * rand(d + 2, n) end