Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zeros #19635

Merged
merged 9 commits into from
Dec 22, 2016
Merged

Zeros #19635

Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------------------

Expand Down
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't right: it allocates an Array{T}(dims...) and passes it to $fname(...), but then that function calls similar, which allocates an second array. So you end up allocating one more array than you need.

I think you just want

($fname)(T::Type, dims::Tuple) = fill!(Array{T}(dims...), $felt(T))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

($fname)(dims::Tuple) = ($fname)(Float64, dims)

($fname)(a::AbstractArray,T::Type,dims::DimOrInd...) = ($fname)(a,T,dims)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DimOrInd (an alias for Union{AbstractUnitRange{T},Integer}) doesn't seem right here. Shouldn't it just be Integer...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stole this from similar, which also uses DimOrInd for some methods. I think DimOrInd is for exotic things like OffsetArrays?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dims::Integer...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stole this from similar, which also uses DimOrInd for some methods. I think DimOrInd is for exotic things like OffsetArrays?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay... abstractarray.jl calls to_shape(dims) to get a tuple, though, and similar also supports dims::NeedsShaping

Copy link
Member

@stevengj stevengj Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to just define:

$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))

so that it can take any dims argument supported by similar and has the same behavior for exotic array types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhh okay, I was not aware of NeedsShaping. I will think about a clean way to support this, if I don't find one, I will switch back to Integer as you suggested in the beginning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NeedsShaping cases will be handled by the dims::Tuple method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

($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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type signature isn't right for dims

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right!


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