Open
Description
I noticed that sum!
and prod!
do not check for aliasing, leading to incorrect results when the same array is passed in both arguments. The incorrect behavior under aliasing is not documented and the result is a silent unexpected wrong result.
julia> a = [1 2 3];
julia> sum!(copy(a), a)
1×3 Matrix{Int64}:
1 2 3
julia> sum!(a, a)
1×3 Matrix{Int64}:
0 0 0
julia> a = [1 2 3];
julia> prod!(copy(a), a)
1×3 Matrix{Int64}:
1 2 3
julia> prod!(a, a)
1×3 Matrix{Int64}:
1 1 1
Not all mutating functions have this problem and cumsum!
can be used this way without error:
julia> a = [1, 2, 3];
julia> cumsum!(a, a)
3-element Vector{Int64}:
1
3
6
Other functions correctly flag the aliasing in the case of ===
arguments and throw an error:
julia> using LinearAlgebra
julia> a = rand(2, 2);
julia> mul!(a, a, a)
ERROR: ArgumentError: output matrix must not be aliased with input matrix
The examples above were generated on Julia 1.6.0-beta1
and I see the same behavior in version 1.5.3
.
Edit: I noticed this issue also applies to any!
and all!
:
julia> let a = [true, false]
any!(a, a)
end
2-element Array{Bool,1}:
0
0
julia> let a = [true, false]
any!(copy(a), a)
end
2-element Array{Bool,1}:
1
0
julia> let a = [true, false]
all!(a, a)
end
2-element Array{Bool,1}:
1
1
julia> let a = [true, false]
all!(copy(a), a)
end
2-element Array{Bool,1}:
1
0