You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why doesn't recursivecopy! allow to copy a vector into a matrix? #589
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:73recursivecopy!(::AbstractVectorOfArray, ::AbstractVectorOfArray)
@ RecursiveArrayTools ~/.julia/packages/RecursiveArrayTools/9sjwU/src/utils.jl:97recursivecopy!(::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
functionrecursivecopy!(
b::AbstractArray{T, N},
a::AbstractArray{T2, N}
) where {T <:Number, T2 <:Number, N}
returncopyto!(b, a)
end
to
functionrecursivecopy!(
b::AbstractArray{T, N},
a::AbstractArray{T2, M}
) where {T <:Number, T2 <:Number, N, M}
returncopyto!(b, a)
end
I don't know if we can also change the other methods that do not have T <: Number.
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.
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.
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.
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.
Activity
ChrisRackauckas commentedon May 11, 2026
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.albertomercurio commentedon May 11, 2026
Ok. Is there a reason why it requires shape matching if the copy function would still work?
ChrisRackauckas commentedon May 11, 2026
copyto!doesn't workalbertomercurio commentedon May 11, 2026
Doesn't work on
VectorOfArray? Because onAbstractArrayit works.ChrisRackauckas commentedon May 12, 2026
When it matches shape it works.
albertomercurio commentedon May 12, 2026
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.ChrisRackauckas-Claude commentedon May 15, 2026
The relevant Julia norm here is
copy!, notcopyto!. From theBase.copy!docstring: "Ifdstandsrcare multidimensional arrays, they must have equalaxes."copyto!is the shape-independent linear-copy version;copy!is the shape-checking version.recursivecopy!is named aftercopy!, and the docstring already says "likecopy!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 allowMatrix ← Vectorwould push the function towardcopyto!semantics and away from its name.For your use case, just call
copyto!(x_mat, y_vec)directly — that's exactly whatcopyto!is for. Orrecursivecopy!(vec(x_mat), y_vec)if you want to keep going through this function (vecis allocation-free for dense arrays).If anything, the existing method should be tightened to require full
axesmatching rather than only matchingN, to actually honor thecopy!-like contract the docstring promises.albertomercurio commentedon May 16, 2026
But the
recursivecopy!function internally callscopyto!and notcopy!.I have this problem because I have to re-initialize a
ODEProblemthat hasu0as aMatrixbut I'm doingreinit(prob; u0 = some_vec). This raises the following error above.ChrisRackauckas commentedon May 17, 2026
I think it seems appropriate that would need a matrix?
albertomercurio commentedon May 17, 2026
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.Add recursivecopyto! with copyto!-style linear-index semantics
ChrisRackauckas commentedon May 17, 2026
#591 should be a fine solution.