comparison test/testLazyTensors.jl @ 447:27e0e256e5d9 feature/inflated_tensormapping

Merge in feature/lazy_identity
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 19 Oct 2020 20:59:08 +0200
parents 1db5ec38955e
children 00c317c9ccfb 912ae510dec9
comparison
equal deleted inserted replaced
446:904aae1899df 447:27e0e256e5d9
211 @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]
212 @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]
213 213
214 end 214 end
215 215
216 @testset "TensorMappingComposition" begin
217 A = rand(2,3)
218 B = rand(3,4)
219
220 Ã = LazyLinearMap(A, (1,), (2,))
221 B̃ = LazyLinearMap(B, (1,), (2,))
222
223 @test Ã∘B̃ isa TensorMappingComposition
224 @test range_size(Ã∘B̃) == (2,)
225 @test domain_size(Ã∘B̃) == (4,)
226 @test_throws DimensionMismatch B̃∘Ã
227
228 # @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)
229
230 v = rand(4)
231 @test Ã∘B̃*v ≈ A*B*v rtol=1e-14
232
233 v = rand(2)
234 @test (Ã∘B̃)'*v ≈ B'*A'*v rtol=1e-14
235 end
236
216 @testset "LazyLinearMap" begin 237 @testset "LazyLinearMap" begin
217 # Test a standard matrix-vector product 238 # Test a standard matrix-vector product
218 # mapping vectors of size 4 to vectors of size 3. 239 # mapping vectors of size 4 to vectors of size 3.
219 A = rand(3,4) 240 A = rand(3,4)
220 Ã = LazyLinearMap(A, (1,), (2,)) 241 Ã = LazyLinearMap(A, (1,), (2,))
258 B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13 279 B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13
259 280
260 end 281 end
261 282
262 283
263 @testset "LazyIdentity" begin 284 @testset "IdentityMapping" begin
264 @test LazyIdentity{Float64}((4,5)) isa LazyIdentity{T,2} where T 285 @test IdentityMapping{Float64}((4,5)) isa IdentityMapping{T,2} where T
265 @test LazyIdentity{Float64}((4,5)) isa TensorMapping{T,2,2} where T 286 @test IdentityMapping{Float64}((4,5)) isa TensorMapping{T,2,2} where T
266 A = rand(3,4)
267 Ã = LazyLinearMap(A, (1,), (2,))
268 v = rand(4)
269 287
270 for sz ∈ [(4,5),(3,),(5,6,4)] 288 for sz ∈ [(4,5),(3,),(5,6,4)]
271 I = LazyIdentity{Float64}(sz) 289 I = IdentityMapping{Float64}(sz)
272 v = rand(sz...) 290 v = rand(sz...)
273 @test I*v == v 291 @test I*v == v
274 @test I'*v == v 292 @test I'*v == v
275 293
276 @test range_size(I) == sz 294 @test range_size(I) == sz
277 @test domain_size(I) == sz 295 @test domain_size(I) == sz
278 end 296 end
279 297
280 I = LazyIdentity{Float64}((4,5)) 298 I = IdentityMapping{Float64}((4,5))
281 v = rand(4,5) 299 v = rand(4,5)
282 @inferred (I*v)[3,2] 300 @inferred (I*v)[3,2]
301 @test_broken @inferred (I'*v)[3,2] # TODO: Should fix the index typing before investigating this
283 @inferred range_size(I) 302 @inferred range_size(I)
284 end 303 end
285 304
286 end 305 end