From 59d031fe1b4003dd1990027aedc90a954969ef66 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 24 Apr 2025 18:43:49 +0530 Subject: [PATCH 1/4] Redirect `mul` to `_mul` --- src/bidiag.jl | 12 ++++-------- src/matmul.jl | 4 +++- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/bidiag.jl b/src/bidiag.jl index 13439ff9..52f72086 100644 --- a/src/bidiag.jl +++ b/src/bidiag.jl @@ -1215,26 +1215,22 @@ function _dibimul!(C::Bidiagonal, A::Diagonal, B::Bidiagonal, _add) end function mul(A::UpperOrUnitUpperTriangular, B::Bidiagonal) - TS = promote_op(matprod, eltype(A), eltype(B)) - C = mul!(similar(A, TS, size(A)), A, B) + C = _mul(A, B) return B.uplo == 'U' ? UpperTriangular(C) : C end function mul(A::LowerOrUnitLowerTriangular, B::Bidiagonal) - TS = promote_op(matprod, eltype(A), eltype(B)) - C = mul!(similar(A, TS, size(A)), A, B) + C = _mul(A, B) return B.uplo == 'L' ? LowerTriangular(C) : C end function mul(A::Bidiagonal, B::UpperOrUnitUpperTriangular) - TS = promote_op(matprod, eltype(A), eltype(B)) - C = mul!(similar(B, TS, size(B)), A, B) + C = _mul(A, B) return A.uplo == 'U' ? UpperTriangular(C) : C end function mul(A::Bidiagonal, B::LowerOrUnitLowerTriangular) - TS = promote_op(matprod, eltype(A), eltype(B)) - C = mul!(similar(B, TS, size(B)), A, B) + C = _mul(A, B) return A.uplo == 'L' ? LowerTriangular(C) : C end diff --git a/src/matmul.jl b/src/matmul.jl index efb9d0fe..10db7454 100644 --- a/src/matmul.jl +++ b/src/matmul.jl @@ -113,7 +113,9 @@ julia> [1 1; 0 1] * [1 0; 1 1] """ (*)(A::AbstractMatrix, B::AbstractMatrix) = mul(A, B) # we add an extra level of indirection to avoid ambiguities in * -function mul(A::AbstractMatrix, B::AbstractMatrix) +# We also define the core functionality within _mul to reuse the code elsewhere +mul(A::AbstractMatrix, B::AbstractMatrix) = _mul(A, B) +function _mul(A::AbstractMatrix, B::AbstractMatrix) TS = promote_op(matprod, eltype(A), eltype(B)) mul!(matprod_dest(A, B, TS), A, B) end From 27bdc528dad8d76b06f9ec83ef7544855f4d5343 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 24 Apr 2025 19:02:32 +0530 Subject: [PATCH 2/4] Add size check to `_mul` --- src/matmul.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/matmul.jl b/src/matmul.jl index 10db7454..39a05aea 100644 --- a/src/matmul.jl +++ b/src/matmul.jl @@ -116,6 +116,7 @@ julia> [1 1; 0 1] * [1 0; 1 1] # We also define the core functionality within _mul to reuse the code elsewhere mul(A::AbstractMatrix, B::AbstractMatrix) = _mul(A, B) function _mul(A::AbstractMatrix, B::AbstractMatrix) + matmul_size_check(size(A), size(B)) TS = promote_op(matprod, eltype(A), eltype(B)) mul!(matprod_dest(A, B, TS), A, B) end From ffa65d19ae8c1417b0b145fbb655b04f001edba6 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sat, 26 Apr 2025 15:28:29 +0530 Subject: [PATCH 3/4] Check size in symmetric mul --- src/symmetric.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/symmetric.jl b/src/symmetric.jl index 09819325..d063bb44 100644 --- a/src/symmetric.jl +++ b/src/symmetric.jl @@ -707,12 +707,14 @@ end mul(A::HermOrSym, B::HermOrSym) = A * copyto!(similar(parent(B)), B) # catch a few potential BLAS-cases function mul(A::HermOrSym{<:BlasFloat,<:StridedMatrix}, B::AdjOrTrans{<:BlasFloat,<:StridedMatrix}) + matmul_size_check(size(A), size(B)) T = promote_type(eltype(A), eltype(B)) mul!(similar(B, T, (size(A, 1), size(B, 2))), convert(AbstractMatrix{T}, A), copy_oftype(B, T)) # make sure the AdjOrTrans wrapper is resolved end function mul(A::AdjOrTrans{<:BlasFloat,<:StridedMatrix}, B::HermOrSym{<:BlasFloat,<:StridedMatrix}) + matmul_size_check(size(A), size(B)) T = promote_type(eltype(A), eltype(B)) mul!(similar(B, T, (size(A, 1), size(B, 2))), copy_oftype(A, T), # make sure the AdjOrTrans wrapper is resolved From f2c500412848a9f0c6651294e74294e9ce9addf4 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sat, 26 Apr 2025 15:42:30 +0530 Subject: [PATCH 4/4] Size checks in matrix-vector product --- src/matmul.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/matmul.jl b/src/matmul.jl index 39a05aea..2b2f7d81 100644 --- a/src/matmul.jl +++ b/src/matmul.jl @@ -51,11 +51,13 @@ end # Matrix-vector multiplication function (*)(A::StridedMaybeAdjOrTransMat{T}, x::StridedVector{S}) where {T<:BlasFloat,S<:Real} + matmul_size_check(size(A), size(x)) TS = promote_op(matprod, T, S) y = isconcretetype(TS) ? convert(AbstractVector{TS}, x) : x mul!(similar(x, TS, size(A,1)), A, y) end function (*)(A::AbstractMatrix{T}, x::AbstractVector{S}) where {T,S} + matmul_size_check(size(A), size(x)) TS = promote_op(matprod, T, S) mul!(similar(x, TS, axes(A,1)), A, x) end