Skip to content

Commit 72c264a

Browse files
committed
Fix weighted FunctionProducts in restricted bases
1 parent 19376c5 commit 72c264a

File tree

3 files changed

+87
-42
lines changed

3 files changed

+87
-42
lines changed

src/densities.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ function FunctionProduct{Conjugated}(L::BasisOrRestricted, R::BasisOrRestricted,
7575
# Orthogonal case
7676
RV = RV[indices(R,2),:]
7777
uniform = all(isone, RV.data)
78+
w = (w isa AbstractMatrix ? BandedMatrix(w)[indices(R,2),indices(R,2)] : w)
7879
C = if uniform
7980
w
8081
else
81-
RV*(w isa AbstractMatrix ? w[indices(R,2),indices(R,2)] : w)
82+
RV*w
83+
end
84+
if C isa AbstractMatrix && bandwidths(C) == (0,0)
85+
C = Diagonal(C)
8286
end
8387
tmp = C === I ? nothing : tmpvec(T, size(RV, 1), nρ)
8488
FunctionProduct{Conjugated}(ρρ,

src/finite_differences.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ end
198198
schafer_corner_fix(ρ, Z) = 1/ρ^2*Z*ρ/8*(1 + Z*ρ)
199199

200200
function log_lin_grid!(r, ρₘᵢₙ, ρₘₐₓ, α, js)
201+
0 < ρₘᵢₙ < Inf && 0 < ρₘₐₓ < Inf ||
202+
throw(DomainError((ρₘᵢₙ, ρₘₐₓ), "Log–linear grid steps need to be finite and larger than zero"))
201203
δρ = ρₘₐₓ-ρₘᵢₙ
202204
for j = js
203205
r[j] = r[j-1] + ρₘᵢₙ + (1-exp(-α*r[j-1]))*δρ

test/densities.jl

Lines changed: 80 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
@testset "Densities" begin
2-
f = x -> sin(2π*x)
3-
g = x -> x*exp(-x)
4-
h = x -> sin(2π*x)*x*exp(-x)
5-
6-
rmin = 0.01
7-
rmax = 10.0
8-
ρ = 0.05
9-
N = ceil(Int, rmax/ρ)
10-
k = 7
11-
Nn = 71
12-
13-
ρmin=rmin
14-
ρmax=0.5
15-
α=0.01
16-
17-
@testset "$R" for (R,kind,rtol) in [
18-
(FiniteDifferences(N, ρ), :orthogonal_uniform, 1e-14),
19-
(StaggeredFiniteDifferences(ρmin, ρmax, α, rmax, 0.0), :orthogonal_non_uniform, 1e-14),
20-
(FEDVR(range(0, stop=rmax, length=Nn), k), :orthogonal_non_uniform, 1e-14),
21-
(BSpline(LinearKnotSet(k, 0, rmax, Nn)), :non_orthogonal, 1e-5),
22-
]
23-
r = axes(R,1)
24-
25-
cf = R \ f.(r)
26-
cg = R \ g.(r)
27-
ch = R \ h.(r)
28-
29-
ρ = Density(applied(*,R,cf), applied(*,R,cg))
30-
ch2 = ρ.ρ
31-
32-
@test eltype(ρ) == eltype(cf)
33-
34-
if kind == :orthogonal_uniform
35-
@test ρ.LV == I
36-
@test ρ.RV == I
37-
@test ρ.C == I
38-
elseif kind == :orthogonal_non_uniform
39-
@test ρ.LV == I
40-
@test ρ.RV == I
41-
end
2+
@testset "Single functions" begin
3+
rmin = 0.01
4+
rmax = 10.0
5+
δr = 0.05
6+
N = ceil(Int, rmax/δr)
7+
k = 7
8+
Nn = 71
9+
10+
δrmin=rmin
11+
δrmax=0.5
12+
α=0.01
13+
14+
@testset "$R" for (R,kind,rtol) in [
15+
(FiniteDifferences(N, δr), :orthogonal_uniform, 1e-14),
16+
(StaggeredFiniteDifferences(δrmin, δrmax, α, rmax, 0.0), :orthogonal_non_uniform, 1e-14),
17+
(FEDVR(range(0, stop=rmax, length=Nn), k), :orthogonal_non_uniform, 1e-14),
18+
(BSpline(LinearKnotSet(k, 0, rmax, Nn)), :non_orthogonal, 1e-5),
19+
]
20+
f = x -> sin(2π*x)
21+
g = x -> x*exp(-x)
22+
h = x -> sin(2π*x)*x*exp(-x)
23+
24+
r = axes(R,1)
25+
26+
cf = R \ f.(r)
27+
cg = R \ g.(r)
28+
ch = R \ h.(r)
29+
30+
ρ = Density(applied(*,R,cf), applied(*,R,cg))
31+
ch2 = ρ.ρ
32+
33+
@test eltype(ρ) == eltype(cf)
34+
35+
if kind == :orthogonal_uniform
36+
@test ρ.LV == I
37+
@test ρ.RV == I
38+
@test ρ.C == I
39+
elseif kind == :orthogonal_non_uniform
40+
@test ρ.LV == I
41+
@test ρ.RV == I
42+
end
4243

43-
@test ch2 ch atol=1e-14 rtol=rtol
44+
@test ch2 ch atol=1e-14 rtol=rtol
45+
end
4446
end
4547

4648
@testset "Multiple functions" begin
@@ -86,6 +88,43 @@
8688
end
8789
end
8890

91+
@testset "Restricted bases" begin
92+
rmin = 0.0
93+
rmax = 10.0
94+
δr = 0.05
95+
N = ceil(Int, rmax/δr)
96+
k = 7
97+
Nn = 71
98+
99+
δrmin=0.01
100+
δrmax=0.5
101+
α=0.01
102+
103+
f = x -> sinpi(x/rmax)
104+
g = x -> cospi(x/rmax)
105+
@testset "$(label)" for (w,label) in [(one,"Unweighted"),(exp,"Weighted")]
106+
h = x -> f(x)*w(x)*g(x)
107+
108+
@testset "$R" for (R,rtol) in [
109+
(FiniteDifferences(N, δr), 1e-15),
110+
(StaggeredFiniteDifferences(δrmin, δrmax, α, rmax, 0.0), 1e-15),
111+
(FEDVR(range(0, stop=rmax, length=Nn), k), 1e-15),
112+
(BSpline(LinearKnotSet(k, 0, rmax, Nn)), 1e-14),
113+
]
114+
display(R)
115+
= R[:,1:ceil(Int, size(R,2))]
116+
117+
r = axes(R̃,1)
118+
119+
u =\ f.(r)
120+
v =\ g.(r)
121+
122+
ρ = Density(applied(*, R̃, u), applied(*, R̃, v), w=w)
123+
@test ρ.ρ (R̃ \ h.(r)) rtol=rtol
124+
end
125+
end
126+
end
127+
89128
@testset "Pretty-printing" begin
90129
R = FiniteDifferences(99, 0.1)
91130
u = R*rand(size(R,2))

0 commit comments

Comments
 (0)