@@ -448,18 +448,18 @@ function dataview(V::BandedMatrixBand)
448448end
449449
450450# B[band(i)]
451- function copyto! (v:: Vector , B:: BandedMatrixBand )
451+ function copyto! (v:: AbstractVector , B:: BandedMatrixBand )
452452 A = parent (parent (B))
453453 if - A. l ≤ band (B) ≤ A. u
454454 copyto! (v, dataview (B))
455455 else
456456 Binds = axes (B,1 )
457- v[Binds] .= 0
457+ v[firstindex (v) - 1 .+ Binds] .= 0
458458 end
459459 return v
460460end
461461
462- # B[band(i)] .= x
462+ # B[band(i)] .= x::Number
463463function fill! (Bv:: BandedMatrixBand , x)
464464 b = band (Bv)
465465 A = parent (parent (Bv))
@@ -472,6 +472,29 @@ function fill!(Bv::BandedMatrixBand, x)
472472 Bv
473473end
474474
475+ # B[band(i)] .= V::AbstractVector
476+ function _copyto! (Bv:: BandedMatrixBand , V:: AbstractVector )
477+ isempty (V) && return Bv
478+ A = parent (parent (Bv))
479+ if - A. l ≤ band (Bv) ≤ A. u
480+ copyto! (dataview (Bv), V)
481+ else
482+ # bounds-checking to work around axis offset of V
483+ destinds, srcinds = LinearIndices (Bv), LinearIndices (V)
484+ idf, isf = first (destinds), first (srcinds)
485+ Δi = idf - isf
486+ (checkbounds (Bool, destinds, isf+ Δi) &
487+ checkbounds (Bool, destinds, last (srcinds)+ Δi)) ||
488+ throw (BoundsError (dest, srcinds))
489+
490+ all (iszero, V) || throw (BandError (A, band (Bv)))
491+ end
492+ return Bv
493+ end
494+
495+ copyto! (Bv:: BandedMatrixBand , V:: AbstractVector ) = _copyto! (Bv, V)
496+ copyto! (Bv:: BandedMatrixBand , V:: BandedMatrixBand ) = _copyto! (Bv, V)
497+
475498# ~ indexing along a row
476499
477500
530553
531554@propagate_inbounds function setindex! (A:: BandedMatrix{T} , v:: AbstractVector , :: Colon ) where {T}
532555 A[:, :] = reshape (v,size (A))
556+ A
533557end
534558
535559
@@ -542,10 +566,10 @@ end
542566 row = A. u - b. i + 1
543567 data, i = A. data, max (b. i + 1 , 1 )
544568 for v in V
545- data[row, i] = convert (T, v) :: T
569+ @inbounds data[row, i] = v
546570 i += 1
547571 end
548- V
572+ A
549573end
550574
551575
@@ -557,12 +581,14 @@ end
557581 @boundscheck checkbandmatch (A,V,:,j)
558582
559583 A. data[data_colrange (A,j)] = V[colrange (A,j)]
560- V
584+ A
561585end
562586
563587# vector - BandRange - integer -- A[1, BandRange] = 2
564- @propagate_inbounds setindex! (A:: BandedMatrix , V:: AbstractVector , :: BandRangeType , j:: Integer ) =
565- (A[colrange (A, j), j] = V) # call range method
588+ @propagate_inbounds function setindex! (A:: BandedMatrix , V:: AbstractVector , :: BandRangeType , j:: Integer )
589+ A[colrange (A, j), j] = V # call range method
590+ A
591+ end
566592
567593# vector - range - integer -- A[1:3, 1] = [1, 2, 3]
568594@propagate_inbounds function setindex! (A:: BandedMatrix , V:: AbstractVector , kr:: AbstractRange , j:: Integer )
580606 inbands_setindex! (data, u, v, k, j)
581607 end
582608 end
583- V
609+ A
584610end
585611
586612
@@ -607,12 +633,14 @@ end
607633 end
608634
609635 A. data[data_rowrange (A,k)] = V[rowrange (A,k)]
610- V
636+ A
611637end
612638
613639# vector - integer - BandRange -- A[1, BandRange] = [1, 2, 3]
614- @propagate_inbounds setindex! (A:: BandedMatrix , V:: AbstractVector , k:: Integer , :: BandRangeType ) =
615- (A[k, rowstart (A, k): rowstop (A, k)] = V) # call range method
640+ @propagate_inbounds function setindex! (A:: BandedMatrix , V:: AbstractVector , k:: Integer , :: BandRangeType )
641+ A[k, rowstart (A, k): rowstop (A, k)] = V # call range method
642+ A
643+ end
616644
617645# vector - integer - range -- A[1, 2:3] = [3, 4]
618646@propagate_inbounds function setindex! (A:: BandedMatrix , V:: AbstractVector , k:: Integer , jr:: AbstractRange )
630658 inbands_setindex! (data, u, v, k, j)
631659 end
632660 end
633- V
661+ A
634662end
635663
636664# ~ indexing over a rectangular block
641669 @boundscheck checkdimensions (kr, jr, V)
642670 @boundscheck checkbandmatch (A, V, kr, jr)
643671 copyto! (view (A, kr, jr), V)
644- V
672+ A
645673end
646674
647675# ~~ end setindex! ~~
0 commit comments