Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add one and UniformScaling constructor #466

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/BandedMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/banded/BandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@
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

Check warning on line 81 in src/banded/BandedMatrix.jl

View check run for this annotation

Codecov / codecov/patch

src/banded/BandedMatrix.jl#L81

Added line #L81 was not covered by tests
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)
Expand Down Expand Up @@ -1008,3 +1022,12 @@
_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
18 changes: 18 additions & 0 deletions test/test_banded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading