Skip to content

Make fillband! public and specialize for banded matrices #1345

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ public AbstractTriangular,
symmetric_type,
zeroslike,
matprod_dest,
fillstored!
fillstored!,
fillband!

const BlasFloat = Union{Float64,Float32,ComplexF64,ComplexF32}
const BlasReal = Union{Float64,Float32}
Expand Down
27 changes: 27 additions & 0 deletions src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1570,3 +1570,30 @@ function Base._sum(A::Bidiagonal, dims::Integer)
end
res
end

function fillband!(B::Bidiagonal, x, l, u)
if l > u
return B
end
if ((B.uplo == 'U' && (l < 0 || u > 1)) ||
(B.uplo == 'L' && (l < -1 || u > 0))) && !iszero(x)
throw_fillband_error(l, u, x)
else
if B.uplo == 'U'
if l <= 1 <= u
fill!(B.ev, x)
end
if l <= 0 <= u
fill!(B.dv, x)
end
else # B.uplo == 'L'
if l <= 0 <= u
fill!(B.dv, x)
end
if l <= -1 <= u
fill!(B.ev, x)
end
end
end
return B
end
17 changes: 17 additions & 0 deletions src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ tril(M::Matrix, k::Integer) = tril!(copy(M), k)
fillband!(A::AbstractMatrix, x, l, u)

Fill the band between diagonals `l` and `u` with the value `x`.

# Examples
```jldoctest
julia> A = zeros(4,4)
4×4 Matrix{Float64}:
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0

julia> LinearAlgebra.fillband!(A, 2, 0, 1)
4×4 Matrix{Float64}:
2.0 2.0 0.0 0.0
0.0 2.0 2.0 0.0
0.0 0.0 2.0 2.0
0.0 0.0 0.0 2.0
```
"""
function fillband!(A::AbstractMatrix{T}, x, l, u) where T
require_one_based_indexing(A)
Expand Down
15 changes: 15 additions & 0 deletions src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1219,3 +1219,18 @@ end

uppertriangular(D::Diagonal) = D
lowertriangular(D::Diagonal) = D

throw_fillband_error(l, u, x) = throw(ArgumentError(lazy"cannot set bands $l:$u to a nonzero value ($x)"))

function fillband!(D::Diagonal, x, l, u)
if l > u
return D
end
if (l < 0 || u > 0) && !iszero(x)
throw_fillband_error(l, u, x)
end
if l <= 0 <= u
fill!(D.diag, x)
end
return D
end
11 changes: 11 additions & 0 deletions src/hessenberg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ lmul!(x::Number, H::UpperHessenberg) = (lmul!(x, H.data); H)

fillstored!(H::UpperHessenberg, x) = (fillband!(H.data, x, -1, size(H,2)-1); H)

function fillband!(H::UpperHessenberg, x, l, u)
if l > u
return H
end
if l < -1 && !iszero(x)
throw_fillband_error(l, u, x)
end
fillband!(H.data, x, l, u)
return H
end

+(A::UpperHessenberg, B::UpperHessenberg) = UpperHessenberg(A.data+B.data)
-(A::UpperHessenberg, B::UpperHessenberg) = UpperHessenberg(A.data-B.data)

Expand Down
30 changes: 30 additions & 0 deletions src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,36 @@ fillstored!(A::UnitLowerTriangular, x) = (fillband!(A.data, x, 1-size(A,1), -1);
fillstored!(A::UpperTriangular, x) = (fillband!(A.data, x, 0, size(A,2)-1); A)
fillstored!(A::UnitUpperTriangular, x) = (fillband!(A.data, x, 1, size(A,2)-1); A)

function fillband!(A::LowerOrUnitLowerTriangular, x, l, u)
if l > u
return A
end
if u > 0 && !iszero(x)
throw_fillband_error(l, u, x)
end
isunit = A isa UnitLowerTriangular
if isunit && u >= 0 && x != oneunit(x)
throw(ArgumentError(lazy"cannot set the diagonal band to a non-unit value ($x)"))
end
fillband!(A.data, x, l, min(u, -isunit))
return A
end

function fillband!(A::UpperOrUnitUpperTriangular, x, l, u)
if l > u
return A
end
if l < 0 && !iszero(x)
throw_fillband_error(l, u, x)
end
isunit = A isa UnitUpperTriangular
if isunit && l <= 0 && x != oneunit(x)
throw(ArgumentError(lazy"cannot set the diagonal band to a non-unit value ($x)"))
end
fillband!(A.data, x, max(l, isunit), u)
return A
end

# Binary operations
# use broadcasting if the parents are strided, where we loop only over the triangular part
function +(A::UpperTriangular, B::UpperTriangular)
Expand Down
41 changes: 41 additions & 0 deletions src/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1189,3 +1189,44 @@
),
normfirst, normend)
end

function fillband!(T::Tridiagonal, x, l, u)
if l > u
return T
end
if (l < -1 || u > 1) && !iszero(x)
throw_fillband_error(l, u, x)
else
if l <= -1 <= u
fill!(T.dl, x)
end
if l <= 0 <= u
fill!(T.d, x)
end
if l <= 1 <= u
fill!(T.du, x)
end
end
return T
end

function fillband!(T::SymTridiagonal, x, l, u)
if l > u
return T
end
if (l <= 1 <= u) != (l <= -1 <= u)
throw(ArgumentError(lazy"cannot set only one off-diagonal band of a SymTridiagonal"))
elseif (l < -1 || u > 1) && !iszero(x)
throw_fillband_error(l, u, x)
elseif l <= 0 <= u && !issymmetric(x)
throw(ArgumentError(lazy"cannot set entries in the diagonal band of a SymTridiagonal to an asymmetric value $x"))

Check warning on line 1222 in src/tridiag.jl

View check run for this annotation

Codecov / codecov/patch

src/tridiag.jl#L1222

Added line #L1222 was not covered by tests
else
if l <= 0 <= u
fill!(T.dv, x)
end
if l <= 1 <= u
fill!(T.ev, x)
end
end
return T
end
52 changes: 52 additions & 0 deletions test/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1228,4 +1228,56 @@ end
@test_throws BoundsError B[LinearAlgebra.BandIndex(0,size(B,1)+1)]
end

@testset "fillband!" begin
@testset "uplo = :U" begin
B = Bidiagonal(zeros(4), zeros(3), :U)
LinearAlgebra.fillband!(B, 2, 1, 1)
@test all(==(2), diagview(B,1))
LinearAlgebra.fillband!(B, 3, 0, 0)
@test all(==(3), diagview(B,0))
@test all(==(2), diagview(B,1))
LinearAlgebra.fillband!(B, 4, 0, 1)
@test all(==(4), diagview(B,0))
@test all(==(4), diagview(B,1))
@test_throws ArgumentError LinearAlgebra.fillband!(B, 3, -1, 0)

LinearAlgebra.fillstored!(B, 1)
LinearAlgebra.fillband!(B, 0, -3, 3)
@test iszero(B)
LinearAlgebra.fillband!(B, 0, -10, 10)
@test iszero(B)
LinearAlgebra.fillstored!(B, 1)
B2 = copy(B)
LinearAlgebra.fillband!(B, 0, -1, -3)
@test B == B2
LinearAlgebra.fillband!(B, 0, 10, 10)
@test B == B2
end

@testset "uplo = :L" begin
B = Bidiagonal(zeros(4), zeros(3), :L)
LinearAlgebra.fillband!(B, 2, -1, -1)
@test all(==(2), diagview(B,-1))
LinearAlgebra.fillband!(B, 3, 0, 0)
@test all(==(3), diagview(B,0))
@test all(==(2), diagview(B,-1))
LinearAlgebra.fillband!(B, 4, -1, 0)
@test all(==(4), diagview(B,0))
@test all(==(4), diagview(B,-1))
@test_throws ArgumentError LinearAlgebra.fillband!(B, 3, 0, 1)

LinearAlgebra.fillstored!(B, 1)
LinearAlgebra.fillband!(B, 0, -3, 3)
@test iszero(B)
LinearAlgebra.fillband!(B, 0, -10, 10)
@test iszero(B)
LinearAlgebra.fillstored!(B, 1)
B2 = copy(B)
LinearAlgebra.fillband!(B, 0, -1, -3)
@test B == B2
LinearAlgebra.fillband!(B, 0, 10, 10)
@test B == B2
end
end

end # module TestBidiagonal
22 changes: 22 additions & 0 deletions test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1499,4 +1499,26 @@ end
@test_throws BoundsError D[LinearAlgebra.BandIndex(0,size(D,1)+1)]
end

@testset "fillband!" begin
D = Diagonal(zeros(4))
LinearAlgebra.fillband!(D, 2, 0, 0)
@test all(==(2), diagview(D,0))
@test all(==(0), diagview(D,-1))
@test_throws ArgumentError LinearAlgebra.fillband!(D, 3, -2, 2)

LinearAlgebra.fillstored!(D, 1)
LinearAlgebra.fillband!(D, 0, -3, 3)
@test iszero(D)
LinearAlgebra.fillstored!(D, 1)
LinearAlgebra.fillband!(D, 0, -10, 10)
@test iszero(D)

LinearAlgebra.fillstored!(D, 1)
D2 = copy(D)
LinearAlgebra.fillband!(D, 0, -1, -3)
@test D == D2
LinearAlgebra.fillband!(D, 0, 10, 10)
@test D == D2
end

end # module TestDiagonal
20 changes: 20 additions & 0 deletions test/hessenberg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,24 @@ end
@test_throws DimensionMismatch hessenberg(zeros(0,0)).Q * ones(1, 2)
end

@testset "fillband" begin
U = UpperHessenberg(zeros(4,4))
@test_throws ArgumentError LinearAlgebra.fillband!(U, 1, -2, 1)
@test iszero(U)

LinearAlgebra.fillband!(U, 10, -1, 2)
@test all(==(10), diagview(U,-1))
@test all(==(10), diagview(U,2))
@test all(==(0), diagview(U,3))

LinearAlgebra.fillband!(U, 0, -5, 5)
@test iszero(U)

U2 = copy(U)
LinearAlgebra.fillband!(U, -10, 1, -2)
@test U == U2
LinearAlgebra.fillband!(U, -10, 10, 10)
@test U == U2
end

end # module TestHessenberg
69 changes: 69 additions & 0 deletions test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -934,4 +934,73 @@ end
end
end

@testset "fillband!" begin
@testset for TT in (UpperTriangular, UnitUpperTriangular)
U = TT(zeros(4,4))
@test_throws ArgumentError LinearAlgebra.fillband!(U, 1, -1, 1)
if U isa UnitUpperTriangular
@test_throws ArgumentError LinearAlgebra.fillband!(U, 2, 0, 1)
end
# check that the error paths do not mutate the array
if U isa UpperTriangular
@test iszero(U)
end

LinearAlgebra.fillband!(U, 1, 0, 1)
@test all(==(1), diagview(U,0))
@test all(==(1), diagview(U,1))
@test all(==(0), diagview(U,2))

LinearAlgebra.fillband!(U, 10, 1, 2)
@test all(==(10), diagview(U,1))
@test all(==(10), diagview(U,2))
@test all(==(1), diagview(U,0))
@test all(==(0), diagview(U,3))

if U isa UpperTriangular
LinearAlgebra.fillband!(U, 0, -5, 5)
@test iszero(U)
end

U2 = copy(U)
LinearAlgebra.fillband!(U, -10, 1, -2)
@test U == U2
LinearAlgebra.fillband!(U, -10, 10, 10)
@test U == U2
end
@testset for TT in (LowerTriangular, UnitLowerTriangular)
L = TT(zeros(4,4))
@test_throws ArgumentError LinearAlgebra.fillband!(L, 1, -1, 1)
if L isa UnitLowerTriangular
@test_throws ArgumentError LinearAlgebra.fillband!(L, 2, -1, 0)
end
# check that the error paths do not mutate the array
if L isa LowerTriangular
@test iszero(L)
end

LinearAlgebra.fillband!(L, 1, -1, 0)
@test all(==(1), diagview(L,0))
@test all(==(1), diagview(L,-1))
@test all(==(0), diagview(L,-2))

LinearAlgebra.fillband!(L, 10, -2, -1)
@test all(==(10), diagview(L,-1))
@test all(==(10), diagview(L,-2))
@test all(==(1), diagview(L,0))
@test all(==(0), diagview(L,-3))

if L isa LowerTriangular
LinearAlgebra.fillband!(L, 0, -5, 5)
@test iszero(L)
end

L2 = copy(L)
LinearAlgebra.fillband!(L, -10, -1, -2)
@test L == L2
LinearAlgebra.fillband!(L, -10, -10, -10)
@test L == L2
end
end

end # module TestTriangular
Loading