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

Implement zero! for arrays #19912

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.
Copy link
Member

@ararslan ararslan Jan 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically imperative grammar is used when describing how functions act. So in this case it would be "Set all elements..." rather than "Sets all elements..." Should also probably be "the array" rather than just "array".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads pretty clearly to me, tbh.


```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

Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ export
vcat,
vec,
view,
zero!,
zeros,

# linear algebra
Expand Down
1 change: 1 addition & 0 deletions doc/src/stdlib/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Base.trues
Base.falses
Base.fill
Base.fill!
Base.zero!
Base.reshape
Base.similar(::AbstractArray)
Base.similar(::Any, ::Tuple)
Expand Down
7 changes: 7 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down