From 1f88c8667fbf1e0de23eaee5ab16fe48ec35b4ac Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Tue, 28 Apr 2026 06:17:38 -0500 Subject: [PATCH] Add missing methods: ==, hash, show Co-authored-by: Copilot --- src/FactoredMatrices.jl | 15 +++++++++++---- test/runtests.jl | 8 +++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/FactoredMatrices.jl b/src/FactoredMatrices.jl index 946df3f..fd2b49d 100644 --- a/src/FactoredMatrices.jl +++ b/src/FactoredMatrices.jl @@ -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 @@ -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)) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 7edae70..fe7a579 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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}