Skip to content
Closed
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
6 changes: 5 additions & 1 deletion src/DataDrivenDiffEq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ using ModelingToolkit: AbstractSystem
using SciMLStructures: SciMLStructures as SS
using SymbolicUtils: operation, arguments, iscall, issym
using Symbolics
using Symbolics: scalarize, variable, value
using Symbolics: scalarize, variable, value, Difference

# Fix for missing nameof method for Difference operator (issue #563)
# This should be upstreamed to Symbolics.jl
Base.nameof(::Difference) = :Difference
@reexport using ModelingToolkit: unknowns, parameters, independent_variable, observed,
get_iv, get_observed

Expand Down
31 changes: 31 additions & 0 deletions test/basis/basis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,34 @@ end
recovered_model2 = ODEProblem(basis2, u0, tspan, params2)
@test recovered_model2 isa ODEProblem
end

@testset "Difference operator display (Issue #563)" begin
# Regression test for issue #563: Error printing basis objects from DMD fits
# The issue was that nameof was not defined for Difference operator
using Symbolics: Difference

@variables t
@variables u(t)[1:2]
u = collect(u)

# Create a basis with Difference operators (as created by DMD algorithms)
iv = DataDrivenDiffEq.get_iv(Basis([u; u .^ 2], u))
d = Difference(iv, dt = 1.0)
eqs = [d(u[1]) ~ 0.5 * u[1] + 0.2 * u[2], d(u[2]) ~ 0.1 * u[1] + 0.8 * u[2]]

basis_with_diff = Basis(eqs, u)

# Test that nameof works for Difference operator
@test nameof(d) == :Difference

# Test that the basis can be displayed without error
# This was throwing "MethodError: no method matching nameof(::Difference)" before the fix
io = IOBuffer()
@test_nowarn print(io, basis_with_diff)
@test_nowarn show(io, MIME"text/plain"(), basis_with_diff)

# Verify the printed output contains expected content
output = String(take!(io))
@test occursin("Difference", output)
@test occursin("equations", output)
end
Loading