Repository navigation
Closed
Description
x-ref #174
These definitions place some very strong assumptions on the input types that are not present in *(A, B):
MutableArithmetics.jl/src/implementations/LinearAlgebra.jl
Lines 380 to 403 in 201aafb
| # Does what `LinearAlgebra/src/matmul.jl` does for abstract matrices and | |
| # vectors: estimate the resulting element type, allocate the resulting array but | |
| # it redirects to `mul_to!` instead of `LinearAlgebra.mul!`. | |
| function operate( | |
| ::typeof(*), | |
| A::AbstractMatrix{S}, | |
| B::AbstractVector{T}, | |
| ) where {T,S} | |
| C = undef_array(promote_array_mul(typeof(A), typeof(B)), axes(A, 1)) | |
| return operate_to!(C, *, A, B) | |
| end | |
| function operate( | |
| ::typeof(*), | |
| A::AbstractMatrix{S}, | |
| B::AbstractMatrix{T}, | |
| ) where {T,S} | |
| C = undef_array( | |
| promote_array_mul(typeof(A), typeof(B)), | |
| axes(A, 1), | |
| axes(B, 2), | |
| ) | |
| return operate_to!(C, *, A, B) | |
| end |
For example, none of these work:
@testset "Abstract eltype in matmul" begin
for T in (Any, Union{String,Int})
x, x12, x22 = T[1, 2], T[1 2], T[1 2; 3 4]
@test MA.operate(*, x, x') == x * x'
@test MA.operate(*, x', x) == x' * x
@test MA.operate(*, x12, x) == x12 * x
@test MA.operate(*, x22, x) == x22 * x
@test MA.operate(*, x', x22) == x' * x22
@test MA.operate(*, x12, x22) == x12 * x22
@test MA.operate(*, x22, x22) == x22 * x22
end
endMetadata
Metadata
Assignees
Labels
No labels
Activity
blegat commentedon Nov 17, 2022
We could replace
typeofwith_concrete_typeofsimilar to_concrete_eltypeof #171odow commentedon Nov 17, 2022
I still think that there's an issue we can't resolve:
Basedoesn't rely on the container types at compile time. It uses promotion based on the run-time values. Pre-allocatingCcannot work in all cases that we expect it to, unless we have an additional loop over all ofAandB?odow commentedon Nov 17, 2022
I have a very simple fix that I'm just testing 😄
odow commentedon Nov 17, 2022
Urgh. Even
Basecan't get this right:blegat commentedon Nov 21, 2022
Yes, LinearAlgebra will start creating a
Matrix{Int}and then when floating points arrive, it will broaden theeltypeso that theIntstill fit instead of promoting and converting theInts already produced intoFloat64(since that might not always work).