Skip to content

Commit 015ea1d

Browse files
authored
Implement diag (#36)
1 parent 93c5df8 commit 015ea1d

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

src/WoodburyMatrices.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module WoodburyMatrices
22

33
using LinearAlgebra
4-
import LinearAlgebra: det, logdet, logabsdet, ldiv!, mul!, adjoint, transpose
4+
import LinearAlgebra: det, logdet, logabsdet, ldiv!, mul!, adjoint, transpose, diag
55
import Base: +, *, \, inv, convert, copy, show, similar, axes, size
66
using SparseArrays
77

src/woodbury.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ function inv(W::AbstractWoodbury)
7676
return Woodbury(A′, U′, C′, V′)
7777
end
7878

79+
# multiplying everything out would be O(m^2 k + m k^2) operations
80+
# best I could think of was O(m^2 k) operations
81+
diag(W::AbstractWoodbury) = diag(W.A) + vec(sum(W.U .* (W.C * W.V)', dims=2))
82+
7983
+(W::Woodbury, X::Woodbury) = Woodbury(W.A + X.A, [W.U X.U],
8084
cat(W.C, X.C; dims=(1,2)), [W.V; X.V])
8185
*::Real, W::Woodbury) = Woodbury*W.A, W.U, α*W.C, W.V)

test/woodbury.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ for elty in (Float32, Float64, ComplexF32, ComplexF64, Int)
5555
iFv = F\v
5656
@test norm(W\v - iFv)/norm(iFv) <= n*cond(F)*ε # Revisit. Condition number is wrong
5757
@test norm(inv(W)*v - iFv)/norm(iFv) <= n*cond(F)*ε
58+
@test norm(diag(W) - diag(F)) <= n*cond(F)*ε # haven't thought about this error bound
5859
@test abs((det(W) - det(F))/det(F)) <= n*cond(F)*ε # Revisit. Condition number is wrong
5960
iWv = similar(iFv)
6061
if elty != Int

0 commit comments

Comments
 (0)