Skip to content

Support higher order diff #117

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

Merged
merged 5 commits into from
Jan 28, 2025
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,7 +1,7 @@
name = "QuasiArrays"
uuid = "c4ea9172-b204-11e9-377d-29865faadc5c"
authors = ["Sheehan Olver <[email protected]>"]
version = "0.11.9"
version = "0.12"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
2 changes: 1 addition & 1 deletion src/QuasiArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Base, LinearAlgebra, LazyArrays, ArrayLayouts, DomainSets, FillArrays, Sta
import Base: getindex, size, axes, axes1, length, ==, isequal, iterate, CartesianIndices, LinearIndices,
Indices, IndexStyle, getindex, setindex!, parent, vec, convert, similar, copy, copyto!, zero,
map, eachindex, eltype, first, last, firstindex, lastindex, in, reshape, permutedims, all,
isreal, iszero, isempty, empty, isapprox, fill!, getproperty, showarg
isreal, iszero, isone, isempty, empty, isapprox, fill!, getproperty, showarg
import Base: @_inline_meta, DimOrInd, OneTo, @_propagate_inbounds_meta, @_noinline_meta,
DimsInteger, error_if_canonical_getindex, @propagate_inbounds, _return_type,
safe_tail, front, tail, _getindex, _maybe_reshape, index_ndims, _unsafe_getindex,
Expand Down
39 changes: 18 additions & 21 deletions src/calculus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,37 @@
# diff
####

@inline diff(a::AbstractQuasiArray; dims::Integer=1) = diff_layout(MemoryLayout(a), a, dims)
function diff_layout(LAY::ApplyLayout{typeof(*)}, V::AbstractQuasiVector, dims...)
@inline diff(a::AbstractQuasiArray, order...; dims::Integer=1) = diff_layout(MemoryLayout(a), a, order...; dims)
function diff_layout(LAY::ApplyLayout{typeof(*)}, V::AbstractQuasiVecOrMat, order...; dims=1)

Check warning on line 55 in src/calculus.jl

View check run for this annotation

Codecov / codecov/patch

src/calculus.jl#L54-L55

Added lines #L54 - L55 were not covered by tests
a = arguments(LAY, V)
*(diff(a[1]), tail(a)...)
dims == 1 || throw(ArgumentError("cannot differentiate a vector along dimension $dims"))
*(diff(a[1], order...), tail(a)...)

Check warning on line 58 in src/calculus.jl

View check run for this annotation

Codecov / codecov/patch

src/calculus.jl#L57-L58

Added lines #L57 - L58 were not covered by tests
end

function diff_layout(LAY::ApplyLayout{typeof(*)}, V::AbstractQuasiMatrix, dims=1)
a = arguments(LAY, V)
@assert dims == 1 #for type stability, for now
# if dims == 1
*(diff(a[1]), tail(a)...)
# else
# *(front(a)..., diff(a[end]; dims=dims))
# end
diff_layout(::MemoryLayout, A, order...; dims...) = diff_size(size(A), A, order...; dims...)
diff_size(sz, a; dims...) = error("diff not implemented for $(typeof(a))")
function diff_size(sz, a, order; dims...)
order < 0 && throw(ArgumentError("order must be non-negative"))
order == 0 && return a
isone(order) ? diff(a) : diff(diff(a), order-1)

Check warning on line 66 in src/calculus.jl

View check run for this annotation

Codecov / codecov/patch

src/calculus.jl#L61-L66

Added lines #L61 - L66 were not covered by tests
end

diff_layout(::MemoryLayout, A, dims...) = diff_size(size(A), A, dims...)
diff_size(sz, a, dims...) = error("diff not implemented for $(typeof(a))")

diff(x::Inclusion; dims::Integer=1) = ones(eltype(x), diffaxes(x))
diff(c::AbstractQuasiFill{<:Any,1}; dims::Integer=1) = zeros(eltype(c), diffaxes(axes(c,1)))
function diff(c::AbstractQuasiFill{<:Any,2}; dims::Integer=1)
diff(x::Inclusion, order::Int; dims::Integer=1) = fill(ifelse(isone(order), one(eltype(x)), zero(eltype(x))), diffaxes(x,order))
diff(c::AbstractQuasiFill{<:Any,1}, order...; dims::Integer=1) = zeros(eltype(c), diffaxes(axes(c,1),order...))
function diff(c::AbstractQuasiFill{<:Any,2}, order...; dims::Integer=1)

Check warning on line 72 in src/calculus.jl

View check run for this annotation

Codecov / codecov/patch

src/calculus.jl#L70-L72

Added lines #L70 - L72 were not covered by tests
a,b = axes(c)
if dims == 1
zeros(eltype(c), diffaxes(a), b)
zeros(eltype(c), diffaxes(a, order...), b)

Check warning on line 75 in src/calculus.jl

View check run for this annotation

Codecov / codecov/patch

src/calculus.jl#L75

Added line #L75 was not covered by tests
else
zeros(eltype(c), a, diffaxes(b))
zeros(eltype(c), a, diffaxes(b, order...))

Check warning on line 77 in src/calculus.jl

View check run for this annotation

Codecov / codecov/patch

src/calculus.jl#L77

Added line #L77 was not covered by tests
end
end


diffaxes(a::Inclusion{<:Any,<:AbstractVector}) = Inclusion(a.domain[1:end-1])
diffaxes(a::OneTo) = oneto(length(a)-1)
diffaxes(a) = a # default is differentiation does not change axes
diffaxes(a::Inclusion{<:Any,<:AbstractVector}, order=1) = Inclusion(a.domain[1:end-order])
diffaxes(a::OneTo, order=1) = oneto(length(a)-order)
diffaxes(a, order...) = a # default is differentiation does not change axes

Check warning on line 84 in src/calculus.jl

View check run for this annotation

Codecov / codecov/patch

src/calculus.jl#L82-L84

Added lines #L82 - L84 were not covered by tests

diff(b::QuasiVector; dims::Integer=1) = QuasiVector(diff(b.parent) ./ diff(b.axes[1]), (diffaxes(axes(b,1)),))
function diff(A::QuasiMatrix; dims::Integer=1)
Expand Down
12 changes: 6 additions & 6 deletions src/quasibroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@
# support (A .* B) * y
_broadcasted_mul(a::Tuple{Number,Vararg{Any}}, b::AbstractQuasiVector) = (first(a)*sum(b), _broadcasted_mul(tail(a), b)...)
_broadcasted_mul(a::Tuple{Number,Vararg{Any}}, B::AbstractQuasiMatrix) = (first(a)*sum(B; dims=1), _broadcasted_mul(tail(a), B)...)
_broadcasted_mul(a::Tuple{AbstractQuasiVector,Vararg{Any}}, b::AbstractQuasiVector) = (first(a)*sum(b), _broadcasted_mul(tail(a), b)...)
_broadcasted_mul(a::Tuple{AbstractQuasiVector,Vararg{Any}}, B::AbstractQuasiMatrix) = (first(a)*sum(B; dims=1), _broadcasted_mul(tail(a), B)...)
_broadcasted_mul(A::Tuple{AbstractQuasiMatrix,Vararg{Any}}, b::AbstractQuasiVector) = (axes(first(A),2) == Base.OneTo(1) ? first(A)*sum(b) : (first(A)*b), _broadcasted_mul(tail(A), b)...)
_broadcasted_mul(A::Tuple{AbstractQuasiMatrix,Vararg{Any}}, B::AbstractQuasiMatrix) = (axes(first(A),2) == Base.OneTo(1) ? first(A)*sum(B; dims=1) : (first(A)*B), _broadcasted_mul(tail(A), B)...)
_broadcasted_mul(a::Tuple{AbstractQuasiVector,Vararg{Any}}, b::AbstractQuasiOrVector) = (first(a)*sum(b), _broadcasted_mul(tail(a), b)...)
_broadcasted_mul(a::Tuple{AbstractQuasiVector,Vararg{Any}}, B::AbstractQuasiOrMatrix) = (first(a)*sum(B; dims=1), _broadcasted_mul(tail(a), B)...)
_broadcasted_mul(A::Tuple{AbstractQuasiMatrix,Vararg{Any}}, b::AbstractQuasiOrVector) = (axes(first(A),2) == Base.OneTo(1) ? first(A)*sum(b) : (first(A)*b), _broadcasted_mul(tail(A), b)...)
_broadcasted_mul(A::Tuple{AbstractQuasiMatrix,Vararg{Any}}, B::AbstractQuasiOrMatrix) = (axes(first(A),2) == Base.OneTo(1) ? first(A)*sum(B; dims=1) : (first(A)*B), _broadcasted_mul(tail(A), B)...)

Check warning on line 192 in src/quasibroadcast.jl

View check run for this annotation

Codecov / codecov/patch

src/quasibroadcast.jl#L189-L192

Added lines #L189 - L192 were not covered by tests
_broadcasted_mul(A::AbstractQuasiMatrix, b::Tuple{Number,Vararg{Any}}) = (sum(A; dims=2)*first(b)[1], _broadcasted_mul(A, tail(b))...)
_broadcasted_mul(A::AbstractQuasiMatrix, b::Tuple{Union{AbstractVector,AbstractQuasiVector},Vararg{Any}}) = (size(first(b),1) == 1 ? (sum(A; dims=2)*first(b)[1]) : (A*first(b)), _broadcasted_mul(A, tail(b))...)
_broadcasted_mul(A::AbstractQuasiMatrix, B::Tuple{Union{AbstractMatrix,AbstractQuasiMatrix},Vararg{Any}}) = (size(first(B),1) == 1 ? (sum(A; dims=2) * first(B)) : (A * first(B)), _broadcasted_mul(A, tail(B))...)
_broadcasted_mul(A::AbstractQuasiMatrix, b::Tuple{AbstractQuasiOrVector,Vararg{Any}}) = (size(first(b),1) == 1 ? (sum(A; dims=2)*first(b)[1]) : (A*first(b)), _broadcasted_mul(A, tail(b))...)
_broadcasted_mul(A::AbstractQuasiMatrix, B::Tuple{AbstractQuasiOrMatrix,Vararg{Any}}) = (size(first(B),1) == 1 ? (sum(A; dims=2) * first(B)) : (A * first(B)), _broadcasted_mul(A, tail(B))...)

Check warning on line 195 in src/quasibroadcast.jl

View check run for this annotation

Codecov / codecov/patch

src/quasibroadcast.jl#L194-L195

Added lines #L194 - L195 were not covered by tests



Expand Down
22 changes: 18 additions & 4 deletions test/test_calculus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,25 @@ using QuasiArrays, IntervalSets, Test

@testset "Diff" begin
x = range(0, 1; length=10_000)
@test diff(Inclusion(x)) == ones(Inclusion(x[1:end-1]))
@test diff(ones(Inclusion(x))) == zeros(Inclusion(x[1:end-1]))
@test diff(Inclusion(x)) == diff(Inclusion(x),1) == ones(Inclusion(x[1:end-1]))
@test diff(Inclusion(x),2) == diff(diff(Inclusion(x))) == zeros(Inclusion(x[1:end-2]))
@test diff(ones(Inclusion(x))) == diff(ones(Inclusion(x)),1) == zeros(Inclusion(x[1:end-1]))
@test diff(ones(Inclusion(x)),2) == diff(diff(ones(Inclusion(x)))) == zeros(Inclusion(x[1:end-2]))

@test diff(ones(Inclusion(x), Inclusion(x))) == zeros(Inclusion(x[1:end-1]), Inclusion(x))
@test diff(ones(Inclusion(x), Inclusion(x)), 2) == zeros(Inclusion(x[1:end-2]), Inclusion(x))
@test diff(ones(Inclusion(x), Inclusion(x)); dims=2) == zeros(Inclusion(x), Inclusion(x[1:end-1]))
@test diff(ones(Inclusion(x), Inclusion(x)), 2; dims=2) == zeros(Inclusion(x), Inclusion(x[1:end-2]))

b = QuasiVector(exp.(x), x)

@test diff(b) ≈ b[Inclusion(x[1:end-1])] atol=1E-2
@test diff(b,2) ≈ b[Inclusion(x[1:end-2])] atol=1E-1


A = QuasiArray(randn(3,2), (1:0.5:2,0:0.5:0.5))
@test diff(A; dims=1)[:,0] == diff(A[:,0])
@test diff(A,2; dims=1)[:,0] == diff(diff(A[:,0]))
@test diff(A; dims=2)[1,:] == diff(A[1,:])

@testset "* diff" begin
Expand All @@ -57,10 +63,18 @@ using QuasiArrays, IntervalSets, Test

@testset "Interval" begin
@test diff(Inclusion(0.0..1)) ≡ ones(Inclusion(0.0..1))
@test diff(ones(Inclusion(0.0..1))) ≡ zeros(Inclusion(0.0..1))
@test diff(ones(Inclusion(0.0..1), Base.OneTo(3))) ≡ zeros(Inclusion(0.0..1), Base.OneTo(3))
@test diff(Inclusion(0.0..1),1) ≡ fill(1.0,Inclusion(0.0..1))
@test diff(Inclusion(0.0..1),2) ≡ fill(0.0,Inclusion(0.0..1))
@test diff(ones(Inclusion(0.0..1))) ≡ diff(ones(Inclusion(0.0..1)),1) ≡ diff(ones(Inclusion(0.0..1)),2) ≡ zeros(Inclusion(0.0..1))
@test diff(ones(Inclusion(0.0..1), Base.OneTo(3))) ≡ diff(ones(Inclusion(0.0..1), Base.OneTo(3)),2) ≡ zeros(Inclusion(0.0..1), Base.OneTo(3))
@test diff(ones(Inclusion(0.0..1), Base.OneTo(3)); dims=2) ≡ zeros(Inclusion(0.0..1), Base.OneTo(2))
@test diff(ones(Base.OneTo(3), Inclusion(0.0..1))) ≡ zeros(Base.OneTo(2), Inclusion(0.0..1))
@test diff(ones(Base.OneTo(3), Inclusion(0.0..1)); dims=2) ≡ zeros(Base.OneTo(3), Inclusion(0.0..1))
end

@testset "Incomplete" begin
struct IncompleteQuasiArray <: AbstractQuasiVector{Int} end
Base.axes(::IncompleteQuasiArray) = (Base.OneTo(3),)
@test_throws ErrorException diff(IncompleteQuasiArray())
end
end
Loading