Skip to content

Commit b8573fa

Browse files
Merge pull request #16 from SciML/ap/similar
feat: implement `similar`
2 parents 3e4c2a9 + b847003 commit b8573fa

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "FastAlmostBandedMatrices"
22
uuid = "9d29842c-ecb8-4973-b1e9-a27b1157504e"
33
authors = ["Avik Pal"]
4-
version = "0.1.3"
4+
version = "0.1.4"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/FastAlmostBandedMatrices.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ end
126126
@inline Base.eltype(::AlmostBandedMatrix{T}) where {T} = T
127127
@inline Base.IndexStyle(::Type{<:AlmostBandedMatrix}) = IndexCartesian()
128128

129+
# If dims is provided we will construct a Matrix but other invokations should return a
130+
# AlmostBandedMatrix
131+
function Base.similar(A::AlmostBandedMatrix, ::Type{T}) where {T}
132+
bands = similar(A.bands, T)
133+
fill = similar(A.fill, T)
134+
return AlmostBandedMatrix{T}(bands, fill)
135+
end
136+
129137
@inline function colsupport(::AbstractAlmostBandedLayout, A, j)
130138
l, u = almostbandwidths(A)
131139
if j l + u

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ using SafeTestsets, Test
1717
@test almostbandedrank(A) == 2
1818
end
1919

20+
@safetestset "similar" begin
21+
using FastAlmostBandedMatrices
22+
23+
A = AlmostBandedMatrix(brand(Float64, 10, 10, 2, 1), rand(Float64, 2, 10))
24+
25+
@test similar(A) isa AlmostBandedMatrix
26+
@test similar(A, Float32) isa AlmostBandedMatrix{Float32}
27+
28+
fallback = similar(A, Float32, 10, 10)
29+
@test fallback isa Matrix{Float32}
30+
@test size(fallback) == size(A)
31+
end
32+
2033
@safetestset "Copy" begin
2134
using FastAlmostBandedMatrices
2235

0 commit comments

Comments
 (0)