comparison test/testLazyTensors.jl @ 498:5a600ec40ccc feature/outer_product

Merge in default
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 05 Nov 2020 15:27:04 +0100
parents 2dc2eac27f75 f906f207571c
children 1c50b5f1b356
comparison
equal deleted inserted replaced
491:2dc2eac27f75 498:5a600ec40ccc
10 struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end 10 struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end
11 LazyTensors.apply(m::DummyMapping{T,R,D}, v, i::NTuple{R,Index{<:Region}}) where {T,R,D} = :apply 11 LazyTensors.apply(m::DummyMapping{T,R,D}, v, i::NTuple{R,Index{<:Region}}) where {T,R,D} = :apply
12 @test range_dim(DummyMapping{Int,2,3}()) == 2 12 @test range_dim(DummyMapping{Int,2,3}()) == 2
13 @test domain_dim(DummyMapping{Int,2,3}()) == 3 13 @test domain_dim(DummyMapping{Int,2,3}()) == 3
14 @test apply(DummyMapping{Int,2,3}(), zeros(Int, (0,0,0)),(Index{Unknown}(0),Index{Unknown}(0))) == :apply 14 @test apply(DummyMapping{Int,2,3}(), zeros(Int, (0,0,0)),(Index{Unknown}(0),Index{Unknown}(0))) == :apply
15 @test eltype(DummyMapping{Int,2,3}()) == Int
16 @test eltype(DummyMapping{Float64,2,3}()) == Float64
15 end 17 end
16 18
17 @testset "Mapping transpose" begin 19 @testset "Mapping transpose" begin
18 struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end 20 struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end
19 21
175 @test (s /̃ v1)[i] == 1/r_div_s[i] 177 @test (s /̃ v1)[i] == 1/r_div_s[i]
176 end 178 end
177 @test_throws BoundsError (v1 +̃ v2)[4] 179 @test_throws BoundsError (v1 +̃ v2)[4]
178 v2 = [1., 2, 3, 4] 180 v2 = [1., 2, 3, 4]
179 # Test that size of arrays is asserted when not specified inbounds 181 # Test that size of arrays is asserted when not specified inbounds
182 # TODO: Replace these errors with SizeMismatch
180 @test_throws DimensionMismatch v1 +̃ v2 183 @test_throws DimensionMismatch v1 +̃ v2
181 184
182 # Test operations on LazyArray 185 # Test operations on LazyArray
183 v1 = DummyArray([1, 2.3, 4]) 186 v1 = DummyArray([1, 2.3, 4])
184 v2 = [1., 2, 3] 187 v2 = [1., 2, 3]
191 @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub_v[i] 194 @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub_v[i]
192 end 195 end
193 @test_throws BoundsError (v1 + v2)[4] 196 @test_throws BoundsError (v1 + v2)[4]
194 v2 = [1., 2, 3, 4] 197 v2 = [1., 2, 3, 4]
195 # Test that size of arrays is asserted when not specified inbounds 198 # Test that size of arrays is asserted when not specified inbounds
199 # TODO: Replace these errors with SizeMismatch
196 @test_throws DimensionMismatch v1 + v2 200 @test_throws DimensionMismatch v1 + v2
197 end 201 end
198 202
199 203
200 @testset "LazyFunctionArray" begin 204 @testset "LazyFunctionArray" begin
224 B̃ = LazyLinearMap(B, (1,), (2,)) 228 B̃ = LazyLinearMap(B, (1,), (2,))
225 229
226 @test Ã∘B̃ isa TensorMappingComposition 230 @test Ã∘B̃ isa TensorMappingComposition
227 @test range_size(Ã∘B̃) == (2,) 231 @test range_size(Ã∘B̃) == (2,)
228 @test domain_size(Ã∘B̃) == (4,) 232 @test domain_size(Ã∘B̃) == (4,)
229 @test_throws DimensionMismatch B̃∘Ã 233 @test_throws SizeMismatch B̃∘Ã
230 234
231 # @test @inbounds B̃∘Ã # Should not error even though dimensions don't match. (Since ]test runs with forced boundschecking this is currently not testable 2020-10-16) 235 # @test @inbounds B̃∘Ã # Should not error even though dimensions don't match. (Since ]test runs with forced boundschecking this is currently not testable 2020-10-16)
232 236
233 v = rand(4) 237 v = rand(4)
234 @test Ã∘B̃*v ≈ A*B*v rtol=1e-14 238 @test Ã∘B̃*v ≈ A*B*v rtol=1e-14
310 @inferred (I'*v)[3,2] 314 @inferred (I'*v)[3,2]
311 @inferred range_size(I) 315 @inferred range_size(I)
312 316
313 @inferred range_dim(I) 317 @inferred range_dim(I)
314 @inferred domain_dim(I) 318 @inferred domain_dim(I)
319
320 Ã = rand(4,2)
321 A = LazyLinearMap(Ã,(1,),(2,))
322 I1 = IdentityMapping{Float64}(2)
323 I2 = IdentityMapping{Float64}(4)
324 @test A∘I1 == A
325 @test I2∘A == A
326 @test I1∘I1 == I1
327 @test_throws SizeMismatch I1∘A
328 @test_throws SizeMismatch A∘I2
329 @test_throws SizeMismatch I1∘I2
315 end 330 end
316 331
317 @testset "InflatedTensorMapping" begin 332 @testset "InflatedTensorMapping" begin
318 I(sz...) = IdentityMapping(sz...) 333 I(sz...) = IdentityMapping(sz...)
319 334
379 394
380 @inferred LazyTensors.split_index(tm,1,2,3,2,2,4) 395 @inferred LazyTensors.split_index(tm,1,2,3,2,2,4)
381 @inferred apply(tm,v,Index{Unknown}.((1,2,3,2,2,4))...) 396 @inferred apply(tm,v,Index{Unknown}.((1,2,3,2,2,4))...)
382 @inferred (tm*v)[1,2,3,2,2,4] 397 @inferred (tm*v)[1,2,3,2,2,4]
383 398
399 @testset "InflatedTensorMapping of InflatedTensorMapping" begin
400 A = ScalingOperator(2.0,(2,3))
401 itm = InflatedTensorMapping(I(3,2), A, I(4))
402 @test InflatedTensorMapping(I(4), itm, I(2)) == InflatedTensorMapping(I(4,3,2), A, I(4,2))
403 @test InflatedTensorMapping(itm, I(2)) == InflatedTensorMapping(I(3,2), A, I(4,2))
404 @test InflatedTensorMapping(I(4), itm) == InflatedTensorMapping(I(4,3,2), A, I(4))
405
406 @test InflatedTensorMapping(I(2), I(2), I(2)) isa InflatedTensorMapping # The constructor should always return its type.
407 end
408
384 end 409 end
385 410
386 @testset "slice_tuple" begin 411 @testset "slice_tuple" begin
387 @test LazyTensors.slice_tuple((1,2,3),Val(1), Val(3)) == (1,2,3) 412 @test LazyTensors.slice_tuple((1,2,3),Val(1), Val(3)) == (1,2,3)
388 @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(2), Val(5)) == (2,3,4,5) 413 @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(2), Val(5)) == (2,3,4,5)