Skip to content

Commit c3513ca

Browse files
committed
rewrite zeros(T, dims) to fill(zero(T), dims)
1 parent e07898e commit c3513ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+130
-130
lines changed

base/env.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if Sys.iswindows()
1313
if len == 0
1414
return Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND ? "" : onError(str)
1515
end
16-
val = zeros(UInt16,len)
16+
val = fill(zero(UInt16), len)
1717
ret = ccall(:GetEnvironmentVariableW,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32),var,val,len)
1818
if (ret == 0 && len != 1) || ret != len-1 || val[end] != 0
1919
error(string("getenv: ", str, ' ', len, "-1 != ", ret, ": ", Libc.FormatMessage()))

base/linalg/bidiag.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ end
135135
#Converting from Bidiagonal to dense Matrix
136136
function Matrix{T}(A::Bidiagonal) where T
137137
n = size(A, 1)
138-
B = zeros(T, n, n)
138+
B = fill(zero(T), n, n)
139139
for i = 1:n - 1
140140
B[i,i] = A.dv[i]
141141
if A.uplo == 'U'
@@ -605,7 +605,7 @@ function eigvecs(M::Bidiagonal{T}) where T
605605
n = length(M.dv)
606606
Q = Matrix{T}(uninitialized, n,n)
607607
blks = [0; find(x -> x == 0, M.ev); n]
608-
v = zeros(T, n)
608+
v = fill(zero(T), n)
609609
if M.uplo == 'U'
610610
for idx_block = 1:length(blks) - 1, i = blks[idx_block] + 1:blks[idx_block + 1] #index of eigenvector
611611
fill!(v, zero(T))

base/linalg/bitarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ end
2323
#if mA == 0; return C; end
2424
#col_ch = num_bit_chunks(mA)
2525
## TODO: avoid using aux chunks and copy (?)
26-
#aux_chunksA = zeros(UInt64, col_ch)
27-
#aux_chunksB = [zeros(UInt64, col_ch) for j=1:nB]
26+
#aux_chunksA = fill(zero(UInt64), col_ch)
27+
#aux_chunksB = [fill(zero(UInt64), col_ch) for j=1:nB]
2828
#for j = 1:nB
2929
#Base.copy_chunks!(aux_chunksB[j], 1, B.chunks, (j-1)*mA+1, mA)
3030
#end

base/linalg/cholesky.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ function getproperty(C::CholeskyPivoted{T}, d::Symbol) where T<:BlasFloat
404404
return getfield(C, :piv)
405405
elseif d == :P
406406
n = size(C, 1)
407-
P = zeros(T, n, n)
407+
P = fill(zero(T), n, n)
408408
for i = 1:n
409409
P[getfield(C, :piv)[i], i] = one(T)
410410
end

base/linalg/dense.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ end
335335
function diagm_container(kv::Pair{<:Integer,<:AbstractVector}...)
336336
T = promote_type(map(x -> eltype(x.second), kv)...)
337337
n = mapreduce(x -> length(x.second) + abs(x.first), max, kv)
338-
return zeros(T, n, n)
338+
return fill(zero(T), n, n)
339339
end
340340
function diagm_container(kv::Pair{<:Integer,<:BitVector}...)
341341
n = mapreduce(x -> length(x.second) + abs(x.first), max, kv)
@@ -1280,7 +1280,7 @@ function pinv(A::StridedMatrix{T}, tol::Real) where T
12801280
if istril(A)
12811281
if istriu(A)
12821282
maxabsA = maximum(abs.(diag(A)))
1283-
B = zeros(Tout, n, m)
1283+
B = fill(zero(Tout), n, m)
12841284
for i = 1:min(m, n)
12851285
if abs(A[i,i]) > tol*maxabsA
12861286
Aii = inv(A[i,i])
@@ -1294,7 +1294,7 @@ function pinv(A::StridedMatrix{T}, tol::Real) where T
12941294
end
12951295
SVD = svdfact(A, full = false)
12961296
Stype = eltype(SVD.S)
1297-
Sinv = zeros(Stype, length(SVD.S))
1297+
Sinv = fill(zero(Stype), length(SVD.S))
12981298
index = SVD.S .> tol*maximum(SVD.S)
12991299
Sinv[index] = one(Stype) ./ SVD.S[index]
13001300
Sinv[find(.!isfinite.(Sinv))] = zero(Stype)

base/linalg/diagonal.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ end
8282
r
8383
end
8484
diagzero(::Diagonal{T},i,j) where {T} = zero(T)
85-
diagzero(D::Diagonal{Matrix{T}},i,j) where {T} = zeros(T, size(D.diag[i], 1), size(D.diag[j], 2))
85+
diagzero(D::Diagonal{Matrix{T}},i,j) where {T} = fill(zero(T), size(D.diag[i], 1), size(D.diag[j], 2))
8686

8787
function setindex!(D::Diagonal, v, i::Int, j::Int)
8888
@boundscheck checkbounds(D, i, j)

base/linalg/hessenberg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ function getproperty(F::Hessenberg, d::Symbol)
6767
end
6868

6969
function getindex(A::HessenbergQ, i::Integer, j::Integer)
70-
x = zeros(eltype(A), size(A, 1))
70+
x = fill(zero(eltype(A)), size(A, 1))
7171
x[i] = 1
72-
y = zeros(eltype(A), size(A, 2))
72+
y = fill(zero(eltype(A)), size(A, 2))
7373
y[j] = 1
7474
return dot(x, mul!(A, y))
7575
end

base/linalg/lapack.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ and `tau`, which stores the elementary reflectors.
736736
"""
737737
function geqp3!(A::AbstractMatrix{<:BlasFloat})
738738
m, n = size(A)
739-
geqp3!(A, zeros(BlasInt, n), similar(A, min(m, n)))
739+
geqp3!(A, fill(zero(BlasInt), n), similar(A, min(m, n)))
740740
end
741741

742742
## Complete orthogonaliztion tools
@@ -1206,7 +1206,7 @@ for (gelsd, gelsy, elty) in
12061206
if size(B, 1) != m
12071207
throw(DimensionMismatch("B has leading dimension $(size(B,1)) but needs $m"))
12081208
end
1209-
newB = [B; zeros($elty, max(0, n - size(B, 1)), size(B, 2))]
1209+
newB = [B; fill(zero($elty), max(0, n - size(B, 1)), size(B, 2))]
12101210
s = similar(A, $elty, min(m, n))
12111211
rnk = Ref{BlasInt}()
12121212
info = Ref{BlasInt}()
@@ -1250,10 +1250,10 @@ for (gelsd, gelsy, elty) in
12501250
if size(B, 1) != m
12511251
throw(DimensionMismatch("B has leading dimension $(size(B,1)) but needs $m"))
12521252
end
1253-
newB = [B; zeros($elty, max(0, n - size(B, 1)), size(B, 2))]
1253+
newB = [B; fill(zero($elty), max(0, n - size(B, 1)), size(B, 2))]
12541254
lda = max(1, stride(A,2))
12551255
ldb = max(1, stride(newB,2))
1256-
jpvt = zeros(BlasInt, n)
1256+
jpvt = fill(zero(BlasInt), n)
12571257
rnk = Ref{BlasInt}()
12581258
work = Vector{$elty}(uninitialized, 1)
12591259
lwork = BlasInt(-1)
@@ -1299,7 +1299,7 @@ for (gelsd, gelsy, elty, relty) in
12991299
if size(B, 1) != m
13001300
throw(DimensionMismatch("B has leading dimension $(size(B,1)) but needs $m"))
13011301
end
1302-
newB = [B; zeros($elty, max(0, n - size(B, 1)), size(B, 2))]
1302+
newB = [B; fill(zero($elty), max(0, n - size(B, 1)), size(B, 2))]
13031303
s = similar(A, $relty, min(m, n))
13041304
rnk = Ref{BlasInt}()
13051305
info = Ref{BlasInt}()
@@ -1345,10 +1345,10 @@ for (gelsd, gelsy, elty, relty) in
13451345
if size(B, 1) != m
13461346
throw(DimensionMismatch("B has leading dimension $(size(B,1)) but needs $m"))
13471347
end
1348-
newB = [B; zeros($elty, max(0, n - size(B, 1)), size(B, 2))]
1348+
newB = [B; fill(zero($elty), max(0, n - size(B, 1)), size(B, 2))]
13491349
lda = max(1, m)
13501350
ldb = max(1, m, n)
1351-
jpvt = zeros(BlasInt, n)
1351+
jpvt = fill(zero(BlasInt), n)
13521352
rnk = Ref{BlasInt}(1)
13531353
work = Vector{$elty}(uninitialized, 1)
13541354
lwork = BlasInt(-1)
@@ -1424,7 +1424,7 @@ for (gglse, elty) in ((:dgglse_, :Float64),
14241424
if length(d) != p
14251425
throw(DimensionMismatch("d has length $(length(d)), needs $p"))
14261426
end
1427-
X = zeros($elty, n)
1427+
X = fill(zero($elty), n)
14281428
info = Ref{BlasInt}()
14291429
work = Vector{$elty}(uninitialized, 1)
14301430
lwork = BlasInt(-1)
@@ -3721,7 +3721,7 @@ for (stev, stebz, stegr, stein, elty) in
37213721
if length(ev_in) != n - 1
37223722
throw(DimensionMismatch("ev_in has length $(length(ev_in)) but needs one less than dv's length, $n)"))
37233723
end
3724-
ev = [ev_in; zeros($elty,1)]
3724+
ev = [ev_in; fill(zero($elty),1)]
37253725
ldz = n #Leading dimension
37263726
#Number of eigenvalues to find
37273727
if !(1 <= length(w_in) <= n)

base/linalg/lq.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function getproperty(F::LQ, d::Symbol)
8686
end
8787

8888
getindex(A::LQPackedQ, i::Integer, j::Integer) =
89-
mul!(A, setindex!(zeros(eltype(A), size(A, 2)), 1, j))[i]
89+
mul!(A, setindex!(fill(zero(eltype(A)), size(A, 2)), 1, j))[i]
9090

9191
function show(io::IO, C::LQ)
9292
println(io, "$(typeof(C)) with factors L and Q:")
@@ -160,7 +160,7 @@ function *(adjA::Adjoint{<:Any,<:LQPackedQ}, B::StridedVecOrMat)
160160
if size(B,1) == size(A.factors,2)
161161
mul!(adjoint(AbstractMatrix{TAB}(A)), copy_oftype(B, TAB))
162162
elseif size(B,1) == size(A.factors,1)
163-
mul!(adjoint(AbstractMatrix{TAB}(A)), [B; zeros(TAB, size(A.factors, 2) - size(A.factors, 1), size(B, 2))])
163+
mul!(adjoint(AbstractMatrix{TAB}(A)), [B; fill(zero(TAB), size(A.factors, 2) - size(A.factors, 1), size(B, 2))])
164164
else
165165
throw(DimensionMismatch("first dimension of B, $(size(B,1)), must equal one of the dimensions of A, $(size(A))"))
166166
end
@@ -235,7 +235,7 @@ function *(A::StridedVecOrMat, Q::LQPackedQ)
235235
if size(A, 2) == size(Q.factors, 2)
236236
C = copy_oftype(A, TR)
237237
elseif size(A, 2) == size(Q.factors, 1)
238-
C = zeros(TR, size(A, 1), size(Q.factors, 2))
238+
C = fill(zero(TR), size(A, 1), size(Q.factors, 2))
239239
copyto!(C, 1, A, 1, length(A))
240240
else
241241
_rightappdimmismatch("columns")
@@ -248,7 +248,7 @@ function *(adjA::Adjoint{<:Any,<:StridedMatrix}, Q::LQPackedQ)
248248
if size(A, 1) == size(Q.factors, 2)
249249
C = adjoint!(similar(A, TR, reverse(size(A))), A)
250250
elseif size(A, 1) == size(Q.factors, 1)
251-
C = zeros(TR, size(A, 2), size(Q.factors, 2))
251+
C = fill(zero(TR), size(A, 2), size(Q.factors, 2))
252252
adjoint!(view(C, :, 1:size(A, 1)), A)
253253
else
254254
_rightappdimmismatch("rows")

base/linalg/qr.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ QRPivoted(factors::AbstractMatrix{T}, τ::Vector{T}, jpvt::Vector{BlasInt}) wher
135135

136136
function qrfactUnblocked!(A::AbstractMatrix{T}) where {T}
137137
m, n = size(A)
138-
τ = zeros(T, min(m,n))
138+
τ = fill(zero(T), min(m,n))
139139
for k = 1:min(m - 1 + !(T<:Real), n)
140140
x = view(A, k:m, k)
141141
τk = reflector!(x)
@@ -460,7 +460,7 @@ function getproperty(F::QRPivoted{T}, d::Symbol) where T
460460
elseif d == :P
461461
p = F.p
462462
n = length(p)
463-
P = zeros(T, n, n)
463+
P = fill(zero(T), n, n)
464464
for i in 1:n
465465
P[p[i],i] = one(T)
466466
end
@@ -514,9 +514,9 @@ size(A::AbstractQ) = size(A, 1), size(A, 2)
514514

515515

516516
function getindex(A::AbstractQ, i::Integer, j::Integer)
517-
x = zeros(eltype(A), size(A, 1))
517+
x = fill(zero(eltype(A)), size(A, 1))
518518
x[i] = 1
519-
y = zeros(eltype(A), size(A, 2))
519+
y = fill(zero(eltype(A)), size(A, 2))
520520
y[j] = 1
521521
return dot(x, mul!(A, y))
522522
end
@@ -558,7 +558,7 @@ function (*)(A::AbstractQ, b::StridedVector)
558558
if size(A.factors, 1) == length(b)
559559
bnew = copy_oftype(b, TAb)
560560
elseif size(A.factors, 2) == length(b)
561-
bnew = [b; zeros(TAb, size(A.factors, 1) - length(b))]
561+
bnew = [b; fill(zero(TAb), size(A.factors, 1) - length(b))]
562562
else
563563
throw(DimensionMismatch("vector must have length either $(size(A.factors, 1)) or $(size(A.factors, 2))"))
564564
end
@@ -570,7 +570,7 @@ function (*)(A::AbstractQ, B::StridedMatrix)
570570
if size(A.factors, 1) == size(B, 1)
571571
Bnew = copy_oftype(B, TAB)
572572
elseif size(A.factors, 2) == size(B, 1)
573-
Bnew = [B; zeros(TAB, size(A.factors, 1) - size(B,1), size(B, 2))]
573+
Bnew = [B; fill(zero(TAB), size(A.factors, 1) - size(B,1), size(B, 2))]
574574
else
575575
throw(DimensionMismatch("first dimension of matrix must have size either $(size(A.factors, 1)) or $(size(A.factors, 2))"))
576576
end
@@ -711,7 +711,7 @@ function *(A::StridedMatrix, adjB::Adjoint{<:Any,<:AbstractQ})
711711
copyto!(AA, A)
712712
return mul!(AA, adjoint(BB))
713713
elseif size(A,2) == size(B.factors,2)
714-
return mul!([A zeros(TAB, size(A, 1), size(B.factors, 1) - size(B.factors, 2))], adjoint(BB))
714+
return mul!([A fill(zero(TAB), size(A, 1), size(B.factors, 1) - size(B.factors, 2))], adjoint(BB))
715715
else
716716
throw(DimensionMismatch("matrix A has dimensions $(size(A)) but matrix B has dimensions $(size(B))"))
717717
end
@@ -788,7 +788,7 @@ function ldiv!(A::QR{T}, B::StridedMatrix{T}) where T
788788
R = A.R
789789
@inbounds begin
790790
if n > m # minimum norm solution
791-
τ = zeros(T,m)
791+
τ = fill(zero(T),m)
792792
for k = m:-1:1 # Trapezoid to triangular by elementary operation
793793
x = view(R, k, [k; m + 1:n])
794794
τk = reflector!(x)
@@ -845,8 +845,8 @@ _cut_B(x::AbstractVector, r::UnitRange) = length(x) > length(r) ? x[r] : x
845845
_cut_B(X::AbstractMatrix, r::UnitRange) = size(X, 1) > length(r) ? X[r,:] : X
846846

847847
## append right hand side with zeros if necessary
848-
_zeros(::Type{T}, b::AbstractVector, n::Integer) where {T} = zeros(T, max(length(b), n))
849-
_zeros(::Type{T}, B::AbstractMatrix, n::Integer) where {T} = zeros(T, max(size(B, 1), n), size(B, 2))
848+
_zeros(::Type{T}, b::AbstractVector, n::Integer) where {T} = fill(zero(T), max(length(b), n))
849+
_zeros(::Type{T}, B::AbstractMatrix, n::Integer) where {T} = fill(zero(T), max(size(B, 1), n), size(B, 2))
850850

851851
function (\)(A::Union{QR{TA},QRCompactWY{TA},QRPivoted{TA}}, B::AbstractVecOrMat{TB}) where {TA,TB}
852852
S = promote_type(TA,TB)

0 commit comments

Comments
 (0)