comparison test/testLazyTensors.jl @ 393:b14eacf823b6 feature/lazy_linear_map

Test applying LazyLinearMap to random vectors and matrices. Fix indentation
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Fri, 02 Oct 2020 14:56:03 +0200
parents 418cfd945715
children 7ad644d112de
comparison
equal deleted inserted replaced
392:418cfd945715 393:b14eacf823b6
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...]
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 "LazyLinearMap" begin 216 @testset "LazyLinearMap" begin
217 # Test a standard matrix-vector product
218 # mapping vectors of size 4 to vectors of size 3.
217 A = rand(3,4) 219 A = rand(3,4)
218 B = rand(3,4,2)
219 v = rand(4) 220 v = rand(4)
220 221
221 Ã = LazyLinearMap(A, (1,), (2,)) 222 Ã = LazyLinearMap(A, (1,), (2,))
222 @test à isa LazyLinearMap{T,1,1} where T 223 @test à isa LazyLinearMap{T,1,1} where T
223 @test à isa TensorMapping{T,1,1} where T 224 @test à isa TensorMapping{T,1,1} where T
224 225
225 @test Ã*ones(4) ≈ A*ones(4) atol=5e-13 226 @test Ã*ones(4) ≈ A*ones(4) atol=5e-13
226 @test Ã*v ≈ A*v atol=5e-13 227 @test Ã*v ≈ A*v atol=5e-13
227 228
228 B̃_21 = LazyLinearMap(B, (1,2), (3,)) 229 # Test more exotic mappings
229 B̃_12 = LazyLinearMap(B, (2,), (3,1)) 230 B = rand(3,4,2)
230 @test B̃_21 isa TensorMapping{T,2,1} where T 231 # Map vectors of size 2 to matrices of size (3,4)
231 @test B̃_12 isa TensorMapping{T,1,2} where T 232 v = rand(2)
232 @test B̃_21*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13 233 B̃ = LazyLinearMap(B, (1,2), (3,))
233 @test B̃_12*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] + 234 @test B̃ isa TensorMapping{T,2,1} where T
234 B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13 235 @test B̃*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13
235 236 @test B̃*v ≈ B[:,:,1]*v[1] + B[:,:,2]*v[2] atol=5e-13
236 end 237
237 238 # Map matrices of size (3,2) to vectors of size 4
238 end 239 B̃ = LazyLinearMap(B, (2,), (3,1))
240 v = rand(3,2)
241 @test B̃ isa TensorMapping{T,1,2} where T
242 @test B̃*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] +
243 B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13
244 @test B̃*v ≈ B[1,:,1]*v[1,1] + B[2,:,1]*v[2,1] + B[3,:,1]*v[3,1] +
245 B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13
246
247 end
248
249 end