Skip to content

Commit 63dfad9

Browse files
1.0.1 formatted
1 parent 85dad52 commit 63dfad9

File tree

8 files changed

+97
-109
lines changed

8 files changed

+97
-109
lines changed

src/cache.jl

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const DEBUG_CACHE = true
22

33
mutable struct ExprCache
4-
eq # the primary expression
5-
f # compiled eq
6-
δeq # the symbolic derivative of eq
7-
δf # compiled δeq
4+
eq::Any# the primary expression
5+
f::Any# compiled eq
6+
δeq::Any# the symbolic derivative of eq
7+
δf::Any# compiled δeq
88
end
99

1010
cache(eq::ExprCache) = eq
@@ -14,46 +14,45 @@ expr(c::ExprCache) = c.eq
1414
expr(c) = c
1515

1616
function deriv!(c::ExprCache, x)
17-
if c.δeq == nothing
18-
c.δeq = expand_derivatives(Differential(x)(expr(c)))
19-
end
20-
return c.δeq
17+
if c.δeq == nothing
18+
c.δeq = expand_derivatives(Differential(x)(expr(c)))
19+
end
20+
return c.δeq
2121
end
2222

2323
function deriv!(c, x)
24-
if DEBUG_CACHE
25-
error("ExprCache object expected")
26-
end
27-
return expand_derivatives(Differential(x)(c))
24+
if DEBUG_CACHE
25+
error("ExprCache object expected")
26+
end
27+
return expand_derivatives(Differential(x)(c))
2828
end
2929

3030
function fun!(c::ExprCache, x)
31-
if c.f == nothing
32-
c.f = build_function(expr(c), x; expression = false)
33-
end
34-
return c.f
31+
if c.f == nothing
32+
c.f = build_function(expr(c), x; expression = false)
33+
end
34+
return c.f
3535
end
3636

3737
function fun!(c, x)
38-
if DEBUG_CACHE
39-
error("ExprCache object expected")
40-
end
41-
return build_function(c, x; expression = false)
38+
if DEBUG_CACHE
39+
error("ExprCache object expected")
40+
end
41+
return build_function(c, x; expression = false)
4242
end
4343

4444
function deriv_fun!(c::ExprCache, x)
45-
if c.δf == nothing
46-
c.δf = build_function(deriv!(c, x), x; expression = false)
47-
end
48-
return c.δf
45+
if c.δf == nothing
46+
c.δf = build_function(deriv!(c, x), x; expression = false)
47+
end
48+
return c.δf
4949
end
5050

5151
function deriv_fun!(c, x)
52-
if DEBUG_CACHE
53-
error("ExprCache object expected")
54-
end
55-
return build_function(deriv!(c, x), x; expression = false)
52+
if DEBUG_CACHE
53+
error("ExprCache object expected")
54+
end
55+
return build_function(deriv!(c, x), x; expression = false)
5656
end
5757

5858
Base.show(io::IO, c::ExprCache) = show(expr(c))
59-

src/homotopy.jl

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function transformer(eq::SymbolicUtils.Mul, f)
2020
return prod(transformer(t, f) for t in arguments(eq); init = 1)
2121
end
2222
function transformer(eq::SymbolicUtils.Div, f)
23-
return transformer(arguments(eq)[1], f) * transformer(arguments(eq)[2] ^ -1, f)
23+
return transformer(arguments(eq)[1], f) * transformer(arguments(eq)[2]^-1, f)
2424
end
2525

2626
function transformer(eq::SymbolicUtils.Pow, f)
@@ -63,7 +63,7 @@ function substitute_x(eq, x, sub)
6363
end
6464

6565
function generate_homotopy(eq, x)
66-
eq = eq isa Num ? eq.val : eq
66+
eq = eq isa Num ? eq.val : eq
6767
q, sub = transform(eq, x)
6868
S = 0
6969

@@ -88,36 +88,32 @@ function ∂(x)
8888
return isequal(d, 0) ? 1 : d
8989
end
9090

91-
partial_int_rules = [
92-
@rule 𝛷(sin(~x)) => (cos(~x), (~x))
91+
partial_int_rules = [@rule 𝛷(sin(~x)) => (cos(~x), (~x))
9392
@rule 𝛷(cos(~x)) => (sin(~x), (~x))
9493
@rule 𝛷(tan(~x)) => (log(cos(~x)), (~x))
9594
@rule 𝛷(csc(~x)) => (log(csc(~x) + cot(~x)), (~x))
9695
@rule 𝛷(sec(~x)) => (log(sec(~x) + tan(~x)), (~x))
9796
@rule 𝛷(cot(~x)) => (log(sin(~x)), (~x))
98-
9997
@rule 𝛷(sinh(~x)) => (cosh(~x), (~x))
10098
@rule 𝛷(cosh(~x)) => (sinh(~x), (~x))
10199
@rule 𝛷(tanh(~x)) => (log(cosh(~x)), (~x))
102100
@rule 𝛷(csch(~x)) => (log(tanh(~x / 2)), (~x))
103101
@rule 𝛷(sech(~x)) => (atan(sinh(~x)), (~x))
104102
@rule 𝛷(coth(~x)) => (log(sinh(~x)), (~x))
105-
106-
@rule 𝛷(^(sin(~x), -1)) => (log(csc(~x) + cot(~x)), (~x))
103+
@rule 𝛷(^(sin(~x), -1)) => (log(csc(~x) + cot(~x)), (~x))
107104
@rule 𝛷(^(cos(~x), -1)) => (log(sec(~x) + tan(~x)), (~x))
108105
@rule 𝛷(^(tan(~x), -1)) => (log(sin(~x)), (~x))
109106
@rule 𝛷(^(csc(~x), -1)) => (cos(~x), (~x))
110107
@rule 𝛷(^(sec(~x), -1)) => (sin(~x), (~x))
111108
@rule 𝛷(^(cot(~x), -1)) => (log(cos(~x)), (~x))
112-
113109
@rule 𝛷(^(sinh(~x), -1)) => (log(tanh(~x / 2)), (~x))
114110
@rule 𝛷(^(cosh(~x), -1)) => (atan(sinh(~x)), (~x))
115111
@rule 𝛷(^(tanh(~x), -1)) => (log(sinh(~x)), (~x))
116112
@rule 𝛷(^(csch(~x), -1)) => (cosh(~x), (~x))
117113
@rule 𝛷(^(sech(~x), -1)) => (sinh(~x), (~x))
118114
@rule 𝛷(^(coth(~x), -1)) => (log(cosh(~x)), (~x))
119-
120-
# @rule 𝛷(^(sin(~x), ~k::is_neg)) => 𝛷(^(csc(~x), -~k))
115+
116+
# @rule 𝛷(^(sin(~x), ~k::is_neg)) => 𝛷(^(csc(~x), -~k))
121117
# @rule 𝛷(^(cos(~x), ~k::is_neg)) => 𝛷(^(sec(~x), -~k))
122118
# @rule 𝛷(^(tan(~x), ~k::is_neg)) => 𝛷(^(cot(~x), -~k))
123119
# @rule 𝛷(^(csc(~x), ~k::is_neg)) => 𝛷(^(sin(~x), -~k))
@@ -129,22 +125,19 @@ partial_int_rules = [
129125
# @rule 𝛷(^(csch(~x), ~k::is_neg)) => 𝛷(^(sinh(~x), -~k))
130126
# @rule 𝛷(^(sech(~x), ~k::is_neg)) => 𝛷(^(cosh(~x), -~k))
131127
# @rule 𝛷(^(coth(~x), ~k::is_neg)) => 𝛷(^(tanh(~x), -~k))
132-
133-
128+
134129
@rule 𝛷(asin(~x)) => (~x * asin(~x) + sqrt(1 - ~x * ~x), (~x))
135130
@rule 𝛷(acos(~x)) => (~x * acos(~x) + sqrt(1 - ~x * ~x), (~x))
136131
@rule 𝛷(atan(~x)) => (~x * atan(~x) + log(~x * ~x + 1), (~x))
137132
@rule 𝛷(acsc(~x)) => (~x * acsc(~x) + acosh(~x), (~x)) # needs an abs inside acosh
138133
@rule 𝛷(asec(~x)) => (~x * asec(~x) + acosh(~x), (~x)) # needs an abs inside acosh
139134
@rule 𝛷(acot(~x)) => (~x * acot(~x) + log(~x * ~x + 1), (~x))
140-
141135
@rule 𝛷(asinh(~x)) => (~x * asinh(~x) + sqrt(~x * ~x + 1), (~x))
142136
@rule 𝛷(acosh(~x)) => (~x * acosh(~x) + sqrt(~x * ~x - 1), (~x))
143137
@rule 𝛷(atanh(~x)) => (~x * atanh(~x) + log(~x + 1), (~x))
144138
@rule 𝛷(acsch(~x)) => (acsch(~x), (~x))
145139
@rule 𝛷(asech(~x)) => (asech(~x), (~x))
146140
@rule 𝛷(acoth(~x)) => (~x * acot(~x) + log(~x + 1), (~x))
147-
148141
@rule 𝛷(log(~x)) => (~x + ~x * log(~x), (~x))
149142
@rule 𝛷(^(~x, ~k::is_abs_half)) => (sum(candidate_sqrt(~x, ~k);
150143
init = one(~x)), 1);
@@ -154,7 +147,8 @@ partial_int_rules = [
154147
@rule 𝛷(sqrt(~x)) => (sum(candidate_sqrt(~x, 0.5); init = one(~x)), 1);
155148
@rule 𝛷(^(sqrt(~x), -1)) => 𝛷(^(~x, -0.5))
156149
@rule 𝛷(^(~x, -1)) => (log(~x), (~x))
157-
@rule 𝛷(^(~x, ~k::is_neg_int)) => (sum(^(~x, i) for i=~k+1:-1), (~x))
150+
@rule 𝛷(^(~x, ~k::is_neg_int)) => (sum(^(~x, i) for i in (~k + 1):-1),
151+
(~x))
158152
@rule 𝛷(1 / ~x) => 𝛷(^(~x, -1))
159153
@rule 𝛷(^(~x, ~k)) => (^(~x, ~k + 1), (~x))
160154
@rule 𝛷(exp(~x)) => (exp(~x), (~x))

src/integral.jl

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ function integrate(eq, x = nothing; abstol = 1e-6, num_steps = 2, num_trials = 5
6060
# homotopy = homotopy && !bypass
6161

6262
s, u, ϵ = integrate_sum(eq, x, l; bypass, abstol, num_trials, num_steps,
63-
radius, show_basis, opt, symbolic,
64-
max_basis, verbose, complex_plane, homotopy)
63+
radius, show_basis, opt, symbolic,
64+
max_basis, verbose, complex_plane, homotopy)
6565
return simplify(s), u, ϵ
6666
end
6767

@@ -77,7 +77,7 @@ end
7777
function integrate_sum(eq, x, l; bypass = false, kwargs...)
7878
solved = 0
7979
unsolved = 0
80-
ϵ₀ = 0
80+
ϵ₀ = 0
8181
ts = bypass ? [eq] : terms(eq)
8282

8383
if length(ts) > 1
@@ -88,16 +88,16 @@ function integrate_sum(eq, x, l; bypass = false, kwargs...)
8888
s, u, ϵ = integrate_term(p, x, l; kwargs...)
8989
solved += s
9090
unsolved += u
91-
ϵ₀ = max(ϵ₀, ϵ)
91+
ϵ₀ = max(ϵ₀, ϵ)
9292
end
9393

9494
if !isequal(unsolved, 0)
95-
eq = factor_rational(simplify_trigs(unsolved))
95+
eq = factor_rational(simplify_trigs(unsolved))
9696

9797
if !isequal(eq, unsolved)
9898
eq = expand(eq)
9999
unsolved = 0
100-
ϵ₀ = 0
100+
ϵ₀ = 0
101101
ts = bypass ? [eq] : terms(eq)
102102

103103
if length(ts) > 1
@@ -110,7 +110,7 @@ function integrate_sum(eq, x, l; bypass = false, kwargs...)
110110
s, u, ϵ = integrate_term(p, x, l; kwargs...)
111111
solved += s
112112
unsolved += u
113-
ϵ₀ = max(ϵ₀, ϵ)
113+
ϵ₀ = max(ϵ₀, ϵ)
114114

115115
if !isequal(u, 0) # premature termination on the first failure
116116
return 0, eq, ϵ₀
@@ -147,7 +147,7 @@ function integrate_term(eq, x, l; kwargs...)
147147
result(l, "Successful", y)
148148
return y, 0, 0
149149
end
150-
150+
151151
eq = cache(eq)
152152
basis = generate_basis(eq, x; homotopy)
153153

@@ -161,20 +161,21 @@ function integrate_term(eq, x, l; kwargs...)
161161
end
162162

163163
# D = Differential(x)
164-
ϵ₀ = Inf
164+
ϵ₀ = Inf
165165
y₀ = 0
166166

167167
# rescue
168-
ϵᵣ = Inf
169-
yᵣ = 0
168+
ϵᵣ = Inf
169+
yᵣ = 0
170170

171171
for i in 1:num_steps
172172
if length(basis) > max_basis
173173
break
174174
end
175175

176176
if symbolic
177-
y, ϵ = try_symbolic(Float64, expr(eq), x, expr.(basis), deriv!.(basis, x); kwargs...)
177+
y, ϵ = try_symbolic(Float64, expr(eq), x, expr.(basis), deriv!.(basis, x);
178+
kwargs...)
178179

179180
if !isequal(y, 0) && accept_solution(eq, x, y, radius) < abstol
180181
result(l, "Successful symbolic", y)
@@ -188,12 +189,12 @@ function integrate_term(eq, x, l; kwargs...)
188189
r = radius #*sqrt(2)^j
189190
y, ϵ = try_integrate(Float64, eq, x, basis, r; kwargs...)
190191

191-
ϵ = accept_solution(eq, x, y, r)
192+
ϵ = accept_solution(eq, x, y, r)
192193
if ϵ < abstol
193194
result(l, "Successful numeric (attempt $j out of $num_trials)", y)
194195
return y, 0, ϵ
195196
elseif ϵ < ϵᵣ
196-
ϵᵣ = ϵ
197+
ϵᵣ = ϵ
197198
yᵣ = y
198199
end
199200
end
@@ -202,7 +203,7 @@ function integrate_term(eq, x, l; kwargs...)
202203

203204
if i < num_steps
204205
basis = expand_basis(basis, x)
205-
206+
206207
if show_basis
207208
inform(l, "Expanding the basis (|β| = $(length(basis)))", basis)
208209
elseif verbose
@@ -302,27 +303,27 @@ function init_basis_matrix!(T, A, X, x, eq, basis, radius, complex_plane; abstol
302303
end
303304
end
304305

305-
function move_toward_roots_poles(z, x, eq; n=1, max_r=100.0)
306-
eq_fun = fun!(eq, x)
306+
function move_toward_roots_poles(z, x, eq; n = 1, max_r = 100.0)
307+
eq_fun = fun!(eq, x)
307308
Δeq_fun = deriv_fun!(eq, x)
308309
is_root = rand() < 0.5
309310
z₀ = z
310-
for i = 1:n
311-
dz = eq_fun(z) / Δeq_fun(z)
312-
if is_root
313-
z -= dz
314-
else
315-
z += dz
316-
end
317-
if abs(z) > max_r
318-
return z₀
319-
end
320-
end
311+
for i in 1:n
312+
dz = eq_fun(z) / Δeq_fun(z)
313+
if is_root
314+
z -= dz
315+
else
316+
z += dz
317+
end
318+
if abs(z) > max_r
319+
return z₀
320+
end
321+
end
321322
return z
322323
end
323324

324325
function modify_basis_matrix!(T, A, X, x, eq, basis, radius; abstol = 1e-6)
325-
n = size(A, 1)
326+
n = size(A, 1)
326327
eq_fun = fun!(eq, x)
327328
Δeq_fun = deriv_fun!(eq, x)
328329
Δbasis_fun = deriv_fun!.(basis, x)
@@ -331,7 +332,7 @@ function modify_basis_matrix!(T, A, X, x, eq, basis, radius; abstol = 1e-6)
331332
# One Newton iteration toward the poles
332333
# note the + sign instead of the usual - in Newton-Raphson's method. This is
333334
# because we are moving toward the poles and not zeros.
334-
335+
335336
X[k] += eq_fun(X[k]) / Δeq_fun(X[k])
336337
b₀ = eq_fun(X[k])
337338
for j in 1:n
@@ -351,12 +352,13 @@ function sparse_fit(T, A, x, basis, opt; abstol = 1e-6)
351352
# q₀ = A \ b
352353
q₀ = DataDrivenDiffEq.init(opt, A, b)
353354
@views sparse_regression!(q₀, A, permutedims(b)', opt, maxiter = 1000)
354-
ϵ = rms(A * q₀ - b)
355+
ϵ = rms(A * q₀ - b)
355356
q = nice_parameter.(q₀)
356357
if sum(iscomplex.(q)) > 2
357358
return nothing, Inf
358359
end # eliminating complex coefficients
359-
return sum(q[i] * expr(basis[i]) for i in 1:length(basis) if q[i] != 0; init = zero(x)),
360+
return sum(q[i] * expr(basis[i]) for i in 1:length(basis) if q[i] != 0;
361+
init = zero(x)),
360362
abs(ϵ)
361363
catch e
362364
println("Error from sparse_fit", e)
@@ -394,4 +396,3 @@ function find_dense(T, A, basis; abstol = 1e-6)
394396
end
395397
return nothing, Inf
396398
end
397-

src/numeric_utils.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,3 @@ function fibonacci_spiral(n, i)
6666
θ, r = mod((i - 1) / ϕ, 1), (i - 1) / n
6767
sqrt(r) * cis(2π * θ)
6868
end
69-

0 commit comments

Comments
 (0)