Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/pdiagmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Member

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:

Suggested change
# https://github.com/JuliaLang/julia/commit/2425ae760fb5151c5c7dd0554e87c5fc9e24de73
if VERSION >= v"1.4.0-DEV.92"

function LinearAlgebra.dot(x::AbstractVector, a::PDiagMat, y::AbstractVector)
Copy link
Member

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

Suggested change
function LinearAlgebra.dot(x::AbstractVector, a::PDiagMat, y::AbstractVector)
function LinearAlgebra.dot(x::AbstractVector{<:Real}, a::PDiagMat, y::AbstractVector{<:Real})

?

dot(x, Diagonal(a.diag), y)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
end

6 changes: 6 additions & 0 deletions src/scalmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here:

Suggested change
# https://github.com/JuliaLang/julia/commit/2425ae760fb5151c5c7dd0554e87c5fc9e24de73
if VERSION >= v"1.4.0-DEV.92"

function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector)
Copy link
Member

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:

Suggested change
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(x, UniformScaling(a.value), y)
Copy link
Member

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

Suggested change
dot(x, UniformScaling(a.value), y)
a.value * dot(x, y)

?

end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
end
end
end

10 changes: 10 additions & 0 deletions test/generics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# https://github.com/JuliaLang/julia/commit/2425ae760fb5151c5c7dd0554e87c5fc9e24de73
if VERSION >= v"1.4.0-DEV.92"

@testset "dot products" begin
pm1 = PDiagMat([1., 2., 3.])
pm2 = ScalMat(3, 2.)
x = [13., 42., .7]
y = [0.5, .125, 20.24]

@test dot(x, pm1, y) ≈ dot(x, Matrix(pm1), y)
@test dot(x, pm2, y) ≈ dot(x, Matrix(pm2), y)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
end
end
end

Loading