Skip to content

Commit 12e9857

Browse files
authored
Overload copymutable_oftype and fillband! (#380)
* Overload copymutable_oftype and fillband! * add tests
1 parent 6caae30 commit 12e9857

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/banded/BandedMatrix.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ convert(::Type{DefaultBandedMatrix}, M::AbstractMatrix{T}) where T = convert(Def
148148

149149
copy(B::BandedMatrix) = _BandedMatrix(copy(B.data), B.raxis, B.l, B.u)
150150

151+
if isdefined(LinearAlgebra, :copymutable_oftype)
152+
LinearAlgebra.copymutable_oftype(B::BandedMatrix, ::Type{S}) where S =
153+
_BandedMatrix(LinearAlgebra.copymutable_oftype(B.data, S), B.raxis, B.l, B.u)
154+
155+
LinearAlgebra.copymutable_oftype(B::Adjoint{<:Any,<:BandedMatrix}, ::Type{S}) where S =
156+
LinearAlgebra.copymutable_oftype(parent(B), S)'
157+
LinearAlgebra.copymutable_oftype(B::Transpose{<:Any,<:BandedMatrix}, ::Type{S}) where S =
158+
transpose(LinearAlgebra.copymutable_oftype(parent(B), S))
159+
end
160+
151161
promote_rule(::Type{BandedMatrix{T1, C1}}, ::Type{BandedMatrix{T2, C2}}) where {T1,C1, T2,C2} =
152162
BandedMatrix{promote_type(T1,T2), promote_type(C1, C2)}
153163

@@ -341,6 +351,9 @@ similar(bm::AbstractBandedMatrix, n::Integer, m::Integer, l::Integer, u::Integer
341351
similar(bm, eltype(bm), m, n, l, u)
342352
similar(bm::AbstractBandedMatrix, nm::Tuple{<:Integer,<:Integer}) = similar(bm, nm...)
343353

354+
355+
356+
344357
## Abstract Array Interface
345358

346359
axes(A::BandedMatrix) = (A.raxis, axes(A.data,2))
@@ -806,6 +819,11 @@ function fill!(A::BandedMatrix, x)
806819
A
807820
end
808821

822+
function LinearAlgebra.fillband!(A::BandedMatrix{T}, x, l, u) where T
823+
fill!(view(A.data, max(A.u+1-u,1):min(A.u+1-l,size(A.data,1)), :), x)
824+
A
825+
end
826+
809827
diag(A::BandedMatrix, k::Integer = 0) = A[band(k)]
810828

811829
## BandedSubBandedMatrix routines

test/test_banded.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,16 @@ Base.similar(::MyMatrix, ::Type{T}, m::Int, n::Int) where T = MyMatrix{T}(undef,
505505
@test @view(p[2:3]) == 2:3
506506
end
507507
end
508+
509+
if isdefined(LinearAlgebra, :copymutable_oftype)
510+
@testset "copymutable_oftype" begin
511+
B = _BandedMatrix((2:3)', 4, -2, 2)
512+
@test LinearAlgebra.copymutable_oftype(B, Float64) == B
513+
@test LinearAlgebra.copymutable_oftype(B, Float64) isa BandedMatrix{Float64}
514+
@test LinearAlgebra.copymutable_oftype(B', Float64) == B'
515+
@test LinearAlgebra.copymutable_oftype(B', Float64) isa Adjoint{Float64,<:BandedMatrix{Float64}}
516+
@test LinearAlgebra.copymutable_oftype(transpose(B), Float64) == transpose(B)
517+
@test LinearAlgebra.copymutable_oftype(transpose(B), Float64) isa Transpose{Float64,<:BandedMatrix{Float64}}
518+
end
519+
end
508520
end

0 commit comments

Comments
 (0)