Skip to content

Commit 7742177

Browse files
common form for negation
1 parent 2b0d847 commit 7742177

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

src/simplify.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function _simplify_constants(O)
4444
end
4545
elseif O.op == identity
4646
return O.args[1]
47+
elseif Symbol(O.op) == :- && length(O.args) == 1
48+
return Operation(*,Expression[-1,O.args[1]])
4749
else
4850
return O
4951
end

test/basic_variables_and_operations.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@ D*x ~ -σ*(y-x)
2222
D*y ~ x*-z)-sin(y)
2323

2424
@test D*t == Constant(1)
25-
null_op = 0*t
26-
@test simplify_constants(null_op) == Constant(0)

test/derivatives.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ expand_derivatives(dsin)
1111

1212
@test expand_derivatives(dsin) == cos(t)
1313
dcsch = D*csch(t)
14-
@test expand_derivatives(dcsch) == Operation(-coth(t)*csch(t))
14+
@test expand_derivatives(dcsch) == Operation(-1*coth(t)*csch(t))
1515
# Chain rule
1616
dsinsin = D*sin(sin(t))
1717
@test expand_derivatives(dsinsin) == cos(sin(t))*cos(t)
@@ -24,7 +24,7 @@ dpow2 = Derivative(^,[x, y],Val{2})
2424
d1 = D*(sin(t)*t)
2525
d2 = D*(sin(t)*cos(t))
2626
@test expand_derivatives(d1) == t*cos(t)+sin(t)
27-
@test expand_derivatives(d2) == cos(t)*cos(t)+sin(t)*-sin(t)
27+
@test expand_derivatives(d2) == cos(t)*cos(t)+sin(t)*(-1*sin(t))
2828

2929
eqs = [0 ~ σ*(y-x),
3030
0 ~ x*-z)-y,

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ using ModelingToolkit, Base.Test
55
@testset "Differentiation Test" begin include("derivatives.jl") end
66
@testset "Internal Test" begin include("internal.jl") end
77
@testset "Domain Test" begin include("domains.jl") end
8+
@testset "Simplify Test" begin include("simplify.jl") end
89
@testset "Ambiguity Test" begin include("ambiguity.jl") end
10+
@testset "Componets Test" begin include("components.jl") end
911
@testset "System Construction Test" begin include("system_construction.jl") end

test/simplify.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using ModelingToolkit
2+
using Base.Test
3+
4+
@IVar t
5+
@DVar x(t) y(t) z(t)
6+
7+
null_op = 0*t
8+
@test simplify_constants(null_op) == Constant(0)
9+
10+
one_op = 1*t
11+
@test simplify_constants(one_op) == t
12+
13+
identity_op = Operation(identity,[x])
14+
@test simplify_constants(identity_op) == x
15+
16+
minus_op = -x
17+
@test simplify_constants(minus_op) == -1*x
18+
simplify_constants(minus_op)

0 commit comments

Comments
 (0)