comparison test/testLazyTensors.jl @ 403:618b7ee73b25 refactor/sbp_operators_tests/collect_and_compare

Merge in default and close branch before merge
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 07 Oct 2020 12:31:54 +0200
parents 3b4b1758a8ad
children 4aa59af074ef d94891b8dfca
comparison
equal deleted inserted replaced
397:3cecbfb3d623 403:618b7ee73b25
116 @test range_size(A+B) == range_size(A) == range_size(B) 116 @test range_size(A+B) == range_size(A) == range_size(B)
117 @test domain_size(A+B) == domain_size(A) == domain_size(B) 117 @test domain_size(A+B) == domain_size(A) == domain_size(B)
118 end 118 end
119 119
120 @testset "LazyArray" begin 120 @testset "LazyArray" begin
121 @testset "LazyConstantArray" begin 121 @testset "LazyConstantArray" begin
122 @test LazyTensors.LazyConstantArray(3,(3,2)) isa LazyArray{Int,2} 122 @test LazyTensors.LazyConstantArray(3,(3,2)) isa LazyArray{Int,2}
123 123
124 lca = LazyTensors.LazyConstantArray(3.0,(3,2)) 124 lca = LazyTensors.LazyConstantArray(3.0,(3,2))
125 @test eltype(lca) == Float64 125 @test eltype(lca) == Float64
126 @test ndims(lca) == 2 126 @test ndims(lca) == 2
127 @test size(lca) == (3,2) 127 @test size(lca) == (3,2)
128 @test lca[2] == 3.0 128 @test lca[2] == 3.0
129 end 129 end
130 struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D} 130 struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D}
131 data::T1 131 data::T1
132 end 132 end
133 Base.size(v::DummyArray) = size(v.data) 133 Base.size(v::DummyArray) = size(v.data)
134 Base.getindex(v::DummyArray{T,D}, I::Vararg{Int,D}) where {T,D} = v.data[I...] 134 Base.getindex(v::DummyArray{T,D}, I::Vararg{Int,D}) where {T,D} = v.data[I...]
191 v2 = [1., 2, 3, 4] 191 v2 = [1., 2, 3, 4]
192 # Test that size of arrays is asserted when not specified inbounds 192 # Test that size of arrays is asserted when not specified inbounds
193 @test_throws DimensionMismatch v1 + v2 193 @test_throws DimensionMismatch v1 + v2
194 end 194 end
195 195
196
196 @testset "LazyFunctionArray" begin 197 @testset "LazyFunctionArray" begin
197 @test LazyFunctionArray(i->i^2, (3,)) == [1,4,9] 198 @test LazyFunctionArray(i->i^2, (3,)) == [1,4,9]
198 @test LazyFunctionArray((i,j)->i*j, (3,2)) == [ 199 @test LazyFunctionArray((i,j)->i*j, (3,2)) == [
199 1 2; 200 1 2;
200 2 4; 201 2 4;
210 @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[4,2] 211 @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[4,2]
211 @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[2,3] 212 @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[2,3]
212 213
213 end 214 end
214 215
215 end 216 @testset "LazyLinearMap" begin
217 # Test a standard matrix-vector product
218 # mapping vectors of size 4 to vectors of size 3.
219 A = rand(3,4)
220 Ã = LazyLinearMap(A, (1,), (2,))
221 v = rand(4)
222
223 @test à isa LazyLinearMap{T,1,1} where T
224 @test à isa TensorMapping{T,1,1} where T
225 @test range_size(Ã) == (3,)
226 @test domain_size(Ã) == (4,)
227
228 @test Ã*ones(4) ≈ A*ones(4) atol=5e-13
229 @test Ã*v ≈ A*v atol=5e-13
230
231 A = rand(2,3,4)
232 @test_throws DomainError LazyLinearMap(A, (3,1), (2,))
233
234 # Test more exotic mappings
235 B = rand(3,4,2)
236 # Map vectors of size 2 to matrices of size (3,4)
237 B̃ = LazyLinearMap(B, (1,2), (3,))
238 v = rand(2)
239
240 @test range_size(B̃) == (3,4)
241 @test domain_size(B̃) == (2,)
242 @test B̃ isa TensorMapping{T,2,1} where T
243 @test B̃*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13
244 @test B̃*v ≈ B[:,:,1]*v[1] + B[:,:,2]*v[2] atol=5e-13
245
246 # Map matrices of size (3,2) to vectors of size 4
247 B̃ = LazyLinearMap(B, (2,), (1,3))
248 v = rand(3,2)
249
250 @test range_size(B̃) == (4,)
251 @test domain_size(B̃) == (3,2)
252 @test B̃ isa TensorMapping{T,1,2} where T
253 @test B̃*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] +
254 B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13
255 @test B̃*v ≈ B[1,:,1]*v[1,1] + B[2,:,1]*v[2,1] + B[3,:,1]*v[3,1] +
256 B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13
257
258 end
259
260 end