Skip to content

Commit 7dcd418

Browse files
Replace Variable subtype with known
1 parent 285e582 commit 7dcd418

File tree

7 files changed

+28
-22
lines changed

7 files changed

+28
-22
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ context-aware single variable of the IR. Its fields are described as follows:
136136
- `name`: the name of the `Variable`. Note that this is not necessarily
137137
the same as the name of the Julia variable. But this symbol itself is considered
138138
the core identifier of the `Variable` in the sense of equality.
139-
- `subtype`: the main denotation of context. Variables within systems
140-
are grouped according to their `subtype`.
139+
- `known`: the main denotation of context, storing whether or not the value of
140+
the variable is known.
141141
- `dependents`: the vector of variables on which the current variable
142142
is dependent. For example, `u(t,x)` has dependents `[t,x]`. Derivatives thus
143143
require this information in order to simplify down.
@@ -252,9 +252,9 @@ is syntactic sugar for:
252252
253253
```julia
254254
t = Parameter(:t)
255-
x = Unknown(:x, dependents = [t])
256-
y = Unknown(:y, dependents = [t])
257-
z = Unknown(:z, dependents = [t])
255+
x = Unknown(:x, [t])
256+
y = Unknown(:y, [t])
257+
z = Unknown(:z, [t])
258258
D = Differential(t)
259259
σ = Parameter()
260260
ρ = Parameter()

src/equations.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ Base.:~(lhs::Expression, rhs::Number ) = Equation(lhs, rhs)
1212
Base.:~(lhs::Number , rhs::Expression) = Equation(lhs, rhs)
1313

1414

15-
_is_dependent(x::Variable) = x.subtype === :Unknown && !isempty(x.dependents)
16-
_is_parameter(iv) = x -> x.subtype === :Parameter && x iv
17-
_subtype(subtype::Symbol) = x -> x.subtype === subtype
15+
_is_dependent(x::Variable) = !x.known && !isempty(x.dependents)
16+
_is_parameter(iv) = x -> x.known && x iv
17+
_is_known(x::Variable) = x.known
18+
_is_unknown(x::Variable) = !x.known
1819

1920
function extract_elements(eqs, predicates)
2021
result = [Variable[] for p predicates]

src/systems/diffeqs/first_order_transform.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ end
55
function lower_varname(var::Variable, idv, order::Int)
66
sym = var.name
77
name = order == 0 ? sym : Symbol(sym, :_, string(idv.name)^order)
8-
return Variable(name, var.subtype, var.dependents)
8+
return Variable(name, var.known, var.dependents)
99
end
1010

1111
function ode_order_lowering(sys::DiffEqSystem)

src/systems/nonlinear/nonlinear_system.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct NonlinearSystem <: AbstractSystem
1818
end
1919

2020
function NonlinearSystem(eqs)
21-
vs, ps = extract_elements(eqs, [_subtype(:Unknown), _subtype(:Parameter)])
21+
vs, ps = extract_elements(eqs, [_is_unknown, _is_known])
2222
NonlinearSystem(eqs, vs, ps)
2323
end
2424

src/variables.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ export Variable, Unknown, Parameter, @Unknown, @Param
33

44
struct Variable <: Expression
55
name::Symbol
6-
subtype::Symbol
6+
known::Bool
77
dependents::Vector{Variable}
88
end
99

10-
Parameter(name; dependents = Variable[]) = Variable(name, :Parameter, dependents)
11-
Unknown(name; dependents = Variable[]) = Variable(name, :Unknown, dependents)
10+
Parameter(name, dependents = Variable[]) = Variable(name, true, dependents)
11+
Unknown(name, dependents = Variable[]) = Variable(name, false, dependents)
1212

1313

1414
struct Constant <: Expression
@@ -22,7 +22,7 @@ Base.isone(ex::Expression) = isa(ex, Constant) && isone(ex.value)
2222

2323

2424
# Variables use isequal for equality since == is an Operation
25-
Base.:(==)(x::Variable, y::Variable) = (x.name, x.subtype) == (y.name, y.subtype)
25+
Base.:(==)(x::Variable, y::Variable) = (x.name, x.known) == (y.name, y.known)
2626
Base.:(==)(::Variable, ::Number) = false
2727
Base.:(==)(::Number, ::Variable) = false
2828
Base.:(==)(::Variable, ::Constant) = false
@@ -32,13 +32,18 @@ Base.:(==)(n::Number, c::Constant) = c.value == n
3232
Base.:(==)(a::Constant, b::Constant) = a.value == b.value
3333

3434
function Base.convert(::Type{Expr}, x::Variable)
35-
x.subtype === :Parameter || return x.name
36-
isempty(x.dependents) && return x.name
35+
x.known || return x.name
36+
isempty(x.dependents) && return x.name
3737
return :($(x.name)($(convert.(Expr, x.dependents)...)))
3838
end
3939
Base.convert(::Type{Expr}, c::Constant) = c.value
4040

41-
Base.show(io::IO, x::Variable) = print(io, x.subtype, '(', x.name, ')')
41+
function Base.show(io::IO, x::Variable)
42+
subtype = x.known ? :Parameter : :Unknown
43+
print(io, subtype, '(', repr(x.name))
44+
isempty(x.dependents) || print(io, ", ", x.dependents)
45+
print(io, ')')
46+
end
4247

4348
# Build variables more easily
4449
function _parse_vars(macroname, fun, x)
@@ -65,7 +70,7 @@ function _parse_vars(macroname, fun, x)
6570
end
6671

6772
push!(var_names, var_name)
68-
expr = :($var_name = $fun($(Meta.quot(var_name)), dependents = $dependents))
73+
expr = :($var_name = $fun($(Meta.quot(var_name)), $dependents))
6974
push!(ex.args, expr)
7075
end
7176
push!(ex.args, build_expr(:tuple, var_names))

test/basic_variables_and_operations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using Test
77

88
# Default values
99
p = Parameter(:p)
10-
u = Unknown(:u, dependents = [t])
10+
u = Unknown(:u, [t])
1111

1212
σ*(y-x)
1313
D(x)

test/variable_parsing.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ using Test
55
@Unknown x(t)
66
@Unknown y(t)
77
@Unknown z(t)
8-
x1 = Unknown(:x ,dependents = [t])
9-
y1 = Unknown(:y ,dependents = [t])
10-
z1 = Unknown(:z ,dependents = [t])
8+
x1 = Unknown(:x, [t])
9+
y1 = Unknown(:y, [t])
10+
z1 = Unknown(:z, [t])
1111
@test x1 == x
1212
@test y1 == y
1313
@test z1 == z

0 commit comments

Comments
 (0)