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

LinearAlgebra traited matrix operations #48861

Open
wants to merge 1 commit 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
36 changes: 36 additions & 0 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,30 @@ See also: `copymutable_oftype`.
"""
copy_similar(A::AbstractArray, ::Type{T}) where {T} = copyto!(similar(A, T, size(A)), A)

"""
AbstractStorageTrait

Supertype for all matrix traits.
The concrete subtypes shall always contain a field named `data` keeping the
attributed object.
"""
abstract type AbstractStorageTrait{T} end
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
abstract type AbstractStorageTrait{T} end
abstract type AbstractStorageTrait{T} end

"""
DenseStorage

Concrete type for all types of matrix related traits.
"""
struct DenseStorage{T} <: AbstractStorageTrait{T}
data::T
DenseStorage(x::T) where T = new{T}(x)
end

"""
Storage(a::T) -> AbstractStorageTrait

Create a trait type object related to `a`.
"""
Storage(a::T) where T = storage_trait(T)(a)

include("adjtrans.jl")
include("transpose.jl")
Expand Down Expand Up @@ -453,6 +477,18 @@ include("schur.jl")
include("structuredbroadcast.jl")
include("deprecated.jl")

const WrapperArrayTypes{T,MT} = Union{
SubArray{T,N,MT} where N,
Adjoint{T,MT},
Transpose{T,MT},
AbstractTriangular{T,MT},
UpperHessenberg{T,MT},
Symmetric{T,MT},
Hermitian{T,MT},
}
storage_trait(::Type{<:AbstractArray}) = DenseStorage
storage_trait(::Type{<:WrapperArrayTypes{T,MT}}) where {T,MT} = storage_trait(MT)

const ⋅ = dot
const × = cross
export ⋅, ×
Expand Down
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,11 @@ true
```
"""
function (\)(A::AbstractMatrix, B::AbstractVecOrMat)
Storage(A) \ B
end

function (\)(ta::DenseStorage, B::AbstractVecOrMat)
A = ta.data
require_one_based_indexing(A, B)
m, n = size(A)
if m == n
Expand Down
8 changes: 8 additions & 0 deletions stdlib/LinearAlgebra/src/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,17 @@ julia> [1 1; 0 1] * [1 0; 1 1]
```
"""
function (*)(A::AbstractMatrix, B::AbstractMatrix)
(*)(Storage(A), Storage(B))
end

# implementation for the general case (getindex should be fast operation)
function (*)(ta::DenseStorage, tb::DenseStorage)
A = ta.data
B = tb.data
TS = promote_op(matprod, eltype(A), eltype(B))
mul!(similar(B, TS, (size(A, 1), size(B, 2))), A, B)
end

# optimization for dispatching to BLAS, e.g. *(::Matrix{Float32}, ::Matrix{Float64})
# but avoiding the case *(::Matrix{<:BlasComplex}, ::Matrix{<:BlasReal})
# which is better handled by reinterpreting rather than promotion
Expand Down
Loading