Skip to content

Implement dot product#209

Open
andreasKroepelin wants to merge 1 commit into
JuliaStats:masterfrom
andreasKroepelin:master
Open

Implement dot product#209
andreasKroepelin wants to merge 1 commit into
JuliaStats:masterfrom
andreasKroepelin:master

Conversation

@andreasKroepelin

Copy link
Copy Markdown

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
Copy Markdown
Author

I'm puzzled why the test is failing, it works for me locally...

Comment thread src/pdiagmat.jl

### dot product

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

Copy link
Copy Markdown
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})

?

Comment thread src/pdiagmat.jl
end

### dot product

Copy link
Copy Markdown
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"

Comment thread src/pdiagmat.jl
function LinearAlgebra.dot(x::AbstractVector, a::PDiagMat, y::AbstractVector)
dot(x, Diagonal(a.diag), y)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
end

Comment thread src/scalmat.jl
end

### dot product

Copy link
Copy Markdown
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"

Comment thread src/scalmat.jl

### dot product

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

Copy link
Copy Markdown
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)

Comment thread src/scalmat.jl
### dot product

function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector)
dot(x, UniformScaling(a.value), y)

Copy link
Copy Markdown
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)

?

Comment thread src/scalmat.jl

function LinearAlgebra.dot(x::AbstractVector, a::ScalMat, y::AbstractVector)
dot(x, UniformScaling(a.value), y)
end

Copy link
Copy Markdown
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

Comment thread test/generics.jl
@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
Copy Markdown
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"

Comment thread test/generics.jl

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

Copy link
Copy Markdown
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