Skip to content

Fix / on Julia >= 1.9 and broken tests #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PDMats"
uuid = "90014a1f-27ba-587c-ab20-58faa44d9150"
version = "0.11.17"
version = "0.11.18"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
8 changes: 6 additions & 2 deletions src/pdiagmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ function \(a::PDiagMat, x::AbstractVecOrMat)
end
function /(x::AbstractVecOrMat, a::PDiagMat)
@check_argdims a.dim == size(x, 2)
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra
return reshape(x, Val(2)) ./ permutedims(a.diag) # = (a' \ x')'
if VERSION < v"1.9-"
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra < 1.9
return reshape(x, Val(2)) ./ permutedims(a.diag) # = (a' \ x')'
else
return x ./ (x isa AbstractVector ? a.diag : a.diag')
end
end
Base.kron(A::PDiagMat, B::PDiagMat) = PDiagMat(vec(permutedims(A.diag) .* B.diag))

Expand Down
15 changes: 13 additions & 2 deletions src/pdmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,19 @@ end
*(a::PDMat, x::AbstractVector) = a.mat * x
*(a::PDMat, x::AbstractMatrix) = a.mat * x
\(a::PDMat, x::AbstractVecOrMat) = a.chol \ x
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra
/(x::AbstractVecOrMat, a::PDMat) = reshape(x, Val(2)) / a.chol
function /(x::AbstractVecOrMat, a::PDMat)
# /(::AbstractVector, ::Cholesky) is not defined
if VERSION < v"1.9-"
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra
return reshape(x, Val(2)) / a.chol
else
if x isa AbstractVector
return vec(reshape(x, Val(2)) / a.chol)
else
return x / a.chol
end
end
end

### Algebra

Expand Down
8 changes: 6 additions & 2 deletions src/scalmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ function \(a::ScalMat, x::AbstractVecOrMat)
end
function /(x::AbstractVecOrMat, a::ScalMat)
@check_argdims a.dim == size(x, 2)
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra
return reshape(x, Val(2)) / a.value
if VERSION < v"1.9-"
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra < 1.9
return reshape(x, Val(2)) / a.value
else
return x / a.value
end
end
Base.kron(A::ScalMat, B::ScalMat) = ScalMat(A.dim * B.dim, A.value * B.value )

Expand Down
20 changes: 12 additions & 8 deletions test/pdmtypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,29 @@ using Test
A = rand(1, 1)
x = randn(1)
y = x / A
@assert x / A isa Matrix{Float64}
@assert size(y) == (1, 1)

for M in (PDiagMat(vec(A)), ScalMat(1, first(A)))
@test x / M isa Matrix{Float64}
@test x / M ≈ y
z = x / M
@test typeof(z) === typeof(y)
@test size(z) == size(y)
@test z ≈ y
end

# requires https://github.com/JuliaLang/julia/pull/32594
if VERSION >= v"1.3.0-DEV.562"
@test x / PDMat(A) isa Matrix{Float64}
@test x / PDMat(A) ≈ y
z = x / PDMat(A)
@test typeof(z) === typeof(y)
@test size(z) == size(y)
@test z ≈ y
end

# right division not defined for CHOLMOD:
# `rdiv!(::Matrix{Float64}, ::SuiteSparse.CHOLMOD.Factor{Float64})` not defined
if !HAVE_CHOLMOD
@test x / PDSparseMat(sparse(first(A), 1, 1)) isa Matrix{Float64}
@test x / PDSparseMat(sparse(first(A), 1, 1)) ≈ y
z = x / PDSparseMat(sparse(first(A), 1, 1))
@test typeof(z) === typeof(y)
@test size(z) == size(y)
@test z ≈ y
end
end

Expand Down
18 changes: 9 additions & 9 deletions test/specialarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ using StaticArrays
x = rand(5)
X = rand(2, 5)
Y = rand(5, 2)
@test P * x ≈ A * x
@test P * Y ≈ A * Y
@test P * x ≈ x
@test P * Y ≈ Y
# Right division with Cholesky requires https://github.com/JuliaLang/julia/pull/32594
if VERSION >= v"1.3.0-DEV.562"
@test X / P ≈ X / A
@test X / P ≈ X
end
@test P \ x ≈ A \ x
@test P \ Y ≈ A \ Y
@test X_A_Xt(P, X) ≈ X * A * X'
@test X_invA_Xt(P, X) ≈ X * (A \ X')
@test Xt_A_X(P, Y) ≈ Y' * A * Y
@test Xt_invA_X(P, Y) ≈ Y' * (A \ Y)
@test P \ x ≈ x
@test P \ Y ≈ Y
@test X_A_Xt(P, X) ≈ X * X'
@test X_invA_Xt(P, X) ≈ X * X'
@test Xt_A_X(P, Y) ≈ Y' * Y
@test Xt_invA_X(P, Y) ≈ Y' * Y
end
end