Skip to content

Commit 40708c9

Browse files
Add Symbolics v7 and ModelingToolkit v11 compatibility
Fixes #563 ## Summary - Add local `Difference` operator since it was removed from Symbolics v7 - Update type annotations from `SymbolicUtils.Symbolic` to `SymbolicUtils.BasicSymbolic` - Update Project.toml compat to support Symbolics v7 and ModelingToolkit v11 ## Changes - `src/difference.jl` (new): Local implementation of the Difference operator that was removed from Symbolics v7, with proper `nameof` method - `src/DataDrivenDiffEq.jl`: Include and export the local Difference operator - `src/basis/utils.jl`: Update type annotations for SymbolicUtils v4 compatibility - `lib/DataDrivenSR/src/DataDrivenSR.jl`: Import Difference from DataDrivenDiffEq - `Project.toml`: Add Symbolics v7 and ModelingToolkit v11 to compat ## Notes There are some test failures unrelated to issue #563 that are due to other API changes in ModelingToolkit v11. These should be addressed separately. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 7cb5d7e commit 40708c9

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ DataInterpolations = "4, 5, 6, 7, 8"
3030
DiffEqBase = "6"
3131
DocStringExtensions = "0.7, 0.8, 0.9"
3232
MLUtils = "0.3, 0.4"
33-
ModelingToolkit = "10"
33+
ModelingToolkit = "10, 11"
3434
OrdinaryDiffEqTsit5 = "1"
3535
Parameters = "0.12"
3636
ProgressMeter = "1.6"
@@ -42,7 +42,7 @@ Setfield = "1"
4242
Statistics = "1"
4343
StatsBase = "0.32.0, 0.33, 0.34"
4444
SymbolicUtils = "2, 3, 4"
45-
Symbolics = "5.30.1, 6"
45+
Symbolics = "5.30.1, 6, 7"
4646
julia = "1.10"
4747

4848
[extras]

lib/DataDrivenSR/src/DataDrivenSR.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module DataDrivenSR
22

33
using DataDrivenDiffEq
44
# Load specific (abstract) types
5-
using DataDrivenDiffEq: AbstractBasis
5+
using DataDrivenDiffEq: AbstractBasis, Difference
66
using DataDrivenDiffEq: AbstractDataDrivenAlgorithm
77
using DataDrivenDiffEq: AbstractDataDrivenResult
88
using DataDrivenDiffEq: AbstractDataDrivenProblem

src/DataDrivenDiffEq.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ using Symbolics: scalarize, variable, value
2121
@reexport using ModelingToolkit: unknowns, parameters, independent_variable, observed,
2222
get_iv, get_observed
2323

24+
# Local Difference operator (removed from Symbolics v7)
25+
include("./difference.jl")
26+
export Difference
27+
2428
using Random
2529
using QuadGK
2630
using Statistics

src/basis/utils.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ function create_linear_independent_eqs(ops::AbstractVector, simplify_eqs::Bool =
106106
return simplify_eqs ? simplify.(Num.(u_o)) : Num.(u_o)
107107
end
108108

109-
function is_dependent(x::SymbolicUtils.Symbolic, y::SymbolicUtils.Symbolic)
109+
function is_dependent(x::SymbolicUtils.BasicSymbolic, y::SymbolicUtils.BasicSymbolic)
110110
occursin(y, x)
111111
end
112112

113-
function is_dependent(x::Any, y::SymbolicUtils.Symbolic)
113+
function is_dependent(x::Any, y::SymbolicUtils.BasicSymbolic)
114114
false
115115
end
116116

117-
function is_dependent(x::SymbolicUtils.Symbolic, y::Any)
117+
function is_dependent(x::SymbolicUtils.BasicSymbolic, y::Any)
118118
false
119119
end
120120

src/difference.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Local Difference operator for discrete-time systems
2+
# This was removed from Symbolics.jl v7, so we define it locally for backwards compatibility
3+
# See: https://github.com/SciML/DataDrivenDiffEq.jl/issues/563
4+
5+
using Symbolics: Operator, value, unwrap, wrap
6+
using SymbolicUtils: term
7+
8+
"""
9+
Difference(t; dt, update=false)
10+
11+
Represents a difference operator for discrete-time systems.
12+
13+
# Fields
14+
15+
- `t`: The independent variable
16+
- `dt`: The time step
17+
- `update`: If true, represents a shift/update operator
18+
19+
# Examples
20+
21+
```julia
22+
@variables t
23+
d = Difference(t; dt = 0.01)
24+
```
25+
"""
26+
struct Difference <: Operator
27+
t
28+
dt
29+
update::Bool
30+
Difference(t; dt, update = false) = new(value(t), dt, update)
31+
end
32+
33+
(D::Difference)(x) = term(D, unwrap(x))
34+
(D::Difference)(x::Num) = wrap(D(unwrap(x)))
35+
36+
# More specific method to avoid ambiguity with SymbolicUtils.Operator method
37+
SymbolicUtils.promote_symtype(::Difference, ::Type{T}) where {T} = T
38+
39+
function Base.show(io::IO, D::Difference)
40+
print(io, "Difference(", D.t, "; dt=", D.dt, ", update=", D.update, ")")
41+
end
42+
Base.nameof(::Difference) = :Difference
43+
44+
function Base.:(==)(D1::Difference, D2::Difference)
45+
isequal(D1.t, D2.t) && isequal(D1.dt, D2.dt) && isequal(D1.update, D2.update)
46+
end
47+
Base.hash(D::Difference, u::UInt) = hash(D.dt, hash(D.t, xor(u, 0x055640d6d952f101)))

0 commit comments

Comments
 (0)