-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Zeros #19635
Changes from 7 commits
39a3bde
b3a93f2
38d34e7
8f824a2
259668b
757e7ac
b71e9e0
e80a695
85c3f0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -564,9 +564,10 @@ julia> ones(Complex128, 2, 3) | |
ones(t,dims) | ||
|
||
""" | ||
ones(A) | ||
ones(A::AbstractArray, T=eltype(A)::Type, dims=size(A)::Tuple) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to document the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are at least some doctests where |
||
|
||
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] | ||
|
@@ -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) | ||
|
@@ -2704,9 +2716,10 @@ julia> zeros(Int8, 2, 3) | |
zeros(t,dims) | ||
|
||
""" | ||
zeros(A) | ||
zeros(A::AbstractArray, T=eltype(A)::Type, dims=size(A)::Tuple) | ||
|
||
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] | ||
|
@@ -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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1927,6 +1927,45 @@ 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 | ||
|
||
# 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stevengj Here is a test that shows why |
||
end | ||
|
||
# issue #11053 | ||
type T11053 | ||
a::Float64 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for splatting here; you can just do
fill!(Array{T}(Dims(dims)), $felt(T))
I think?