comparison test/Grids/grid_test.jl @ 1854:654a2b7e6824 tooling/benchmarks

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 11 Jan 2025 10:19:47 +0100
parents 471a948cd2b2
children
comparison
equal deleted inserted replaced
1378:2b5480e2d4bf 1854:654a2b7e6824
1 using Test 1 using Test
2 using Sbplib.Grids 2 using Diffinitive.Grids
3 using Sbplib.LazyTensors 3 using Diffinitive.LazyTensors
4 using StaticArrays 4 using StaticArrays
5 5
6 @testset "Grid" begin 6 @testset "Grid" begin
7 struct DummyGrid{T,D} <: Grid{T,D} end 7 struct DummyGrid{T,D} <: Grid{T,D} end
8 8
13 13
14 @test coordinate_size(DummyGrid{Int, 1}()) == 1 14 @test coordinate_size(DummyGrid{Int, 1}()) == 1
15 @test coordinate_size(DummyGrid{SVector{3,Float64}, 2}()) == 3 15 @test coordinate_size(DummyGrid{SVector{3,Float64}, 2}()) == 3
16 16
17 @test coordinate_size(DummyGrid{SVector{3,Float64}, 2}) == 3 17 @test coordinate_size(DummyGrid{SVector{3,Float64}, 2}) == 3
18 end
18 19
19 @testset "component_type" begin 20 @testset "component_type" begin
20 @test component_type(DummyGrid{Int,1}()) == Int 21 @test component_type(DummyGrid{Int,1}()) == Int
21 @test component_type(DummyGrid{Float64,1}()) == Float64 22 @test component_type(DummyGrid{Float64,1}()) == Float64
22 @test component_type(DummyGrid{Rational,1}()) == Rational 23 @test component_type(DummyGrid{Rational,1}()) == Rational
23 24
24 @test component_type(DummyGrid{SVector{3,Int},2}()) == Int 25 @test component_type(DummyGrid{SVector{3,Int},2}()) == Int
25 @test component_type(DummyGrid{SVector{2,Float64},3}()) == Float64 26 @test component_type(DummyGrid{SVector{2,Float64},3}()) == Float64
26 @test component_type(DummyGrid{SVector{4,Rational},4}()) == Rational 27 @test component_type(DummyGrid{SVector{4,Rational},4}()) == Rational
27 28
28 @test component_type(DummyGrid{Float64,1}) == Float64 29 @test component_type(DummyGrid{Float64,1}) == Float64
29 @test component_type(DummyGrid{SVector{2,Float64},3}) == Float64 30 @test component_type(DummyGrid{SVector{2,Float64},3}) == Float64
30 end 31
32 @test component_type(fill(@SVector[1,2], 4,2)) == Int
31 end 33 end
32 34
33 @testset "eval_on" begin 35 @testset "eval_on" begin
34 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), x̄->x̄[1]+x̄[2]) isa LazyArray 36 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), x̄->x̄[1]+x̄[2]) isa LazyArray
35 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), x̄->x̄[1]+x̄[2]) == fill(3.) 37 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), x̄->x̄[1]+x̄[2]) == fill(3.)
36 @test eval_on(ZeroDimGrid(@SVector[3.,2.]), x̄->x̄[1]+x̄[2]) == fill(5.) 38 @test eval_on(ZeroDimGrid(@SVector[3.,2.]), x̄->x̄[1]+x̄[2]) == fill(5.)
37 39
38 @test eval_on(ZeroDimGrid(1.), x̄->2x̄) isa LazyArray 40 @test eval_on(ZeroDimGrid(1.), x̄->2x̄) isa LazyArray
39 @test eval_on(ZeroDimGrid(1.), x̄->2x̄) == fill(2.) 41 @test eval_on(ZeroDimGrid(1.), x̄->2x̄) == fill(2.)
40 42
43 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), π) isa LazyArray
44 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), π) == fill(π)
45
41 @test eval_on(EquidistantGrid(range(0,1,length=4)), x->2x) isa LazyArray 46 @test eval_on(EquidistantGrid(range(0,1,length=4)), x->2x) isa LazyArray
42 @test eval_on(EquidistantGrid(range(0,1,length=4)), x->2x) == 2 .* range(0,1,length=4) 47 @test eval_on(EquidistantGrid(range(0,1,length=4)), x->2x) == 2 .* range(0,1,length=4)
43 48
44 49
45 g = equidistant_grid((5,3), (0.0,0.0), (2.0,1.0)) 50 g = equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3)
46 51
47 @test eval_on(g, x̄ -> 0.) isa LazyArray 52 @test eval_on(g, x̄ -> 0.) isa LazyArray
48 @test eval_on(g, x̄ -> 0.) == fill(0., (5,3)) 53 @test eval_on(g, x̄ -> 0.) == fill(0., (5,3))
49 54
50 @test eval_on(g, x̄ -> sin(x̄[1])*cos(x̄[2])) == map(x̄->sin(x̄[1])*cos(x̄[2]), g) 55 @test eval_on(g, x̄ -> sin(x̄[1])*cos(x̄[2])) == map(x̄->sin(x̄[1])*cos(x̄[2]), g)
56
57 @test eval_on(g, π) == fill(π, (5,3))
51 58
52 # Vector valued function 59 # Vector valued function
53 @test eval_on(g, x̄ -> @SVector[x̄[2], x̄[1]]) isa LazyArray{SVector{2,Float64}} 60 @test eval_on(g, x̄ -> @SVector[x̄[2], x̄[1]]) isa LazyArray{SVector{2,Float64}}
54 @test eval_on(g, x̄ -> @SVector[x̄[2], x̄[1]]) == map(x̄ -> @SVector[x̄[2], x̄[1]], g) 61 @test eval_on(g, x̄ -> @SVector[x̄[2], x̄[1]]) == map(x̄ -> @SVector[x̄[2], x̄[1]], g)
55 62
56 # Multi-argument functions 63 # Multi-argument functions
57 f(x,y) = sin(x)*cos(y) 64 f(x,y) = sin(x)*cos(y)
58 @test eval_on(g, f) == map(x̄->f(x̄...), g) 65 @test eval_on(g, f) == map(x̄->f(x̄...), g)
66 end
67
68 @testset "componentview" begin
69 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
70
71 @test componentview(v, 1, 1) isa AbstractArray
72 @test componentview(v, 1, :) isa AbstractArray
73
74 A = @SMatrix[
75 1 4 7;
76 2 5 8;
77 3 6 9;
78 ]
79 v = [A .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
80 @test componentview(v, 2:3, 1:2) isa AbstractArray
81
82 # Correctness of the result is tested in ArrayComponentView
83 end
84
85 @testset "ArrayComponentView" begin
86 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
87
88 @testset "==" begin
89 @test ArrayComponentView(v, (1,1)) == ArrayComponentView(v, (1,1))
90 @test ArrayComponentView(v, (1,1)) == ArrayComponentView(copy(v), (1,1))
91 @test ArrayComponentView(v, (1,1)) == [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
92 @test [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] == ArrayComponentView(v, (1,1))
93 end
94
95 @testset "components" begin
96 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
97
98 @test ArrayComponentView(v, (1, 1)) == [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
99 @test ArrayComponentView(v, (1, 2)) == [3 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
100 @test ArrayComponentView(v, (2, 1)) == [2 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
101
102 @test ArrayComponentView(v, (1, :)) == [@SVector[1,3] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
103 @test ArrayComponentView(v, (2, :)) == [@SVector[2,4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
104 @test ArrayComponentView(v, (:, 1)) == [@SVector[1,2] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
105 @test ArrayComponentView(v, (:, 2)) == [@SVector[3,4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
106
107
108 A = @SMatrix[
109 1 4 7;
110 2 5 8;
111 3 6 9;
112 ]
113 v = [A .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
114 @test ArrayComponentView(v, (1:2, 1:2)) == [@SMatrix[1 4;2 5] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
115 @test ArrayComponentView(v, (2:3, 1:2)) == [@SMatrix[2 5;3 6] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
116 end
59 end 117 end
60 118
61 @testset "_ncomponents" begin 119 @testset "_ncomponents" begin
62 @test Grids._ncomponents(Int) == 1 120 @test Grids._ncomponents(Int) == 1
63 @test Grids._ncomponents(Float64) == 1 121 @test Grids._ncomponents(Float64) == 1