Skip to content

Commit

Permalink
Zeros (#19635)
Browse files Browse the repository at this point in the history
* Fix #19265.
* Add methods to zeros, ones with analgous signature to similar.
* news
  • Loading branch information
jw3126 authored and ViralBShah committed Dec 22, 2016
1 parent 758418c commit 66ab171
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 27 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Library improvements
That is, not every member of the input iterable will be visited if a `true` (in the case of `any`) or
`false` (in the case of `all`) value is found, and `mapreduce` will visit all members of the iterable.

* Additional methods for `ones` and `zeros` functions to support the same signature as the `similar` function ([#19635]).

Compiler/Runtime improvements
-----------------------------

Expand Down
12 changes: 9 additions & 3 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,15 @@ fill(v, dims::Integer...) = fill!(Array{typeof(v)}(dims...), v)

for (fname, felt) in ((:zeros,:zero), (:ones,:one))
@eval begin
($fname)(T::Type, dims...) = fill!(Array{T}(dims...), ($felt)(T))
($fname)(dims...) = fill!(Array{Float64}(dims...), ($felt)(Float64))
($fname){T}(A::AbstractArray{T}) = fill!(similar(A), ($felt)(T))
# allow signature of similar
$fname(a::AbstractArray, T::Type, dims::Tuple) = fill!(similar(a, T, dims), $felt(T))
$fname(a::AbstractArray, T::Type, dims...) = fill!(similar(a,T,dims...), $felt(T))
$fname(a::AbstractArray, T::Type=eltype(a)) = fill!(similar(a,T), $felt(T))

$fname(T::Type, dims::Tuple) = fill!(Array{T}(Dims(dims)), $felt(T))
$fname(dims::Tuple) = ($fname)(Float64, dims)
$fname(T::Type, dims...) = $fname(T, dims)
$fname(dims...) = $fname(dims)
end
end

Expand Down
67 changes: 43 additions & 24 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -549,26 +549,24 @@ to synchronous `File`'s and `IOStream`'s not to any of the asynchronous streams.
"""
fd


"""
ones(type, dims)
ones([A::AbstractArray,] [T=eltype(A)::Type,] [dims=size(A)::Tuple])
Create an array of all ones of specified type. The type defaults to `Float64` if not specified.
Create an array of all ones with the same layout as `A`, element type `T` and size `dims`.
The `A` argument can be skipped, which behaves like `Array{Float64,0}()` was passed.
For convenience `dims` may also be passed in variadic form.
```jldoctest
julia> ones(Complex128, 2, 3)
2×3 Array{Complex{Float64},2}:
1.0+0.0im 1.0+0.0im 1.0+0.0im
1.0+0.0im 1.0+0.0im 1.0+0.0im
```
"""
ones(t,dims)
"""
ones(A)
Create an array of all ones with the same element type and shape as `A`.
julia> ones(1,2)
1×2 Array{Float64,2}:
1.0 1.0
```jldoctest
julia> A = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
Expand All @@ -578,9 +576,21 @@ julia> ones(A)
2×2 Array{Int64,2}:
1 1
1 1
julia> ones(A, Float64)
2×2 Array{Float64,2}:
1. 1.
1. 1.
julia> ones(A, Bool, (3,))
3-element Array{Bool,1}:
true
true
true
```
See also [`zeros`](@ref), [`similar`](@ref).
"""
ones(A)
ones

"""
reshape(A, dims)
Expand Down Expand Up @@ -2689,26 +2699,23 @@ Test whether any values along the given dimensions of an array are `true`.
any(::AbstractArray,dims)

"""
zeros(type, dims)
zeros([A::AbstractArray,] [T=eltype(A)::Type,] [dims=size(A)::Tuple])
Create an array of all zeros with the same layout as `A`, element type `T` and size `dims`.
The `A` argument can be skipped, which behaves like `Array{Float64,0}()` was passed.
For convenience `dims` may also be passed in variadic form.
Create an array of all zeros of specified type.
The type defaults to `Float64` if not specified.
```jldoctest
julia> zeros(1)
1-element Array{Float64,1}:
0.0
julia> zeros(Int8, 2, 3)
2×3 Array{Int8,2}:
0 0 0
0 0 0
```
"""
zeros(t,dims)

"""
zeros(A)
Create an array of all zeros with the same element type and shape as `A`.
```jldoctest
julia> A = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
Expand All @@ -2718,9 +2725,21 @@ julia> zeros(A)
2×2 Array{Int64,2}:
0 0
0 0
julia> zeros(A, Float64)
2×2 Array{Float64,2}:
0.0 0.0
0.0 0.0
julia> zeros(A, Bool, (3,))
3-element Array{Bool,1}:
false
false
false
```
See also [`ones`](@ref), [`similar`](@ref).
"""
zeros(A)
zeros

"""
Symbol(x...) -> Symbol
Expand Down
40 changes: 40 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,46 @@ using TestHelpers.OAs
@test accumulate(op, [10 20 30], 2) == [10 op(10, 20) op(op(10, 20), 30)] == [10 40 110]
end

@testset "zeros and ones" begin
@test ones([1,2], Float64, (2,3)) == ones(2,3)
@test ones(2) == ones(Int, 2) == ones([2,3], Float32, 2) == [1,1]
@test isa(ones(2), Vector{Float64})
@test isa(ones(Int, 2), Vector{Int})
@test isa(ones([2,3], Float32, 2), Vector{Float32})

function test_zeros(arr, T, s)
@test all(arr .== 0)
@test isa(arr, T)
@test size(arr) == s
end
test_zeros(zeros(), Array{Float64, 0}, ())
test_zeros(zeros(2), Vector{Float64}, (2,))
test_zeros(zeros(2,3), Matrix{Float64}, (2,3))
test_zeros(zeros((2,3)), Matrix{Float64}, (2,3))

test_zeros(zeros(Int, 6), Vector{Int}, (6,))
test_zeros(zeros(Int, 2, 3), Matrix{Int}, (2,3))
test_zeros(zeros(Int, (2, 3)), Matrix{Int}, (2,3))

test_zeros(zeros([1 2; 3 4]), Matrix{Int}, (2, 2))
test_zeros(zeros([1 2; 3 4], Float64), Matrix{Float64}, (2, 2))

zs = zeros(SparseMatrixCSC([1 2; 3 4]), Complex{Float64}, (2,3))
test_zeros(zs, SparseMatrixCSC{Complex{Float64}}, (2, 3))

@testset "#19265" begin
@test_throws MethodError zeros(Float64, [1.])
x = [1.]
test_zeros(zeros(x, Float64), Vector{Float64}, (1,))
@test x == [1.]
end

# exotic indexing
oarr = zeros(randn(3), UInt16, 1:3, -1:0)
@test indices(oarr) == (1:3, -1:0)
test_zeros(oarr.parent, Matrix{UInt16}, (3, 2))
end

# issue #11053
type T11053
a::Float64
Expand Down

0 comments on commit 66ab171

Please sign in to comment.