Skip to content

Commit 21172fc

Browse files
committed
Add diff(::AbstractFill)
1 parent 41d3e16 commit 21172fc

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/FillArrays.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ module FillArrays
22

33
using LinearAlgebra, SparseArrays
44
import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
5-
+, -, *, /, sum, cumsum, maximum, minimum, sort, sort!,
5+
+, -, *, /, \, diff, sum, cumsum, maximum, minimum, sort, sort!,
66
any, all, axes, isone, iterate, unique, allunique, permutedims, inv
77

8-
import Base.\
9-
108
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag
119

1210
import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
@@ -379,6 +377,13 @@ cumsum(x::Ones{II}) where II<:Integer = Base.OneTo{II}(length(x))
379377
cumsum(x::Ones{Bool}) = Base.OneTo{Int}(length(x))
380378
cumsum(x::AbstractFill{Bool}) = cumsum(convert(AbstractFill{Int}, x))
381379

380+
381+
#########
382+
# Diff
383+
#########
384+
385+
diff(x::AbstractFill{T,1}) where T = Zeros{T}(length(x)-1)
386+
382387
#########
383388
# unique
384389
#########

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ end
393393
@test sort(Ones(5)) == sort!(Ones(5))
394394
end
395395

396-
@testset "Cumsum" begin
396+
@testset "Cumsum and diff" begin
397397
@test sum(Fill(3,10)) 30
398398
@test cumsum(Fill(3,10)) 3:3:30
399399

@@ -412,6 +412,9 @@ end
412412
@test cumsum(Zeros{Bool}(10)) Zeros{Bool}(10)
413413
@test cumsum(Ones{Bool}(10)) Base.OneTo{Int}(10)
414414
@test cumsum(Fill(true,10)) 1:1:10
415+
416+
@test diff(Fill(1,10)) Zeros{Int}(9)
417+
@test diff(Ones{Float64}(10)) Zeros{Float64}(9)
415418
end
416419

417420
@testset "Broadcast" begin

0 commit comments

Comments
 (0)