Skip to content
This repository was archived by the owner on Sep 28, 2024. It is now read-only.

Commit d259ce3

Browse files
committed
complete chebyshev_filter
1 parent 157728e commit d259ce3

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

src/Transform/polynomials.jl

+17-19
Original file line numberDiff line numberDiff line change
@@ -165,25 +165,23 @@ function chebyshev_filter(k)
165165
Φ1 = zeros(k, k)
166166
ϕ, ψ1, ψ2 = chebyshev_ϕ_ψ(k)
167167

168-
# ----------------------------------------------------------
169-
170-
# x = Symbol('x')
171-
# kUse = 2*k
172-
# roots = Poly(chebyshevt(kUse, 2*x-1)).all_roots()
173-
# x_m = np.array([rt.evalf(20) for rt in roots]).astype(np.float64)
174-
# # x_m[x_m==0.5] = 0.5 + 1e-8 # add small noise to avoid the case of 0.5 belonging to both phi(2x) and phi(2x-1)
175-
# # not needed for our purpose here, we use even k always to avoid
176-
# wm = np.pi / kUse / 2
177-
178-
# for ki in range(k):
179-
# for kpi in range(k):
180-
# H0[ki, kpi] = 1/np.sqrt(2) * (wm * phi[ki](x_m/2) * phi[kpi](x_m)).sum()
181-
# G0[ki, kpi] = 1/np.sqrt(2) * (wm * psi(psi1, psi2, ki, x_m/2) * phi[kpi](x_m)).sum()
182-
# H1[ki, kpi] = 1/np.sqrt(2) * (wm * phi[ki]((x_m+1)/2) * phi[kpi](x_m)).sum()
183-
# G1[ki, kpi] = 1/np.sqrt(2) * (wm * psi(psi1, psi2, ki, (x_m+1)/2) * phi[kpi](x_m)).sum()
184-
185-
# PHI0[ki, kpi] = (wm * phi[ki](2*x_m) * phi[kpi](2*x_m)).sum() * 2
186-
# PHI1[ki, kpi] = (wm * phi[ki](2*x_m-1) * phi[kpi](2*x_m-1)).sum() * 2
168+
k_use = 2k
169+
c = convert(Polynomial, gen_poly(Chebyshev, k_use))
170+
x_m = roots(c(Polynomial([-1, 2]))) # 2x-1
171+
# x_m[x_m==0.5] = 0.5 + 1e-8 # add small noise to avoid the case of 0.5 belonging to both phi(2x) and phi(2x-1)
172+
# not needed for our purpose here, we use even k always to avoid
173+
wm = π / k_use / 2
174+
175+
for ki in 0:(k-1)
176+
for kpi in 0:(k-1)
177+
H0[ki+1, kpi+1] = 1/sqrt(2) * sum(wm .* ϕ[ki+1].(x_m/2) .* ϕ[kpi+1].(x_m))
178+
H1[ki+1, kpi+1] = 1/sqrt(2) * sum(wm .* ϕ[ki+1].((x_m.+1)/2) .* ϕ[kpi+1].(x_m))
179+
G0[ki+1, kpi+1] = 1/sqrt(2) * sum(wm .* ψ(ψ1, ψ2, ki, x_m/2) .* ϕ[kpi+1].(x_m))
180+
G1[ki+1, kpi+1] = 1/sqrt(2) * sum(wm .* ψ(ψ1, ψ2, ki, (x_m.+1)/2) .* ϕ[kpi+1].(x_m))
181+
Φ0[ki+1, kpi+1] = 2*sum(wm .* ϕ[ki+1].(2x_m) .* ϕ[kpi+1].(2x_m))
182+
Φ1[ki+1, kpi+1] = 2*sum(wm .* ϕ[ki+1].(2 .* x_m .- 1) .* ϕ[kpi+1].(2 .* x_m .- 1))
183+
end
184+
end
187185

188186
zero_out!(H0)
189187
zero_out!(H1)

test/polynomials.jl

+19-19
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,25 @@
8585
end
8686

8787
@testset "chebyshev_filter" begin
88-
# H0, H1, G0, G1, Φ1, Φ2 = NeuralOperators.chebyshev_filter(3)
88+
H0, H1, G0, G1, Φ0, Φ1 = NeuralOperators.chebyshev_filter(3)
8989

90-
# @test H0 ≈ [0.70710678 0. 0. ;
91-
# -0.5 0.35355339 0. ;
92-
# -0.25 -0.70710678 0.1767767]
93-
# @test H1 ≈ [0.70710678 0. 0. ;
94-
# 0.5 0.35355339 0. ;
95-
# -0.25 0.70710678 0.1767767]
96-
# @test G0 ≈ [0.60944614 0.77940383 0. ;
97-
# 0.66325172 1.02726613 1.14270252;
98-
# 0.61723435 0.90708619 1.1562954 ]
99-
# @test G1 ≈ [-0.60944614 0.77940383 0. ;
100-
# 0.66325172 -1.02726613 1.14270252;
101-
# -0.61723435 0.90708619 -1.1562954 ]
102-
# @test Φ1 ≈ [1. -0.40715364 -0.21440101;
103-
# -0.40715364 0.84839559 -0.44820615;
104-
# -0.21440101 -0.44820615 0.84002127]
105-
# @test Φ2 ≈ [1. 0.40715364 -0.21440101;
106-
# 0.40715364 0.84839559 0.44820615;
107-
# -0.21440101 0.44820615 0.84002127]
90+
@test H0 [0.70710678 0. 0. ;
91+
-0.5 0.35355339 0. ;
92+
-0.25 -0.70710678 0.1767767]
93+
@test H1 [0.70710678 0. 0. ;
94+
0.5 0.35355339 0. ;
95+
-0.25 0.70710678 0.1767767]
96+
@test G0 [0.60944614 0.77940383 0. ;
97+
0.66325172 1.02726613 1.14270252;
98+
0.61723435 0.90708619 1.1562954 ]
99+
@test G1 [-0.60944614 0.77940383 0. ;
100+
0.66325172 -1.02726613 1.14270252;
101+
-0.61723435 0.90708619 -1.1562954 ]
102+
@test Φ0 [1. -0.40715364 -0.21440101;
103+
-0.40715364 0.84839559 -0.44820615;
104+
-0.21440101 -0.44820615 0.84002127]
105+
@test Φ1 [1. 0.40715364 -0.21440101;
106+
0.40715364 0.84839559 0.44820615;
107+
-0.21440101 0.44820615 0.84002127]
108108
end
109109
end

0 commit comments

Comments
 (0)