Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tests for PiecewiseLegendrePolyVector #53

Closed
terasakisatoshi opened this issue Nov 12, 2024 · 1 comment · Fixed by #55
Closed

Implement tests for PiecewiseLegendrePolyVector #53

terasakisatoshi opened this issue Nov 12, 2024 · 1 comment · Fixed by #55

Comments

@terasakisatoshi
Copy link
Contributor

Ref #51

@terasakisatoshi
Copy link
Contributor Author

Using Pluto notebook, I'm writing tests

using Markdown
using InteractiveUtils

# ╔═╡ e59fe3f2-a166-11ef-34c5-e76693e02115
using SparseIR

# ╔═╡ 1876a95e-8cef-4add-af2c-5af171f1b6f6
begin
    using StableRNGs: StableRNG
    using Test
end

# ╔═╡ f9841528-e9ad-4ffa-bc39-3b36506ce149
sve_result = SparseIR.SVEResult(LogisticKernel(1.0))

# ╔═╡ 1c69cea9-cc82-4c8e-86b4-ae0a652fd759
foreach(println, sve_result.u[1].data)

# ╔═╡ 3b56eb73-26f2-4c10-9262-ff3a330bf07e
sve_result.u[1].knots

# ╔═╡ 5d06aa6a-2e4a-4c0e-af1a-bacf93d23fcf
sve_result.u[1].l

# ╔═╡ afef9f6c-f1d5-4ddd-aa70-cba3a4818293
foreach(println, sve_result.u[2].data)

# ╔═╡ 3b0f871a-0223-4f0a-9a72-3c0fe646762c
sve_result.u[2].knots

# ╔═╡ 7ecec192-d154-4e49-b86e-9b572ca831bc
foreach(println, sve_result.u[3].data)

# ╔═╡ 0f318cd4-b81b-471c-99af-e827b6ca0db7
sve_result.u[3].knots

# ╔═╡ 5d1fa13c-da4d-4a80-b1e3-5723a7822652
sve_result.u[3].l

# ╔═╡ ec27c11b-6910-410f-9cca-dfa1d486b7ef
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

# ╔═╡ 4f869c8a-2266-4b46-a087-4a1bd816be3b
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

# ╔═╡ 679b430d-156c-42bf-8494-6bef308b635c
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

# ╔═╡ bf5a1d96-261a-41dd-99d0-284e8f00354d
begin
    pwlp1 = SparseIR.PiecewiseLegendrePoly(data1, knots1, l1)
    pwlp2 = SparseIR.PiecewiseLegendrePoly(data2, knots2, l2)
    pwlp3 = SparseIR.PiecewiseLegendrePoly(data3, knots3, l3)

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

# ╔═╡ c0f095dd-d1fd-4c54-87b1-6ce00a7e49c9
@testset "PiecewiseLegendrePolyVector" begin
    @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
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant