Skip to content

Commit

Permalink
Add tests for PiecewiseLegendrePolyVector
Browse files Browse the repository at this point in the history
  • Loading branch information
terasakisatoshi committed Nov 14, 2024
1 parent e7b487e commit e619fb8
Showing 1 changed file with 200 additions and 0 deletions.
200 changes: 200 additions & 0 deletions test/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,206 @@ isdefined(Main, :sve_logistic) || include("_conftest.jl")
end
end

@testset "PiecewiseLegendrePolyVector" begin
#=
julia> # The following data and knots are generated by
julia> using SparseIR
julia> sve_result = SparseIR.SVEResult(LogisticKernel(1.0))
julia> data1 = sve_result.u[1].data
julia> data2 = sve_result.u[2].data
julia> data3 = sve_result.u[3].data
julia> knots1 = sve_result.u[1].knots
julia> knots2 = sve_result.u[2].knots
julia> knots3 = sve_result.u[3].knots
julia> l1 = sve_result.u[1].l
julia> l2 = sve_result.u[2].l
julia> l3 = sve_result.u[3].l
=#
begin
data1 = reshape(
[
0.49996553669802485
-0.009838135710548356
0.003315915376286483
-2.4035906967802686e-5
3.4824832610792906e-6
-1.6818592059096e-8
1.5530850593697272e-9
-5.67191158452736e-12
3.8438802553084145e-13
-1.12861464373688e-15
-1.4028528586225198e-16
5.199431653846204e-18
-3.490774002228127e-16
4.339342349553959e-18
-8.247505551908268e-17
7.379549188001237e-19
0.49996553669802485
0.009838135710548356
0.003315915376286483
2.4035906967802686e-5
3.4824832610792906e-6
1.6818592059096e-8
1.5530850593697272e-9
5.67191158452736e-12
3.8438802553084145e-13
1.12861464373688e-15
-1.4028528586225198e-16
-5.199431653846204e-18
-3.490774002228127e-16
-4.339342349553959e-18
-8.247505551908268e-17
-7.379549188001237e-19
],
16,
2,
)

knots1 = [-1.0, 0.0, 1.0]
l1 = 0
end

begin
data2 = reshape(
[
-0.43195475509329695
0.436151579050162
-0.005257007544885257
0.0010660519696441624
-6.611545612452212e-6
7.461310619506964e-7
-3.2179499894475862e-9
2.5166526274315926e-10
-8.387341925898803e-13
5.008268649326024e-14
3.7750894390998034e-17
-2.304983535459561e-16
3.0252856483620636e-16
-1.923751082183687e-16
7.201014354168769e-17
-3.2715804561902326e-17
0.43195475509329695
0.436151579050162
0.005257007544885257
0.0010660519696441624
6.611545612452212e-6
7.461310619506964e-7
3.2179499894475862e-9
2.5166526274315926e-10
8.387341925898803e-13
5.008268649326024e-14
-3.7750894390998034e-17
-2.304983535459561e-16
-3.0252856483620636e-16
-1.923751082183687e-16
-7.201014354168769e-17
-3.2715804561902326e-17
],
16,
2,
)

knots2 = [-1.0, 0.0, 1.0]
l2 = 1
end

begin
data3 = reshape(
[
-0.005870438661638806
-0.8376202388555938
0.28368166184926036
-0.0029450618222246236
0.0004277118923277169
-2.4101642603229184e-6
2.2287962786878678e-7
-8.875091544426018e-10
6.021488924175155e-11
-1.8705305570705647e-13
9.924398482443944e-15
4.299521053905097e-16
-1.0697019178666955e-16
3.6972269778329906e-16
-8.848885164903329e-17
6.327687614609368e-17
-0.005870438661638806
0.8376202388555938
0.28368166184926036
0.0029450618222246236
0.0004277118923277169
2.4101642603229184e-6
2.2287962786878678e-7
8.875091544426018e-10
6.021488924175155e-11
1.8705305570705647e-13
9.924398482443944e-15
-4.299521053905097e-16
-1.0697019178666955e-16
-3.6972269778329906e-16
-8.848885164903329e-17
-6.327687614609368e-17
],
16,
2,
)

knots3 = [-1.0, 0.0, 1.0]
l3 = 2
end

pwlp1 = SparseIR.PiecewiseLegendrePoly(data1, knots1, l1)
pwlp2 = SparseIR.PiecewiseLegendrePoly(data2, knots2, l2)
pwlp3 = SparseIR.PiecewiseLegendrePoly(data3, knots3, l3)

polys = SparseIR.PiecewiseLegendrePolyVector([pwlp1, pwlp2, pwlp3])

@test length(polys) == 3

polys = SparseIR.PiecewiseLegendrePolyVector(
[
SparseIR.PiecewiseLegendrePoly(data1, knots1, l1)
SparseIR.PiecewiseLegendrePoly(data2, knots2, l2)
SparseIR.PiecewiseLegendrePoly(data3, knots3, l3)
],
)

@test SparseIR.xmin(polys) == SparseIR.xmin(pwlp1)
@test SparseIR.xmax(polys) == SparseIR.xmax(pwlp1)
@test SparseIR.knots(polys) == SparseIR.knots(pwlp1)
@test SparseIR.Δx(polys) == SparseIR.Δx(pwlp1)
@test SparseIR.polyorder(polys) == SparseIR.polyorder(pwlp1)
@test SparseIR.norms(polys) == SparseIR.norms(pwlp1)
@test SparseIR.symm(polys) == SparseIR.symm.([pwlp1, pwlp2, pwlp3])

@testset "polys(x::Float64)" begin
x = rand(StableRNG(42))
@test polys(x) == [pwlp1(x), pwlp2(x), pwlp3(x)]
@test SparseIR.data(polys) ==
cat(SparseIR.data.([pwlp1, pwlp2, pwlp3])..., dims = 3)
end

@testset "polys(x::Array)" begin
x = rand(StableRNG(42), 2, 1, 4)
tar = polys(x)
@test size(tar) == (3, 2, 1, 4)
ref = reshape(vcat([[pwlp1(e), pwlp2(e), pwlp3(e)] for e in x]...), 3, 2, 1, 4)
@test tar == ref
end

@testset "PiecewiseLegendrePolyVector(polys::PiecewiseLegendrePolyVector, knots::AbstractVector)" begin
new_knots = [-1.0, 0.0, 1.0]

new_polys = SparseIR.PiecewiseLegendrePolyVector(
polys, new_knots,
symm=zeros(Int, size(SparseIR.data(polys), 3)),
)

@test SparseIR.data(new_polys) == SparseIR.data(polys)
@test SparseIR.knots(new_polys) == SparseIR.knots(new_polys)
@test SparseIR.Δx(new_polys) == diff(new_knots)
end
end

@testset "deriv" begin
# independent from sve.jl
# https://github.com/SpM-lab/SparseIR.jl/issues/51
Expand Down

0 comments on commit e619fb8

Please sign in to comment.