Skip to content

Commit bbb1fdb

Browse files
fix invW
1 parent d42e596 commit bbb1fdb

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ include("variables.jl")
1818
Base.promote_rule(::Type{T},::Type{T2}) where {T<:Number,T2<:Expression} = Expression
1919
Base.one(::Type{T}) where T<:Expression = Constant(1)
2020
Base.zero(::Type{T}) where T<:Expression = Constant(0)
21+
Base.convert(::Type{Variable},x::Int64) = Constant(x)
2122

2223
function caclulate_jacobian end
2324

src/operations.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ Base.:(==)(x::Operation,y::Number) = false
1212
Base.:(==)(x::Number,y::Operation) = false
1313
Base.:(==)(x::Operation,y::Void) = false
1414
Base.:(==)(x::Void,y::Operation) = false
15-
Base.:(==)(x::Variable,y::Operation) = y == Operation(identity,Expression[x])
16-
Base.:(==)(x::Operation,y::Variable) = x == Operation(identity,Expression[y])
17-
18-
# Don't recurse inversion for Jacobians
19-
Base.inv(x::Operation) = x
15+
Base.:(==)(x::Variable,y::Operation) = false
16+
Base.:(==)(x::Operation,y::Variable) = false
2017

2118
function Base.Expr(O::Operation)
2219
Expr(:call, Symbol(O.op), Expr.(O.args)...)

src/simplify.jl

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
1-
function simplify_constants(O::Operation)
1+
function simplify_constants(O::Operation,shorten_tree = true)
22
O_last = nothing
33
_O = O
44
while _O != O_last
55
O_last = _O
6-
_O = _simplify_constants(_O)
6+
_O = _simplify_constants(_O,shorten_tree)
77
if typeof(_O) <: Operation
8-
_O = Operation(_O.op,simplify_constants.(_O.args))
8+
_O = Operation(_O.op,simplify_constants.(_O.args,shorten_tree))
99
end
1010
end
1111
_O
1212
end
1313

1414
const TREE_SHRINK_OPS = [:*,:+]
1515

16-
function _simplify_constants(O)
16+
function _simplify_constants(O,shorten_tree = true)
1717
# Tree shrinking
18-
for cur_op in TREE_SHRINK_OPS
19-
if Symbol(O.op) == cur_op
20-
# Shrink tree
21-
if any(x->typeof(x)<:Operation && Symbol(x.op) == cur_op ,O.args)
22-
idxs = find(x->typeof(x)<:Operation && Symbol(x.op) == cur_op,O.args)
23-
keep_idxs = 1:length(O.args) .∉ (idxs,)
24-
args = Vector{Expression}[O.args[i].args for i in idxs]
25-
push!(args,O.args[keep_idxs])
26-
return Operation(O.op,vcat(args...))
27-
# Collapse constants
28-
elseif length(find(x->typeof(x)<:Variable && x.subtype == :Constant ,O.args)) > 1
29-
idxs = find(x->typeof(x)<:Variable && x.subtype == :Constant ,O.args)
30-
other_idxs = 1:length(O.args) .∉ (idxs,)
31-
if cur_op == :*
32-
new_var = Constant(prod(x->x.value,O.args[idxs]))
33-
elseif cur_op == :+
34-
new_var = Constant(sum(x->x.value,O.args[idxs]))
35-
end
36-
new_args = O.args[other_idxs]
37-
push!(new_args,new_var)
38-
if length(new_args) > 1
39-
return Operation(O.op,new_args)
40-
else
41-
return new_args[1]
42-
end
18+
if shorten_tree
19+
for cur_op in TREE_SHRINK_OPS
20+
if Symbol(O.op) == cur_op
21+
# Shrink tree
22+
if any(x->typeof(x)<:Operation && Symbol(x.op) == cur_op ,O.args)
23+
idxs = find(x->typeof(x)<:Operation && Symbol(x.op) == cur_op,O.args)
24+
keep_idxs = 1:length(O.args) .∉ (idxs,)
25+
args = Vector{Expression}[O.args[i].args for i in idxs]
26+
push!(args,O.args[keep_idxs])
27+
return Operation(O.op,vcat(args...))
28+
# Collapse constants
29+
elseif length(find(x->typeof(x)<:Variable && x.subtype == :Constant ,O.args)) > 1
30+
idxs = find(x->typeof(x)<:Variable && x.subtype == :Constant ,O.args)
31+
other_idxs = 1:length(O.args) .∉ (idxs,)
32+
if cur_op == :*
33+
new_var = Constant(prod(x->x.value,O.args[idxs]))
34+
elseif cur_op == :+
35+
new_var = Constant(sum(x->x.value,O.args[idxs]))
36+
end
37+
new_args = O.args[other_idxs]
38+
push!(new_args,new_var)
39+
if length(new_args) > 1
40+
return Operation(O.op,new_args)
41+
else
42+
return new_args[1]
43+
end
44+
end
4345
end
4446
end
4547
end
@@ -63,9 +65,8 @@ function _simplify_constants(O)
6365
else
6466
return O
6567
end
66-
elseif (Symbol(O.op) == :+ || Symbol(O.op) == :-) &&
67-
any(x->typeof(x)<:Variable && (isequal(x,Constant(0)) ||
68-
isequal(x,Constant(-0))),O.args)
68+
elseif Symbol(O.op) == :+ && any(x->typeof(x)<:Variable &&
69+
(isequal(x,Constant(0)) || isequal(x,Constant(-0))),O.args)
6970
# If there are Constant(0)s in a big `+` expression, get rid of them
7071
idxs = find(x->typeof(x)<:Variable && (isequal(x,Constant(0)) || isequal(x,Constant(-0))),O.args)
7172
_O = Operation(O.op,O.args[1:length(O.args) .∉ (idxs,)])
@@ -76,15 +77,18 @@ function _simplify_constants(O)
7677
else
7778
return O
7879
end
80+
#=
7981
elseif O.op == identity
8082
return O.args[1]
83+
=#
8184
elseif Symbol(O.op) == :- && length(O.args) == 1
8285
return Operation(*,Expression[-1,O.args[1]])
8386
else
8487
return O
8588
end
89+
return O
8690
end
87-
simplify_constants(x::Variable) = x
88-
_simplify_constants(x::Variable) = x
91+
simplify_constants(x::Variable,y) = x
92+
_simplify_constants(x::Variable,y) = x
8993

9094
export simplify_constants

0 commit comments

Comments
 (0)