Skip to content

Commit 06b24a5

Browse files
inverse W
1 parent 9665d46 commit 06b24a5

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/operations.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ function find_replace!(O::Operation,x::Variable,y::Expression)
5757
end
5858
end
5959
end
60+
61+
# For inv
62+
Base.convert(::Type{Operation},x::Int) = Operation(identity,Expression[Constant(x)])
63+
Base.convert(::Type{Operation},x::Variable) = Operation(identity,Expression[x])

src/simplify.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function _simplify_constants(O)
1616
# If any variable is `Constant(0)`, zero the whole thing
1717
# If any variable is `Constant(1)`, remove that `Constant(1)` unless
1818
# they are all `Constant(1)`, in which case simplify to a single variable
19-
if any(x->typeof(x)<:Variable && isequal(x,Constant(0)),O.args)
19+
if any(x->typeof(x)<:Variable && (isequal(x,Constant(0)) || isequal(x,Constant(-0))),O.args)
2020
return Constant(0)
2121
elseif any(x->typeof(x)<:Variable && isequal(x,Constant(1)),O.args)
2222
idxs = find(x->typeof(x)<:Variable && isequal(x,Constant(1)),O.args)
@@ -31,9 +31,9 @@ function _simplify_constants(O)
3131
else
3232
return O
3333
end
34-
elseif Symbol(O.op) == :+ && any(x->typeof(x)<:Variable && isequal(x,Constant(0)),O.args)
34+
elseif Symbol(O.op) == :+ && any(x->typeof(x)<:Variable && (isequal(x,Constant(0)) || isequal(x,Constant(-0))),O.args)
3535
# If there are Constant(0)s in a big `+` expression, get rid of them
36-
idxs = find(x->typeof(x)<:Variable && isequal(x,Constant(0)),O.args)
36+
idxs = find(x->typeof(x)<:Variable && (isequal(x,Constant(0)) || isequal(x,Constant(-0))),O.args)
3737
_O = Operation(O.op,O.args[1:length(O.args) .∉ (idxs,)])
3838
if isempty(_O.args)
3939
return Constant(0)
@@ -42,6 +42,8 @@ function _simplify_constants(O)
4242
else
4343
return O
4444
end
45+
elseif O.op == identity
46+
return O.args[1]
4547
else
4648
return O
4749
end

test/system_construction.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ SciCompDSL.generate_ode_function(de)
1818
jac_expr = SciCompDSL.generate_ode_jacobian(de)
1919
jac = SciCompDSL.calculate_jacobian(de)
2020
f = DiffEqFunction(de)
21-
I - jac
22-
@test_broken inv(I - jac)
21+
W = I - jac
22+
simplify_constants.(inv(W))
2323

2424
# Differential equation with automatic extraction of variables on rhs
2525
de2 = DiffEqSystem(eqs, [t])

0 commit comments

Comments
 (0)