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

Define and export undefs similar to zeros and ones #42620

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 25 additions & 3 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ fill(v, dims::Tuple{}) = (a=Array{typeof(v),0}(undef, dims); fill!(a, v); a)
zeros([T=Float64,] dims...)

Create an `Array`, with element type `T`, of all zeros with size specified by `dims`.
See also [`fill`](@ref), [`ones`](@ref), [`zero`](@ref).
See also [`fill`](@ref), [`ones`](@ref), [`zero`](@ref), [`undefs`](@ref).

# Examples
```jldoctest
Expand All @@ -559,7 +559,7 @@ function zeros end
ones([T=Float64,] dims...)

Create an `Array`, with element type `T`, of all ones with size specified by `dims`.
See also [`fill`](@ref), [`zeros`](@ref).
See also [`fill`](@ref), [`zeros`](@ref), [`undefs`](@ref).

# Examples
```jldoctest
Expand All @@ -575,12 +575,31 @@ julia> ones(ComplexF64, 2, 3)
"""
function ones end

for (fname, felt) in ((:zeros, :zero), (:ones, :one))
"""
undefs([T=Float64,] dims::Tuple)
undefs([T=Float64,] dims...)

Create an `Array`, with element type `T`, with size specified by `dims`.
The element values are undefined and are not initialized to any value.
This is equivalent to `Array{T}(undef, dims)`.
See also [`fill`](@ref), [`ones`](@ref), [`zeros`](@ref).

!!! compat "Julia 1.8"
`undefs` is available as of Julia 1.8. Prior to Julia 1.8, use `Array{T}(undef, dims)`.

"""
function undefs end

for fname in (:zeros, :ones, :undefs)
@eval begin
$fname(dims::DimOrInd...) = $fname(dims)
$fname(::Type{T}, dims::DimOrInd...) where {T} = $fname(T, dims)
$fname(dims::Tuple{Vararg{DimOrInd}}) = $fname(Float64, dims)
$fname(::Type{T}, dims::NTuple{N, Union{Integer, OneTo}}) where {T,N} = $fname(T, map(to_dim, dims))
end
end
for (fname, felt) in ((:zeros, :zero), (:ones, :one))
@eval begin
function $fname(::Type{T}, dims::NTuple{N, Integer}) where {T,N}
a = Array{T,N}(undef, dims)
fill!(a, $felt(T))
Expand All @@ -594,6 +613,9 @@ for (fname, felt) in ((:zeros, :zero), (:ones, :one))
end
end

undefs(::Type{T}, dims::NTuple{N, Integer}) where {T,N} = Array{T,N}(undef, dims)
undefs(::Type{T}, dims::Tuple{}) where {T} = Array{T}(undef)

function _one(unit::T, x::AbstractMatrix) where T
require_one_based_indexing(x)
m,n = size(x)
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ export
sum!,
sum,
to_indices,
undefs,
vcat,
vec,
view,
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Base.StridedVecOrMat
Base.getindex(::Type, ::Any...)
Base.zeros
Base.ones
Base.undefs
Base.BitArray
Base.BitArray(::UndefInitializer, ::Integer...)
Base.BitArray(::Any)
Expand Down