Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/FactoredMatrices.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module FactoredMatrices

using LinearAlgebra
import Base: *, Array, size, length, getindex, similar, adjoint, transpose
import Base: *, Array, Matrix, size, length, similar, ==, hash, adjoint, transpose
import LinearAlgebra: mul!, dot

export FactoredMatrix
Expand Down Expand Up @@ -78,6 +78,15 @@ similar(::FactoredMatrix, T, dims) = Array{T}(undef, dims)
length(A::FactoredMatrix) = size(A.U, 1) * size(A.V, 2)

Array(A::FactoredMatrix) = A.U * A.V
Matrix(A::FactoredMatrix) = Array(A)

==(A::FactoredMatrix, B::FactoredMatrix) = A.U == B.U && A.V == B.V
const FMhash = Int === Int64 ? 0x99ac4c2c56e5bd6e : 0x70c3ac8c
hash(A::FactoredMatrix, h::UInt) = hash(A.U, hash(A.V, hash(FMhash, h)))

function Base.show(io::IO, A::FactoredMatrix)
print(io, "FactoredMatrix of size ", size(A), " with rank ", size(A.U, 2))
end

adjoint(A::FactoredMatrix{T}) where T = FactoredMatrix{T}(adjoint(A.V), adjoint(A.U))
transpose(A::FactoredMatrix{T}) where T = FactoredMatrix{T}(transpose(A.V), transpose(A.U))
Expand Down Expand Up @@ -214,9 +223,7 @@ end

# --- Queries ---

function LinearAlgebra.issymmetric(A::FactoredMatrix)
size(A.U, 1) == size(A.V, 2) && A.U == A.V'
end
LinearAlgebra.issymmetric(A::FactoredMatrix) = A.U == A.V'

function dot(A::FactoredMatrix, B::FactoredMatrix)
M1 = B.U' * A.U
Expand Down
8 changes: 7 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ end
U = myrand(T, 15, 3)
V = myrand(T, 3, 10)
Mf = FactoredMatrix(U, V)
M = Array(Mf)
M = Matrix(Mf)
@test M == U*V
@test M == Array(Mf)
Mf2 = FactoredMatrix(copy(U), copy(V))
@test Mf == Mf2
@test hash(Mf) == hash(Mf2)
@test hash(Mf) != hash(U, hash(V)) # we can distinguish the hash of the wrapped object from its component parts
@test sprint(show, Mf) == "FactoredMatrix of size (15, 10) with rank 3"
# lowercase adjoint(Mf) and transpose(Mf) re-wrap `U` and `V`
@test Mf' isa FactoredMatrix{T}
@test transpose(Mf) isa FactoredMatrix{T}
Expand Down
Loading