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

add zeros! and ones!, mutating versions of zeros and ones #40614

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,28 @@ copymutable(itr) = collect(itr)

zero(x::AbstractArray{T}) where {T} = fill!(similar(x), zero(T))

"""
zeros!(x::AbstractArray)

Fills `x` with zeros. This is equivalent to `fill!(x, zero(eltype(x)))`.
See also [`zeros`](@ref) and [`ones!`](@ref).

!!! compat "Julia 1.7"
This method is available as of Julia 1.7.
"""
zeros!(x::AbstractArray) = fill!(x, zero(eltype(x)))

"""
ones!(x::AbstractArray)

Fills `x` with ones. This is equivalent to `fill!(x, one(eltype(x)))`.
See also [`ones`](@ref) and [`zeros!`](@ref).

!!! compat "Julia 1.7"
This method is available as of Julia 1.7.
"""
ones!(x::AbstractArray) = fill!(x, one(eltype(x)))

## iteration support for arrays by iterating over `eachindex` in the array ##
# Allows fast iteration by default for both IndexLinear and IndexCartesian arrays

Expand Down
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export
minmax,
ndims,
ones,
ones!,
parent,
parentindices,
partialsort,
Expand Down Expand Up @@ -447,6 +448,7 @@ export
vec,
view,
zeros,
zeros!,

# search, find, match and related functions
contains,
Expand Down
2 changes: 2 additions & 0 deletions doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ Base.StridedMatrix
Base.StridedVecOrMat
Base.getindex(::Type, ::Any...)
Base.zeros
Base.zeros!
Base.ones
Base.ones!
Base.BitArray
Base.BitArray(::UndefInitializer, ::Integer...)
Base.BitArray(::Any)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ end

function reset_caches!(r::MersenneTwister)
# zeroing the caches makes comparing two MersenneTwister RNGs easier
fill!(r.vals, 0.0)
fill!(r.ints, zero(UInt128))
zeros!(r.vals)
zeros!(r.ints)
mt_setempty!(r)
mt_setempty!(r, UInt128)
r.adv_vals = -1
Expand Down