Skip to content

Commit

Permalink
Make Woodbury immutable factorization objects
Browse files Browse the repository at this point in the history
Also fix printing. Fixes #19.
  • Loading branch information
timholy committed Jul 2, 2018
1 parent 33595fe commit 08fdd53
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
julia 0.7
julia 0.7-alpha
16 changes: 12 additions & 4 deletions src/SymWoodburyMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import SparseArrays.sparse
"""
Represents a matrix of the form A + BDBᵀ.
"""
mutable struct SymWoodbury{T,AType, BType, DType} <: AbstractMatrix{T}
A::AType;
B::BType;
D::DType;
struct SymWoodbury{T,AType, BType, DType} <: Factorization{T}
A::AType
B::BType
D::DType
end

"""
Expand Down Expand Up @@ -186,3 +186,11 @@ SparseArrays.sparse(O::SymWoodbury) = sparse(Matrix(O))
adjoint(O::SymWoodbury) = O

det(W::SymWoodbury) = det(convert(Woodbury, W))

function show(io::IO, W::SymWoodbury)
println(io, "Symmetric Woodbury factorization:\nA:")
show(io, MIME("text/plain"), W.A)
print(io, "\nB:\n")
Base.print_matrix(IOContext(io,:compact=>true), W.B)
print(io, "\nD: ", W.D)
end
20 changes: 9 additions & 11 deletions src/WoodburyMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export Woodbury, SymWoodbury, liftFactor
`W = Woodbury(A, U, C, V)` creates a matrix `W` identical to `A + U*C*V` whose inverse will be calculated using
the Woodbury matrix identity.
"""
mutable struct Woodbury{T,AType,UType,VType,CType,CpType} <: AbstractMatrix{T}
struct Woodbury{T,AType,UType,VType,CType,CpType} <: Factorization{T}
A::AType
U::UType
C::CType
Expand Down Expand Up @@ -50,27 +50,25 @@ function Woodbury(A, U::AbstractMatrix{T}, C, V::AbstractMatrix{T}) where {T}
end

Woodbury(A, U::Vector{T}, C, V::Matrix{T}) where {T} = Woodbury(A, reshape(U, length(U), 1), C, V)
@static if VERSION <= v"0.7.0-DEV.3040"
Woodbury(A, U::AbstractVector, C, V::RowVector) = Woodbury(A, U, C, Matrix(V))
else
Woodbury(A, U::AbstractVector, C, V::Adjoint) = Woodbury(A, U, C, Matrix(V))
end

Woodbury(A, U::AbstractVector, C, V::Adjoint) = Woodbury(A, U, C, Matrix(V))

size(W::Woodbury) = size(W.A)
size(W::Woodbury, d) = size(W.A, d)

function show(io::IO, W::Woodbury)
println(io, summary(W), ":")
print(io, "A:\n", W.A)
println(io, "Woodbury factorization:\nA:")
show(io, MIME("text/plain"), W.A)
print(io, "\nU:\n")
Base.print_matrix(io, W.U)
Base.print_matrix(IOContext(io, :compact=>true), W.U)
if isa(W.C, Matrix)
print(io, "\nC:\n")
Base.print_matrix(io, W.C)
Base.print_matrix(IOContext(io, :compact=>true), W.C)
else
print(io, "\nC: ", W.C)
end
print(io, "\nV:\n")
Base.print_matrix(io, W.V)
Base.print_matrix(IOContext(io, :compact=>true), W.V)
end

Base.Matrix(W::Woodbury{T}) where {T} = convert(Matrix{T}, W)
Expand Down

0 comments on commit 08fdd53

Please sign in to comment.