Mercurial > repos > public > sbplib_julia
comparison test/Grids/tensor_grid_test.jl @ 2057:8a2a0d678d6f feature/lazy_tensors/pretty_printing
Merge default
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Tue, 10 Feb 2026 22:41:19 +0100 |
| parents | fa96b57af6a1 |
| children |
comparison
equal
deleted
inserted
replaced
| 1110:c0bff9f6e0fb | 2057:8a2a0d678d6f |
|---|---|
| 1 using Test | |
| 2 using Diffinitive.Grids | |
| 3 using StaticArrays | |
| 4 | |
| 5 @testset "TensorGrid" begin | |
| 6 g₁ = EquidistantGrid(range(0,1,length=11)) | |
| 7 g₂ = EquidistantGrid(range(2,3,length=6)) | |
| 8 g₃ = EquidistantGrid(1:10) | |
| 9 g₄ = ZeroDimGrid(@SVector[1,2]) | |
| 10 | |
| 11 @test TensorGrid(g₁, g₂) isa TensorGrid | |
| 12 @test TensorGrid(g₁, g₂) isa Grid{SVector{2,Float64}, 2} | |
| 13 @test TensorGrid(g₃, g₃) isa Grid{SVector{2,Int}, 2} | |
| 14 @test TensorGrid(g₁, g₂, g₃) isa Grid{SVector{3,Float64}, 3} | |
| 15 @test TensorGrid(g₁, g₄) isa Grid{SVector{3,Float64}, 1} | |
| 16 @test TensorGrid(g₁, g₄, g₂) isa Grid{SVector{4,Float64}, 2} | |
| 17 | |
| 18 @testset "Indexing Interface" begin | |
| 19 @testset "regular indexing" begin | |
| 20 @test TensorGrid(g₁, g₂)[1,1] isa SVector{2,Float64} | |
| 21 @test TensorGrid(g₁, g₂)[1,1] == [0.0,2.0] | |
| 22 @test TensorGrid(g₁, g₂)[3,5] == [0.2,2.8] | |
| 23 @test TensorGrid(g₁, g₂)[10,6] == [0.9,3.0] | |
| 24 | |
| 25 @test TensorGrid(g₁, g₃)[1,1] isa SVector{2,Float64} | |
| 26 @test TensorGrid(g₁, g₃)[1,1] == [0.0,1.0] | |
| 27 | |
| 28 @test TensorGrid(g₁, g₂, g₃)[3,4,5] isa SVector{3,Float64} | |
| 29 @test TensorGrid(g₁, g₂, g₃)[3,4,5] == [0.2, 2.6, 5.0] | |
| 30 | |
| 31 @test TensorGrid(g₁, g₄)[3] isa SVector{3,Float64} | |
| 32 @test TensorGrid(g₁, g₄)[3] == [0.2, 1., 2.] | |
| 33 | |
| 34 @test TensorGrid(g₁, g₄, g₂)[3,2] isa SVector{4,Float64} | |
| 35 @test TensorGrid(g₁, g₄, g₂)[3,2] == [0.2, 1., 2., 2.2] | |
| 36 | |
| 37 g = TensorGrid(g₁, g₂) | |
| 38 @test g[begin, begin] == g[1,1] | |
| 39 @test g[begin, end] == g[1,6] | |
| 40 @test g[end, end] == g[11,6] | |
| 41 end | |
| 42 | |
| 43 @testset "cartesian indexing" begin | |
| 44 cases = [ | |
| 45 (TensorGrid(g₁, g₂), (1,1) ), | |
| 46 (TensorGrid(g₁, g₂), (3,5) ), | |
| 47 (TensorGrid(g₁, g₂), (10,6) ), | |
| 48 (TensorGrid(g₁, g₃), (1,1) ), | |
| 49 (TensorGrid(g₁, g₂, g₃), (3,4,5)), | |
| 50 (TensorGrid(g₁, g₄), (3) ), | |
| 51 (TensorGrid(g₁, g₄, g₂), (3,2) ), | |
| 52 ] | |
| 53 | |
| 54 @testset "i = $is" for (g, is) ∈ cases | |
| 55 @test g[CartesianIndex(is...)] == g[is...] | |
| 56 end | |
| 57 end | |
| 58 | |
| 59 @testset "eachindex" begin | |
| 60 @test eachindex(TensorGrid(g₁, g₂)) == CartesianIndices((11,6)) | |
| 61 @test eachindex(TensorGrid(g₁, g₃)) == CartesianIndices((11,10)) | |
| 62 @test eachindex(TensorGrid(g₁, g₂, g₃)) == CartesianIndices((11,6,10)) | |
| 63 @test eachindex(TensorGrid(g₁, g₄)) == CartesianIndices((11,)) | |
| 64 @test eachindex(TensorGrid(g₁, g₄, g₂)) == CartesianIndices((11,6)) | |
| 65 end | |
| 66 | |
| 67 @testset "firstindex" begin | |
| 68 @test firstindex(TensorGrid(g₁, g₂, g₃), 1) == 1 | |
| 69 @test firstindex(TensorGrid(g₁, g₂, g₃), 2) == 1 | |
| 70 @test firstindex(TensorGrid(g₁, g₂, g₃), 3) == 1 | |
| 71 end | |
| 72 | |
| 73 @testset "lastindex" begin | |
| 74 @test lastindex(TensorGrid(g₁, g₂, g₃), 1) == 11 | |
| 75 @test lastindex(TensorGrid(g₁, g₂, g₃), 2) == 6 | |
| 76 @test lastindex(TensorGrid(g₁, g₂, g₃), 3) == 10 | |
| 77 end | |
| 78 end | |
| 79 | |
| 80 @testset "Iterator interface" begin | |
| 81 @test eltype(TensorGrid(g₁, g₂)) == SVector{2,Float64} | |
| 82 @test eltype(TensorGrid(g₁, g₃)) == SVector{2,Float64} | |
| 83 @test eltype(TensorGrid(g₁, g₂, g₃)) == SVector{3,Float64} | |
| 84 @test eltype(TensorGrid(g₁, g₄)) == SVector{3,Float64} | |
| 85 @test eltype(TensorGrid(g₁, g₄, g₂)) == SVector{4,Float64} | |
| 86 | |
| 87 @test eltype(typeof(TensorGrid(g₁, g₂))) == SVector{2,Float64} | |
| 88 @test eltype(typeof(TensorGrid(g₁, g₃))) == SVector{2,Float64} | |
| 89 @test eltype(typeof(TensorGrid(g₁, g₂, g₃))) == SVector{3,Float64} | |
| 90 @test eltype(typeof(TensorGrid(g₁, g₄))) == SVector{3,Float64} | |
| 91 @test eltype(typeof(TensorGrid(g₁, g₄, g₂))) == SVector{4,Float64} | |
| 92 | |
| 93 @test size(TensorGrid(g₁, g₂)) == (11,6) | |
| 94 @test size(TensorGrid(g₁, g₃)) == (11,10) | |
| 95 @test size(TensorGrid(g₁, g₂, g₃)) == (11,6,10) | |
| 96 @test size(TensorGrid(g₁, g₄)) == (11,) | |
| 97 @test size(TensorGrid(g₁, g₄, g₂)) == (11,6) | |
| 98 | |
| 99 @test size(TensorGrid(g₁, g₂, g₃),1) == 11 | |
| 100 @test size(TensorGrid(g₁, g₂, g₃),2) == 6 | |
| 101 @test size(TensorGrid(g₁, g₂, g₃),3) == 10 | |
| 102 @test size(TensorGrid(g₁, g₄, g₂),1) == 11 | |
| 103 @test size(TensorGrid(g₁, g₄, g₂),2) == 6 | |
| 104 | |
| 105 @test length(TensorGrid(g₁, g₂)) == 66 | |
| 106 @test length(TensorGrid(g₁, g₃)) == 110 | |
| 107 @test length(TensorGrid(g₁, g₂, g₃)) == 660 | |
| 108 @test length(TensorGrid(g₁, g₄)) == 11 | |
| 109 @test length(TensorGrid(g₁, g₄, g₂)) == 66 | |
| 110 | |
| 111 @test Base.IteratorSize(TensorGrid(g₁, g₂)) == Base.HasShape{2}() | |
| 112 @test Base.IteratorSize(TensorGrid(g₁, g₃)) == Base.HasShape{2}() | |
| 113 @test Base.IteratorSize(TensorGrid(g₁, g₂, g₃)) == Base.HasShape{3}() | |
| 114 @test Base.IteratorSize(TensorGrid(g₁, g₄)) == Base.HasShape{1}() | |
| 115 @test Base.IteratorSize(TensorGrid(g₁, g₄, g₂)) == Base.HasShape{2}() | |
| 116 | |
| 117 @test iterate(TensorGrid(g₁, g₂))[1] isa SVector{2,Float64} | |
| 118 @test iterate(TensorGrid(g₁, g₃))[1] isa SVector{2,Float64} | |
| 119 @test iterate(TensorGrid(g₁, g₂, g₃))[1] isa SVector{3,Float64} | |
| 120 @test iterate(TensorGrid(g₁, g₄))[1] isa SVector{3,Float64} | |
| 121 @test iterate(TensorGrid(g₁, g₄, g₂))[1] isa SVector{4,Float64} | |
| 122 | |
| 123 @test collect(TensorGrid(g₁, g₂)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)] | |
| 124 @test collect(TensorGrid(g₁, g₃)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ 1:10] | |
| 125 @test collect(TensorGrid(g₁, g₂, g₃)) == [@SVector[x,y,z] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6), z ∈ 1:10] | |
| 126 @test collect(TensorGrid(g₁, g₄)) == [@SVector[x,1,2] for x ∈ range(0,1,length=11)] | |
| 127 @test collect(TensorGrid(g₁, g₄, g₂)) == [@SVector[x,1,2,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)] | |
| 128 end | |
| 129 | |
| 130 @testset "Base" begin | |
| 131 g₁ = EquidistantGrid(range(0,1,length=11)) | |
| 132 g₂ = EquidistantGrid(range(2,3,length=6)) | |
| 133 g = TensorGrid(g₁, g₂) | |
| 134 | |
| 135 @test axes(g, 1) == 1:11 | |
| 136 @test axes(g, 2) == 1:6 | |
| 137 @test axes(g) == (1:11,1:6) | |
| 138 end | |
| 139 | |
| 140 @testset "spacing" begin | |
| 141 g₁ = EquidistantGrid(range(0,1,length=11)) | |
| 142 g₂ = EquidistantGrid(range(2,3,length=6)) | |
| 143 g₃ = ZeroDimGrid(@SVector[1,2]) | |
| 144 | |
| 145 @test spacing(TensorGrid(g₁)) == (1/10,) | |
| 146 @test spacing(TensorGrid(g₂)) == (1/5,) | |
| 147 | |
| 148 @test spacing(TensorGrid(g₁, g₂)) == (1/10, 1/5) | |
| 149 | |
| 150 @test spacing(TensorGrid(g₁, g₃)) == (1/10,) | |
| 151 @test spacing(TensorGrid(g₃, g₂)) == (1/5,) | |
| 152 | |
| 153 | |
| 154 @test spacing(TensorGrid(g₁, g₂, g₁)) == (1/10, 1/5, 1/10) | |
| 155 | |
| 156 @test spacing(TensorGrid(g₃, g₂, g₁)) == (1/5, 1/10) | |
| 157 @test spacing(TensorGrid(g₁, g₃, g₁)) == (1/10, 1/10) | |
| 158 @test spacing(TensorGrid(g₁, g₂, g₃)) == (1/10, 1/5) | |
| 159 end | |
| 160 | |
| 161 @testset "min_spacing" begin | |
| 162 g₁ = EquidistantGrid(range(0,1,length=11)) | |
| 163 g₂ = EquidistantGrid(range(2,3,length=6)) | |
| 164 g₃ = ZeroDimGrid(@SVector[1,2]) | |
| 165 | |
| 166 @test min_spacing(TensorGrid(g₁, g₂)) == 1/10 | |
| 167 @test min_spacing(TensorGrid(g₂, g₃)) == 1/5 | |
| 168 end | |
| 169 | |
| 170 @testset "refine" begin | |
| 171 g1(n) = EquidistantGrid(range(0,1,length=n)) | |
| 172 g2(n) = EquidistantGrid(range(2,3,length=n)) | |
| 173 | |
| 174 @test refine(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6)) | |
| 175 @test refine(TensorGrid(g1(11), g2(6)),2) == TensorGrid(g1(21), g2(11)) | |
| 176 @test refine(TensorGrid(g1(11), g2(6)),3) == TensorGrid(g1(31), g2(16)) | |
| 177 @test refine(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄) | |
| 178 @test refine(TensorGrid(g1(11), g₄), 2) == TensorGrid(g1(21), g₄) | |
| 179 end | |
| 180 | |
| 181 @testset "coarsen" begin | |
| 182 g1(n) = EquidistantGrid(range(0,1,length=n)) | |
| 183 g2(n) = EquidistantGrid(range(2,3,length=n)) | |
| 184 | |
| 185 @test coarsen(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6)) | |
| 186 @test coarsen(TensorGrid(g1(21), g2(11)),2) == TensorGrid(g1(11), g2(6)) | |
| 187 @test coarsen(TensorGrid(g1(31), g2(16)),3) == TensorGrid(g1(11), g2(6)) | |
| 188 @test coarsen(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄) | |
| 189 @test coarsen(TensorGrid(g1(21), g₄), 2) == TensorGrid(g1(11), g₄) | |
| 190 end | |
| 191 | |
| 192 @testset "boundary_identifiers" begin | |
| 193 @test boundary_identifiers(TensorGrid(g₁, g₂)) == map((n,id)->TensorGridBoundary{n,id}(), (1,1,2,2), (LowerBoundary,UpperBoundary,LowerBoundary,UpperBoundary)) | |
| 194 @test boundary_identifiers(TensorGrid(g₁, g₄)) == (TensorGridBoundary{1,LowerBoundary}(),TensorGridBoundary{1,UpperBoundary}()) | |
| 195 end | |
| 196 | |
| 197 @testset "boundary_grid" begin | |
| 198 @test boundary_grid(TensorGrid(g₁, g₂), TensorGridBoundary{1, UpperBoundary}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₂) | |
| 199 @test boundary_grid(TensorGrid(g₁, g₄), TensorGridBoundary{1, UpperBoundary}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₄) | |
| 200 end | |
| 201 | |
| 202 @testset "boundary_indices" begin | |
| 203 g₁ = EquidistantGrid(range(0,1,length=11)) | |
| 204 g₂ = EquidistantGrid(range(2,3,length=6)) | |
| 205 g₄ = ZeroDimGrid(@SVector[1,2]) | |
| 206 | |
| 207 gf = reshape(1:(11*6),11,6) | |
| 208 @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, LowerBoundary}())] == gf[1,:] | |
| 209 @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, UpperBoundary}())] == gf[11,:] | |
| 210 @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, LowerBoundary}())] == gf[:,1] | |
| 211 @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, UpperBoundary}())] == gf[:,6] | |
| 212 | |
| 213 gf = rand(11) | |
| 214 @test gf[boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, LowerBoundary}())] == gf[1] | |
| 215 @test gf[boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, UpperBoundary}())] == gf[11] | |
| 216 @test gf[boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, LowerBoundary}())] == gf[1] | |
| 217 @test gf[boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, UpperBoundary}())] == gf[11] | |
| 218 | |
| 219 @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, LowerBoundary}())) == [CartesianIndex(1,i) for i ∈ 1:6] | |
| 220 @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, UpperBoundary}())) == [CartesianIndex(11,i) for i ∈ 1:6] | |
| 221 @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, LowerBoundary}())) == [CartesianIndex(i,1) for i ∈ 1:11] | |
| 222 @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, UpperBoundary}())) == [CartesianIndex(i,6) for i ∈ 1:11] | |
| 223 @test collect(boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, LowerBoundary}())) == fill(1) | |
| 224 @test collect(boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, UpperBoundary}())) == fill(11) | |
| 225 @test collect(boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, LowerBoundary}())) == fill(1) | |
| 226 @test collect(boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, UpperBoundary}())) == fill(11) | |
| 227 end | |
| 228 end | |
| 229 | |
| 230 @testset "combined_coordinate_vector_type" begin | |
| 231 @test Grids.combined_coordinate_vector_type(Float64) == Float64 | |
| 232 @test Grids.combined_coordinate_vector_type(Float64, Int) == SVector{2,Float64} | |
| 233 @test Grids.combined_coordinate_vector_type(Float32, Int16, Int32) == SVector{3,Float32} | |
| 234 | |
| 235 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}) == SVector{2,Float64} | |
| 236 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}, SVector{1,Float64}) == SVector{3,Float64} | |
| 237 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}, SVector{1,Int}, SVector{3, Float32}) == SVector{6,Float64} | |
| 238 end | |
| 239 | |
| 240 @testset "combine_coordinates" begin | |
| 241 @test Grids.combine_coordinates(1,2,3) isa SVector{3, Int} | |
| 242 @test Grids.combine_coordinates(1,2,3) == [1,2,3] | |
| 243 @test Grids.combine_coordinates(1,2.,3) isa SVector{3, Float64} | |
| 244 @test Grids.combine_coordinates(1,2.,3) == [1,2,3] | |
| 245 @test Grids.combine_coordinates(1,@SVector[2.,3]) isa SVector{3, Float64} | |
| 246 @test Grids.combine_coordinates(1,@SVector[2.,3]) == [1,2,3] | |
| 247 end | |
| 248 | |
| 249 @testset "grid_and_local_dim_index" begin | |
| 250 cases = [ | |
| 251 ((1,), 1) => (1,1), | |
| 252 | |
| 253 ((1,1), 1) => (1,1), | |
| 254 ((1,1), 2) => (2,1), | |
| 255 | |
| 256 ((1,2), 1) => (1,1), | |
| 257 ((1,2), 2) => (2,1), | |
| 258 ((1,2), 3) => (2,2), | |
| 259 | |
| 260 ((2,1), 1) => (1,1), | |
| 261 ((2,1), 2) => (1,2), | |
| 262 ((2,1), 3) => (2,1), | |
| 263 | |
| 264 ((2,1,3), 1) => (1,1), | |
| 265 ((2,1,3), 2) => (1,2), | |
| 266 ((2,1,3), 3) => (2,1), | |
| 267 ((2,1,3), 4) => (3,1), | |
| 268 ((2,1,3), 5) => (3,2), | |
| 269 ((2,1,3), 6) => (3,3), | |
| 270 ] | |
| 271 | |
| 272 @testset "grid_and_local_dim_index$args" for (args, expected) ∈ cases | |
| 273 @test Grids.grid_and_local_dim_index(args...) == expected | |
| 274 end | |
| 275 end |
