@@ -293,7 +293,62 @@ downstream packages like `ModelingToolkit.jl`, hence the need for this separate
293293function.
294294"""
295295function isequal_with_metadata (a:: BasicSymbolic , b:: BasicSymbolic ):: Bool
296- isequal (a, b) && isequal (metadata (a), metadata (b))
296+ isequal (a, b) && isequal_with_metadata (metadata (a), metadata (b))
297+ end
298+
299+ """
300+ $(TYPEDSIGNATURES)
301+
302+ Compare the metadata of two `BasicSymbolic`s to ensure it is equal, recursively calling
303+ `isequal_with_metadata` to ensure symbolic variables in the metadata also have equal
304+ metadata.
305+ """
306+ function isequal_with_metadata (a:: Union{AbstractDict, NamedTuple} , b:: Union{AbstractDict, NamedTuple} )
307+ typeof (a) == typeof (b) || return false
308+ length (a) == length (b) || return false
309+
310+ for (k, v) in pairs (a)
311+ haskey (b, k) || return false
312+ isequal_with_metadata (v, b[k]) || return false
313+ end
314+
315+ for (k, v) in pairs (b)
316+ haskey (a, k) || return false
317+ isequal_with_metadata (v, a[k]) || return false
318+ end
319+
320+ return true
321+ end
322+
323+ """
324+ $(TYPEDSIGNATURES)
325+
326+ Fallback method which uses `isequal`.
327+ """
328+ isequal_with_metadata (a, b) = isequal (a, b)
329+
330+ """
331+ $(TYPEDSIGNATURES)
332+
333+ Specialized methods to check if two ranges are equal without comparing each element.
334+ """
335+ isequal_with_metadata (a:: AbstractRange , b:: AbstractRange ) = isequal (a, b)
336+
337+ """
338+ $(TYPEDSIGNATURES)
339+
340+ Check if two arrays/tuples are equal by calling `isequal_with_metadata` on each element.
341+ This is to ensure true equality of any symbolic elements, if present.
342+ """
343+ function isequal_with_metadata (a:: Union{AbstractArray, Tuple} , b:: Union{AbstractArray, Tuple} )
344+ typeof (a) == typeof (b) || return false
345+ if a isa AbstractArray
346+ size (a) == size (b) || return false
347+ end # otherwise they're tuples and type equality also checks length equality
348+ for (x, y) in zip (a, b)
349+ isequal_with_metadata (x, y) || return false
350+ end
351+ return true
297352end
298353
299354Base. one ( s:: Symbolic ) = one ( symtype (s))
0 commit comments