From 39a3bde760072c10ada0a68894f8a00cc1e54035 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Thu, 10 Nov 2016 17:19:32 +0100 Subject: [PATCH 1/9] Add methods to zeros, ones. * Fix #19265. * Add methods to zeros, ones with analgous signature to similar. --- base/array.jl | 12 +++++++++--- base/docs/helpdb/Base.jl | 32 ++++++++++++++++++++++++++++---- test/arrayops.jl | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/base/array.jl b/base/array.jl index e25aa09b64417..9e3fbce308229 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 cb199b0d0c6c8..3a2bf8a33ab19 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -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] @@ -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) @@ -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] @@ -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) diff --git a/test/arrayops.jl b/test/arrayops.jl index 694f6f14614a4..c5a5e96ade185 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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 From b3a93f277b743eddfb2b59b28270ba54448d1ded Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Wed, 21 Dec 2016 11:03:11 +0100 Subject: [PATCH 2/9] news --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 6f7c04aec3095..4d1a9028156c9 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. These have the same signature as the `similar` function. + Compiler/Runtime improvements ----------------------------- From 38d34e7b1d22fd9f760a8be10a66cc6ef87b7570 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Wed, 21 Dec 2016 15:29:18 +0100 Subject: [PATCH 3/9] fix --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 4d1a9028156c9..f5c9efd6abfbb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -97,7 +97,7 @@ 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. These have the same signature as the `similar` function. + * Additional methods for `ones` and `zeros` functions to support the same signature as the `similar` function ([#19635]). Compiler/Runtime improvements ----------------------------- From 8f824a2375960ce8791055cce471420729268ddc Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Wed, 21 Dec 2016 17:07:07 +0100 Subject: [PATCH 4/9] fix --- base/array.jl | 2 +- test/arrayops.jl | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/base/array.jl b/base/array.jl index 9e3fbce308229..b69253882f15e 100644 --- a/base/array.jl +++ b/base/array.jl @@ -210,7 +210,7 @@ for (fname, felt) in ((:zeros,:zero), (:ones,:one)) 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)(T::Type, dims::Tuple) = fill!(Array{T}(dims...), $felt(T)) ($fname)(dims::Tuple) = ($fname)(Float64, dims) ($fname)(a::AbstractArray,T::Type,dims::DimOrInd...) = ($fname)(a,T,dims) diff --git a/test/arrayops.jl b/test/arrayops.jl index c5a5e96ade185..26c46f99009ec 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1959,6 +1959,11 @@ end 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 From 259668bfb71508d4bdfd917fc83acb3e6625db9f Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Wed, 21 Dec 2016 17:57:16 +0100 Subject: [PATCH 5/9] fix --- base/docs/helpdb/Base.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 3a2bf8a33ab19..ee625fb06b5e5 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -564,7 +564,7 @@ julia> ones(Complex128, 2, 3) ones(t,dims) """ - ones(A::AbstractArray, T=eltype(A)::Type, dims=size(A)::DimOrInd) + ones(A::AbstractArray, T=eltype(A)::Type, dims=size(A)::Tuple) Create an array of all ones with the same layout as `A`. Element type and size can optionally be adjusted. @@ -2716,7 +2716,7 @@ julia> zeros(Int8, 2, 3) zeros(t,dims) """ - zeros(A::AbstractArray, T=eltype(A)::Type, dims=size(A)::DimOrInd) + 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 and size can optionally be adjusted. From 757e7ac52961c0f223f5904c7d9c29b9572e4e0d Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Wed, 21 Dec 2016 21:37:08 +0100 Subject: [PATCH 6/9] fix --- base/array.jl | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/base/array.jl b/base/array.jl index b69253882f15e..a729172764da0 100644 --- a/base/array.jl +++ b/base/array.jl @@ -207,15 +207,14 @@ fill(v, dims::Integer...) = fill!(Array{typeof(v)}(dims...), v) for (fname, felt) in ((:zeros,:zero), (:ones,:one)) @eval begin - 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) = fill!(Array{T}(dims...), $felt(T)) - ($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) + $fname(a::AbstractArray, T::Type, dims::Tuple) = 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(a::AbstractArray, T::Type, dims...) = fill!(similar(a,T,dims...), $felt(T)) + $fname(T::Type, dims...) = $fname(T, dims) + $fname(dims...) = $fname(dims) end end From b71e9e0ad280ba9fcd0e7ba530f329d7e5312bd8 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Wed, 21 Dec 2016 21:40:20 +0100 Subject: [PATCH 7/9] fix --- base/array.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/base/array.jl b/base/array.jl index a729172764da0..381bf7ae0bcd3 100644 --- a/base/array.jl +++ b/base/array.jl @@ -207,12 +207,13 @@ fill(v, dims::Integer...) = fill!(Array{typeof(v)}(dims...), v) for (fname, felt) in ((:zeros,:zero), (:ones,:one)) @eval begin + # 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(a::AbstractArray, T::Type, dims...) = fill!(similar(a,T,dims...), $felt(T)) $fname(T::Type, dims...) = $fname(T, dims) $fname(dims...) = $fname(dims) end From e80a69548de4605752dd4da8a61a46dae17ca64d Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Wed, 21 Dec 2016 23:11:57 +0100 Subject: [PATCH 8/9] fix --- base/array.jl | 2 +- base/docs/helpdb/Base.jl | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/base/array.jl b/base/array.jl index 381bf7ae0bcd3..793037106cb39 100644 --- a/base/array.jl +++ b/base/array.jl @@ -212,7 +212,7 @@ for (fname, felt) in ((:zeros,:zero), (:ones,:one)) $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(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) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index ee625fb06b5e5..780e18c0305e0 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -552,6 +552,8 @@ fd """ ones(type, dims) + ones(type, dims...) + Create an array of all ones of specified type. The type defaults to `Float64` if not specified. ```jldoctest @@ -566,6 +568,8 @@ ones(t,dims) """ ones(A::AbstractArray, T=eltype(A)::Type, dims=size(A)::Tuple) + ones(A::AbstractArray, T::Type, dims...) + Create an array of all ones with the same layout as `A`. Element type and size can optionally be adjusted. @@ -2703,6 +2707,8 @@ any(::AbstractArray,dims) """ zeros(type, dims) + zeros(type, dims...) + Create an array of all zeros of specified type. The type defaults to `Float64` if not specified. @@ -2718,6 +2724,8 @@ zeros(t,dims) """ zeros(A::AbstractArray, T=eltype(A)::Type, dims=size(A)::Tuple) + zeros(A::AbstractArray, T::Type, dims...) + Create an array of all zeros with the same layout as `A`. Element type and size can optionally be adjusted. From 85c3f0a4b6d5c7396a4b5e2541f17321c7554d16 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Wed, 21 Dec 2016 23:51:16 +0100 Subject: [PATCH 9/9] fix --- base/docs/helpdb/Base.jl | 53 +++++++++++++++------------------------- test/arrayops.jl | 7 +++--- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 780e18c0305e0..57d07e3300dc6 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -549,31 +549,24 @@ to synchronous `File`'s and `IOStream`'s not to any of the asynchronous streams. """ fd -""" - ones(type, dims) - 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::AbstractArray, T=eltype(A)::Type, dims=size(A)::Tuple) - - ones(A::AbstractArray, T::Type, dims...) +julia> ones(1,2) +1×2 Array{Float64,2}: + 1.0 1.0 -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] 2×2 Array{Int64,2}: 1 2 @@ -595,8 +588,9 @@ julia> ones(A) true true ``` +See also [`zeros`](@ref), [`similar`](@ref). """ -ones(A) +ones """ reshape(A, dims) @@ -2705,31 +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]) - zeros(type, dims...) +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::AbstractArray, T=eltype(A)::Type, dims=size(A)::Tuple) - - zeros(A::AbstractArray, T::Type, dims...) -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] 2×2 Array{Int64,2}: 1 2 @@ -2751,8 +2737,9 @@ julia> zeros(A) 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 26c46f99009ec..88ea835314bf0 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1939,9 +1939,10 @@ end @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(), 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))