Skip to content

Why doesn't recursivecopy! allow to copy a vector into a matrix? #589

Closed

Description

@albertomercurio

Is your feature request related to a problem? Please describe.

I need to copy the content of a vector into a matrix. This is handled in pure Julia through the copyto! function

x_mat = rand(10, 10)
y_mat = rand(100, 100)
y_vec = view(y_mat, :, 1)

copyto!(x_mat, y_vec)

but it is not supported by the recursivecopy! function

using RecursiveArrayTools

recursivecopy!(x_mat, y_vec)
ERROR: MethodError: no method matching recursivecopy!(::Matrix{Float64}, ::SubArray{Float64, 1, Matrix{Float64}, Tuple{Base.Slice{…}, Int64}, true})
The function `recursivecopy!` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  recursivecopy!(::AbstractArray{T, N}, ::AbstractArray{T2, N}) where {T<:Number, T2<:Number, N}
   @ RecursiveArrayTools ~/.julia/packages/RecursiveArrayTools/9sjwU/src/utils.jl:73
  recursivecopy!(::AbstractVectorOfArray, ::AbstractVectorOfArray)
   @ RecursiveArrayTools ~/.julia/packages/RecursiveArrayTools/9sjwU/src/utils.jl:97
  recursivecopy!(::T, ::T) where T<:JumpProcesses.ExtendedJumpArray
   @ JumpProcesses ~/.julia/packages/JumpProcesses/MF0Pb/src/extended_jump_array.jl:143
  ...

Stacktrace:
 [1] top-level scope
   @ Untitled-1:41
Some type information was truncated. Use `show(err)` to see complete types.

Describe the solution you’d like

Perhaps changing the current method

function recursivecopy!(
        b::AbstractArray{T, N},
        a::AbstractArray{T2, N}
    ) where {T <: Number, T2 <: Number, N}
    return copyto!(b, a)
end

to

function recursivecopy!(
        b::AbstractArray{T, N},
        a::AbstractArray{T2, M}
    ) where {T <: Number, T2 <: Number, N, M}
    return copyto!(b, a)
end

I don't know if we can also change the other methods that do not have T <: Number.

Activity

  1. ChrisRackauckas commented on May 11, 2026

    @ChrisRackauckas
    Member

    It requires matching shapes. do recursivecopy!(vec(a),vec(b)) if you don't care about shape: the vec won't allocate so it's fine. I think it's good to shape check.

  2. albertomercurio commented on May 11, 2026

    @albertomercurio
    Author

    Ok. Is there a reason why it requires shape matching if the copy function would still work?

  3. ChrisRackauckas commented on May 11, 2026

    @ChrisRackauckas
    Member

    copyto! doesn't work

  4. albertomercurio commented on May 11, 2026

    @albertomercurio
    Author

    Doesn't work on VectorOfArray? Because on AbstractArray it works.

  5. ChrisRackauckas commented on May 12, 2026

    @ChrisRackauckas
    Member

    When it matches shape it works.

  6. albertomercurio commented on May 12, 2026

    @albertomercurio
    Author

    The example I gave in the first comment uses copyto! from a Vector to a Matrix. So it works even if the shapes don't match.

  7. ChrisRackauckas-Claude commented on May 15, 2026

    @ChrisRackauckas-Claude
    Member

    The relevant Julia norm here is copy!, not copyto!. From the Base.copy! docstring: "If dst and src are multidimensional arrays, they must have equal axes." copyto! is the shape-independent linear-copy version; copy! is the shape-checking version.

    recursivecopy! is named after copy!, and the docstring already says "like copy! on arrays of scalars", so the intent has always been shape-checking semantics. The current (::AbstractArray{T,N}, ::AbstractArray{T2,N}) signature enforces matching dimensionality, which is a partial enforcement of that — relaxing it to allow Matrix ← Vector would push the function toward copyto! semantics and away from its name.

    For your use case, just call copyto!(x_mat, y_vec) directly — that's exactly what copyto! is for. Or recursivecopy!(vec(x_mat), y_vec) if you want to keep going through this function (vec is allocation-free for dense arrays).

    If anything, the existing method should be tightened to require full axes matching rather than only matching N, to actually honor the copy!-like contract the docstring promises.

  8. albertomercurio commented on May 16, 2026

    @albertomercurio
    Author

    But the recursivecopy! function internally calls copyto! and not copy!.

    I have this problem because I have to re-initialize a ODEProblem that has u0 as a Matrix but I'm doing reinit(prob; u0 = some_vec). This raises the following error above.

  9. ChrisRackauckas commented on May 17, 2026

    @ChrisRackauckas
    Member

    I think it seems appropriate that would need a matrix?

  10. albertomercurio commented on May 17, 2026

    @albertomercurio
    Author

    Yes I can for sure reshape it, but then studying the error, I found it was caused by recursivecopy! which internally uses copyto!`, which allows for different shapes arrays.

    So I was just wondering if this error can be fixed fo free by just relaxing this condition, as copyto! doesn't have this constraint.

  11. ChrisRackauckas commented on May 17, 2026

    @ChrisRackauckas
    Member

    #591 should be a fine solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions