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 all 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))
# 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

Expand Down
67 changes: 43 additions & 24 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

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

"variadic form" is not very clear, we don't use that terminology anywhere else

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would you call it instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd spell out an example

Copy link
Member

Choose a reason for hiding this comment

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

Maybe:

For convenience, `dims` may be passed as individual integer arguments, as in `zeros(3,4,5)`, rather than as a tuple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are examples where dims... is used already e.g. zeros(Int8, 2, 3). But I can open a new PR with stevengj's suggestion if you like.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also I think if we don't have a terminology for dims..., we should introduce one. It occurs quite frequently and feels cumbersome not having a word for it.

Copy link
Member

Choose a reason for hiding this comment

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

I think "variadic form" is fine, but it should be spelled out with an example as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

without any explanation that's terribly jargony and not very clear

Copy link
Member

Choose a reason for hiding this comment

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

Then please make a PR that improves it.


```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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stevengj Here is a test that shows why ::DimOrInd instead of ::Integers was choosen.

end

# issue #11053
type T11053
a::Float64
Expand Down