|
| 1 | +using LinearAlgebra |
| 2 | + |
| 3 | +# This function is re-defined during testing, to check we hit the fast path: |
| 4 | +linearalgebra_count() = nothing |
| 5 | + |
| 6 | +function LinearAlgebra.mul!(C::StridedVecOrMat{<:AbstractQuantity{T}}, |
| 7 | + A::StridedMatrix{<:AbstractQuantity{T}}, |
| 8 | + B::StridedVecOrMat{<:AbstractQuantity{T}}, |
| 9 | + alpha::Bool, beta::Bool) where {T<:Base.HWNumber} |
| 10 | + # This is exactly how A * B creates C = similar(B, T, ...) |
| 11 | + eltype(C) == Base.promote_op(LinearAlgebra.matprod, eltype(A), eltype(B)) || error("bad eltypes") |
| 12 | + C0 = ustrip(C) |
| 13 | + A0 = ustrip(A) |
| 14 | + B0 = ustrip(B) |
| 15 | + mul!(C0, A0, B0) |
| 16 | + linearalgebra_count() |
| 17 | + return C |
| 18 | +end |
| 19 | + |
| 20 | +function LinearAlgebra.mul!(C::StridedVecOrMat{<:AbstractQuantity{T}}, |
| 21 | + A::LinearAlgebra.AdjOrTransAbsMat{<:AbstractQuantity{T}, <:StridedMatrix}, |
| 22 | + B::StridedVecOrMat{<:AbstractQuantity{T}}, |
| 23 | + alpha::Bool, beta::Bool) where {T<:Base.HWNumber} |
| 24 | + |
| 25 | + eltype(C) == Base.promote_op(LinearAlgebra.matprod, eltype(A), eltype(B)) || error("bad eltypes") |
| 26 | + C0 = ustrip(C) |
| 27 | + A0 = A isa Adjoint ? adjoint(ustrip(parent(A))) : transpose(ustrip(parent(A))) |
| 28 | + B0 = ustrip(B) |
| 29 | + mul!(C0, A0, B0) |
| 30 | + linearalgebra_count() |
| 31 | + return C |
| 32 | +end |
| 33 | + |
| 34 | +function LinearAlgebra.dot(A::StridedArray{<:AbstractQuantity{T}}, |
| 35 | + B::StridedArray{<:AbstractQuantity{T}}) where {T<:Base.HWNumber} |
| 36 | + A0 = ustrip(A) |
| 37 | + B0 = ustrip(B) |
| 38 | + C0 = dot(A0, B0) |
| 39 | + linearalgebra_count() |
| 40 | + C = C0 * oneunit(eltype(A)) * oneunit(eltype(B)) # surely there is an official way |
| 41 | + return C |
| 42 | +end |
0 commit comments