From 38b965485e79f7a312bd5839248b8b5ebf583395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 14 Feb 2024 14:58:51 +0100 Subject: [PATCH 1/2] Fix promotion of outer product --- src/implementations/LinearAlgebra.jl | 4 ++-- test/matmul.jl | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/implementations/LinearAlgebra.jl b/src/implementations/LinearAlgebra.jl index d577e6c6..00ff2b65 100644 --- a/src/implementations/LinearAlgebra.jl +++ b/src/implementations/LinearAlgebra.jl @@ -213,14 +213,14 @@ function promote_array_mul( ::Type{<:AbstractVector{S}}, ::Type{<:LinearAlgebra.Adjoint{T,<:AbstractVector{T}}}, ) where {S,T} - return Matrix{promote_sum_mul(S, T)} + return Matrix{promote_operation(*, S, T)} end function promote_array_mul( ::Type{<:AbstractVector{S}}, ::Type{<:LinearAlgebra.Transpose{T,<:AbstractVector{T}}}, ) where {S,T} - return Matrix{promote_sum_mul(S, T)} + return Matrix{promote_operation(*, S, T)} end function promote_array_mul( diff --git a/test/matmul.jl b/test/matmul.jl index 73920660..c0300947 100644 --- a/test/matmul.jl +++ b/test/matmul.jl @@ -403,3 +403,23 @@ end y = MA.@rewrite sum(A[i, :] * LinearAlgebra.transpose(x[i, :]) for i in 1:2) @test y == BigInt[10 14; 14 20] end + +struct Monomial +end +LinearAlgebra.transpose(m::Monomial) = m +LinearAlgebra.adjoint(m::Monomial) = m +MA.promote_operation(::typeof(*), ::Type{Monomial}, ::Type{Monomial}) = Monomial +Base.:*(m::Monomial, ::Monomial) = m + +# `Monomial` does not implement `+`, we should check that it does not prevent +# to do outer products of vectors +@testset "Vector*Transpose{Vector}_issue_256 with Monomial" begin + m = Monomial() + a = [m, m] + for f in [LinearAlgebra.transpose, LinearAlgebra.adjoint] + b = f(a) + T = MA.promote_operation(*, typeof(a), typeof(b)) + @test T == typeof(a * b) + @test T == typeof(MA.operate(*, a, b)) + end +end From 35358ab20ecdc30c6f093b3a29727f14b2cef03f Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 15 Feb 2024 09:28:23 +1300 Subject: [PATCH 2/2] Update matmul.jl --- test/matmul.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/matmul.jl b/test/matmul.jl index c0300947..339b87e1 100644 --- a/test/matmul.jl +++ b/test/matmul.jl @@ -404,8 +404,7 @@ end @test y == BigInt[10 14; 14 20] end -struct Monomial -end +struct Monomial end LinearAlgebra.transpose(m::Monomial) = m LinearAlgebra.adjoint(m::Monomial) = m MA.promote_operation(::typeof(*), ::Type{Monomial}, ::Type{Monomial}) = Monomial