Skip to content

Commit

Permalink
Implement dot product for diagonal and scale matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasKroepelin committed Oct 2, 2024
1 parent 592634b commit 5b89d41
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
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

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

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

@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

0 comments on commit 5b89d41

Please sign in to comment.