diff --git a/src/banded/BandedMatrix.jl b/src/banded/BandedMatrix.jl index b11659d6..7b9c0cb7 100644 --- a/src/banded/BandedMatrix.jl +++ b/src/banded/BandedMatrix.jl @@ -148,6 +148,16 @@ convert(::Type{DefaultBandedMatrix}, M::AbstractMatrix{T}) where T = convert(Def copy(B::BandedMatrix) = _BandedMatrix(copy(B.data), B.raxis, B.l, B.u) +if isdefined(LinearAlgebra, :copymutable_oftype) + LinearAlgebra.copymutable_oftype(B::BandedMatrix, ::Type{S}) where S = + _BandedMatrix(LinearAlgebra.copymutable_oftype(B.data, S), B.raxis, B.l, B.u) + + LinearAlgebra.copymutable_oftype(B::Adjoint{<:Any,<:BandedMatrix}, ::Type{S}) where S = + LinearAlgebra.copymutable_oftype(parent(B), S)' + LinearAlgebra.copymutable_oftype(B::Transpose{<:Any,<:BandedMatrix}, ::Type{S}) where S = + transpose(LinearAlgebra.copymutable_oftype(parent(B), S)) +end + promote_rule(::Type{BandedMatrix{T1, C1}}, ::Type{BandedMatrix{T2, C2}}) where {T1,C1, T2,C2} = BandedMatrix{promote_type(T1,T2), promote_type(C1, C2)} @@ -341,6 +351,9 @@ similar(bm::AbstractBandedMatrix, n::Integer, m::Integer, l::Integer, u::Integer similar(bm, eltype(bm), m, n, l, u) similar(bm::AbstractBandedMatrix, nm::Tuple{<:Integer,<:Integer}) = similar(bm, nm...) + + + ## Abstract Array Interface axes(A::BandedMatrix) = (A.raxis, axes(A.data,2)) @@ -806,6 +819,11 @@ function fill!(A::BandedMatrix, x) A end +function LinearAlgebra.fillband!(A::BandedMatrix{T}, x, l, u) where T + fill!(view(A.data, max(A.u+1-u,1):min(A.u+1-l,size(A.data,1)), :), x) + A +end + diag(A::BandedMatrix, k::Integer = 0) = A[band(k)] ## BandedSubBandedMatrix routines diff --git a/test/test_banded.jl b/test/test_banded.jl index 9cf7f32c..a82f24b3 100644 --- a/test/test_banded.jl +++ b/test/test_banded.jl @@ -505,4 +505,16 @@ Base.similar(::MyMatrix, ::Type{T}, m::Int, n::Int) where T = MyMatrix{T}(undef, @test @view(p[2:3]) == 2:3 end end + + if isdefined(LinearAlgebra, :copymutable_oftype) + @testset "copymutable_oftype" begin + B = _BandedMatrix((2:3)', 4, -2, 2) + @test LinearAlgebra.copymutable_oftype(B, Float64) == B + @test LinearAlgebra.copymutable_oftype(B, Float64) isa BandedMatrix{Float64} + @test LinearAlgebra.copymutable_oftype(B', Float64) == B' + @test LinearAlgebra.copymutable_oftype(B', Float64) isa Adjoint{Float64,<:BandedMatrix{Float64}} + @test LinearAlgebra.copymutable_oftype(transpose(B), Float64) == transpose(B) + @test LinearAlgebra.copymutable_oftype(transpose(B), Float64) isa Transpose{Float64,<:BandedMatrix{Float64}} + end + end end