Skip to content

Conversation

@ChrisRackauckas-Claude
Copy link

Summary

Changes

  • src/difference.jl (new): Local implementation of the Difference operator that was removed from Symbolics v7. Includes proper nameof method to fix the display error.
  • src/DataDrivenDiffEq.jl: Include and export the local Difference operator
  • src/basis/utils.jl: Update type annotations from SymbolicUtils.Symbolic to SymbolicUtils.BasicSymbolic for SymbolicUtils v4 compatibility
  • lib/DataDrivenSR/src/DataDrivenSR.jl: Import Difference from DataDrivenDiffEq
  • Project.toml: Add Symbolics v7 and ModelingToolkit v11 to compat

Background

The Difference operator was removed from Symbolics.jl in v7.0.0 (commit cdbae19352). This caused issue #563 where displaying Basis objects containing Difference operators (as produced by DMD algorithms) would fail with MethodError: no method matching nameof(::Difference).

Rather than using type piracy by extending Base.nameof for a type from another package, this PR defines a local Difference operator in DataDrivenDiffEq with the proper methods.

Test plan

  • Verified the original issue Error printing basis objects from DMD fits #563 scenario works - basis with Difference operators can be displayed
  • Package compiles successfully with Symbolics v7 and MTK v11
  • CI tests (note: some unrelated test failures exist due to other MTK v11 API changes)

🤖 Generated with Claude Code

ChrisRackauckas and others added 3 commits December 15, 2025 07:50
Fixes SciML#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 SciML#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 <noreply@anthropic.com>
Remove backwards compatibility with older versions since the local
Difference operator is designed for the new API.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit fixes multiple API changes introduced in Symbolics v7 and SymbolicUtils v4:

1. SymbolicUtils.Symbolic → SymbolicUtils.BasicSymbolic
2. Handle Const type in count_operation (constants are now wrapped in Const)
3. Fix remove_constant_factor to use isconst() instead of isa(x, Number)
4. Fix is_dependent to use get_variables() instead of removed occursin()
5. Fix __assert_linearity to handle get_variables returning Set instead of Vector
6. Fix get_parameter_values/get_parameter_map for Symbolics v7 metadata changes
7. Update test files to handle API changes:
   - Use collect(reduce(union, ...)) for get_variables Sets
   - Replace @mtkmodel with manual System definition to avoid SafeTestsets issues
   - Update expected output order in linear_independent basis test

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@ChrisRackauckas-Claude
Copy link
Author

Update: Additional fixes for Symbolics v7 / SymbolicUtils v4 compatibility

This commit fixes multiple API changes introduced in Symbolics v7 and SymbolicUtils v4. All 260 tests now pass.

Changes made:

  1. src/basis/utils.jl:

    • Added _is_constant() helper to detect both Number and SymbolicUtils.Const types
    • Updated count_operation() to handle Const type by checking !iscall(x) before calling operation()
    • Updated remove_constant_factor() to use _is_constant() instead of isa(x, Number)
    • Updated is_dependent() to use Symbolics.get_variables() instead of the removed occursin()
  2. src/utils/build_basis.jl:

    • Updated __assert_linearity() to handle get_variables() returning Set instead of Vector
    • Use reduce(union, ...) to flatten Sets
  3. src/basis/type.jl:

    • Updated get_parameter_values() and get_parameter_map() to use try-catch with getdefaultval() instead of checking hasmetadata() (which no longer works correctly for default values in Symbolics v7)
  4. Test file updates:

    • test/basis/implicit_basis.jl: Updated to handle get_variables() returning Sets
    • test/basis/basis.jl: Updated expected output order for linear_independent basis (internal ordering changed)
    • test/problem/problem.jl: Replaced @mtkmodel macro with manual System definition to avoid SafeTestsets module isolation issues

Key API changes handled:

Old (Symbolics v6 / SymbolicUtils v3) New (Symbolics v7 / SymbolicUtils v4)
SymbolicUtils.Symbolic SymbolicUtils.BasicSymbolic
isa(x, Number) for constants SymbolicUtils.isconst(x)
occursin(y, x) for dependency y in Symbolics.get_variables(x)
get_variables() returns Vector get_variables() returns Set
hasmetadata(p, VariableDefaultValue) try getdefaultval(p) catch

@ChrisRackauckas ChrisRackauckas merged commit f0e6dc3 into SciML:master Dec 16, 2025
12 of 34 checks passed
Comment on lines +8 to +25
"""
Difference(t; dt, update=false)

Represents a difference operator for discrete-time systems.

# Fields

- `t`: The independent variable
- `dt`: The time step
- `update`: If true, represents a shift/update operator

# Examples

```julia
@variables t
d = Difference(t; dt = 0.01)
```
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AayushSabharwal is this the right approach here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Difference was MTK's old way of handling discrete systems. That doesn't work anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisRackauckas have you already released this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It doesn't work anymore for MTK but this package needs a representation of it in order to not break.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it not use the new MTK discrete system interface?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't even use MTK at all, why would it start?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error printing basis objects from DMD fits

4 participants