diff --git a/base/array.jl b/base/array.jl index e25aa09b644178..9e3fbce3082297 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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 diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 0ad868983f4c67..b6546180d36eea 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -619,9 +619,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] @@ -633,6 +634,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) @@ -2892,9 +2904,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] @@ -2906,6 +2919,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) diff --git a/test/arrayops.jl b/test/arrayops.jl index e81eaf44c87963..c19bdf02267c12 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1926,3 +1926,38 @@ using TestHelpers.OAs @test accumulate(op, [10,20, 30]) == [10, op(10, 20), op(op(10, 20), 30)] == [10, 40, 110] @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