@@ -51,40 +51,37 @@ cumsum_size(::NTuple{N,Integer}, A, dims) where N = error("Not implemented")
5151# diff
5252# ###
5353
54- @inline diff (a:: AbstractQuasiArray ; dims:: Integer = 1 ) = diff_layout (MemoryLayout (a), a, dims)
55- function diff_layout (LAY:: ApplyLayout{typeof(*)} , V:: AbstractQuasiVector , dims ... )
54+ @inline diff (a:: AbstractQuasiArray , order ... ; dims:: Integer = 1 ) = diff_layout (MemoryLayout (a), a, order ... ; dims)
55+ function diff_layout (LAY:: ApplyLayout{typeof(*)} , V:: AbstractQuasiVecOrMat , order ... ; dims = 1 )
5656 a = arguments (LAY, V)
57- * (diff (a[1 ]), tail (a)... )
57+ dims == 1 || throw (ArgumentError (" cannot differentiate a vector along dimension $dims " ))
58+ * (diff (a[1 ], order... ), tail (a)... )
5859end
5960
60- function diff_layout (LAY:: ApplyLayout{typeof(*)} , V:: AbstractQuasiMatrix , dims= 1 )
61- a = arguments (LAY, V)
62- @assert dims == 1 # for type stability, for now
63- # if dims == 1
64- * (diff (a[1 ]), tail (a)... )
65- # else
66- # *(front(a)..., diff(a[end]; dims=dims))
67- # end
61+ diff_layout (:: MemoryLayout , A, order... ; dims... ) = diff_size (size (A), A, order... ; dims... )
62+ diff_size (sz, a; dims... ) = error (" diff not implemented for $(typeof (a)) " )
63+ function diff_size (sz, a, order; dims... )
64+ order < 0 && throw (ArgumentError (" order must be non-negative" ))
65+ order == 0 && return a
66+ isone (order) ? diff (a) : diff (diff (a), order- 1 )
6867end
6968
70- diff_layout (:: MemoryLayout , A, dims... ) = diff_size (size (A), A, dims... )
71- diff_size (sz, a, dims... ) = error (" diff not implemented for $(typeof (a)) " )
72-
7369diff (x:: Inclusion ; dims:: Integer = 1 ) = ones (eltype (x), diffaxes (x))
74- diff (c:: AbstractQuasiFill{<:Any,1} ; dims:: Integer = 1 ) = zeros (eltype (c), diffaxes (axes (c,1 )))
75- function diff (c:: AbstractQuasiFill{<:Any,2} ; dims:: Integer = 1 )
70+ diff (x:: Inclusion , order:: Int ; dims:: Integer = 1 ) = fill (ifelse (isone (order), one (eltype (x)), zero (eltype (x))), diffaxes (x,order))
71+ diff (c:: AbstractQuasiFill{<:Any,1} , order... ; dims:: Integer = 1 ) = zeros (eltype (c), diffaxes (axes (c,1 ),order... ))
72+ function diff (c:: AbstractQuasiFill{<:Any,2} , order... ; dims:: Integer = 1 )
7673 a,b = axes (c)
7774 if dims == 1
78- zeros (eltype (c), diffaxes (a), b)
75+ zeros (eltype (c), diffaxes (a, order ... ), b)
7976 else
80- zeros (eltype (c), a, diffaxes (b))
77+ zeros (eltype (c), a, diffaxes (b, order ... ))
8178 end
8279end
8380
8481
85- diffaxes (a:: Inclusion{<:Any,<:AbstractVector} ) = Inclusion (a. domain[1 : end - 1 ])
86- diffaxes (a:: OneTo ) = oneto (length (a)- 1 )
87- diffaxes (a) = a # default is differentiation does not change axes
82+ diffaxes (a:: Inclusion{<:Any,<:AbstractVector} , order = 1 ) = Inclusion (a. domain[1 : end - order ])
83+ diffaxes (a:: OneTo , order = 1 ) = oneto (length (a)- order )
84+ diffaxes (a, order ... ) = a # default is differentiation does not change axes
8885
8986diff (b:: QuasiVector ; dims:: Integer = 1 ) = QuasiVector (diff (b. parent) ./ diff (b. axes[1 ]), (diffaxes (axes (b,1 )),))
9087function diff (A:: QuasiMatrix ; dims:: Integer = 1 )
0 commit comments