Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f228eda

Browse files
committedDec 21, 2017
at-deprecate ones
1 parent 131cb0c commit f228eda

File tree

5 files changed

+19
-31
lines changed

5 files changed

+19
-31
lines changed
 

‎NEWS.md

+8
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,14 @@ Deprecated or removed
612612
and/or shape depending on `opts...`. For an algebraic multiplicative identity,
613613
consider `one(A)` ([#24656]).
614614

615+
* `ones(dims...)` and `ones(T, dims)` have been deprecated. The direct replacements are
616+
`fill(1.0, dims...)` and `fill(one(T), dims...)`/`fill(T(1), dims...)`, but for common
617+
types it is often shorter to use numeric literals, e.g. `fill(1, dims...)` for `Int`s,
618+
`fill(1f0, dims...)` for `Float32`s, `fill(1+0im, dims...)` for complex types,
619+
`fill(0x1, dims...)` for `UInt8`s etc. Other common patterns, such as `x*ones(dims)`
620+
for some value `x` can, with advantage, instead be rewritten as
621+
`fill(x, dims)` ([#25087]).
622+
615623
* The `Operators` module is deprecated. Instead, import required operators explicitly
616624
from `Base`, e.g. `import Base: +, -, *, /` ([#22251]).
617625

‎base/array.jl

+5-29
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ fill(v, dims::Integer...) = fill!(Array{typeof(v)}(uninitialized, dims...), v)
343343
zeros([T=Float64,] dims...)
344344
345345
Create an `Array`, with element type `T`, of all zeros with size specified by `dims`.
346-
See also [`similar`](@ref).
346+
See also [`fill`](@ref), [`similar`](@ref).
347347
348348
# Examples
349349
```jldoctest
@@ -359,34 +359,10 @@ julia> zeros(Int8, 2, 3)
359359
"""
360360
function zeros end
361361

362-
"""
363-
ones([T=Float64,] dims...)
364-
365-
Create an `Array`, with element type `T`, of all ones with size specified by `dims`.
366-
See also [`zeros`](@ref), [`similar`](@ref).
367-
368-
# Examples
369-
```jldoctest
370-
julia> ones(1,2)
371-
1×2 Array{Float64,2}:
372-
1.0 1.0
373-
374-
julia> ones(ComplexF64, 2, 3)
375-
2×3 Array{Complex{Float64},2}:
376-
1.0+0.0im 1.0+0.0im 1.0+0.0im
377-
1.0+0.0im 1.0+0.0im 1.0+0.0im
378-
```
379-
"""
380-
function ones end
381-
382-
for (fname, felt) in ((:zeros, :zero), (:ones, :one))
383-
@eval begin
384-
$fname(::Type{T}, dims::NTuple{N, Any}) where {T, N} = fill!(Array{T,N}(uninitialized, convert(Dims, dims)::Dims), $felt(T))
385-
$fname(dims::Tuple) = ($fname)(Float64, dims)
386-
$fname(::Type{T}, dims...) where {T} = $fname(T, dims)
387-
$fname(dims...) = $fname(dims)
388-
end
389-
end
362+
zeros(::Type{T}, dims::NTuple{N, Any}) where {T, N} = fill!(Array{T,N}(uninitialized, convert(Dims, dims)), zero(T))
363+
zeros(dims::Tuple) = (zeros)(Float64, dims)
364+
zeros(::Type{T}, dims...) where {T} = zeros(T, dims)
365+
zeros(dims...) = zeros(dims)
390366

391367
function _one(unit::T, x::AbstractMatrix) where T
392368
m,n = size(x)

‎base/deprecated.jl

+6
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,12 @@ end
17661766
@deprecate zeros(a::AbstractArray, ::Type{T}) where {T} fill!(similar(a, T), 0)
17671767
@deprecate zeros(a::AbstractArray) fill!(similar(a), 0)
17681768

1769+
# deprecate ones
1770+
@deprecate ones(::Type{T}, dims::Tuple) where {T} fill(one(T), dims)
1771+
@deprecate ones(dims::Tuple) fill(1., dims)
1772+
@deprecate ones(::Type{T}, dims...) where {T} fill(one(T), dims...)
1773+
@deprecate ones(dims...) fill(1., dims...)
1774+
17691775
# PR #23711
17701776
@eval LibGit2 begin
17711777
@deprecate get_creds!(cache::CachedCredentials, credid, default) get!(cache, credid, default)

‎base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ export
477477
minmax,
478478
ndims,
479479
nonzeros,
480-
ones,
481480
parent,
482481
parentindices,
483482
partialsort,

‎doc/src/stdlib/arrays.md

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Base.Matrix(::Nothing, ::Any, ::Any)
2222
Base.Matrix(::Missing, ::Any, ::Any)
2323
Base.getindex(::Type, ::Any...)
2424
Base.zeros
25-
Base.ones
2625
Base.BitArray
2726
Base.BitArray(::Uninitialized, ::Integer...)
2827
Base.BitArray(::Any)

0 commit comments

Comments
 (0)
Please sign in to comment.