From fa9a5dc799ba2d39068da0f03156180c9ce98a88 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Thu, 19 Dec 2024 16:14:44 +0000 Subject: [PATCH] Add one and UniformScaling constructor --- Project.toml | 2 +- src/BandedMatrices.jl | 2 +- src/banded/BandedMatrix.jl | 23 +++++++++++++++++++++++ test/test_banded.jl | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index d04b17a3..e353618c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "BandedMatrices" uuid = "aae01518-5342-5314-be14-df237901396f" -version = "1.8.0" +version = "1.9.0" [deps] ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" diff --git a/src/BandedMatrices.jl b/src/BandedMatrices.jl index 4070958e..36959ba6 100644 --- a/src/BandedMatrices.jl +++ b/src/BandedMatrices.jl @@ -7,7 +7,7 @@ import Base: axes, axes1, getproperty, getindex, setindex!, *, +, -, ==, <, <=, >=, /, \, adjoint, transpose, showerror, convert, size, view, unsafe_indices, first, last, size, length, unsafe_length, step, to_indices, to_index, show, fill!, similar, copy, promote_rule, real, imag, - copyto!, Array, sum, sum! + copyto!, Array, sum, sum!, one using Base.Broadcast: AbstractArrayStyle, DefaultArrayStyle, Broadcasted import Base.Broadcast: BroadcastStyle, broadcasted diff --git a/src/banded/BandedMatrix.jl b/src/banded/BandedMatrix.jl index 26f2ffc8..4fa7f2f1 100644 --- a/src/banded/BandedMatrix.jl +++ b/src/banded/BandedMatrix.jl @@ -74,6 +74,20 @@ BandedMatrix{T}(::UndefInitializer, nm::NTuple{2,OneTo{Int}}, ab::NTuple{2,Integ BandedMatrix{V}(M::BandedMatrix) where {V} = _BandedMatrix(AbstractMatrix{V}(M.data), M.raxis, M.l, M.u) BandedMatrix(M::BandedMatrix{V}) where {V} = _BandedMatrix(AbstractMatrix{V}(M.data), M.raxis, M.l, M.u) +function BandedMatrix{T, C, Ax}(A::UniformScaling, nm::NTuple{2,Integer}, (l,u)::NTuple{2,Integer}=(0,0)) where {T,C,Ax} + ret = BandedMatrix{T, C, Ax}(undef, nm, (l,u)) + zero!(ret.data) + ret.data[u+1,:] .= A.λ + ret +end +BandedMatrix{T,C}(A::UniformScaling, nm::NTuple{2,Integer}, ab::NTuple{2,Integer}=(0,0)) where {T,C} = + BandedMatrix{T, C, OneTo{Int}}(A, nm, ab) +BandedMatrix{T}(A::UniformScaling, nm::NTuple{2,Integer}, ab::NTuple{2,Integer}=(0,0)) where T = + BandedMatrix{T, Matrix{T}}(A, nm, ab) +BandedMatrix(A::UniformScaling, nm::NTuple{2,Integer}, ab::NTuple{2,Integer}=(0,0)) = + BandedMatrix{eltype(A)}(A, nm, ab) + + convert(::Type{BandedMatrix{V}}, M::BandedMatrix{V}) where {V} = M convert(::Type{BandedMatrix{V}}, M::BandedMatrix) where {V} = _BandedMatrix(convert(AbstractMatrix{V}, M.data), M.raxis, M.l, M.u) @@ -1008,3 +1022,12 @@ function resize(A::BandedSubBandedMatrix, n::Integer, m::Integer) _BandedMatrix(reshape(resize!(vec(copy(bandeddata(A))), (l+u+1)*m), l+u+1, m), n, l,u) end +### +# one +### + +function one(A::BandedMatrix) + m,n = size(A) + m==n || throw(DimensionMismatch("multiplicative identity defined only for square matrices")) + typeof(A)(I, (m,n)) +end \ No newline at end of file diff --git a/test/test_banded.jl b/test/test_banded.jl index 390ceb76..b718ad36 100644 --- a/test/test_banded.jl +++ b/test/test_banded.jl @@ -568,6 +568,24 @@ include("mymatrix.jl") @test resize(view(A,2:3,2:5),5,5) isa BandedMatrix @test resize(view(A,2:3,2:5),5,5)[1:2,1:4] == A[2:3,2:5] end + + @testset "UniformScaling" begin + @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)) + @test eltype(BandedMatrix(I, (3,4))) == Bool + @test eltype(BandedMatrix{Int}(I, (3,4))) == Int + @test bandwidths(BandedMatrix(I, (3,4))) == (0,0) + @test bandwidths(BandedMatrix(I, (3,4), (1,2))) == (1,2) + @test_throws BoundsError BandedMatrix(I, (3,4), (-1,2)) + end + + + @testset "one" begin + @test_throws DimensionMismatch one(brand(4,5,1,1)) + Q = one(brand(5,5,1,1)) + @test bandwidths(Q) == (0,0) + @test Q == I(5) + @test eltype(Q) == Float64 + end end end # module