-
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
Implement dot product #209
base: master
Are you sure you want to change the base?
Conversation
I'm puzzled why the test is failing, it works for me locally... |
@@ -203,3 +203,9 @@ function invquad(a::PDiagMat{<:Real,<:Vector}, x::Matrix) | |||
return invquad!(Vector{T}(undef, size(x, 2)), a, x) | |||
end | |||
|
|||
### dot product | |||
|
|||
function LinearAlgebra.dot(x::AbstractVector, a::PDiagMat, y::AbstractVector) |
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 we restrict this to
function LinearAlgebra.dot(x::AbstractVector, a::PDiagMat, y::AbstractVector) | |
function LinearAlgebra.dot(x::AbstractVector{<:Real}, a::PDiagMat, y::AbstractVector{<:Real}) |
?
@@ -203,3 +203,9 @@ function invquad(a::PDiagMat{<:Real,<:Vector}, x::Matrix) | |||
return invquad!(Vector{T}(undef, size(x, 2)), a, x) | |||
end | |||
|
|||
### dot product | |||
|
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.
This has to be put behind a version check:
# https://github.com/JuliaLang/julia/commit/2425ae760fb5151c5c7dd0554e87c5fc9e24de73 | |
if VERSION >= v"1.4.0-DEV.92" |
function LinearAlgebra.dot(x::AbstractVector, a::PDiagMat, y::AbstractVector) | ||
dot(x, Diagonal(a.diag), y) | ||
end | ||
|
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.
end |
@@ -187,3 +187,9 @@ function Xt_invA_X(a::ScalMat, x::Matrix{<:Real}) | |||
@check_argdims LinearAlgebra.checksquare(a) == size(x, 1) | |||
return Symmetric(_rdiv!(transpose(x) * x, a.value)) | |||
end | |||
|
|||
### dot product | |||
|
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.
Same here:
# https://github.com/JuliaLang/julia/commit/2425ae760fb5151c5c7dd0554e87c5fc9e24de73 | |
if VERSION >= v"1.4.0-DEV.92" |
|
||
### dot product | ||
|
||
function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector) |
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.
We should check that the dimensions match:
function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector) | |
function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector) | |
@check_argdims LinearAlgebra.checksquare(a) == length(x) == length(y) |
### dot product | ||
|
||
function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector) | ||
dot(x, UniformScaling(a.value), y) |
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 guess even simpler would be
dot(x, UniformScaling(a.value), y) | |
a.value * dot(x, y) |
?
|
||
function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector) | ||
dot(x, UniformScaling(a.value), y) | ||
end |
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.
end | |
end | |
end |
@@ -28,3 +28,13 @@ end | |||
@test isposdef(PDMat([1.0 0.0; 0.0 1.0])) | |||
@test isposdef(PDiagMat([1.0, 1.0])) | |||
@test isposdef(ScalMat(2, 3.0)) | |||
|
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.
# https://github.com/JuliaLang/julia/commit/2425ae760fb5151c5c7dd0554e87c5fc9e24de73 | |
if VERSION >= v"1.4.0-DEV.92" |
|
||
@test dot(x, pm1, y) ≈ dot(x, Matrix(pm1), y) | ||
@test dot(x, pm2, y) ≈ dot(x, Matrix(pm2), y) | ||
end |
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.
end | |
end | |
end |
Currently, calling
LinearAlgebra.dot(x, A, y)
for anAbstractPDMat
A
falls back to the default implementation forA::AbstractMatrix
. Especially forA::PDiagMat
andA::ScalMat
that is pretty wasteful, so I implemented the three-argumentdot
function in terms ofLinearAlgebra.Diagonal
andLinearAlgebra.UniformScaling
, respectively.Does it make sense to implement a special case for
A::PDMat
as well? Something similar to thequad
function?Also, is the test I added at a suitable location?