Skip to content

Commit 68865e2

Browse files
committed
implement fillband! and call it from fillstored!(::AbstractTriangular)
1 parent 197ada2 commit 68865e2

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

base/linalg/dense.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,26 @@ function tril!(M::AbstractMatrix, k::Integer)
242242
end
243243
tril(M::Matrix, k::Integer) = tril!(copy(M), k)
244244

245+
"""
246+
fillband!(A::AbstractMatrix, x, l, u)
247+
248+
Fill the band between diagonals `l` and `u` with the value `x`.
249+
"""
250+
function fillband!(A::AbstractMatrix{T}, x, l, u) where T
251+
m, n = size(A)
252+
xT = convert(T, x)
253+
idx = 1
254+
for j in 0:n-1
255+
iu = min(max(0, j-u), m) + idx
256+
il = min(max(0, j-l), m-1) + idx
257+
for i in iu:il
258+
A[i] = xT
259+
end
260+
idx += m
261+
end
262+
return A
263+
end
264+
245265
function diagind(m::Integer, n::Integer, k::Integer=0)
246266
if !(-m <= k <= n)
247267
throw(ArgumentError(string("requested diagonal, $k, must be at least $(-m) and ",

base/linalg/triangular.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,10 @@ end
418418
scale!(A::Union{UpperTriangular,LowerTriangular}, c::Number) = scale!(A,A,c)
419419
scale!(c::Number, A::Union{UpperTriangular,LowerTriangular}) = scale!(A,c)
420420

421-
fillstored!(A::AbstractTriangular, x) = (fill!(A.data, x); A)
421+
fillstored!(A::LowerTriangular, x) = (fillband!(A.data, x, size(A,1), 0); A)
422+
fillstored!(A::UnitLowerTriangular, x) = (fillband!(A.data, x, size(A,1), -1); A)
423+
fillstored!(A::UpperTriangular, x) = (fillband!(A.data, x, 0, size(A,2)); A)
424+
fillstored!(A::UnitUpperTriangular, x) = (fillband!(A.data, x, 1, size(A,2)); A)
422425

423426
# Binary operations
424427
+(A::UpperTriangular, B::UpperTriangular) = UpperTriangular(A.data + B.data)

0 commit comments

Comments
 (0)