Skip to content
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

Disambiguate structured and abstract matrix multiplication #52464

Merged
merged 9 commits into from
Jan 6, 2024
Prev Previous commit
Next Next commit
return temporarily _init*-stuff
  • Loading branch information
dkarrasch committed Dec 30, 2023
commit 69aee20edc67434c664f6be2529d86ff87b2f6fb
20 changes: 20 additions & 0 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,26 @@ matprod_dest(A::Diagonal, B::Diagonal, TS) = similar(B, TS)
matprod_dest(A::HermOrSym, B::Diagonal, TS) = similar(A, TS, size(A))
matprod_dest(A::Diagonal, B::HermOrSym, TS) = similar(B, TS, size(B))

# TODO: remove once not used anymore in SparseArrays.jl
# some trait like this would be cool
# onedefined(::Type{T}) where {T} = hasmethod(one, (T,))
# but we are actually asking for oneunit(T), that is, however, defined for generic T as
# `T(one(T))`, so the question is equivalent for whether one(T) is defined
onedefined(::Type) = false
onedefined(::Type{<:Number}) = true

# initialize return array for op(A, B)
_init_eltype(::typeof(*), ::Type{TA}, ::Type{TB}) where {TA,TB} =
(onedefined(TA) && onedefined(TB)) ?
typeof(matprod(oneunit(TA), oneunit(TB))) :
promote_op(matprod, TA, TB)
_init_eltype(op, ::Type{TA}, ::Type{TB}) where {TA,TB} =
(onedefined(TA) && onedefined(TB)) ?
typeof(op(oneunit(TA), oneunit(TB))) :
promote_op(op, TA, TB)
_initarray(op, ::Type{TA}, ::Type{TB}, C) where {TA,TB} =
similar(C, _init_eltype(op, TA, TB), size(C))

# General fallback definition for handling under- and overdetermined system as well as square problems
# While this definition is pretty general, it does e.g. promote to common element type of lhs and rhs
# which is required by LAPACK but not SuiteSparse which allows real-complex solves in some cases. Hence,
Expand Down