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

[WIP] constructors for Array from zeros/ones #24389

Closed
wants to merge 1 commit 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
12 changes: 12 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,18 @@ for (fname, felt) in ((:zeros, :zero), (:ones, :one))
end
end

## Array construction from zeros/ones
for (iterfn, scalfn) in ((ones, one), (zeros, zero))
Array{T,N}(::typeof(iterfn), dims::Dims{N}) where {T,N} = fill!(Array{T,N}(dims), scalfn(T))
Array{T}(::typeof(iterfn), dims::Dims{N}) where {T,N} = Array{T,N}(iterfn, dims)
Array{T,N}(::typeof(iterfn), dims...) where {T,N} = Array{T,N}(iterfn, Dims(dims))
Array{T}(::typeof(iterfn), dims...) where {T} = Array{T}(iterfn, Dims(dims))
end
Array(::typeof(zeros), dims::Dims{N}) where {N} = Array{Float64,N}(zeros, dims)
Array(::typeof(zeros), dims...) = Array(zeros, Dims(dims))
Array(::typeof(ones), dims::Dims{N}) where {N} = Array{Float64,N}(ones, dims)
Array(::typeof(ones), dims...) = Array(ones, Dims(dims))

"""
eye([T::Type=Float64,] m::Integer, n::Integer)

Expand Down
19 changes: 19 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,25 @@ end
test_zeros(oarr.parent, Matrix{UInt16}, (3, 2))
end

@testset "Array construction from $(iterfn)" for (iterfn, val) in ((zeros, 0), (ones, 1))
m, n, k = 3, 4, 5
@test Array(iterfn, m)::Vector{Float64} == fill(val, m)
@test Array(iterfn, m, n)::Matrix{Float64} == fill(val, m, n)
@test Array(iterfn, m, n, k)::Array{Float64,3} == fill(val, m, n, k)
@test Array{Float32}(iterfn, m)::Vector{Float32} == fill(val, m)
@test Array{Float32}(iterfn, m, n)::Matrix{Float32} == fill(val, m, n)
@test Array{Float32}(iterfn, m, n, k)::Array{Float32,3} == fill(val, m, n, k)
@test Array{Real}(iterfn, m)::Vector{Real} == fill(val, m)
@test Array{Real}(iterfn, m, n)::Matrix{Real} == fill(val, m, n)
@test Array{Real}(iterfn, m, n, k)::Array{Real,3} == fill(val, m, n, k)
@test Array{Float32,1}(iterfn, m)::Vector{Float32} == fill(val, m)
@test Array{Float32,2}(iterfn, m, n)::Matrix{Float32} == fill(val, m, n)
@test Array{Float32,3}(iterfn, m, n, k)::Array{Float32,3} == fill(val, m, n, k)
@test Array{Real,1}(iterfn, m)::Vector{Real} == fill(val, m)
@test Array{Real,2}(iterfn, m, n)::Matrix{Real} == fill(val, m, n)
@test Array{Real,3}(iterfn, m, n, k)::Array{Real,3} == fill(val, m, n, k)
end

# issue #11053
mutable struct T11053
a::Float64
Expand Down