Skip to content

Commit 3fe9243

Browse files
authored
Add one and UniformScaling constructor (#466)
1 parent fd26b83 commit 3fe9243

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
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 = "1.8.0"
3+
version = "1.9.0"
44

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

src/BandedMatrices.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Base: axes, axes1, getproperty, getindex, setindex!, *, +, -, ==, <, <=,
77
>=, /, \, adjoint, transpose, showerror, convert, size, view,
88
unsafe_indices, first, last, size, length, unsafe_length, step, to_indices,
99
to_index, show, fill!, similar, copy, promote_rule, real, imag,
10-
copyto!, Array, sum, sum!
10+
copyto!, Array, sum, sum!, one
1111

1212
using Base.Broadcast: AbstractArrayStyle, DefaultArrayStyle, Broadcasted
1313
import Base.Broadcast: BroadcastStyle, broadcasted

src/banded/BandedMatrix.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ BandedMatrix{T}(::UndefInitializer, nm::NTuple{2,OneTo{Int}}, ab::NTuple{2,Integ
7474
BandedMatrix{V}(M::BandedMatrix) where {V} = _BandedMatrix(AbstractMatrix{V}(M.data), M.raxis, M.l, M.u)
7575
BandedMatrix(M::BandedMatrix{V}) where {V} = _BandedMatrix(AbstractMatrix{V}(M.data), M.raxis, M.l, M.u)
7676

77+
function BandedMatrix{T, C, Ax}(A::UniformScaling, nm::NTuple{2,Integer}, (l,u)::NTuple{2,Integer}=(0,0)) where {T,C,Ax}
78+
ret = BandedMatrix{T, C, Ax}(undef, nm, (l,u))
79+
zero!(ret.data)
80+
ret.data[u+1,:] .= A.λ
81+
ret
82+
end
83+
BandedMatrix{T,C}(A::UniformScaling, nm::NTuple{2,Integer}, ab::NTuple{2,Integer}=(0,0)) where {T,C} =
84+
BandedMatrix{T, C, OneTo{Int}}(A, nm, ab)
85+
BandedMatrix{T}(A::UniformScaling, nm::NTuple{2,Integer}, ab::NTuple{2,Integer}=(0,0)) where T =
86+
BandedMatrix{T, Matrix{T}}(A, nm, ab)
87+
BandedMatrix(A::UniformScaling, nm::NTuple{2,Integer}, ab::NTuple{2,Integer}=(0,0)) =
88+
BandedMatrix{eltype(A)}(A, nm, ab)
89+
90+
7791
convert(::Type{BandedMatrix{V}}, M::BandedMatrix{V}) where {V} = M
7892
convert(::Type{BandedMatrix{V}}, M::BandedMatrix) where {V} =
7993
_BandedMatrix(convert(AbstractMatrix{V}, M.data), M.raxis, M.l, M.u)
@@ -1008,3 +1022,12 @@ function resize(A::BandedSubBandedMatrix, n::Integer, m::Integer)
10081022
_BandedMatrix(reshape(resize!(vec(copy(bandeddata(A))), (l+u+1)*m), l+u+1, m), n, l,u)
10091023
end
10101024

1025+
###
1026+
# one
1027+
###
1028+
1029+
function one(A::BandedMatrix)
1030+
m,n = size(A)
1031+
m==n || throw(DimensionMismatch("multiplicative identity defined only for square matrices"))
1032+
typeof(A)(I, (m,n))
1033+
end

test/test_banded.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,24 @@ include("mymatrix.jl")
568568
@test resize(view(A,2:3,2:5),5,5) isa BandedMatrix
569569
@test resize(view(A,2:3,2:5),5,5)[1:2,1:4] == A[2:3,2:5]
570570
end
571+
572+
@testset "UniformScaling" begin
573+
@test BandedMatrix(I, (3,4)) == BandedMatrix{Int}(I, (3,4)) == BandedMatrix{Int,Matrix{Int}}(I, (3,4)) == BandedMatrix{Int,Matrix{Int},Base.OneTo{Int}}(I, (3,4)) == Matrix(I,(3,4))
574+
@test eltype(BandedMatrix(I, (3,4))) == Bool
575+
@test eltype(BandedMatrix{Int}(I, (3,4))) == Int
576+
@test bandwidths(BandedMatrix(I, (3,4))) == (0,0)
577+
@test bandwidths(BandedMatrix(I, (3,4), (1,2))) == (1,2)
578+
@test_throws BoundsError BandedMatrix(I, (3,4), (-1,2))
579+
end
580+
581+
582+
@testset "one" begin
583+
@test_throws DimensionMismatch one(brand(4,5,1,1))
584+
Q = one(brand(5,5,1,1))
585+
@test bandwidths(Q) == (0,0)
586+
@test Q == I(5)
587+
@test eltype(Q) == Float64
588+
end
571589
end
572590

573591
end # module

0 commit comments

Comments
 (0)