Skip to content

Commit 08f181f

Browse files
format SciML Style
1 parent 6c47a02 commit 08f181f

File tree

15 files changed

+502
-427
lines changed

15 files changed

+502
-427
lines changed

.JuliaFormatter.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style = "sciml"

.github/workflows/FormatCheck.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: format-check
2+
3+
on:
4+
push:
5+
branches:
6+
- 'master'
7+
- 'release-'
8+
tags: '*'
9+
pull_request:
10+
11+
jobs:
12+
build:
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
matrix:
16+
julia-version: [1]
17+
julia-arch: [x86]
18+
os: [ubuntu-latest]
19+
steps:
20+
- uses: julia-actions/setup-julia@latest
21+
with:
22+
version: ${{ matrix.julia-version }}
23+
24+
- uses: actions/checkout@v1
25+
- name: Install JuliaFormatter and format
26+
# This will use the latest version by default but you can set the version like so:
27+
#
28+
# julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))'
29+
run: |
30+
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
31+
julia -e 'using JuliaFormatter; format(".", verbose=true)'
32+
- name: Format check
33+
run: |
34+
julia -e '
35+
out = Cmd(`git diff --name-only`) |> read |> String
36+
if out == ""
37+
exit(0)
38+
else
39+
@error "Some files have not been formatted !!!"
40+
write(stdout, out)
41+
exit(1)
42+
end'

docs/make.jl

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@ using Documenter, SymbolicNumericIntegration
22

33
include("pages.jl")
44

5-
makedocs(
6-
sitename="SymbolicNumericIntegration.jl",
7-
authors="Shahriar Iravanian",
8-
modules=[SymbolicNumericIntegration],
9-
clean=true,doctest=false,
10-
format = Documenter.HTML(analytics = "UA-90474609-3",
11-
assets = ["assets/favicon.ico"],
12-
canonical="https://symbolicnumericintegration.sciml.ai/stable/"),
13-
pages=pages
14-
)
5+
makedocs(sitename = "SymbolicNumericIntegration.jl",
6+
authors = "Shahriar Iravanian",
7+
modules = [SymbolicNumericIntegration],
8+
clean = true, doctest = false,
9+
format = Documenter.HTML(analytics = "UA-90474609-3",
10+
assets = ["assets/favicon.ico"],
11+
canonical = "https://symbolicnumericintegration.sciml.ai/stable/"),
12+
pages = pages)
1513

16-
deploydocs(
17-
repo = "github.com/SciML/SymbolicNumericIntegration.jl.git";
18-
push_preview = true
19-
)
14+
deploydocs(repo = "github.com/SciML/SymbolicNumericIntegration.jl.git";
15+
push_preview = true)

docs/pages.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Put in a separate page so it can be used by SciMLDocs.jl
22

3-
pages=[
3+
pages = [
44
"Home" => "index.md",
5-
"symbolicnumericintegration.md"
6-
]
5+
"symbolicnumericintegration.md",
6+
]

src/candidates.jl

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using DataStructures
22

33
# this is the main heurisctic used to find the test fragments
4-
function generate_basis(eq, x; homotopy=true)
4+
function generate_basis(eq, x; homotopy = true)
55
# if homotopy return generate_homotopy(eq, x) end
66
eq = expand(eq)
77
S = 0 #Set{Any}()
@@ -22,9 +22,9 @@ function generate_basis(eq, x; homotopy=true)
2222
C₂ = find_candidates(p, x)
2323
end
2424

25-
S += sum(c₁*c₂ for c₁ in C₁ for c₂ in C₂)
25+
S += sum(c₁ * c₂ for c₁ in C₁ for c₂ in C₂)
2626
end
27-
return unique([one(x); [equivalent(t,x) for t in terms(S)]])
27+
return unique([one(x); [equivalent(t, x) for t in terms(S)]])
2828
end
2929

3030
function expand_basis(basis, x)
@@ -36,8 +36,10 @@ function expand_basis(basis, x)
3636
return [one(x); [s for s in S]]
3737
end
3838

39-
function closure(eq, x; max_terms=50)
40-
if !isdependent(eq, x) return [one(x)] end
39+
function closure(eq, x; max_terms = 50)
40+
if !isdependent(eq, x)
41+
return [one(x)]
42+
end
4143
D = Differential(x)
4244
S = Set{Any}()
4345
q = Queue{Any}()
@@ -47,7 +49,7 @@ function closure(eq, x; max_terms=50)
4749
y = dequeue!(q)
4850
enqueue_expr!(S, q, expand_derivatives(D(y)), x)
4951
end
50-
unique([one(x); [s for s in S]; [s*x for s in S]])
52+
unique([one(x); [s for s in S]; [s * x for s in S]])
5153
end
5254

5355
function find_candidates(eq, x)
@@ -66,17 +68,19 @@ function find_candidates(eq, x)
6668
return unique([one(x); [s for s in S]])
6769
end
6870

69-
function candidate_pow_minus(p, k; abstol=1e-8)
71+
function candidate_pow_minus(p, k; abstol = 1e-8)
7072
if isnan(poly_deg(p))
71-
return [p^k, p^(k+1), log(p)]
73+
return [p^k, p^(k + 1), log(p)]
7274
end
7375

7476
x = var(p)
7577
d = poly_deg(p)
7678

77-
for j = 1:10 # will try 10 times to find the roots
79+
for j in 1:10 # will try 10 times to find the roots
7880
r, s = find_roots(p, x)
79-
if length(r) + length(s) >= d break end
81+
if length(r) + length(s) >= d
82+
break
83+
end
8084
end
8185
s = s[1:2:end]
8286
r = nice_parameter.(r)
@@ -87,8 +91,8 @@ function candidate_pow_minus(p, k; abstol=1e-8)
8791
for i in eachindex(s)
8892
β = s[i]
8993
if abs(imag(β)) > abstol
90-
push!(q, atan((x - real(β))/imag(β)))
91-
push!(q, (1+x)*log(x^2 - 2*real(β)*x + abs2(β)))
94+
push!(q, atan((x - real(β)) / imag(β)))
95+
push!(q, (1 + x) * log(x^2 - 2 * real(β) * x + abs2(β)))
9296
else
9397
push!(q, log(x - real(β)))
9498
end
@@ -99,14 +103,14 @@ function candidate_pow_minus(p, k; abstol=1e-8)
99103
if k -1
100104
return [[p^k]; q]
101105
else
102-
return [[p^k, p^(k+1)]; q]
106+
return [[p^k, p^(k + 1)]; q]
103107
end
104108
end
105109

106110
function candidate_sqrt(p, k)
107111
x = var(p)
108112

109-
h = Any[p^k, p^(k+1)]
113+
h = Any[p^k, p^(k + 1)]
110114

111115
if poly_deg(p) == 2
112116
r, s = find_roots(p, x)
@@ -115,18 +119,18 @@ function candidate_sqrt(p, k)
115119
if sum(r) 0
116120
r₁ = abs(r[1])
117121
if l > 0
118-
push!(h, acosh(x/r₁))
122+
push!(h, acosh(x / r₁))
119123
else
120-
push!(h, asin(x/r₁))
124+
push!(h, asin(x / r₁))
121125
end
122126
end
123127
elseif real(s[1]) 0
124-
push!(h, asinh(x/imag.(s[1])))
128+
push!(h, asinh(x / imag.(s[1])))
125129
end
126130
end
127131

128132
Δ = expand_derivatives(Differential(x)(p))
129-
push!(h, log(0.5*Δ + sqrt(p)))
133+
push!(h, log(0.5 * Δ + sqrt(p)))
130134
return h
131135
end
132136

src/homotopy.jl

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@ function next_variable!(f, eq)
1313
return μ
1414
end
1515

16-
transformer(eq::SymbolicUtils.Add, f) = sum(transformer(t,f) for t in arguments(eq); init=0)
17-
transformer(eq::SymbolicUtils.Mul, f) = prod(transformer(t,f) for t in arguments(eq); init=1)
18-
transformer(eq::SymbolicUtils.Div, f) = transformer(arguments(eq)[1],f) * transformer(inv(arguments(eq)[2]),f)
16+
function transformer(eq::SymbolicUtils.Add, f)
17+
sum(transformer(t, f) for t in arguments(eq); init = 0)
18+
end
19+
function transformer(eq::SymbolicUtils.Mul, f)
20+
prod(transformer(t, f) for t in arguments(eq); init = 1)
21+
end
22+
function transformer(eq::SymbolicUtils.Div, f)
23+
transformer(arguments(eq)[1], f) * transformer(inv(arguments(eq)[2]), f)
24+
end
1925

2026
function transformer(eq::SymbolicUtils.Pow, f)
2127
y, k = arguments(eq)
2228

2329
if is_pos_int(k)
2430
μ = next_variable!(f, y)
25-
return μ ^ k
31+
return μ^k
2632
elseif is_neg_int(k)
2733
μ = next_variable!(f, inv(y))
28-
return μ ^ -k
34+
return μ^-k
2935
else
3036
return next_variable!(f, y^k)
3137
end
@@ -60,7 +66,7 @@ function generate_homotopy(eq, x)
6066
q, sub = transform(eq, x)
6167
S = 0
6268

63-
for i = 1:length(sub)
69+
for i in 1:length(sub)
6470
μ = u[i]
6571
h₁, ∂h₁ = apply_partial_int_rules(sub[μ])
6672
h₂ = expand_derivatives(Differential(μ)(q))
@@ -71,7 +77,7 @@ function generate_homotopy(eq, x)
7177
S += expand((1 + h₁) * (1 + h₂))
7278
end
7379

74-
unique([one(x); [equivalent(t,x) for t in terms(S)]])
80+
unique([one(x); [equivalent(t, x) for t in terms(S)]])
7581
end
7682

7783
##############################################################################
@@ -81,63 +87,57 @@ function ∂(x)
8187
return isequal(d, 0) ? 1 : d
8288
end
8389

84-
partial_int_rules = [
85-
@rule 𝛷(^(sin(~x), ~k::is_neg)) => 𝛷(^(csc(~x), -~k))
86-
@rule 𝛷(^(cos(~x), ~k::is_neg)) => 𝛷(^(sec(~x), -~k))
87-
@rule 𝛷(^(tan(~x), ~k::is_neg)) => 𝛷(^(cot(~x), -~k))
88-
@rule 𝛷(^(csc(~x), ~k::is_neg)) => 𝛷(^(sin(~x), -~k))
89-
@rule 𝛷(^(sec(~x), ~k::is_neg)) => 𝛷(^(cos(~x), -~k))
90-
@rule 𝛷(^(cot(~x), ~k::is_neg)) => 𝛷(^(tan(~x), -~k))
91-
92-
@rule 𝛷(^(sinh(~x), ~k::is_neg)) => 𝛷(^(csch(~x), -~k))
93-
@rule 𝛷(^(cosh(~x), ~k::is_neg)) => 𝛷(^(sech(~x), -~k))
94-
@rule 𝛷(^(tanh(~x), ~k::is_neg)) => 𝛷(^(coth(~x), -~k))
95-
@rule 𝛷(^(csch(~x), ~k::is_neg)) => 𝛷(^(sinh(~x), -~k))
96-
@rule 𝛷(^(sech(~x), ~k::is_neg)) => 𝛷(^(cosh(~x), -~k))
97-
@rule 𝛷(^(coth(~x), ~k::is_neg)) => 𝛷(^(tanh(~x), -~k))
98-
99-
@rule 𝛷(sin(~x)) => (cos(~x), (~x))
100-
@rule 𝛷(cos(~x)) => (sin(~x), (~x))
101-
@rule 𝛷(tan(~x)) => (log(cos(~x)), (~x))
102-
@rule 𝛷(csc(~x)) => (log(sin(~x)^-1 - cos(~x)*sin(~x)^-1), (~x))
103-
@rule 𝛷(sec(~x)) => (log(cos(~x)^-1 + sin(~x)*cos(~x)^-1), (~x))
104-
@rule 𝛷(cot(~x)) => (log(sin(~x)), (~x))
105-
106-
@rule 𝛷(sinh(~x)) => (cosh(~x), (~x))
107-
@rule 𝛷(cosh(~x)) => (sinh(~x), (~x))
108-
@rule 𝛷(tanh(~x)) => (log(cosh(~x)), (~x))
109-
@rule 𝛷(csch(~x)) => (log(sinh(~x)^-1 + cosh(~x)*sinh(~x)^-1), (~x))
110-
@rule 𝛷(sech(~x)) => (log(cosh(~x)^-1 + sinh(~x)*cosh(~x)^-1), (~x))
111-
@rule 𝛷(coth(~x)) => (log(sinh(~x)), (~x))
112-
113-
@rule 𝛷(asin(~x)) => (~x*asin(~x) + sqrt(1 - ~x*~x), (~x))
114-
@rule 𝛷(acos(~x)) => (~x*acos(~x) + sqrt(1 - ~x*~x), (~x))
115-
@rule 𝛷(atan(~x)) => (~x*atan(~x) + log(~x*~x + 1), (~x))
116-
@rule 𝛷(acsc(~x)) => (~x*acsc(~x) + acosh(~x), (~x)) # needs an abs inside acosh
117-
@rule 𝛷(asec(~x)) => (~x*asec(~x) + acosh(~x), (~x)) # needs an abs inside acosh
118-
@rule 𝛷(acot(~x)) => (~x*acot(~x) + log(~x*~x + 1), (~x))
119-
120-
@rule 𝛷(asinh(~x)) => (~x*asinh(~x) + sqrt(~x*~x + 1), (~x))
121-
@rule 𝛷(acosh(~x)) => (~x*acosh(~x) + sqrt(~x*~x - 1), (~x))
122-
@rule 𝛷(atanh(~x)) => (~x*atanh(~x) + log(~x + 1), (~x))
123-
@rule 𝛷(acsch(~x)) => (acsch(~x), (~x))
124-
@rule 𝛷(asech(~x)) => (asech(~x), (~x))
125-
@rule 𝛷(acoth(~x)) => (~x*acot(~x) + log(~x + 1), (~x))
126-
127-
@rule 𝛷(log(~x)) => (~x + ~x * log(~x), (~x))
128-
129-
@rule 𝛷(^(~x, ~k::is_abs_half)) => (sum(candidate_sqrt(~x,~k); init=one(~x)), 1)
130-
@rule 𝛷(^(~x::is_poly, ~k::is_neg)) => (sum(candidate_pow_minus(~x, ~k); init=one(~x)), 1)
131-
@rule 𝛷(sqrt(~x)) => (sum(candidate_sqrt(~x,0.5); init=one(~x)), 1)
132-
@rule 𝛷(^(sqrt(~x),-1)) => 𝛷(^(~x,-0.5))
133-
134-
@rule 𝛷(^(~x, -1)) => (log(~x), (~x))
135-
@rule 𝛷(1 / ~x) => 𝛷(^(~x, -1))
136-
@rule 𝛷(^(~x, ~k)) => (^(~x, ~k+1), (~x))
137-
138-
@rule 𝛷(exp(~x)) => (exp(~x), (~x))
139-
@rule 𝛷(1) => (𝑥, 1)
140-
@rule 𝛷(~x) => ((~x + ^(~x,2)), (~x))
141-
]
142-
143-
apply_partial_int_rules(eq) = expand(Fixpoint(Prewalk(Chain(partial_int_rules))))(𝛷(value(eq)))
90+
partial_int_rules = [@rule 𝛷(^(sin(~x), ~k::is_neg)) => 𝛷(^(csc(~x), -~k))
91+
@rule 𝛷(^(cos(~x), ~k::is_neg)) => 𝛷(^(sec(~x), -~k))
92+
@rule 𝛷(^(tan(~x), ~k::is_neg)) => 𝛷(^(cot(~x), -~k))
93+
@rule 𝛷(^(csc(~x), ~k::is_neg)) => 𝛷(^(sin(~x), -~k))
94+
@rule 𝛷(^(sec(~x), ~k::is_neg)) => 𝛷(^(cos(~x), -~k))
95+
@rule 𝛷(^(cot(~x), ~k::is_neg)) => 𝛷(^(tan(~x), -~k))
96+
@rule 𝛷(^(sinh(~x), ~k::is_neg)) => 𝛷(^(csch(~x), -~k))
97+
@rule 𝛷(^(cosh(~x), ~k::is_neg)) => 𝛷(^(sech(~x), -~k))
98+
@rule 𝛷(^(tanh(~x), ~k::is_neg)) => 𝛷(^(coth(~x), -~k))
99+
@rule 𝛷(^(csch(~x), ~k::is_neg)) => 𝛷(^(sinh(~x), -~k))
100+
@rule 𝛷(^(sech(~x), ~k::is_neg)) => 𝛷(^(cosh(~x), -~k))
101+
@rule 𝛷(^(coth(~x), ~k::is_neg)) => 𝛷(^(tanh(~x), -~k))
102+
@rule 𝛷(sin(~x)) => (cos(~x), (~x))
103+
@rule 𝛷(cos(~x)) => (sin(~x), (~x))
104+
@rule 𝛷(tan(~x)) => (log(cos(~x)), (~x))
105+
@rule 𝛷(csc(~x)) => (log(sin(~x)^-1 - cos(~x) * sin(~x)^-1), (~x))
106+
@rule 𝛷(sec(~x)) => (log(cos(~x)^-1 + sin(~x) * cos(~x)^-1), (~x))
107+
@rule 𝛷(cot(~x)) => (log(sin(~x)), (~x))
108+
@rule 𝛷(sinh(~x)) => (cosh(~x), (~x))
109+
@rule 𝛷(cosh(~x)) => (sinh(~x), (~x))
110+
@rule 𝛷(tanh(~x)) => (log(cosh(~x)), (~x))
111+
@rule 𝛷(csch(~x)) => (log(sinh(~x)^-1 + cosh(~x) * sinh(~x)^-1), (~x))
112+
@rule 𝛷(sech(~x)) => (log(cosh(~x)^-1 + sinh(~x) * cosh(~x)^-1), (~x))
113+
@rule 𝛷(coth(~x)) => (log(sinh(~x)), (~x))
114+
@rule 𝛷(asin(~x)) => (~x * asin(~x) + sqrt(1 - ~x * ~x), (~x))
115+
@rule 𝛷(acos(~x)) => (~x * acos(~x) + sqrt(1 - ~x * ~x), (~x))
116+
@rule 𝛷(atan(~x)) => (~x * atan(~x) + log(~x * ~x + 1), (~x))
117+
@rule 𝛷(acsc(~x)) => (~x * acsc(~x) + acosh(~x), (~x)) # needs an abs inside acosh
118+
@rule 𝛷(asec(~x)) => (~x * asec(~x) + acosh(~x), (~x)) # needs an abs inside acosh
119+
@rule 𝛷(acot(~x)) => (~x * acot(~x) + log(~x * ~x + 1), (~x))
120+
@rule 𝛷(asinh(~x)) => (~x * asinh(~x) + sqrt(~x * ~x + 1), (~x))
121+
@rule 𝛷(acosh(~x)) => (~x * acosh(~x) + sqrt(~x * ~x - 1), (~x))
122+
@rule 𝛷(atanh(~x)) => (~x * atanh(~x) + log(~x + 1), (~x))
123+
@rule 𝛷(acsch(~x)) => (acsch(~x), (~x))
124+
@rule 𝛷(asech(~x)) => (asech(~x), (~x))
125+
@rule 𝛷(acoth(~x)) => (~x * acot(~x) + log(~x + 1), (~x))
126+
@rule 𝛷(log(~x)) => (~x + ~x * log(~x), (~x))
127+
@rule 𝛷(^(~x, ~k::is_abs_half)) => (sum(candidate_sqrt(~x, ~k);
128+
init = one(~x)), 1);
129+
@rule 𝛷(^(~x::is_poly, ~k::is_neg)) => (sum(candidate_pow_minus(~x,
130+
~k);
131+
init = one(~x)), 1)
132+
@rule 𝛷(sqrt(~x)) => (sum(candidate_sqrt(~x, 0.5); init = one(~x)), 1);
133+
@rule 𝛷(^(sqrt(~x), -1)) => 𝛷(^(~x, -0.5))
134+
@rule 𝛷(^(~x, -1)) => (log(~x), (~x))
135+
@rule 𝛷(1 / ~x) => 𝛷(^(~x, -1))
136+
@rule 𝛷(^(~x, ~k)) => (^(~x, ~k + 1), (~x))
137+
@rule 𝛷(exp(~x)) => (exp(~x), (~x))
138+
@rule 𝛷(1) => (𝑥, 1)
139+
@rule 𝛷(~x) => ((~x + ^(~x, 2)), (~x))]
140+
141+
function apply_partial_int_rules(eq)
142+
expand(Fixpoint(Prewalk(Chain(partial_int_rules))))(𝛷(value(eq)))
143+
end

0 commit comments

Comments
 (0)