11using DataStructures
22
33# this is the main heurisctic used to find the test fragments
4- function generate_basis (eq, x, try_kernel = true ; homotopy = true )
5- if homotopy && ! try_kernel
4+ function generate_basis (eq, x, try_kernel = true )
5+ if ! try_kernel
66 S = sum (generate_homotopy (expr (eq), x))
7- return cache .(unique ([one (x); [ equivalent (t, x) for t in terms (S)] ]))
7+ return cache .(unique ([equivalent (t, x) for t in terms (S)]))
88 end
99
1010 S = 0
@@ -13,33 +13,39 @@ function generate_basis(eq, x, try_kernel = true; homotopy = true)
1313 for t in terms (eq)
1414 q = equivalent (t, x)
1515 f = kernel (q)
16+ p = q / f
1617
17- C₁ = closure (f, x)
18- p = q * inverse (f)
19-
20- if homotopy
21- if isdependent (p, x)
22- C₂ = generate_homotopy (p, x)
23- else
24- C₂ = 1
25- end
18+ if isdependent (p, x)
19+ C₂ = generate_homotopy (p, x)
2620 else
27- C₂ = find_candidates (p, x)
21+ C₂ = 1
2822 end
2923
24+ C₁ = closure (f, x)
25+
3026 S += sum (c₁ * c₂ for c₁ in C₁ for c₂ in C₂)
3127 end
32- return cache .(unique ([one (x); [ equivalent (t, x) for t in terms (S)] ]))
28+ return cache .(unique ([equivalent (t, x) for t in terms (S)]))
3329end
3430
35- function expand_basis (basis, x)
31+ function expand_basis (basis, x; Kmax = 1000 )
32+ if isempty (basis)
33+ return basis, false
34+ end
35+
3636 b = sum (expr .(basis))
37+
38+ Kb = complexity (b)# Kolmogorov complexity
39+ if is_proper (Kb) && Kb > Kmax
40+ return basis, false
41+ end
42+
3743 δb = sum (deriv! .(basis, x))
3844 eq = (1 + x) * (b + δb)
3945 eq = expand (eq)
4046 S = Set {Any} ()
4147 enqueue_expr! (S, eq, x)
42- return cache .([one (x); [ s for s in S]])
48+ return cache .([s for s in S]), true
4349end
4450
4551function closure (eq, x; max_terms = 50 )
@@ -55,23 +61,7 @@ function closure(eq, x; max_terms = 50)
5561 y = dequeue! (q)
5662 enqueue_expr! (S, q, expand_derivatives (D (y)), x)
5763 end
58- unique ([one (x); [s for s in S]; [s * x for s in S]])
59- end
60-
61- function find_candidates (eq, x)
62- eq = apply_d_rules (eq)
63- D = Differential (x)
64-
65- S = Set {Any} ()
66- q = Queue {Any} ()
67- enqueue_expr! (S, q, eq, x)
68-
69- for y in q
70- ∂y = expand_derivatives (D (y))
71- enqueue_expr! (S, ∂y, x)
72- end
73-
74- return unique ([one (x); [s for s in S]])
64+ unique ([[s for s in S]; [s * x for s in S]])
7565end
7666
7767function candidate_pow_minus (p, k; abstol = 1e-8 )
@@ -142,27 +132,31 @@ end
142132
143133# ##############################################################################
144134
145- function enqueue_expr! (S, q, eq:: SymbolicUtils.Add , x)
135+ enqueue_expr! (S, q, eq, x) = enqueue_expr!! (S, q, ops (eq)... , x)
136+
137+ function enqueue_expr!! (S, q, :: Add , eq, x)
146138 for t in arguments (eq)
147139 enqueue_expr! (S, q, t, x)
148140 end
149141end
150142
151- function enqueue_expr! (S, q, eq, x)
143+ function enqueue_expr!! (S, q, :: Any , eq, x)
152144 y = eq / coef (eq, x)
153145 if y ∉ S && isdependent (y, x)
154146 enqueue! (q, y)
155147 push! (S, y)
156148 end
157149end
158150
159- function enqueue_expr! (S, eq:: SymbolicUtils.Add , x)
151+ enqueue_expr! (S, eq, x) = enqueue_expr!! (S, ops (eq)... , x)
152+
153+ function enqueue_expr!! (S, :: Add , eq, x)
160154 for t in arguments (eq)
161155 enqueue_expr! (S, t, x)
162156 end
163157end
164158
165- function enqueue_expr! (S , eq, x)
159+ function enqueue_expr!! (S, :: Any , eq, x)
166160 y = eq / coef (eq, x)
167161 if y ∉ S && isdependent (y, x)
168162 push! (S, y)
0 commit comments