Skip to content

Commit

Permalink
Overload copymutable_oftype and fillband! (#380)
Browse files Browse the repository at this point in the history
* Overload copymutable_oftype and fillband!

* add tests
  • Loading branch information
dlfivefifty committed Jul 20, 2023
1 parent 6caae30 commit 12e9857
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/banded/BandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/test_banded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 comments on commit 12e9857

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: "Tag with name v0.17.31 already exists and points to a different commit"

Please sign in to comment.