diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 64d58c2bc8a95..29bc66d2eba3e 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -683,6 +683,25 @@ copymutable(itr) = collect(itr) zero{T}(x::AbstractArray{T}) = fill!(similar(x), zero(T)) +""" + zero!(x::AbstractArray) + +Sets all elements of array `x` to 0. Equivalent to `fill!(x, 0)`. + +```jldoctest +julia> A = ones(2,2) +2×2 Array{Float64,2}: + 1.0 1.0 + 1.0 1.0 + +julia> zero!(A) +2×2 Array{Float64,2}: + 0.0 0.0 + 0.0 0.0 +``` +""" +zero!{T}(x::AbstractArray{T}) = fill!(x, zero(T)) + ## iteration support for arrays by iterating over `eachindex` in the array ## # Allows fast iteration by default for both LinearFast and LinearSlow arrays diff --git a/base/exports.jl b/base/exports.jl index c9be9f24ad702..717f408f30939 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -580,6 +580,7 @@ export vcat, vec, view, + zero!, zeros, # linear algebra diff --git a/doc/src/stdlib/arrays.md b/doc/src/stdlib/arrays.md index 8d400d7559ce1..b6c14a6c5b7e9 100644 --- a/doc/src/stdlib/arrays.md +++ b/doc/src/stdlib/arrays.md @@ -32,6 +32,7 @@ Base.trues Base.falses Base.fill Base.fill! +Base.zero! Base.reshape Base.similar(::AbstractArray) Base.similar(::Any, ::Tuple) diff --git a/test/arrayops.jl b/test/arrayops.jl index c073f0b21751a..2a86a484d7e2a 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1978,6 +1978,13 @@ end test_zeros(oarr.parent, Matrix{UInt16}, (3, 2)) end +@testset "zero!" begin + A = ones(2,2) + B = zero(A) + zero!(A) + @test A == B +end + # issue #11053 type T11053 a::Float64