diff --git a/NEWS.md b/NEWS.md index 6f7c04aec3095..f5c9efd6abfbb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -97,6 +97,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 ----------------------------- diff --git a/base/array.jl b/base/array.jl index e25aa09b64417..793037106cb39 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)) + # 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 diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index cb199b0d0c6c8..57d07e3300dc6 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/test/arrayops.jl b/test/arrayops.jl index 694f6f14614a4..88ea835314bf0 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1927,6 +1927,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