-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathequations.jl
70 lines (55 loc) · 1.74 KB
/
equations.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
$(TYPEDEF)
An equality relationship between two expressions.
# Fields
$(FIELDS)
"""
struct Equation
"""The expression on the left-hand side of the equation."""
lhs
"""The expression on the right-hand side of the equation."""
rhs
end
Base.:(==)(a::Equation, b::Equation) = all(isequal.((a.lhs, a.rhs), (b.lhs, b.rhs)))
Base.hash(a::Equation, salt::UInt) = hash(a.lhs, hash(a.rhs, salt))
Base.show(io::IO, eq::Equation) = print(io, eq.lhs, " ~ ", eq.rhs)
SymbolicUtils.simplify(x::Equation; kw...) = simplify(x.lhs; kw...) ~ simplify(x.rhs; kw...)
"""
$(TYPEDSIGNATURES)
Create an [`Equation`](@ref) out of two [`Num`](@ref) instances, or an
`Num` and a `Number`.
# Examples
```jldoctest
julia> using ModelingToolkit
julia> @variables x y;
julia> x ~ y
Equation(x(), y())
julia> x - y ~ 0
Equation(x() - y(), 0)
```
"""
Base.:~(lhs::Num, rhs::Num) = Equation(value(lhs), value(rhs))
Base.:~(lhs::Num, rhs::Number ) = Equation(value(lhs), value(rhs))
Base.:~(lhs::Number , rhs::Num) = Equation(value(lhs), value(rhs))
Base.:~(lhs::Symbolic, rhs::Symbolic) = Equation(value(lhs), value(rhs))
Base.:~(lhs::Symbolic, rhs::Any ) = Equation(value(lhs), value(rhs))
Base.:~(lhs::Any, rhs::Symbolic ) = Equation(value(lhs), value(rhs))
struct ConstrainedEquation
constraints
eq
end
function _eq_unordered(a, b)
length(a) === length(b) || return false
n = length(a)
idxs = Set(1:n)
for x ∈ a
idx = findfirst(isequal(x), b)
idx === nothing && return false
idx ∈ idxs || return false
delete!(idxs, idx)
end
return true
end
function expand_derivatives(eq::Equation, simplify=false)
return Equation(expand_derivatives(eq.lhs, simplify), expand_derivatives(eq.rhs, simplify))
end