From 233e5c410100b84579ce72134bc0fd964cdbf6fa Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 15 Aug 2024 18:43:07 +0530 Subject: [PATCH 1/2] FillMatrix times vector returns FillVector --- src/fillalgebra.jl | 16 ++++++++++++---- test/runtests.jl | 25 +++++++++++++++++++------ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index c17e46cd..405a60ad 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -102,9 +102,17 @@ for MT in (:(AbstractMatrix{T}), :(Transpose{<:Any, <:AbstractMatrix{T}}), :(Adj :(AbstractTriangular{T})) @eval *(a::$MT, b::AbstractZerosVector) where {T} = mult_zeros(a, b) end -*(a::Transpose{<:Any, <:AbstractVector}, b::AbstractZerosMatrix) = transpose(transpose(b) * parent(a)) -*(a::Adjoint{<:Any, <:AbstractVector}, b::AbstractZerosMatrix) = adjoint(adjoint(b) * parent(a)) +for T in (:AbstractZerosMatrix, :AbstractFillMatrix) + @eval begin + *(a::Transpose{<:Any, <:AbstractVector}, b::$T) = transpose(transpose(b) * parent(a)) + *(a::Adjoint{<:Any, <:AbstractVector}, b::$T) = adjoint(adjoint(b) * parent(a)) + end +end *(a::AbstractZerosMatrix, b::AbstractVector) = mult_zeros(a, b) +function *(F::AbstractFillMatrix, v::AbstractVector) + check_matmul_sizes(F, v) + Fill(getindex_value(F) * sum(v), (axes(F,1),)) +end function lmul_diag(a::Diagonal, b) size(a,2) == size(b,1) || throw(DimensionMismatch("A has dimensions $(size(a)) but B has dimensions $(size(b))")) @@ -322,7 +330,7 @@ function _adjvec_mul_zeros(a, b) return a1 * b[1] end -for MT in (:AbstractMatrix, :AbstractTriangular, :(Adjoint{<:Any,<:TransposeAbsVec})) +for MT in (:AbstractMatrix, :AbstractTriangular, :(Adjoint{<:Any,<:TransposeAbsVec}), :AbstractFillMatrix) @eval *(a::AdjointAbsVec{<:Any,<:AbstractZerosVector}, b::$MT) = (b' * a')' end # ambiguity @@ -332,7 +340,7 @@ function *(a::AdjointAbsVec{<:Any,<:AbstractZerosVector}, b::TransposeAbsVec{<:A a * b2 end *(a::AdjointAbsVec{<:Any,<:AbstractZerosVector}, b::AbstractZerosMatrix) = (b' * a')' -for MT in (:AbstractMatrix, :AbstractTriangular, :(Transpose{<:Any,<:AdjointAbsVec})) +for MT in (:AbstractMatrix, :AbstractTriangular, :(Transpose{<:Any,<:AdjointAbsVec}), :AbstractFillMatrix) @eval *(a::TransposeAbsVec{<:Any,<:AbstractZerosVector}, b::$MT) = transpose(transpose(b) * transpose(a)) end *(a::TransposeAbsVec{<:Any,<:AbstractZerosVector}, b::AbstractZerosMatrix) = transpose(transpose(b) * transpose(a)) diff --git a/test/runtests.jl b/test/runtests.jl index 08bee6ac..349c2fe1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1640,14 +1640,16 @@ end @test Fill(2,3)*A ≈ Vector(Fill(2,3))*A @test Fill(2,0)*A ≈ Vector(Fill(2,0))*A - @test Fill(2,3,mA)*A ≈ Matrix(Fill(2,3,mA))*A - @test Fill(2,3,la)*a ≈ Matrix(Fill(2,3,la))*a + @test Fill(2,3,mA)*A ≈ mul!(similar(A, 3,nA), Fill(2,3,mA), A) ≈ Matrix(Fill(2,3,mA))*A + @test Fill(2,3,la)*a ≈ mul!(similar(a, 3), Fill(2,3,la), a) ≈ Matrix(Fill(2,3,la))*a + @test Fill(2,3,la)*a isa Fill @test Ones(3)*A ≈ Vector(Ones(3))*A - @test Ones(3,mA)*A ≈ Matrix(Ones(3,mA))*A - @test Ones(3,la)*a ≈ Matrix(Ones(3,la))*a + @test Ones(3,mA)*A ≈ mul!(similar(A, 3, nA), Ones(3,mA), A) ≈ Matrix(Ones(3,mA))*A + @test Ones(3,la)*a ≈ mul!(similar(a, 3), Ones(3,la), a) ≈ Matrix(Ones(3,la))*a + @test Ones(3,la)*a isa Fill @test Zeros(3)*A ≡ Zeros(3,nA) - @test Zeros(3,mA)*A == Zeros(3,nA) - @test Zeros(3,la)*a == Zeros(3) + @test Zeros(3,mA)*A == mul!(similar(A, 3, nA), Zeros(3,mA), A) == Zeros(3,nA) + @test Zeros(3,la)*a == mul!(similar(A, 3), Zeros(3,la), a) == Zeros(3) @test A*Fill(2,nA) ≈ A*Vector(Fill(2,nA)) @test A*Fill(2,nA,1) ≈ A*Matrix(Fill(2,nA,1)) @@ -1669,6 +1671,17 @@ end @test Zeros(la)' * Transpose(Adjoint(a)) == 0.0 + F = Fill(2, mA, 3) + @test transpose(A) * F ≈ transpose(Fill(2, 3, mA) * A) + F = Fill(2, la, 3) + FS = Fill(2, (Base.OneTo(la), SOneTo(3))) + @testset for (adjf, adjT) in ((transpose, Transpose), (adjoint, Adjoint)) + @test adjf(a) * F ≈ adjf(Fill(2, 3, la) * a) + @test adjf(a) * F isa adjT{<:Any, <:Fill{<:Any,1}} + @test adjf(a) * FS ≈ adjf(Fill(2, 3, la) * a) + @test axes(adjf(a) * FS, 2) == SOneTo(3) + end + w = zeros(mA) @test mul!(w, A, Fill(2,nA), true, false) ≈ A * fill(2,nA) w .= 2 From 42a1f8660a6b5e67e0cf833993e222aee6dcd11a Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 15 Aug 2024 19:23:49 +0530 Subject: [PATCH 2/2] Bump version to v1.12.0 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index dd70d301..c88b5ac0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FillArrays" uuid = "1a297f60-69ca-5386-bcde-b61e274b549b" -version = "1.11" +version = "1.12.0" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"