Skip to content

Commit b725a0c

Browse files
homotopy rules expanded
1 parent 14a7fd0 commit b725a0c

File tree

5 files changed

+62
-32
lines changed

5 files changed

+62
-32
lines changed

src/candidates.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using DataStructures
22

33
# this is the main heurisctic used to find the test fragments
44
function generate_basis(eq, x; homotopy=false)
5+
# if homotopy return generate_homotopy2(eq, x) end
56
eq = expand(eq)
67
S = Set{Any}()
78
for t in terms(eq)

src/homotopy.jl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,38 @@ function generate_homotopy2(eq, x)
138138
h₂ = substitute_x(h₂, x, sub)
139139

140140
H = sum((Differential(x)^i)(h₂) for i=1:d-1; init=(1 + h₂))
141-
I = expand(expand_derivatives((1 + h₁) * H))
141+
I = expand(expand_derivatives((1 + h₁) * H))
142+
# H = sum((Differential(x)^i)(h₁*h₂) for i=1:d-1; init=(h₁ + h₂ + h₁*h₂))
143+
# I = expand(expand_derivatives(H))
142144
enqueue_expr_ex!(S, I, x)
143145
end
144146

145-
# H = sum((Differential(x)^i)(eq) for i=1:deg-1; init=eq)
146-
# I = expand((1+x) * expand_derivatives(H))
147-
# enqueue_expr_ex!(S, I, x)
148-
149-
# return [one(x); [s for s in S]]
150147
return [one(x); [s for s in S]]
151148
end
152149

153150
##############################################################################
154151

155152
(x) = expand_derivatives(Differential(𝑥)(x))
153+
cabs(x) = sqrt(x * conj(x))
156154

157155
H_rules = [
158156
# @rule 𝛷(+(~~xs)) => sum(map(𝛷, ~~xs))
159157
# @rule 𝛷(*(~~xs)) => prod(map(𝛷, ~~xs))
160158

159+
@rule 𝛷(^(sin(~x), ~k::is_neg)) => 𝛷(^(csc(~x), -~k))
160+
@rule 𝛷(^(cos(~x), ~k::is_neg)) => 𝛷(^(sec(~x), -~k))
161+
@rule 𝛷(^(tan(~x), ~k::is_neg)) => 𝛷(^(cot(~x), -~k))
162+
@rule 𝛷(^(csc(~x), ~k::is_neg)) => 𝛷(^(sin(~x), -~k))
163+
@rule 𝛷(^(sec(~x), ~k::is_neg)) => 𝛷(^(cos(~x), -~k))
164+
@rule 𝛷(^(cot(~x), ~k::is_neg)) => 𝛷(^(tan(~x), -~k))
165+
166+
@rule 𝛷(^(sinh(~x), ~k::is_neg)) => 𝛷(^(csch(~x), -~k))
167+
@rule 𝛷(^(cosh(~x), ~k::is_neg)) => 𝛷(^(sech(~x), -~k))
168+
@rule 𝛷(^(tanh(~x), ~k::is_neg)) => 𝛷(^(coth(~x), -~k))
169+
@rule 𝛷(^(csch(~x), ~k::is_neg)) => 𝛷(^(sinh(~x), -~k))
170+
@rule 𝛷(^(sech(~x), ~k::is_neg)) => 𝛷(^(cosh(~x), -~k))
171+
@rule 𝛷(^(coth(~x), ~k::is_neg)) => 𝛷(^(tanh(~x), -~k))
172+
161173
@rule 𝛷(sin(~x)) => cos(~x) * (~x)^-1
162174
@rule 𝛷(cos(~x)) => sin(~x) * (~x)^-1
163175
@rule 𝛷(tan(~x)) => log(cos(~x)) * (~x)^-1
@@ -175,8 +187,8 @@ H_rules = [
175187
@rule 𝛷(asin(~x)) => (~x*asin(~x) + sqrt(1 - ~x*~x)) * (~x)^-1
176188
@rule 𝛷(acos(~x)) => (~x*acos(~x) + sqrt(1 - ~x*~x)) * (~x)^-1
177189
@rule 𝛷(atan(~x)) => (~x*atan(~x) + log(~x*~x + 1)) * (~x)^-1
178-
@rule 𝛷(acsc(~x)) => acsc(~x) * (~x)^-1
179-
@rule 𝛷(asec(~x)) => asec(~x) * (~x)^-1
190+
@rule 𝛷(acsc(~x)) => (~x*acsc(~x) + acosh(cabs(~x))) * (~x)^-1
191+
@rule 𝛷(asec(~x)) => (~x*asec(~x) + acosh(cabs(~x))) * (~x)^-1
180192
@rule 𝛷(acot(~x)) => (~x*acot(~x) + log(~x*~x + 1)) * (~x)^-1
181193

182194
@rule 𝛷(asinh(~x)) => (~x*asinh(~x) + sqrt(~x*~x + 1)) * (~x)^-1

src/integral.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ function try_integrate(T, eq, x, basis, Δbasis, radius; kwargs...)
351351
end
352352

353353
function init_basis_matrix!(T, A, X, x, eq, Δbasis, radius, complex_plane; abstol=1e-6)
354-
n = size(A, 1)
355-
# X = zeros(Complex{T}, n)
354+
n = size(A, 1)
356355
k = 1
357356
i = 1
358357

@@ -372,7 +371,7 @@ function init_basis_matrix!(T, A, X, x, eq, Δbasis, radius, complex_plane; abst
372371
end
373372
end
374373
catch e
375-
println(e)
374+
println("Error from init_basis_matrix!: ", e)
376375
end
377376
end
378377
end
@@ -408,7 +407,7 @@ function sparse_fit(T, A, x, basis, Δbasis, opt; abstol=1e-6)
408407
q = nice_parameter.(q₀)
409408
return sum(q[i]*basis[i] for i = 1:length(basis) if q[i] != 0; init=zero(x)), abs(ϵ)
410409
catch e
411-
println(e)
410+
println("Error from spart_fit", e)
412411
return nothing, Inf
413412
end
414413
end

src/rules.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function decompose_rational(eq)
170170
r = nice_parameter.(r)
171171
s = nice_parameter.(s)
172172

173-
F = [(x^2 - 2*real(u)*x + abs2(u))^-1 for u in s]
173+
F = Any[(x^2 - 2*real(u)*x + abs2(u))^-1 for u in s]
174174
[x*(x^2 - 2*real(u)*x + abs2(u))^-1 for u in s]
175175
for i in eachindex(r)
176176
μ = sum(r[1:i] .== r[i])

test/axiom.jl

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,52 @@ using SymbolicUtils.Rewriters
55
using PyCall
66
sympy = pyimport("sympy")
77

8-
@syms x t 𝐞
8+
function convert_axiom(name::AbstractString)
9+
@syms 𝐞 a b c d e f g h i j k l m n o p q r s t u v w x y z
910

10-
r1 = @rule ^(𝐞, ~k) => exp(~k)
11-
r2 = @rule 𝐞 => MathConstants.e
11+
axion_rules = [
12+
@rule ^(𝐞, ~k) => exp(~k)
13+
@rule 𝐞 => MathConstants.e
14+
]
1215

13-
function convert_axiom(name::AbstractString)
14-
f = open(name, "r")
15-
r = r"\[.*\]"
16+
fd = open(name, "r")
17+
re = r"\[.*\]"
1618
L = []
1719

18-
for l in readlines(f)
19-
l = strip(l)
20-
m = match(r, l)
21-
if m != nothing
22-
l = replace(m.match, "%e" => 𝐞)
23-
l = replace(m.match, "%i" => im)
20+
D = Dict{Any,Int}()
21+
22+
for (lineno,line) in enumerate(readlines(fd))
23+
line = strip(line)
24+
ma = match(re, line)
25+
if ma != nothing
26+
line = replace(ma.match, "%e" => 𝐞)
27+
line = replace(ma.match, "%i" => im)
28+
2429
try
25-
expr = Meta.parse(l)
26-
p = eval(expr)
27-
p[1] = Prewalk(PassThrough(Chain([r1,r2])))(p[1])
28-
p[4] = Prewalk(PassThrough(Chain([r1,r2])))(p[4])
29-
push!(L, p)
30-
catch e
30+
expr = Meta.parse(line)
31+
P = eval(expr)
32+
33+
print(lineno, ": ", P[1], " => ")
34+
P[1] = Prewalk(PassThrough(Chain(axion_rules)))(P[1])
35+
P[4] = Prewalk(PassThrough(Chain(axion_rules)))(P[4])
36+
37+
for ν in get_variables(P[1])
38+
if !isequal(ν, x) && !haskey(D, ν)
39+
D[ν] = length(D) + 1
40+
end
41+
end
42+
43+
P[1] = substitute(P[1], D)
44+
P[4] = substitute(P[4], D)
45+
46+
println(P[1])
47+
push!(L, P)
48+
catch err
3149
end
3250
end
3351
end
3452

35-
close(f)
53+
close(fd)
3654
return L
3755
end
3856

0 commit comments

Comments
 (0)