Skip to content

Commit

Permalink
Add methods to zeros, ones.
Browse files Browse the repository at this point in the history
* Fix JuliaLang#19265.
* Add methods to zeros, ones with analgous signature to similar.
  • Loading branch information
jw3126 committed Dec 20, 2016
1 parent 99b6a8c commit 39a3bde
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
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))
function ($fname)(a::AbstractArray, T::Type=eltype(a), dims::Tuple=size(a))
fill!(similar(a,T,dims), $felt(T))
end
($fname)(T::Type, dims::Tuple) = ($fname)(Array{T}(dims...), T, dims)
($fname)(dims::Tuple) = ($fname)(Float64, dims)

($fname)(a::AbstractArray,T::Type,dims::DimOrInd...) = ($fname)(a,T,dims)
($fname)(T::Type,dims::DimOrInd...) = ($fname)(T,dims)
($fname)(dims::DimOrInd...) = ($fname)(dims)
end
end

Expand Down
32 changes: 28 additions & 4 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,10 @@ julia> ones(Complex128, 2, 3)
ones(t,dims)

"""
ones(A)
ones(A::AbstractArray, T=eltype(A)::Type, dims=size(A)::DimOrInd)
Create an array of all ones with the same element type and shape as `A`.
Create an array of all ones with the same layout as `A`. Element type and size
can optionally be adjusted.
```jldoctest
julia> A = [1 2; 3 4]
Expand All @@ -578,6 +579,17 @@ 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
```
"""
ones(A)
Expand Down Expand Up @@ -2704,9 +2716,10 @@ julia> zeros(Int8, 2, 3)
zeros(t,dims)

"""
zeros(A)
zeros(A::AbstractArray, T=eltype(A)::Type, dims=size(A)::DimOrInd)
Create an array of all zeros with the same element type and shape as `A`.
Create an array of all zeros with the same layout as `A`. Element type and size
can optionally be adjusted.
```jldoctest
julia> A = [1 2; 3 4]
Expand All @@ -2718,6 +2731,17 @@ 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
```
"""
zeros(A)
Expand Down
34 changes: 34 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,40 @@ 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(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
end

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

0 comments on commit 39a3bde

Please sign in to comment.