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

Conversation

andreasKroepelin
Copy link

Currently, calling LinearAlgebra.dot(x, A, y) for an AbstractPDMat A falls back to the default implementation for A::AbstractMatrix. Especially for A::PDiagMat and A::ScalMat that is pretty wasteful, so I implemented the three-argument dot function in terms of LinearAlgebra.Diagonal and LinearAlgebra.UniformScaling, respectively.

Does it make sense to implement a special case for A::PDMat as well? Something similar to the quad function?
Also, is the test I added at a suitable location?

@andreasKroepelin
Copy link
Author

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)
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})

?

@@ -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)
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

@@ -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"


### dot product

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 product

function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector)
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)

?


function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector)
dot(x, UniformScaling(a.value), 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

@@ -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"


@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants