Skip to content

Commit 66ab171

Browse files
jw3126ViralBShah
authored andcommitted
Zeros (#19635)
* Fix #19265. * Add methods to zeros, ones with analgous signature to similar. * news
1 parent 758418c commit 66ab171

File tree

4 files changed

+94
-27
lines changed

4 files changed

+94
-27
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Library improvements
100100
That is, not every member of the input iterable will be visited if a `true` (in the case of `any`) or
101101
`false` (in the case of `all`) value is found, and `mapreduce` will visit all members of the iterable.
102102

103+
* Additional methods for `ones` and `zeros` functions to support the same signature as the `similar` function ([#19635]).
104+
103105
Compiler/Runtime improvements
104106
-----------------------------
105107

base/array.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,15 @@ fill(v, dims::Integer...) = fill!(Array{typeof(v)}(dims...), v)
207207

208208
for (fname, felt) in ((:zeros,:zero), (:ones,:one))
209209
@eval begin
210-
($fname)(T::Type, dims...) = fill!(Array{T}(dims...), ($felt)(T))
211-
($fname)(dims...) = fill!(Array{Float64}(dims...), ($felt)(Float64))
212-
($fname){T}(A::AbstractArray{T}) = fill!(similar(A), ($felt)(T))
210+
# allow signature of similar
211+
$fname(a::AbstractArray, T::Type, dims::Tuple) = fill!(similar(a, T, dims), $felt(T))
212+
$fname(a::AbstractArray, T::Type, dims...) = fill!(similar(a,T,dims...), $felt(T))
213+
$fname(a::AbstractArray, T::Type=eltype(a)) = fill!(similar(a,T), $felt(T))
214+
215+
$fname(T::Type, dims::Tuple) = fill!(Array{T}(Dims(dims)), $felt(T))
216+
$fname(dims::Tuple) = ($fname)(Float64, dims)
217+
$fname(T::Type, dims...) = $fname(T, dims)
218+
$fname(dims...) = $fname(dims)
213219
end
214220
end
215221

base/docs/helpdb/Base.jl

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -549,26 +549,24 @@ to synchronous `File`'s and `IOStream`'s not to any of the asynchronous streams.
549549
"""
550550
fd
551551

552+
552553
"""
553-
ones(type, dims)
554+
ones([A::AbstractArray,] [T=eltype(A)::Type,] [dims=size(A)::Tuple])
554555
555-
Create an array of all ones of specified type. The type defaults to `Float64` if not specified.
556+
Create an array of all ones with the same layout as `A`, element type `T` and size `dims`.
557+
The `A` argument can be skipped, which behaves like `Array{Float64,0}()` was passed.
558+
For convenience `dims` may also be passed in variadic form.
556559
557560
```jldoctest
558561
julia> ones(Complex128, 2, 3)
559562
2×3 Array{Complex{Float64},2}:
560563
1.0+0.0im 1.0+0.0im 1.0+0.0im
561564
1.0+0.0im 1.0+0.0im 1.0+0.0im
562-
```
563-
"""
564-
ones(t,dims)
565565
566-
"""
567-
ones(A)
568-
569-
Create an array of all ones with the same element type and shape as `A`.
566+
julia> ones(1,2)
567+
1×2 Array{Float64,2}:
568+
1.0 1.0
570569
571-
```jldoctest
572570
julia> A = [1 2; 3 4]
573571
2×2 Array{Int64,2}:
574572
1 2
@@ -578,9 +576,21 @@ julia> ones(A)
578576
2×2 Array{Int64,2}:
579577
1 1
580578
1 1
579+
580+
julia> ones(A, Float64)
581+
2×2 Array{Float64,2}:
582+
1. 1.
583+
1. 1.
584+
585+
julia> ones(A, Bool, (3,))
586+
3-element Array{Bool,1}:
587+
true
588+
true
589+
true
581590
```
591+
See also [`zeros`](@ref), [`similar`](@ref).
582592
"""
583-
ones(A)
593+
ones
584594

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

26912701
"""
2692-
zeros(type, dims)
2702+
zeros([A::AbstractArray,] [T=eltype(A)::Type,] [dims=size(A)::Tuple])
2703+
2704+
Create an array of all zeros with the same layout as `A`, element type `T` and size `dims`.
2705+
The `A` argument can be skipped, which behaves like `Array{Float64,0}()` was passed.
2706+
For convenience `dims` may also be passed in variadic form.
26932707
2694-
Create an array of all zeros of specified type.
2695-
The type defaults to `Float64` if not specified.
26962708
26972709
```jldoctest
2710+
julia> zeros(1)
2711+
1-element Array{Float64,1}:
2712+
0.0
2713+
26982714
julia> zeros(Int8, 2, 3)
26992715
2×3 Array{Int8,2}:
27002716
0 0 0
27012717
0 0 0
2702-
```
2703-
"""
2704-
zeros(t,dims)
2705-
2706-
"""
2707-
zeros(A)
2708-
2709-
Create an array of all zeros with the same element type and shape as `A`.
27102718
2711-
```jldoctest
27122719
julia> A = [1 2; 3 4]
27132720
2×2 Array{Int64,2}:
27142721
1 2
@@ -2718,9 +2725,21 @@ julia> zeros(A)
27182725
2×2 Array{Int64,2}:
27192726
0 0
27202727
0 0
2728+
2729+
julia> zeros(A, Float64)
2730+
2×2 Array{Float64,2}:
2731+
0.0 0.0
2732+
0.0 0.0
2733+
2734+
julia> zeros(A, Bool, (3,))
2735+
3-element Array{Bool,1}:
2736+
false
2737+
false
2738+
false
27212739
```
2740+
See also [`ones`](@ref), [`similar`](@ref).
27222741
"""
2723-
zeros(A)
2742+
zeros
27242743

27252744
"""
27262745
Symbol(x...) -> Symbol

test/arrayops.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,46 @@ using TestHelpers.OAs
19381938
@test accumulate(op, [10 20 30], 2) == [10 op(10, 20) op(op(10, 20), 30)] == [10 40 110]
19391939
end
19401940

1941+
@testset "zeros and ones" begin
1942+
@test ones([1,2], Float64, (2,3)) == ones(2,3)
1943+
@test ones(2) == ones(Int, 2) == ones([2,3], Float32, 2) == [1,1]
1944+
@test isa(ones(2), Vector{Float64})
1945+
@test isa(ones(Int, 2), Vector{Int})
1946+
@test isa(ones([2,3], Float32, 2), Vector{Float32})
1947+
1948+
function test_zeros(arr, T, s)
1949+
@test all(arr .== 0)
1950+
@test isa(arr, T)
1951+
@test size(arr) == s
1952+
end
1953+
test_zeros(zeros(), Array{Float64, 0}, ())
1954+
test_zeros(zeros(2), Vector{Float64}, (2,))
1955+
test_zeros(zeros(2,3), Matrix{Float64}, (2,3))
1956+
test_zeros(zeros((2,3)), Matrix{Float64}, (2,3))
1957+
1958+
test_zeros(zeros(Int, 6), Vector{Int}, (6,))
1959+
test_zeros(zeros(Int, 2, 3), Matrix{Int}, (2,3))
1960+
test_zeros(zeros(Int, (2, 3)), Matrix{Int}, (2,3))
1961+
1962+
test_zeros(zeros([1 2; 3 4]), Matrix{Int}, (2, 2))
1963+
test_zeros(zeros([1 2; 3 4], Float64), Matrix{Float64}, (2, 2))
1964+
1965+
zs = zeros(SparseMatrixCSC([1 2; 3 4]), Complex{Float64}, (2,3))
1966+
test_zeros(zs, SparseMatrixCSC{Complex{Float64}}, (2, 3))
1967+
1968+
@testset "#19265" begin
1969+
@test_throws MethodError zeros(Float64, [1.])
1970+
x = [1.]
1971+
test_zeros(zeros(x, Float64), Vector{Float64}, (1,))
1972+
@test x == [1.]
1973+
end
1974+
1975+
# exotic indexing
1976+
oarr = zeros(randn(3), UInt16, 1:3, -1:0)
1977+
@test indices(oarr) == (1:3, -1:0)
1978+
test_zeros(oarr.parent, Matrix{UInt16}, (3, 2))
1979+
end
1980+
19411981
# issue #11053
19421982
type T11053
19431983
a::Float64

0 commit comments

Comments
 (0)