-
Notifications
You must be signed in to change notification settings - Fork 43
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
Remove inv_value
and inv_diag
#146
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,12 @@ | ||
""" | ||
Positive definite diagonal matrix. | ||
""" | ||
struct PDiagMat{T<:Real,V<:AbstractVector} <: AbstractPDMat{T} | ||
struct PDiagMat{T<:Real,V<:AbstractVector{T}} <: AbstractPDMat{T} | ||
dim::Int | ||
diag::V | ||
inv_diag::V | ||
|
||
PDiagMat{T,S}(d::Int,v::AbstractVector,inv_v::AbstractVector) where {T,S} = | ||
new{T,S}(d,v,inv_v) | ||
end | ||
|
||
function PDiagMat(v::AbstractVector,inv_v::AbstractVector) | ||
@check_argdims length(v) == length(inv_v) | ||
PDiagMat{eltype(v),typeof(v)}(length(v), v, inv_v) | ||
end | ||
|
||
PDiagMat(v::AbstractVector) = PDiagMat(v, inv.(v)) | ||
PDiagMat(v::AbstractVector{<:Real}) = PDiagMat{eltype(v),typeof(v)}(length(v), v) | ||
|
||
### Conversion | ||
Base.convert(::Type{PDiagMat{T}}, a::PDiagMat) where {T<:Real} = PDiagMat(convert(AbstractArray{T}, a.diag)) | ||
|
@@ -51,13 +42,13 @@ end | |
*(a::PDiagMat, c::T) where {T<:Real} = PDiagMat(a.diag * c) | ||
*(a::PDiagMat, x::AbstractVector) = a.diag .* x | ||
*(a::PDiagMat, x::AbstractMatrix) = a.diag .* x | ||
\(a::PDiagMat, x::AbstractVecOrMat) = a.inv_diag .* x | ||
/(x::AbstractVecOrMat, a::PDiagMat) = a.inv_diag .* x | ||
\(a::PDiagMat, x::AbstractVecOrMat) = x ./ a.diag | ||
/(x::AbstractVecOrMat, a::PDiagMat) = x ./ a.diag | ||
Base.kron(A::PDiagMat, B::PDiagMat) = PDiagMat( vcat([A.diag[i] * B.diag for i in 1:dim(A)]...) ) | ||
|
||
### Algebra | ||
|
||
Base.inv(a::PDiagMat) = PDiagMat(a.inv_diag, a.diag) | ||
Base.inv(a::PDiagMat) = PDiagMat(map(inv, a.diag)) | ||
function LinearAlgebra.logdet(a::PDiagMat) | ||
diag = a.diag | ||
return isempty(diag) ? zero(log(zero(eltype(diag)))) : sum(log, diag) | ||
|
@@ -71,9 +62,9 @@ LinearAlgebra.eigmin(a::PDiagMat) = minimum(a.diag) | |
function whiten!(r::StridedVector, a::PDiagMat, x::StridedVector) | ||
n = dim(a) | ||
@check_argdims length(r) == length(x) == n | ||
v = a.inv_diag | ||
v = a.diag | ||
for i = 1:n | ||
r[i] = x[i] * sqrt(v[i]) | ||
r[i] = x[i] / sqrt(v[i]) | ||
end | ||
return r | ||
end | ||
|
@@ -88,17 +79,21 @@ function unwhiten!(r::StridedVector, a::PDiagMat, x::StridedVector) | |
return r | ||
end | ||
|
||
whiten!(r::StridedMatrix, a::PDiagMat, x::StridedMatrix) = | ||
broadcast!(*, r, x, sqrt.(a.inv_diag)) | ||
function whiten!(r::StridedMatrix, a::PDiagMat, x::StridedMatrix) | ||
r .= x ./ sqrt.(a.diag) | ||
return r | ||
end | ||
|
||
unwhiten!(r::StridedMatrix, a::PDiagMat, x::StridedMatrix) = | ||
broadcast!(*, r, x, sqrt.(a.diag)) | ||
function unwhiten!(r::StridedMatrix, a::PDiagMat, x::StridedMatrix) | ||
r .= x .* sqrt.(a.diag) | ||
return r | ||
end | ||
|
||
|
||
### quadratic forms | ||
|
||
quad(a::PDiagMat, x::AbstractVector) = wsumsq(a.diag, x) | ||
invquad(a::PDiagMat, x::AbstractVector) = wsumsq(a.inv_diag, x) | ||
invquad(a::PDiagMat, x::AbstractVector) = invwsumsq(a.diag, x) | ||
|
||
function quad!(r::AbstractArray, a::PDiagMat, x::StridedMatrix) | ||
m, n = size(x) | ||
|
@@ -116,12 +111,12 @@ end | |
|
||
function invquad!(r::AbstractArray, a::PDiagMat, x::StridedMatrix) | ||
m, n = size(x) | ||
ainvd = a.inv_diag | ||
@check_argdims m == length(ainvd) && length(r) == n | ||
ad = a.diag | ||
@check_argdims m == length(ad) && length(r) == n | ||
@inbounds for j = 1:n | ||
s = zero(promote_type(eltype(ainvd), eltype(x))) | ||
s = zero(zero(eltype(x)) / zero(eltype(ad))) | ||
for i in 1:m | ||
s += ainvd[i] * abs2(x[i,j]) | ||
s += abs2(x[i,j]) / ad[i] | ||
end | ||
r[j] = s | ||
end | ||
|
@@ -132,7 +127,7 @@ end | |
### tri products | ||
|
||
function X_A_Xt(a::PDiagMat, x::StridedMatrix) | ||
z = x .* reshape(sqrt.(a.diag), 1, dim(a)) | ||
z = x .* sqrt.(reshape(a.diag, 1, dim(a))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This (and similar line below) should be fixed in any case. It seems the current implementation prevents broadcast fusion and creates an additional intermediate array. |
||
z * transpose(z) | ||
end | ||
|
||
|
@@ -142,11 +137,11 @@ function Xt_A_X(a::PDiagMat, x::StridedMatrix) | |
end | ||
|
||
function X_invA_Xt(a::PDiagMat, x::StridedMatrix) | ||
z = x .* reshape(sqrt.(a.inv_diag), 1, dim(a)) | ||
z = x ./ sqrt.(reshape(a.diag, 1, dim(a))) | ||
z * transpose(z) | ||
end | ||
|
||
function Xt_invA_X(a::PDiagMat, x::StridedMatrix) | ||
z = x .* sqrt.(a.inv_diag) | ||
z = x ./ sqrt.(a.diag) | ||
transpose(z) * z | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this have bumped to 0.11.5? It looks like 0.11.4 was already released when this was merged: https://github.com/JuliaStats/PDMats.jl/releases/tag/v0.11.4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should, JuliaRegistrator noticed it as well: 2f3a83e#commitcomment-60431201 The PR was created before the other PR was merged and released, so the diff in the PR was outdated and based on an earlier version of the master branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it: 15fecad 🙂