Mercurial > repos > public > sbplib_julia
comparison test/Grids/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 | 3edcf447298d |
| children |
comparison
equal
deleted
inserted
replaced
| 1110:c0bff9f6e0fb | 2057:8a2a0d678d6f |
|---|---|
| 1 using Test | |
| 2 using Diffinitive.Grids | |
| 3 using Diffinitive.LazyTensors | |
| 4 using StaticArrays | |
| 5 | |
| 6 @testset "Grid" begin | |
| 7 struct DummyGrid{T,D} <: Grid{T,D} end | |
| 8 | |
| 9 @test eltype(DummyGrid{Int, 2}) == Int | |
| 10 @test eltype(DummyGrid{Int, 2}()) == Int | |
| 11 | |
| 12 @test ndims(DummyGrid{Int, 2}()) == 2 | |
| 13 | |
| 14 @test coordinate_size(DummyGrid{Int, 1}()) == 1 | |
| 15 @test coordinate_size(DummyGrid{SVector{3,Float64}, 2}()) == 3 | |
| 16 | |
| 17 @test coordinate_size(DummyGrid{SVector{3,Float64}, 2}) == 3 | |
| 18 end | |
| 19 | |
| 20 @testset "component_type" begin | |
| 21 @test component_type(DummyGrid{Int,1}()) == Int | |
| 22 @test component_type(DummyGrid{Float64,1}()) == Float64 | |
| 23 @test component_type(DummyGrid{Rational,1}()) == Rational | |
| 24 | |
| 25 @test component_type(DummyGrid{SVector{3,Int},2}()) == Int | |
| 26 @test component_type(DummyGrid{SVector{2,Float64},3}()) == Float64 | |
| 27 @test component_type(DummyGrid{SVector{4,Rational},4}()) == Rational | |
| 28 | |
| 29 @test component_type(DummyGrid{Float64,1}) == Float64 | |
| 30 @test component_type(DummyGrid{SVector{2,Float64},3}) == Float64 | |
| 31 | |
| 32 @test component_type(fill(@SVector[1,2], 4,2)) == Int | |
| 33 end | |
| 34 | |
| 35 @testset "eval_on" begin | |
| 36 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), x̄->x̄[1]+x̄[2]) isa LazyArray | |
| 37 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), x̄->x̄[1]+x̄[2]) == fill(3.) | |
| 38 @test eval_on(ZeroDimGrid(@SVector[3.,2.]), x̄->x̄[1]+x̄[2]) == fill(5.) | |
| 39 | |
| 40 @test eval_on(ZeroDimGrid(1.), x̄->2x̄) isa LazyArray | |
| 41 @test eval_on(ZeroDimGrid(1.), x̄->2x̄) == fill(2.) | |
| 42 | |
| 43 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), π) isa LazyArray | |
| 44 @test eval_on(ZeroDimGrid(@SVector[1.,2.]), π) == fill(π) | |
| 45 | |
| 46 @test eval_on(EquidistantGrid(range(0,1,length=4)), x->2x) isa LazyArray | |
| 47 @test eval_on(EquidistantGrid(range(0,1,length=4)), x->2x) == 2 .* range(0,1,length=4) | |
| 48 | |
| 49 | |
| 50 g = equidistant_grid((0.0,0.0), (2.0,1.0), 5, 3) | |
| 51 | |
| 52 @test eval_on(g, x̄ -> 0.) isa LazyArray | |
| 53 @test eval_on(g, x̄ -> 0.) == fill(0., (5,3)) | |
| 54 | |
| 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)) | |
| 58 | |
| 59 # Vector valued function | |
| 60 @test eval_on(g, x̄ -> @SVector[x̄[2], x̄[1]]) isa LazyArray{SVector{2,Float64}} | |
| 61 @test eval_on(g, x̄ -> @SVector[x̄[2], x̄[1]]) == map(x̄ -> @SVector[x̄[2], x̄[1]], g) | |
| 62 | |
| 63 # Multi-argument functions | |
| 64 f(x,y) = sin(x)*cos(y) | |
| 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 | |
| 117 | |
| 118 @testset "size" begin | |
| 119 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] | |
| 120 cv = ArrayComponentView(v, (1, 1)) | |
| 121 | |
| 122 @test size(cv) == (3,4) | |
| 123 end | |
| 124 | |
| 125 @testset "IndexStyle" begin | |
| 126 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] | |
| 127 cv = ArrayComponentView(v, (1, 1)) | |
| 128 | |
| 129 @test IndexStyle(cv) == IndexStyle(v) | |
| 130 end | |
| 131 | |
| 132 @testset "_array_type" begin | |
| 133 v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] | |
| 134 cv = ArrayComponentView(v, (1, 1)) | |
| 135 | |
| 136 @test Grids._array_type(cv) == typeof(v) | |
| 137 end | |
| 138 end | |
| 139 | |
| 140 @testset "_ncomponents" begin | |
| 141 @test Grids._ncomponents(Int) == 1 | |
| 142 @test Grids._ncomponents(Float64) == 1 | |
| 143 @test Grids._ncomponents(Rational) == 1 | |
| 144 | |
| 145 @test Grids._ncomponents(SVector{3,Int}) == 3 | |
| 146 @test Grids._ncomponents(SVector{2,Float64}) == 2 | |
| 147 @test Grids._ncomponents(SVector{4,Rational}) == 4 | |
| 148 end |
