diff --git a/Project.toml b/Project.toml index 5530fed32..a46d712d9 100644 --- a/Project.toml +++ b/Project.toml @@ -36,8 +36,9 @@ TreeViews = "0.3" julia = "1.2" [extras] +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test", "SafeTestsets"] +test = ["Test", "SafeTestsets", "Pkg"] diff --git a/test/array.jl b/test/overloads.jl similarity index 51% rename from test/array.jl rename to test/overloads.jl index 93cb577f0..656991d24 100644 --- a/test/array.jl +++ b/test/overloads.jl @@ -1,9 +1,8 @@ -using Symbolics +using ModelingToolkit using LinearAlgebra using SparseArrays: sparse using Test - @variables a,b,c,d,e,f,g,h,i # test hashing @@ -14,6 +13,10 @@ aa = a; # old a @test isequal(a, aa) @test hash(a) == hash(aa) +@test isequal(get_variables(a+aa+1), [a]) + +@test hash(a+b ~ c+d) == hash(a+b ~ c+d) + # test some matrix operations don't throw errors X = [0 b c; d e f; g h i] @test iszero(simplify(det(X) - ((d * ((b * i) - (c * h))) + (g * ((b * f) - (c * e)))))) @@ -22,7 +25,7 @@ F = lu(X) R = simplify.(F.L * F.U - X[F.p, :], polynorm=true) @test iszero(R) @test simplify.(F \ X) == I -@test Symbolics._solve(X, X) == I +@test ModelingToolkit._solve(X, X, true) == I inv(X) qr(X) @@ -53,24 +56,25 @@ D = sparse([1, 2], [2, 1], [d, d]) @test isequal(C * D, sparse([1,2], [1,2], [c * d, c * d])) -@variables t σ ρ β +@parameters t σ ρ β @variables x(t) y(t) z(t) - +D = Differential(t) Dx = Differential(x) Dy = Differential(y) Dz = Differential(z) -eqs = [σ*(y-x), - x*(ρ-z)-y, - x*y - β*z] -J = Num[Dx(eqs[1]) Dy(eqs[1]) Dz(eqs[1]) - Dx(eqs[2]) Dy(eqs[2]) Dz(eqs[2]) - Dx(eqs[3]) Dy(eqs[3]) Dz(eqs[3])] +eqs = [D(x) ~ σ*(y-x), + D(y) ~ x*(ρ-z)-y, + D(z) ~ x*y - β*z] +J = Num[Dx(eqs[1].rhs) Dy(eqs[1].rhs) Dz(eqs[1].rhs) + Dx(eqs[2].rhs) Dy(eqs[2].rhs) Dz(eqs[2].rhs) + Dx(eqs[3].rhs) Dy(eqs[3].rhs) Dz(eqs[3].rhs)] J = expand_derivatives.(J) using LinearAlgebra luJ = lu(J,Val(false)) +using ModelingToolkit @variables M[1:2,1:2] inv(M) @@ -85,3 +89,43 @@ M \ reshape(b,2,1) M = [1 a; 0 2] M \ b M \ [1, 2] + +# test det +@variables X[1:4,1:4] +d1 = det(X, laplace=true) +d2 = det(X, laplace=false) +_det1 = eval(build_function(d1, X)) +_det2 = eval(build_function(d2, X)) +A = [1 1 1 1 + 1 0 1 1 + 1 1 0 1 + 1 1 1 0] +@test _det1(A) == -1 +@test _det2(A) == -1 + +@variables X[1:3,1:3] +d1 = det(X, laplace=true) +d2 = det(X, laplace=false) +_det1 = eval(build_function(d1, X)) +_det2 = eval(build_function(d2, X)) +A = [1 1 1 + 1 0 1 + 1 1 1] +@test _det1(A) == 0 +@test _det2(A) == 0 + +@variables a b c d +z1 = a + b * im +z2 = c + d * im +@test z1 * 2 - Complex(2a, 2b) == 0 +@test isequal(2z1, Complex(2a, 2b)) +@test isequal(z1 / z1, 1) +@test isequal(z1 / z2, Complex((a*c + b*d)/(c^2 + d^2), (b*c - a*d)/(c^2 + d^2))) +@test isequal(1 / z2, Complex(c/(c^2 + d^2), -d/(c^2 + d^2))) +@test isequal(z1 * z2, Complex(a*c - b*d, a*d + b*c)) +@test isequal(z1 - z2, Complex(a - c, b - d)) +@test isequal(z1 + z2, Complex(a + c, b + d)) +@test isequal(z1 + 2, Complex(a + 2, b)) +@test isequal(2 + z1, Complex(2 + a, b)) +@test isequal(z1 - 2, Complex(a - 2, b)) +@test isequal(2 - z1, Complex(2 - a, -b)) diff --git a/test/runtests.jl b/test/runtests.jl index d114104b8..f312aafeb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,10 @@ using SafeTestsets, Test @safetestset "Differentiation Test" begin include("diff.jl") end -@safetestset "Array Test" begin include("array.jl") end +@safetestset "Overloading Test" begin include("overloads.jl") end + +if haskey(ENV, "CI") + using Pkg + Pkg.add(url="https://github.com/SciML/ModelingToolkit.jl.git", rev="master") + Pkg.test("ModelingToolkit") +end