Skip to content

Implement dot product#209

Open
andreasKroepelin wants to merge 1 commit intoJuliaStats:masterfrom
andreasKroepelin:master
Open

Implement dot product#209
andreasKroepelin wants to merge 1 commit intoJuliaStats: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...


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

?

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"

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

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"


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

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

?


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

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


@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