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

copyto! with CartesianIndices assumes identical step in source and destination indices #45282

Closed
jishnub opened this issue May 11, 2022 · 0 comments · Fixed by #45289
Closed
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior

Comments

@jishnub
Copy link
Contributor

jishnub commented May 11, 2022

julia/base/multidimensional.jl

Lines 1105 to 1129 in 9a0e797

function copyto!(dest::AbstractArray{T1,N}, Rdest::CartesianIndices{N},
src::AbstractArray{T2,N}, Rsrc::CartesianIndices{N}) where {T1,T2,N}
isempty(Rdest) && return dest
if size(Rdest) != size(Rsrc)
throw(ArgumentError("source and destination must have same size (got $(size(Rsrc)) and $(size(Rdest)))"))
end
checkbounds(dest, first(Rdest))
checkbounds(dest, last(Rdest))
checkbounds(src, first(Rsrc))
checkbounds(src, last(Rsrc))
src′ = unalias(dest, src)
ΔI = first(Rdest) - first(Rsrc)
if @generated
quote
@nloops $N i (n->Rsrc.indices[n]) begin
@inbounds @nref($N,dest,n->i_n+ΔI[n]) = @nref($N,src′,i)
end
end
else
for I in Rsrc
@inbounds dest[I + ΔI] = src′[I]
end
end
dest
end

Now that CartesianIndices may contain StepRanges, the implementation doesn't seem correct.

julia> a = rand(4)
4-element Vector{Float64}:
 0.7766672217743518
 0.5138339000883105
 0.7793932733439368
 0.6377375487030642

julia> b = zeros(2);

julia> copyto!(b, CartesianIndices(b), a, CartesianIndices((1:2:3,)))
2-element Vector{Float64}:
 0.7766672217743518
 0.0

julia> a[CartesianIndices((1:2:3,))]
2-element Vector{Float64}:
 0.7766672217743518
 0.7793932733439368
@N5N3 N5N3 added bug Indicates an unexpected problem or unintended behavior arrays [a, r, r, a, y, s] labels May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior
Projects
None yet
2 participants