Skip to content

Size check in 2-argument mul #1315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 4 additions & 8 deletions src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,7 +115,10 @@ 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)
matmul_size_check(size(A), size(B))
TS = promote_op(matprod, eltype(A), eltype(B))
mul!(matprod_dest(A, B, TS), A, B)
end
Expand Down
2 changes: 2 additions & 0 deletions src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down