Skip to content

Commit 47512d7

Browse files
Kolmogorov complexity added
1 parent addb9e9 commit 47512d7

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

src/candidates.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ function generate_basis(eq, x, try_kernel = true)
2828
return cache.(unique([one(x); [equivalent(t, x) for t in terms(S)]]))
2929
end
3030

31-
function expand_basis(basis, x)
32-
b = sum(expr.(basis))
33-
δb = sum(deriv!.(basis, x))
31+
function expand_basis(basis, x; Kmax=1000)
32+
b = sum(expr.(basis))
33+
34+
Kb = complexity(b) # Kolmogorov complexity
35+
if Kb > Kmax
36+
return basis, false
37+
end
38+
39+
δb = sum(deriv!.(basis, x))
3440
eq = (1 + x) * (b + δb)
3541
eq = expand(eq)
3642
S = Set{Any}()
3743
enqueue_expr!(S, eq, x)
38-
return cache.([one(x); [s for s in S]])
44+
return cache.([one(x); [s for s in S]]), true
3945
end
4046

4147
function closure(eq, x; max_terms = 50)

src/homotopy.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ function ∂(x)
112112
return isequal(d, 0) ? 1 : d
113113
end
114114

115+
@syms 𝛷(x)
116+
115117
partial_int_rules = [
116118
# trigonometric functions
117119
@rule 𝛷(sin(~x)) => (cos(~x) + si(~x), (~x))
@@ -128,19 +130,19 @@ partial_int_rules = [
128130
@rule 𝛷(sech(~x)) => (atan(sinh(~x)), (~x))
129131
@rule 𝛷(coth(~x)) => (log(sinh(~x)), (~x))
130132
# 1/trigonometric functions
131-
@rule 𝛷(1 / sin(~x)) => (log(csc(~x) + cot(~x)), (~x))
132-
@rule 𝛷(1 / cos(~x)) => (log(sec(~x) + tan(~x)), (~x))
133-
@rule 𝛷(1 / tan(~x)) => (log(sin(~x)), (~x))
134-
@rule 𝛷(1 / csc(~x)) => (cos(~x), (~x))
135-
@rule 𝛷(1 / sec(~x)) => (sin(~x), (~x))
136-
@rule 𝛷(1 / cot(~x)) => (log(cos(~x)), (~x))
133+
@rule 𝛷(1 / sin(~x)) => (log(csc(~x) + cot(~x)) + log(sin(~x)), (~x))
134+
@rule 𝛷(1 / cos(~x)) => (log(sec(~x) + tan(~x)) + log(cos(~x)), (~x))
135+
@rule 𝛷(1 / tan(~x)) => (log(sin(~x)) + log(tan(~x)), (~x))
136+
@rule 𝛷(1 / csc(~x)) => (cos(~x) + log(csc(~x)), (~x))
137+
@rule 𝛷(1 / sec(~x)) => (sin(~x) + log(sec(~x)), (~x))
138+
@rule 𝛷(1 / cot(~x)) => (log(cos(~x)) + log(cot(~x)), (~x))
137139
# 1/hyperbolic functions
138-
@rule 𝛷(1 / sinh(~x)) => (log(tanh(~x / 2)), (~x))
139-
@rule 𝛷(1 / cosh(~x)) => (atan(sinh(~x)), (~x))
140-
@rule 𝛷(1 / tanh(~x)) => (log(sinh(~x)), (~x))
141-
@rule 𝛷(1 / csch(~x)) => (cosh(~x), (~x))
142-
@rule 𝛷(1 / sech(~x)) => (sinh(~x), (~x))
143-
@rule 𝛷(1 / coth(~x)) => (log(cosh(~x)), (~x))
140+
@rule 𝛷(1 / sinh(~x)) => (log(tanh(~x / 2)) + log(sinh(~x)), (~x))
141+
@rule 𝛷(1 / cosh(~x)) => (atan(sinh(~x)) + log(cosh(~x)), (~x))
142+
@rule 𝛷(1 / tanh(~x)) => (log(sinh(~x)) + log(tanh(~x)), (~x))
143+
@rule 𝛷(1 / csch(~x)) => (cosh(~x) + log(csch(~x)), (~x))
144+
@rule 𝛷(1 / sech(~x)) => (sinh(~x) + log(sech(~x)), (~x))
145+
@rule 𝛷(1 / coth(~x)) => (log(cosh(~x)) + log(coth(~x)), (~x))
144146
# inverse trigonometric functions
145147
@rule 𝛷(asin(~x)) => (~x * asin(~x) + sqrt(1 - ~x * ~x), (~x))
146148
@rule 𝛷(acos(~x)) => (~x * acos(~x) + sqrt(1 - ~x * ~x), (~x))
@@ -168,15 +170,15 @@ partial_int_rules = [
168170
@rule 𝛷(sqrt(~x)) => (sum(candidate_sqrt(~x, 0.5); init = one(~x)), (~x));
169171
@rule 𝛷(1 / sqrt(~x)) => (sum(candidate_sqrt(~x, -0.5); init = one(~x)), (~x));
170172
# rational functions
171-
@rule 𝛷(1 / ^(~x::is_poly, ~k)) => (sum(candidate_pow_minus(~x, -~k);
173+
@rule 𝛷(1 / ^(~x::is_poly, ~k::is_pos_int)) => (sum(candidate_pow_minus(~x, -~k);
172174
init = one(~x)), 1)
173175
@rule 𝛷(1 / ~x::is_poly) => (sum(candidate_pow_minus(~x, -1);
174176
init = one(~x)), 1)
175177
@rule 𝛷(^(~x, -1)) => (log(~x), (~x))
176178
@rule 𝛷(^(~x, ~k::is_neg_int)) => (sum(^(~x, i) for i in (~k + 1):-1),
177179
(~x))
178180
@rule 𝛷(1 / ~x) => (log(~x), (~x))
179-
@rule 𝛷(^(~x, ~k)) => (sum(^(~x, i+1) for i=1:~k+1), (~x))
181+
@rule 𝛷(^(~x, ~k::is_pos_int)) => (sum(^(~x, i+1) for i=1:~k+1), (~x))
180182
@rule 𝛷(1) => (𝑥, 1)
181183
@rule 𝛷(~x) => ((~x + ^(~x, 2)), (~x))]
182184

src/integral.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,12 @@ function integrate_term(eq, x, l; kwargs...)
204204
inform(l, "Failed numeric")
205205

206206
if i < num_steps
207-
basis1 = expand_basis(basis1, x)
208-
basis2 = expand_basis(basis2, x)
207+
basis1, ok1 = expand_basis(basis1, x)
208+
basis2, ok2 = expand_basis(basis2, x)
209+
210+
if !ok1 && ~ok2
211+
break
212+
end
209213

210214
if show_basis
211215
inform(l, "Expanding the basis (|β| = $(length(basis)))", basis1)

src/rules.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,24 @@ inv_rules = [@rule Ω(1 / ~x) => ~x
258258

259259
inverse(eq) = Prewalk(PassThrough(Chain(inv_rules)))(Ω(value(eq)))
260260

261+
###############################################################################
262+
263+
is_sym(x) = first(ops(x)) isa Sym
264+
265+
h_rules = [
266+
@rule +(~~xs) => 1 + sum(~~xs)
267+
@rule *(~~xs) => 1 + sum(~~xs)
268+
@rule ~x / ~y => 1 + ~x + ~y
269+
@rule ^(~x, ~y) => 1 + ~x + ~y
270+
@rule log(~x) => 1 + ~x
271+
@rule (~f)(~x) => 1 + ~x
272+
@rule ~x::is_sym => 1
273+
]
274+
275+
# complexity returns a measure of the complexity of an equation
276+
# it is roughly similar ro kolmogorov complexity
277+
function complexity(eq)
278+
_, eq = ops(eq)
279+
return Prewalk(PassThrough(Chain(h_rules)))(eq)
280+
end
261281

0 commit comments

Comments
 (0)