Skip to content

Commit 76a25db

Browse files
authored
broadcast value to an entire band (#366)
* broadcast value to an entire band * Add tests
1 parent 7b6da31 commit 76a25db

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BandedMatrices"
22
uuid = "aae01518-5342-5314-be14-df237901396f"
3-
version = "0.17.24"
3+
version = "0.17.25"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/banded/BandedMatrix.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ band(V::BandedMatrixBand) = first(parentindices(V)).band.i
414414
415415
Forward a view of a band of a `BandedMatrix` to the parent's data matrix.
416416
417+
!!! warn
418+
This will error if the indexing is out-of-bounds for the data matrix, even if it is inbounds
419+
for the parent `BandedMatrix`
420+
417421
# Examples
418422
```jldoctest
419423
julia> A = BandedMatrix(0=>1:4, 1=>5:7, -1=>8:10)
@@ -443,6 +447,7 @@ function dataview(V::BandedMatrixBand)
443447
view(A.data, A.u - b + 1, max(b,0)+1:min(n,m+b))
444448
end
445449

450+
# B[band(i)]
446451
function copyto!(v::Vector, B::BandedMatrixBand)
447452
A = parent(parent(B))
448453
if -A.l band(B) A.u
@@ -454,6 +459,19 @@ function copyto!(v::Vector, B::BandedMatrixBand)
454459
return v
455460
end
456461

462+
# B[band(i)] .= x
463+
function fill!(Bv::BandedMatrixBand, x)
464+
b = band(Bv)
465+
A = parent(parent(Bv))
466+
l, u = bandwidths(A)
467+
if -l <= b <= u
468+
fill!(dataview(Bv), x)
469+
elseif !iszero(x) # allow setting outside bands to zero
470+
throw(BandError(A,b))
471+
end
472+
Bv
473+
end
474+
457475
# ~ indexing along a row
458476

459477

test/test_banded.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,5 +479,21 @@ Base.similar(::MyMatrix, ::Type{T}, m::Int, n::Int) where T = MyMatrix{T}(undef,
479479
A[1:2:end,1:2:end] = X1
480480
@test A == B
481481
end
482+
483+
@testset "broadcasting to a band" begin
484+
B = brand(Int8, 6, 6, -2, 2)
485+
B[band(2)] .= 10
486+
@test all(==(10), diag(B, 2))
487+
@test all(==(10), B[band(2)])
488+
489+
B = brand(Int8, 2, 4, 1, 1)
490+
B[band(-1)] .= 2
491+
B[band(0)] .= 3
492+
B[band(1)] .= 4
493+
@test B == [3 4 0 0; 2 3 4 0]
494+
495+
@test_throws BandError B[band(100)] .= 10
496+
@test_throws BandError B[band(-100)] .= 10
497+
end
482498
end
483499

0 commit comments

Comments
 (0)