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

implement bandwidths for OneElement #447

Merged
20 changes: 20 additions & 0 deletions src/interfaceimpl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@
sublayout(::AbstractTridiagonalLayout, ::Type{<:Tuple{AbstractUnitRange{Int},AbstractUnitRange{Int}}}) =
BandedLayout()

function bandwidths(o::OneElement)
pos = FillArrays.nzind(o)
dlfivefifty marked this conversation as resolved.
Show resolved Hide resolved
if(length(pos) == 1)
dlfivefifty marked this conversation as resolved.
Show resolved Hide resolved
n = length(o)
if(pos[1] > n)

Check warning on line 63 in src/interfaceimpl.jl

View check run for this annotation

Codecov / codecov/patch

src/interfaceimpl.jl#L59-L63

Added lines #L59 - L63 were not covered by tests
#Empty BandedMatrix
(-1,0)

Check warning on line 65 in src/interfaceimpl.jl

View check run for this annotation

Codecov / codecov/patch

src/interfaceimpl.jl#L65

Added line #L65 was not covered by tests
dlfivefifty marked this conversation as resolved.
Show resolved Hide resolved
else
(pos[1] - 1, -pos[1] + 1)

Check warning on line 67 in src/interfaceimpl.jl

View check run for this annotation

Codecov / codecov/patch

src/interfaceimpl.jl#L67

Added line #L67 was not covered by tests
end
elseif(length(pos) == 2)
n,m = size(o)
if(pos[1] > n || pos[2] > m)
(-1,0)

Check warning on line 72 in src/interfaceimpl.jl

View check run for this annotation

Codecov / codecov/patch

src/interfaceimpl.jl#L69-L72

Added lines #L69 - L72 were not covered by tests
else
(pos[1]-pos[2],pos[2]-pos[1])

Check warning on line 74 in src/interfaceimpl.jl

View check run for this annotation

Codecov / codecov/patch

src/interfaceimpl.jl#L74

Added line #L74 was not covered by tests
end
end
end

###
# rot180
###
Expand Down
14 changes: 13 additions & 1 deletion test/test_interface.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TestInterface

using BandedMatrices, LinearAlgebra, ArrayLayouts, FillArrays, Test
using BandedMatrices, LinearAlgebra, ArrayLayouts, FillArrays, Test, Random
import BandedMatrices: isbanded, AbstractBandedLayout, BandedStyle,
BandedColumns, bandeddata
import ArrayLayouts: OnesLayout, UnknownLayout
Expand Down Expand Up @@ -310,6 +310,18 @@ end
@test layout_getindex(T,1:10,1:10) isa BandedMatrix
end

@testset "OneElement" begin
o = OneElement(1, 3, 5)
@test bandwidths(o) == (2,-2)
n,m = rand(1:10,2)
o = OneElement(1, (rand(1:n),rand(1:m)), (n, m))
@test bandwidths(o) == bandwidths(BandedMatrix(o))
Copy link
Member

Choose a reason for hiding this comment

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

This test is a tautology since BandedMatrix calls bandwidths(o) to determine its bandwidths. I think the following will be better:

Suggested change
@test bandwidths(o) == bandwidths(BandedMatrix(o))
@test bandwidths(o) == bandwidths(sparse(o))

You'll need to add SparseArrays to the using statement

o = OneElement(1, (n+1,m+1), (n, m))
@test bandwidths(o) == (-1, 0)
o = OneElement(1, 6, 5)
@test bandwidths(o) == (-1, 0)
end

@testset "rot180" begin
A = brand(5,5,1,2)
R = rot180(A)
Expand Down
Loading