11using Symbolics
2- using Symbolics: symbolic_to_float, var_from_nested_derivative, unwrap
2+ import Symbolics: symbolic_to_float, var_from_nested_derivative, unwrap,
3+ isblock, flatten_expr!, build_expr, get_variables,
4+ is_singleton, diff2term, tosymbol, lower_varname,
5+ makesubscripts, degree, coeff
36
47@testset " get_variables" begin
58 @variables t x y z (t)
6366 arg = Symbolics. fixpoint_sub (p (x), [p => sqrt, x => 2 y + 3 , y => 1.0 + p (4 )])
6467 @test arg ≈ 3.0
6568end
69+
70+ # Helper Functions
71+ @variables x y z t u (x, t) v (t) w[1 : 2 ]
72+
73+ @testset " isblock" begin
74+ @test isblock ([Expr (:block , :(x + y))]) == true
75+ @test isblock ([Expr (:call , :f , x, y)]) == false
76+ @test isblock ([Expr (:block , :(begin x; y; end ))]) == true
77+ @test isblock ([Expr (:return , :(x + y))]) == false
78+ end
79+
80+ @testset " flatten_expr!" begin
81+ expr = Expr (:block , :(x + y))
82+ @test flatten_expr! (expr. args) == Any[:(x + y)]
83+
84+ expr2 = Expr (:block , :(begin x + y; z; end ))
85+ @test flatten_expr! (expr2. args) == Any[:(x + y), :z ]
86+ end
87+
88+ @testset " build_expr" begin
89+ expr = build_expr (:block , [:(x + y), :(y + z)])
90+ @test expr. head == :block
91+ @test expr. args == [:(x + y), :(y + z)]
92+ end
93+
94+ @testset " is_singleton" begin
95+ @test is_singleton (x) == false
96+ @test is_singleton (sin (x)) == false
97+ @test is_singleton (u) == false
98+ end
99+
100+ @testset " tosymbol" begin
101+ expr1 = sin (x)
102+ @test tosymbol (expr1) == Symbol (" sin(x)" )
103+
104+ expr2 = cos (y)
105+ @test tosymbol (expr2) == Symbol (" cos(y)" )
106+
107+ expr4 = u
108+ @test tosymbol (expr4) == Symbol (" u(x, t)" )
109+ end
110+
111+ @testset " degree" begin
112+ expr1 = x^ 2 + y^ 3
113+ @test degree (expr1, x) == 2
114+ @test degree (expr1, y) == 3
115+
116+ expr2 = x * y^ 2 + x^ 3
117+ @test degree (expr2, x) == 3
118+ @test degree (expr2, y) == 2
119+
120+ expr3 = 1
121+ @test degree (expr3, x) == 0
122+ end
123+
124+ @testset " coeff" begin
125+ expr1 = 3 x + 2 y
126+ @test coeff (expr1, x) == 3
127+ @test coeff (expr1, y) == 2
128+ @test coeff (expr1, x^ 2 ) == 0
129+
130+ expr2 = x^ 2 + 3 x + 2
131+ @test coeff (expr2, x) == 3
132+ @test coeff (expr2, x^ 2 ) == 1
133+ end
134+
135+ @testset " makesubscripts" begin
136+ sub1 = makesubscripts (5 )
137+ @test length (sub1) == 5
138+ @test typeof (sub1[1 ]) == SymbolicUtils. BasicSymbolic{Int64}
139+
140+ sub2 = makesubscripts (10 )
141+ @test length (sub2) == 10
142+ end
143+
144+ @testset " diff2term" begin
145+ @variables x t u (x, t) z (t)
146+ Dt = Differential (t)
147+ Dx = Differential (x)
148+
149+ test_var = x
150+ result = diff2term (test_var)
151+ @test result === x
152+
153+ test_nested_derivative = Dx (Dt (Dt (u)))
154+ result = diff2term (Symbolics. value (test_nested_derivative))
155+ @test typeof (result) === Symbolics. BasicSymbolic{Real}
156+ end
0 commit comments