I recently had the following question
regarding the function mean! of the Statistics.jl package.
The following bug is reported regarding the behavior of mean!. As indicated, the mean! function does not properly consider that its arguments may alias each other. In some such cases the result from mean! is not correct:
julia> let a = [1 2 3]
mean!(a, a)
end
1×3 Array{Int64,2}:
0 0 0
julia> let a = [1 2 3]
mean!(copy(a), a)
end
1×3 Array{Int64,2}:
1 2 3
julia> versioninfo()
Julia Version 1.5.3
Commit 788b2c77c1 (2020-11-09 13:37 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin18.7.0)
CPU: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
However, I think that this behavior is normal since viewing the definition of mean!, the result of the operation mean!(r, v) is written in r. Therefore it seems logical to me that if you use the same object as variable r and as variable v, the result is unpredictable.
I have seen that this also happens with the sum! function.
Someone can tell me if I am right or indeed, there is something that I am not understanding.