Skip to content

Commit 8119ad5

Browse files
committed
Remove Operation, Constant, and Expression
1 parent e38827e commit 8119ad5

21 files changed

+55
-111
lines changed

src/ModelingToolkit.jl

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@ import TreeViews
3131

3232
using Requires
3333

34-
"""
35-
$(TYPEDEF)
36-
37-
Base type for a symbolic expression.
38-
"""
39-
abstract type Expression <: Number end
40-
41-
42-
export Num
34+
export Num, Variable
4335
"""
4436
$(TYPEDEF)
4537
@@ -141,14 +133,6 @@ TODO
141133
abstract type AbstractSystem end
142134
abstract type AbstractODESystem <: AbstractSystem end
143135

144-
Base.promote_rule(::Type{<:Number},::Type{<:Expression}) = Expression
145-
Base.zero(::Type{<:Expression}) = Constant(0)
146-
Base.zero(::Expression) = Constant(0)
147-
Base.one(::Type{<:Expression}) = Constant(1)
148-
Base.one(::Expression) = Constant(1)
149-
Base.oneunit(::Expression) = Constant(1)
150-
Base.oneunit(::Type{<:Expression}) = Constant(1)
151-
152136
"""
153137
$(TYPEDSIGNATURES)
154138
@@ -172,7 +156,6 @@ function parameters end
172156

173157
include("variables.jl")
174158
include("context_dsl.jl")
175-
include("operations.jl")
176159
include("differentials.jl")
177160

178161
include("equations.jl")
@@ -228,7 +211,7 @@ export Reaction, ReactionSystem, ismassaction, oderatelaw, jumpratelaw
228211
export Differential, expand_derivatives, @derivatives
229212
export IntervalDomain, ProductDomain, , CircleDomain
230213
export Equation, ConstrainedEquation
231-
export Term, Sym, Operation, Expression, Variable
214+
export Term, Sym
232215
export independent_variable, states, parameters, equations, controls, pins, observed
233216

234217
export calculate_jacobian, generate_jacobian, generate_function

src/build_function.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct DaggerForm <: ParallelForm end
1313
"""
1414
`build_function`
1515
16-
Generates a numerically-usable function from a ModelingToolkit `Expression`.
16+
Generates a numerically-usable function from a ModelingToolkit `Num`.
1717
1818
```julia
1919
build_function(ex, args...;
@@ -24,7 +24,7 @@ build_function(ex, args...;
2424
2525
Arguments:
2626
27-
- `ex`: The `Expression` to compile
27+
- `ex`: The `Num` to compile
2828
- `args`: The arguments of the function
2929
- `expression`: Whether to generate code or whether to generate the compiled form.
3030
By default, `expression = Val{true}`, which means that the code for the
@@ -176,7 +176,7 @@ Generates a Julia function which can then be utilized for further evaluations.
176176
If expression=Val{false}, the return is a Julia function which utilizes
177177
RuntimeGeneratedFunctions.jl in order to be free of world-age issues.
178178
179-
If the `Expression` is an `Operation`, the generated function is a function
179+
If the `Num` is an `Operation`, the generated function is a function
180180
with a scalar output, otherwise if it's an `AbstractArray{Operation}`, the output
181181
is two functions, one for out-of-place AbstractArray output and a second which
182182
is a mutating function. The outputted functions match the given argument order,

src/differentials.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function expand_derivatives(O::Term, simplify=true; occurances=nothing)
7171
end
7272

7373
_isfalse(occurances) && return 0
74-
occurances isa Bool && return 1 # means it's a Constant(true)
74+
occurances isa Bool && return 1 # means it's a `true`
7575

7676
(D, o) = (O.op, arg)
7777

src/direct.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
```julia
3-
gradient(O::Expression, vars::AbstractVector; simplify = true)
3+
gradient(O, vars::AbstractVector; simplify = true)
44
```
55
66
A helper function for computing the gradient of an expression with respect to
@@ -81,7 +81,7 @@ end
8181

8282
"""
8383
```julia
84-
hessian(O::Expression, vars::AbstractVector; simplify = true)
84+
hessian(O, vars::AbstractVector; simplify = true)
8585
```
8686
8787
A helper function for computing the Hessian of an expression with respect to
@@ -105,7 +105,7 @@ isidx(x) = x isa TermCombination
105105

106106
"""
107107
```julia
108-
hessian_sparsity(ops::AbstractVector{<:Expression}, vars::AbstractVector{<:Expression})
108+
hessian_sparsity(ops::AbstractVector, vars::AbstractVector)
109109
```
110110
111111
Return the sparsity pattern of the Hessian of an array of expressions with respect to
@@ -162,7 +162,7 @@ end
162162

163163
"""
164164
```julia
165-
islinear(ex::Expression, u)
165+
islinear(ex, u)
166166
```
167167
Check if an expression is linear with respect to a list of variable expressions.
168168
"""
@@ -172,7 +172,7 @@ end
172172

173173
"""
174174
```julia
175-
sparsehessian(O::Expression, vars::AbstractVector; simplify = true)
175+
sparsehessian(O, vars::AbstractVector; simplify = true)
176176
```
177177
178178
A helper function for computing the sparse Hessian of an expression with respect to
@@ -211,7 +211,7 @@ function toexpr(O::Term)
211211
return Expr(:call, toexpr(O.op), toexpr.(O.args)...)
212212
end
213213
if O.op === (^)
214-
if length(O.args) > 1 && O.args[2] isa Constant && O.args[2].value < 0
214+
if length(O.args) > 1 && O.args[2] isa Number && O.args[2] < 0
215215
return Expr(:call, :^, Expr(:call, :inv, toexpr(O.args[1])), -(O.args[2].value))
216216
end
217217
end

src/equations.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ SymbolicUtils.simplify(x::Equation; kw...) = simplify(x.lhs; kw...) ~ simplify(x
2020
"""
2121
$(TYPEDSIGNATURES)
2222
23-
Create an [`Equation`](@ref) out of two [`Expression`](@ref) instances, or an
24-
`Expression` and a `Number`.
23+
Create an [`Equation`](@ref) out of two [`Num`](@ref) instances, or an
24+
`Num` and a `Number`.
2525
2626
# Examples
2727

src/latexify_recipes.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
rhs = getfield.(eqs, :rhs)
1010
rhs = toexpr.(rhs)
11-
rhs = [postwalk(x -> x isa ModelingToolkit.Constant ? x.value : x, eq) for eq in rhs]
1211
rhs = [postwalk(x -> x isa Expr && length(x.args) == 1 ? x.args[1] : x, eq) for eq in rhs]
1312
rhs = [postwalk(x -> x isa Expr && x.args[1] == :derivative && length(x.args[2].args) == 2 ? :($(Symbol(:d, x.args[2]))/($(Symbol(:d, x.args[2].args[2])))) : x, eq) for eq in rhs]
1413
rhs = [postwalk(x -> x isa Expr && x.args[1] == :derivative ? "\\frac{d\\left($(Latexify.latexraw(x.args[2]))\\right)}{d$(Latexify.latexraw(x.args[3]))}" : x, eq) for eq in rhs]
1514

1615
lhs = getfield.(eqs, :lhs)
1716
lhs = toexpr.(lhs)
18-
lhs = [postwalk(x -> x isa ModelingToolkit.Constant ? x.value : x, eq) for eq in lhs]
1917
lhs = [postwalk(x -> x isa Expr && length(x.args) == 1 ? x.args[1] : x, eq) for eq in lhs]
2018
lhs = [postwalk(x -> x isa Expr && x.args[1] == :derivative && length(x.args[2].args) == 2 ? :($(Symbol(:d, x.args[2]))/($(Symbol(:d, x.args[2].args[2])))) : x, eq) for eq in lhs]
2119
lhs = [postwalk(x -> x isa Expr && x.args[1] == :derivative ? "\\frac{d\\left($(Latexify.latexraw(x.args[2]))\\right)}{d$(Latexify.latexraw(x.args[3]))}" : x, eq) for eq in lhs]

src/register_function.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ macro register(expr, Ts = [Num, Symbolic, Real])
2121
for ts in types]...) |> esc
2222
end
2323

24-
# Ensure that Operations that get @registered from outside the ModelingToolkit
24+
# Ensure that Num that get @registered from outside the ModelingToolkit
2525
# module can work without having to bring in the associated function into the
2626
# ModelingToolkit namespace. We basically store information about functions
2727
# registered at runtime in a ModelingToolkit variable,

src/solve.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ function _solve(A, b)
7575
SymbolicUtils.simplify.(ldiv(sym_lu(A), b))
7676
end
7777

78-
LinearAlgebra.:(\)(A::AbstractMatrix{<:Expression}, b::AbstractVector{<:Expression}) = _solve(A, b)
79-
LinearAlgebra.:(\)(A::AbstractMatrix{<:Expression}, b::AbstractVector) = _solve(A, b)
80-
LinearAlgebra.:(\)(A::AbstractMatrix, b::AbstractVector{<:Expression}) = _solve(A, b)
81-
8278
# ldiv below
8379

8480
_iszero(x::Number) = iszero(x)

src/systems/abstractsystem.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ calculate_tgrad(sys::AbstractSystem)
55
66
Calculate the time gradient of a system.
77
8-
Returns a vector of [`Expression`](@ref) instances. The result from the first
8+
Returns a vector of [`Num`](@ref) instances. The result from the first
99
call will be cached in the system object.
1010
"""
1111
function calculate_tgrad end
@@ -17,7 +17,7 @@ calculate_gradient(sys::AbstractSystem)
1717
1818
Calculate the gradient of a scalar system.
1919
20-
Returns a vector of [`Expression`](@ref) instances. The result from the first
20+
Returns a vector of [`Num`](@ref) instances. The result from the first
2121
call will be cached in the system object.
2222
"""
2323
function calculate_gradient end
@@ -29,7 +29,7 @@ calculate_jacobian(sys::AbstractSystem)
2929
3030
Calculate the jacobian matrix of a system.
3131
32-
Returns a matrix of [`Expression`](@ref) instances. The result from the first
32+
Returns a matrix of [`Num`](@ref) instances. The result from the first
3333
call will be cached in the system object.
3434
"""
3535
function calculate_jacobian end
@@ -41,7 +41,7 @@ calculate_factorized_W(sys::AbstractSystem)
4141
4242
Calculate the factorized W-matrix of a system.
4343
44-
Returns a matrix of [`Expression`](@ref) instances. The result from the first
44+
Returns a matrix of [`Num`](@ref) instances. The result from the first
4545
call will be cached in the system object.
4646
"""
4747
function calculate_factorized_W end
@@ -53,7 +53,7 @@ calculate_hessian(sys::AbstractSystem)
5353
5454
Calculate the hessian matrix of a scalar system.
5555
56-
Returns a matrix of [`Expression`](@ref) instances. The result from the first
56+
Returns a matrix of [`Num`](@ref) instances. The result from the first
5757
call will be cached in the system object.
5858
"""
5959
function calculate_hessian end

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ function calculate_massmatrix(sys::AbstractODESystem; simplify=true)
9898
end
9999
M = simplify ? ModelingToolkit.simplify.(M) : M
100100
# M should only contain concrete numbers
101-
M = map(x->x isa Constant ? x.value : x, M)
102101
M == I ? I : M
103102
end
104103

0 commit comments

Comments
 (0)