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

fix 5-arg mul! for vectors of vectors #47665

Merged
merged 4 commits into from
Nov 28, 2022

Conversation

ranocha
Copy link
Member

@ranocha ranocha commented Nov 22, 2022

On current master and release, vectors of vectors work with * and 3-arg mul! but not with 5-arg mul!:

julia> using LinearAlgebra

julia> u = [[1, 2], [3, 4]]
2-element Vector{Vector{Int64}}:
 [1, 2]
 [3, 4]

julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
 1  2
 3  4

julia> A * u
2-element Vector{Vector{Int64}}:
 [7, 10]
 [15, 22]

julia> v = [[0, 0], [0, 0]]
2-element Vector{Vector{Int64}}:
 [0, 0]
 [0, 0]

julia> mul!(v, A, u)
2-element Vector{Vector{Int64}}:
 [7, 10]
 [15, 22]

julia> mul!(v, A, u, 1, 1)
ERROR: MethodError: no method matching +(::Vector{Int64}, ::Int64)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar

Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...)
   @ Base operators.jl:578
  +(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}
   @ Base int.jl:87
  +(::Base.TwicePrecision, ::Number)
   @ Base twiceprecision.jl:290
  ...

Stacktrace:
 [1] MulAddMul
   @ ~/.../julia/stdlib/v1.10/LinearAlgebra/src/generic.jl:58 [inlined]
 [2] generic_matvecmul!(C::Vector{Vector{Int64}}, tA::Char, A::Matrix{Int64}, B::Vector{Vector{Int64}}, _add::LinearAlgebra.MulAddMul{true, false, Int64, Int64})
   @ LinearAlgebra ~/.../julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:810
 [3] mul!(y::Vector{Vector{Int64}}, A::Matrix{Int64}, x::Vector{Vector{Int64}}, alpha::Int64, beta::Int64)
   @ LinearAlgebra ~/.../julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:81
 [4] top-level scope
   @ REPL[7]:1

Since matrix vector products only need to be able to

  • multiply elements of the matrix and the vector
  • add elements of the vector multiplied by entries of the matrix

this is an unnecessary restriction I fixed here. In practice, this appears for us when multiplying vectors of SVectors from StaticArrays.jl (or StructArrays thereof from StructArrays.jl) by matrices, e.g., when discretizing differential equations.

@ranocha ranocha added the linear algebra Linear algebra label Nov 22, 2022
@dkarrasch
Copy link
Member

What happens if u is a matrix of vectors? Does it also require a fix?

@ranocha ranocha added backport 1.8 Change should be backported to release-1.8 backport 1.9 Change should be backported to release-1.9 labels Nov 23, 2022
@ranocha ranocha force-pushed the hr/mul_vector_of_vectors branch from 3132cec to 17112e5 Compare November 23, 2022 08:20
@ranocha
Copy link
Member Author

ranocha commented Nov 23, 2022

What happens if u is a matrix of vectors? Does it also require a fix?

I added additional tests with matrices of vectors/matrices - they work correctly in this PR.

@ranocha ranocha force-pushed the hr/mul_vector_of_vectors branch from 17112e5 to 741ab6b Compare November 24, 2022 07:39
@ranocha
Copy link
Member Author

ranocha commented Nov 24, 2022

The only failing test is "test x86_64-w64-mingw32" with message "Received cancellation signal, interrupting", which does not seem to be related to this PR.

@DilumAluthge DilumAluthge force-pushed the hr/mul_vector_of_vectors branch from 741ab6b to 3e9761f Compare November 24, 2022 09:05
@brenhinkeller
Copy link
Contributor

Looks like that failing test is just #47710 which should be fixed by #47711, so seems good to go here

@dkarrasch dkarrasch merged commit 902e8a7 into JuliaLang:master Nov 28, 2022
@ranocha ranocha deleted the hr/mul_vector_of_vectors branch November 28, 2022 11:54
KristofferC pushed a commit that referenced this pull request Nov 28, 2022
Co-authored-by: N5N3 <2642243996@qq.com>
(cherry picked from commit 902e8a7)
KristofferC pushed a commit that referenced this pull request Nov 28, 2022
Co-authored-by: N5N3 <2642243996@qq.com>
(cherry picked from commit 902e8a7)
@KristofferC KristofferC mentioned this pull request Dec 14, 2022
26 tasks
KristofferC pushed a commit that referenced this pull request Dec 14, 2022
Co-authored-by: N5N3 <2642243996@qq.com>
(cherry picked from commit 902e8a7)
@KristofferC KristofferC removed the backport 1.8 Change should be backported to release-1.8 label Dec 16, 2022
@KristofferC KristofferC removed the backport 1.9 Change should be backported to release-1.9 label Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants