Skip to content

Fix incorrect code generated for Symbolics.build_function(Symbolics.derivative(mod(x), x)) #1123 #1514

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Distributions = "0.25"
DocStringExtensions = "0.9"
DomainSets = "0.6, 0.7"
DynamicPolynomials = "0.5, 0.6"
FiniteDiff = "2"
ForwardDiff = "0.10.36"
Groebner = "0.8.2, 0.9"
LaTeXStrings = "1.3"
Expand Down Expand Up @@ -96,10 +97,12 @@ SymbolicUtils = "3.24"
TermInterface = "2"
julia = "1.10"


[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Groebner = "0b43b601-686d-58a3-8a1c-6623616c7cd4"
LambertW = "984bce1d-4616-540c-a9ee-88d1112d94c9"
Expand All @@ -113,4 +116,4 @@ SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "SafeTestsets", "Pkg", "PkgBenchmark", "PreallocationTools", "ForwardDiff", "Groebner", "BenchmarkTools", "ReferenceTests", "Random", "LambertW", "Lux", "ComponentArrays", "Nemo", "DynamicQuantities"]
test = ["Test", "SafeTestsets", "Pkg", "PkgBenchmark", "PreallocationTools", "ForwardDiff", "Groebner", "BenchmarkTools", "ReferenceTests", "Random", "LambertW", "Lux", "ComponentArrays", "Nemo", "DynamicQuantities","FiniteDiff"]
3 changes: 3 additions & 0 deletions src/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ for (modu, fun, arity) ∈ DiffRules.diffrules(; filter_modules=(:Base, :Special
end
end

derivative(::typeof(mod), args::NTuple{2, Any}, ::Val{1}) = ifelse(mod(args...) == 0, NaN, 1)
derivative(::typeof(mod), args::NTuple{2, Any}, ::Val{2}) = ifelse(mod(args...) == 0, NaN, -floor(args[1] / args[2]))

derivative(::typeof(+), args::NTuple{N,Any}, ::Val) where {N} = 1
derivative(::typeof(*), args::NTuple{N,Any}, ::Val{i}) where {N,i} = *(deleteat!(collect(args), i)...)
derivative(::typeof(one), args::Tuple{<:Any}, ::Val) = 0
Expand Down
12 changes: 12 additions & 0 deletions test/diff.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Symbolics
using Test
using Symbolics: value
using FiniteDiff

# Derivatives
@variables t σ ρ β
Expand Down Expand Up @@ -604,3 +605,14 @@ let
@test isequal(expand_derivatives(Dt(y[1])), Dt(x) * Dx(y[1])) # same for vector var
@test isequal(expand_derivatives(Dt(Y[1,1])), Dt(x) * Dx(Y[1,1])) # same for matrix arr
end

@testset "Derivative of mod function" begin
f(x, y) = mod(x, y)

@test isnan(derivative(mod, (4, 2), Val(1)))
@test derivative(mod, (5, 2), Val(1)) == FiniteDiff.finite_difference_derivative(x -> f(x, 2), 5)


@test isnan(derivative(mod, (4, 2), Val(2)))
@test derivative(mod, (5, 2), Val(2)) == FiniteDiff.finite_difference_derivative(y -> f(5, y), 2)
end