comparison test/LazyTensors/lazy_tensor_operations_test.jl @ 995:1ba8a398af9c refactor/lazy_tensors

Rename types
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 18 Mar 2022 21:14:47 +0100
parents 2f9beee56a4c
children 20c376dffe84
comparison
equal deleted inserted replaced
994:55ab7801c45f 995:1ba8a398af9c
3 using Sbplib.RegionIndices 3 using Sbplib.RegionIndices
4 4
5 using Tullio 5 using Tullio
6 6
7 @testset "Mapping transpose" begin 7 @testset "Mapping transpose" begin
8 struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end 8 struct DummyMapping{T,R,D} <: LazyTensor{T,R,D} end
9 9
10 LazyTensors.apply(m::DummyMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = :apply 10 LazyTensors.apply(m::DummyMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = :apply
11 LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, I::Vararg{Any,D}) where {T,R,D} = :apply_transpose 11 LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, I::Vararg{Any,D}) where {T,R,D} = :apply_transpose
12 12
13 LazyTensors.range_size(m::DummyMapping) = :range_size 13 LazyTensors.range_size(m::DummyMapping) = :range_size
14 LazyTensors.domain_size(m::DummyMapping) = :domain_size 14 LazyTensors.domain_size(m::DummyMapping) = :domain_size
15 15
16 m = DummyMapping{Float64,2,3}() 16 m = DummyMapping{Float64,2,3}()
17 @test m' isa TensorMapping{Float64, 3,2} 17 @test m' isa LazyTensor{Float64, 3,2}
18 @test m'' == m 18 @test m'' == m
19 @test apply(m',zeros(Float64,(0,0)), 0, 0, 0) == :apply_transpose 19 @test apply(m',zeros(Float64,(0,0)), 0, 0, 0) == :apply_transpose
20 @test apply(m'',zeros(Float64,(0,0,0)), 0, 0) == :apply 20 @test apply(m'',zeros(Float64,(0,0,0)), 0, 0) == :apply
21 @test apply_transpose(m', zeros(Float64,(0,0,0)), 0, 0) == :apply 21 @test apply_transpose(m', zeros(Float64,(0,0,0)), 0, 0) == :apply
22 22
23 @test range_size(m') == :domain_size 23 @test range_size(m') == :domain_size
24 @test domain_size(m') == :range_size 24 @test domain_size(m') == :range_size
25 end 25 end
26 26
27 @testset "TensorApplication" begin 27 @testset "TensorApplication" begin
28 struct SizeDoublingMapping{T,R,D} <: TensorMapping{T,R,D} 28 struct SizeDoublingMapping{T,R,D} <: LazyTensor{T,R,D}
29 domain_size::NTuple{D,Int} 29 domain_size::NTuple{D,Int}
30 end 30 end
31 31
32 LazyTensors.apply(m::SizeDoublingMapping{T,R}, v, i::Vararg{Any,R}) where {T,R} = (:apply,v,i) 32 LazyTensors.apply(m::SizeDoublingMapping{T,R}, v, i::Vararg{Any,R}) where {T,R} = (:apply,v,i)
33 LazyTensors.range_size(m::SizeDoublingMapping) = 2 .* m.domain_size 33 LazyTensors.range_size(m::SizeDoublingMapping) = 2 .* m.domain_size
106 @inferred m*v 106 @inferred m*v
107 @inferred (m*v)[1] 107 @inferred (m*v)[1]
108 end 108 end
109 end 109 end
110 110
111 @testset "TensorMapping binary operations" begin 111 @testset "LazyTensor binary operations" begin
112 A = ScalingTensor(2.0, (3,)) 112 A = ScalingTensor(2.0, (3,))
113 B = ScalingTensor(3.0, (3,)) 113 B = ScalingTensor(3.0, (3,))
114 114
115 v = [1.1,1.2,1.3] 115 v = [1.1,1.2,1.3]
116 for i ∈ eachindex(v) 116 for i ∈ eachindex(v)
129 129
130 @test ((A+B)*ComplexF64[1.1,1.2,1.3])[3] isa ComplexF64 130 @test ((A+B)*ComplexF64[1.1,1.2,1.3])[3] isa ComplexF64
131 end 131 end
132 132
133 133
134 @testset "TensorMappingComposition" begin 134 @testset "LazyTensorComposition" begin
135 A = rand(2,3) 135 A = rand(2,3)
136 B = rand(3,4) 136 B = rand(3,4)
137 137
138 Ã = LazyLinearMap(A, (1,), (2,)) 138 Ã = LazyLinearMap(A, (1,), (2,))
139 B̃ = LazyLinearMap(B, (1,), (2,)) 139 B̃ = LazyLinearMap(B, (1,), (2,))
140 140
141 @test Ã∘B̃ isa TensorMappingComposition 141 @test Ã∘B̃ isa LazyTensorComposition
142 @test range_size(Ã∘B̃) == (2,) 142 @test range_size(Ã∘B̃) == (2,)
143 @test domain_size(Ã∘B̃) == (4,) 143 @test domain_size(Ã∘B̃) == (4,)
144 @test_throws SizeMismatch B̃∘Ã 144 @test_throws SizeMismatch B̃∘Ã
145 145
146 # @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) 146 # @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)
162 Ã = LazyLinearMap(A, (1,), (2,)) 162 Ã = LazyLinearMap(A, (1,), (2,))
163 v = rand(4) 163 v = rand(4)
164 w = rand(3) 164 w = rand(3)
165 165
166 @test à isa LazyLinearMap{T,1,1} where T 166 @test à isa LazyLinearMap{T,1,1} where T
167 @test à isa TensorMapping{T,1,1} where T 167 @test à isa LazyTensor{T,1,1} where T
168 @test range_size(Ã) == (3,) 168 @test range_size(Ã) == (3,)
169 @test domain_size(Ã) == (4,) 169 @test domain_size(Ã) == (4,)
170 170
171 @test Ã*ones(4) ≈ A*ones(4) atol=5e-13 171 @test Ã*ones(4) ≈ A*ones(4) atol=5e-13
172 @test Ã*v ≈ A*v atol=5e-13 172 @test Ã*v ≈ A*v atol=5e-13
181 B̃ = LazyLinearMap(B, (1,2), (3,)) 181 B̃ = LazyLinearMap(B, (1,2), (3,))
182 v = rand(2) 182 v = rand(2)
183 183
184 @test range_size(B̃) == (3,4) 184 @test range_size(B̃) == (3,4)
185 @test domain_size(B̃) == (2,) 185 @test domain_size(B̃) == (2,)
186 @test B̃ isa TensorMapping{T,2,1} where T 186 @test B̃ isa LazyTensor{T,2,1} where T
187 @test B̃*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13 187 @test B̃*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13
188 @test B̃*v ≈ B[:,:,1]*v[1] + B[:,:,2]*v[2] atol=5e-13 188 @test B̃*v ≈ B[:,:,1]*v[1] + B[:,:,2]*v[2] atol=5e-13
189 189
190 # Map matrices of size (3,2) to vectors of size 4 190 # Map matrices of size (3,2) to vectors of size 4
191 B̃ = LazyLinearMap(B, (2,), (1,3)) 191 B̃ = LazyLinearMap(B, (2,), (1,3))
192 v = rand(3,2) 192 v = rand(3,2)
193 193
194 @test range_size(B̃) == (4,) 194 @test range_size(B̃) == (4,)
195 @test domain_size(B̃) == (3,2) 195 @test domain_size(B̃) == (3,2)
196 @test B̃ isa TensorMapping{T,1,2} where T 196 @test B̃ isa LazyTensor{T,1,2} where T
197 @test B̃*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] + 197 @test B̃*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] +
198 B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13 198 B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13
199 @test B̃*v ≈ B[1,:,1]*v[1,1] + B[2,:,1]*v[2,1] + B[3,:,1]*v[3,1] + 199 @test B̃*v ≈ B[1,:,1]*v[1,1] + B[2,:,1]*v[2,1] + B[3,:,1]*v[3,1] +
200 B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13 200 B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13
201 201
203 # TODO: 203 # TODO:
204 # @inferred (B̃*v)[2] 204 # @inferred (B̃*v)[2]
205 end 205 end
206 206
207 207
208 @testset "IdentityMapping" begin 208 @testset "IdentityTensor" begin
209 @test IdentityMapping{Float64}((4,5)) isa IdentityMapping{T,2} where T 209 @test IdentityTensor{Float64}((4,5)) isa IdentityTensor{T,2} where T
210 @test IdentityMapping{Float64}((4,5)) isa TensorMapping{T,2,2} where T 210 @test IdentityTensor{Float64}((4,5)) isa LazyTensor{T,2,2} where T
211 @test IdentityMapping{Float64}((4,5)) == IdentityMapping{Float64}(4,5) 211 @test IdentityTensor{Float64}((4,5)) == IdentityTensor{Float64}(4,5)
212 212
213 @test IdentityMapping(3,2) isa IdentityMapping{Float64,2} 213 @test IdentityTensor(3,2) isa IdentityTensor{Float64,2}
214 214
215 for sz ∈ [(4,5),(3,),(5,6,4)] 215 for sz ∈ [(4,5),(3,),(5,6,4)]
216 I = IdentityMapping{Float64}(sz) 216 I = IdentityTensor{Float64}(sz)
217 v = rand(sz...) 217 v = rand(sz...)
218 @test I*v == v 218 @test I*v == v
219 @test I'*v == v 219 @test I'*v == v
220 220
221 v = rand(ComplexF64,sz...) 221 v = rand(ComplexF64,sz...)
224 224
225 @test range_size(I) == sz 225 @test range_size(I) == sz
226 @test domain_size(I) == sz 226 @test domain_size(I) == sz
227 end 227 end
228 228
229 I = IdentityMapping{Float64}((4,5)) 229 I = IdentityTensor{Float64}((4,5))
230 v = rand(4,5) 230 v = rand(4,5)
231 @inferred (I*v)[3,2] 231 @inferred (I*v)[3,2]
232 @inferred (I'*v)[3,2] 232 @inferred (I'*v)[3,2]
233 @inferred range_size(I) 233 @inferred range_size(I)
234 234
235 @inferred range_dim(I) 235 @inferred range_dim(I)
236 @inferred domain_dim(I) 236 @inferred domain_dim(I)
237 237
238 Ã = rand(4,2) 238 Ã = rand(4,2)
239 A = LazyLinearMap(Ã,(1,),(2,)) 239 A = LazyLinearMap(Ã,(1,),(2,))
240 I1 = IdentityMapping{Float64}(2) 240 I1 = IdentityTensor{Float64}(2)
241 I2 = IdentityMapping{Float64}(4) 241 I2 = IdentityTensor{Float64}(4)
242 @test A∘I1 == A 242 @test A∘I1 == A
243 @test I2∘A == A 243 @test I2∘A == A
244 @test I1∘I1 == I1 244 @test I1∘I1 == I1
245 @test_throws SizeMismatch I1∘A 245 @test_throws SizeMismatch I1∘A
246 @test_throws SizeMismatch A∘I2 246 @test_throws SizeMismatch A∘I2
247 @test_throws SizeMismatch I1∘I2 247 @test_throws SizeMismatch I1∘I2
248 end 248 end
249 249
250 @testset "ScalingTensor" begin 250 @testset "ScalingTensor" begin
251 st = ScalingTensor(2.,(3,4)) 251 st = ScalingTensor(2.,(3,4))
252 @test st isa TensorMapping{Float64, 2, 2} 252 @test st isa LazyTensor{Float64, 2, 2}
253 @test range_size(st) == (3,4) 253 @test range_size(st) == (3,4)
254 @test domain_size(st) == (3,4) 254 @test domain_size(st) == (3,4)
255 255
256 v = rand(3,4) 256 v = rand(3,4)
257 @test st*v == 2.0 .* v 257 @test st*v == 2.0 .* v
259 259
260 @inferred (st*v)[2,2] 260 @inferred (st*v)[2,2]
261 @inferred (st'*v)[2,2] 261 @inferred (st'*v)[2,2]
262 end 262 end
263 263
264 @testset "InflatedTensorMapping" begin 264 @testset "InflatedLazyTensor" begin
265 I(sz...) = IdentityMapping(sz...) 265 I(sz...) = IdentityTensor(sz...)
266 266
267 Ã = rand(4,2) 267 Ã = rand(4,2)
268 B̃ = rand(4,2,3) 268 B̃ = rand(4,2,3)
269 C̃ = rand(4,2,3) 269 C̃ = rand(4,2,3)
270 270
271 A = LazyLinearMap(Ã,(1,),(2,)) 271 A = LazyLinearMap(Ã,(1,),(2,))
272 B = LazyLinearMap(B̃,(1,2),(3,)) 272 B = LazyLinearMap(B̃,(1,2),(3,))
273 C = LazyLinearMap(C̃,(1,),(2,3)) 273 C = LazyLinearMap(C̃,(1,),(2,3))
274 274
275 @testset "Constructors" begin 275 @testset "Constructors" begin
276 @test InflatedTensorMapping(I(3,2), A, I(4)) isa TensorMapping{Float64, 4, 4} 276 @test InflatedLazyTensor(I(3,2), A, I(4)) isa LazyTensor{Float64, 4, 4}
277 @test InflatedTensorMapping(I(3,2), B, I(4)) isa TensorMapping{Float64, 5, 4} 277 @test InflatedLazyTensor(I(3,2), B, I(4)) isa LazyTensor{Float64, 5, 4}
278 @test InflatedTensorMapping(I(3), C, I(2,3)) isa TensorMapping{Float64, 4, 5} 278 @test InflatedLazyTensor(I(3), C, I(2,3)) isa LazyTensor{Float64, 4, 5}
279 @test InflatedTensorMapping(C, I(2,3)) isa TensorMapping{Float64, 3, 4} 279 @test InflatedLazyTensor(C, I(2,3)) isa LazyTensor{Float64, 3, 4}
280 @test InflatedTensorMapping(I(3), C) isa TensorMapping{Float64, 2, 3} 280 @test InflatedLazyTensor(I(3), C) isa LazyTensor{Float64, 2, 3}
281 @test InflatedTensorMapping(I(3), I(2,3)) isa TensorMapping{Float64, 3, 3} 281 @test InflatedLazyTensor(I(3), I(2,3)) isa LazyTensor{Float64, 3, 3}
282 end 282 end
283 283
284 @testset "Range and domain size" begin 284 @testset "Range and domain size" begin
285 @test range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) 285 @test range_size(InflatedLazyTensor(I(3,2), A, I(4))) == (3,2,4,4)
286 @test domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) 286 @test domain_size(InflatedLazyTensor(I(3,2), A, I(4))) == (3,2,2,4)
287 287
288 @test range_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,4,2,4) 288 @test range_size(InflatedLazyTensor(I(3,2), B, I(4))) == (3,2,4,2,4)
289 @test domain_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,3,4) 289 @test domain_size(InflatedLazyTensor(I(3,2), B, I(4))) == (3,2,3,4)
290 290
291 @test range_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,4,2,3) 291 @test range_size(InflatedLazyTensor(I(3), C, I(2,3))) == (3,4,2,3)
292 @test domain_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,2,3,2,3) 292 @test domain_size(InflatedLazyTensor(I(3), C, I(2,3))) == (3,2,3,2,3)
293 293
294 @inferred range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) 294 @inferred range_size(InflatedLazyTensor(I(3,2), A, I(4))) == (3,2,4,4)
295 @inferred domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) 295 @inferred domain_size(InflatedLazyTensor(I(3,2), A, I(4))) == (3,2,2,4)
296 end 296 end
297 297
298 @testset "Application" begin 298 @testset "Application" begin
299 # Testing regular application and transposed application with inflation "before", "after" and "before and after". 299 # Testing regular application and transposed application with inflation "before", "after" and "before and after".
300 # The inflated tensor mappings are chosen to preserve, reduce and increase the dimension of the result compared to the input. 300 # The inflated tensor mappings are chosen to preserve, reduce and increase the dimension of the result compared to the input.
301 tests = [ 301 tests = [
302 ( 302 (
303 InflatedTensorMapping(I(3,2), A, I(4)), 303 InflatedLazyTensor(I(3,2), A, I(4)),
304 (v-> @tullio res[a,b,c,d] := Ã[c,i]*v[a,b,i,d]), # Expected result of apply 304 (v-> @tullio res[a,b,c,d] := Ã[c,i]*v[a,b,i,d]), # Expected result of apply
305 (v-> @tullio res[a,b,c,d] := Ã[i,c]*v[a,b,i,d]), # Expected result of apply_transpose 305 (v-> @tullio res[a,b,c,d] := Ã[i,c]*v[a,b,i,d]), # Expected result of apply_transpose
306 ), 306 ),
307 ( 307 (
308 InflatedTensorMapping(I(3,2), B, I(4)), 308 InflatedLazyTensor(I(3,2), B, I(4)),
309 (v-> @tullio res[a,b,c,d,e] := B̃[c,d,i]*v[a,b,i,e]), 309 (v-> @tullio res[a,b,c,d,e] := B̃[c,d,i]*v[a,b,i,e]),
310 (v-> @tullio res[a,b,c,d] := B̃[i,j,c]*v[a,b,i,j,d]), 310 (v-> @tullio res[a,b,c,d] := B̃[i,j,c]*v[a,b,i,j,d]),
311 ), 311 ),
312 ( 312 (
313 InflatedTensorMapping(I(3,2), C, I(4)), 313 InflatedLazyTensor(I(3,2), C, I(4)),
314 (v-> @tullio res[a,b,c,d] := C̃[c,i,j]*v[a,b,i,j,d]), 314 (v-> @tullio res[a,b,c,d] := C̃[c,i,j]*v[a,b,i,j,d]),
315 (v-> @tullio res[a,b,c,d,e] := C̃[i,c,d]*v[a,b,i,e]), 315 (v-> @tullio res[a,b,c,d,e] := C̃[i,c,d]*v[a,b,i,e]),
316 ), 316 ),
317 ( 317 (
318 InflatedTensorMapping(I(3,2), A), 318 InflatedLazyTensor(I(3,2), A),
319 (v-> @tullio res[a,b,c] := Ã[c,i]*v[a,b,i]), 319 (v-> @tullio res[a,b,c] := Ã[c,i]*v[a,b,i]),
320 (v-> @tullio res[a,b,c] := Ã[i,c]*v[a,b,i]), 320 (v-> @tullio res[a,b,c] := Ã[i,c]*v[a,b,i]),
321 ), 321 ),
322 ( 322 (
323 InflatedTensorMapping(I(3,2), B), 323 InflatedLazyTensor(I(3,2), B),
324 (v-> @tullio res[a,b,c,d] := B̃[c,d,i]*v[a,b,i]), 324 (v-> @tullio res[a,b,c,d] := B̃[c,d,i]*v[a,b,i]),
325 (v-> @tullio res[a,b,c] := B̃[i,j,c]*v[a,b,i,j]), 325 (v-> @tullio res[a,b,c] := B̃[i,j,c]*v[a,b,i,j]),
326 ), 326 ),
327 ( 327 (
328 InflatedTensorMapping(I(3,2), C), 328 InflatedLazyTensor(I(3,2), C),
329 (v-> @tullio res[a,b,c] := C̃[c,i,j]*v[a,b,i,j]), 329 (v-> @tullio res[a,b,c] := C̃[c,i,j]*v[a,b,i,j]),
330 (v-> @tullio res[a,b,c,d] := C̃[i,c,d]*v[a,b,i]), 330 (v-> @tullio res[a,b,c,d] := C̃[i,c,d]*v[a,b,i]),
331 ), 331 ),
332 ( 332 (
333 InflatedTensorMapping(A,I(4)), 333 InflatedLazyTensor(A,I(4)),
334 (v-> @tullio res[a,b] := Ã[a,i]*v[i,b]), 334 (v-> @tullio res[a,b] := Ã[a,i]*v[i,b]),
335 (v-> @tullio res[a,b] := Ã[i,a]*v[i,b]), 335 (v-> @tullio res[a,b] := Ã[i,a]*v[i,b]),
336 ), 336 ),
337 ( 337 (
338 InflatedTensorMapping(B,I(4)), 338 InflatedLazyTensor(B,I(4)),
339 (v-> @tullio res[a,b,c] := B̃[a,b,i]*v[i,c]), 339 (v-> @tullio res[a,b,c] := B̃[a,b,i]*v[i,c]),
340 (v-> @tullio res[a,b] := B̃[i,j,a]*v[i,j,b]), 340 (v-> @tullio res[a,b] := B̃[i,j,a]*v[i,j,b]),
341 ), 341 ),
342 ( 342 (
343 InflatedTensorMapping(C,I(4)), 343 InflatedLazyTensor(C,I(4)),
344 (v-> @tullio res[a,b] := C̃[a,i,j]*v[i,j,b]), 344 (v-> @tullio res[a,b] := C̃[a,i,j]*v[i,j,b]),
345 (v-> @tullio res[a,b,c] := C̃[i,a,b]*v[i,c]), 345 (v-> @tullio res[a,b,c] := C̃[i,a,b]*v[i,c]),
346 ), 346 ),
347 ] 347 ]
348 348
363 @test tm'*v ≈ true_value rtol=1e-14 363 @test tm'*v ≈ true_value rtol=1e-14
364 end 364 end
365 end 365 end
366 366
367 @testset "application to other type" begin 367 @testset "application to other type" begin
368 tm = InflatedTensorMapping(I(3,2), A, I(4)) 368 tm = InflatedLazyTensor(I(3,2), A, I(4))
369 369
370 v = rand(ComplexF64, domain_size(tm)...) 370 v = rand(ComplexF64, domain_size(tm)...)
371 @test (tm*v)[1,2,3,1] isa ComplexF64 371 @test (tm*v)[1,2,3,1] isa ComplexF64
372 372
373 v = rand(ComplexF64, domain_size(tm')...) 373 v = rand(ComplexF64, domain_size(tm')...)
374 @test (tm'*v)[1,2,2,1] isa ComplexF64 374 @test (tm'*v)[1,2,2,1] isa ComplexF64
375 end 375 end
376 376
377 @testset "Inference of application" begin 377 @testset "Inference of application" begin
378 tm = InflatedTensorMapping(I(2,3),ScalingTensor(2.0, (3,2)),I(3,4)) 378 tm = InflatedLazyTensor(I(2,3),ScalingTensor(2.0, (3,2)),I(3,4))
379 v = rand(domain_size(tm)...) 379 v = rand(domain_size(tm)...)
380 380
381 @inferred apply(tm,v,1,2,3,2,2,4) 381 @inferred apply(tm,v,1,2,3,2,2,4)
382 @inferred (tm*v)[1,2,3,2,2,4] 382 @inferred (tm*v)[1,2,3,2,2,4]
383 end 383 end
384 end 384 end
385 385
386 @testset "InflatedTensorMapping of InflatedTensorMapping" begin 386 @testset "InflatedLazyTensor of InflatedLazyTensor" begin
387 A = ScalingTensor(2.0,(2,3)) 387 A = ScalingTensor(2.0,(2,3))
388 itm = InflatedTensorMapping(I(3,2), A, I(4)) 388 itm = InflatedLazyTensor(I(3,2), A, I(4))
389 @test InflatedTensorMapping(I(4), itm, I(2)) == InflatedTensorMapping(I(4,3,2), A, I(4,2)) 389 @test InflatedLazyTensor(I(4), itm, I(2)) == InflatedLazyTensor(I(4,3,2), A, I(4,2))
390 @test InflatedTensorMapping(itm, I(2)) == InflatedTensorMapping(I(3,2), A, I(4,2)) 390 @test InflatedLazyTensor(itm, I(2)) == InflatedLazyTensor(I(3,2), A, I(4,2))
391 @test InflatedTensorMapping(I(4), itm) == InflatedTensorMapping(I(4,3,2), A, I(4)) 391 @test InflatedLazyTensor(I(4), itm) == InflatedLazyTensor(I(4,3,2), A, I(4))
392 392
393 @test InflatedTensorMapping(I(2), I(2), I(2)) isa InflatedTensorMapping # The constructor should always return its type. 393 @test InflatedLazyTensor(I(2), I(2), I(2)) isa InflatedLazyTensor # The constructor should always return its type.
394 end 394 end
395 end 395 end
396 396
397 @testset "split_index" begin 397 @testset "split_index" begin
398 @test LazyTensors.split_index(Val(2),Val(1),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,5,6),(3,4)) 398 @test LazyTensors.split_index(Val(2),Val(1),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,5,6),(3,4))
460 A = ScalingTensor(2.0, (5,)) 460 A = ScalingTensor(2.0, (5,))
461 B = ScalingTensor(3.0, (3,)) 461 B = ScalingTensor(3.0, (3,))
462 C = ScalingTensor(5.0, (3,2)) 462 C = ScalingTensor(5.0, (3,2))
463 463
464 AB = LazyOuterProduct(A,B) 464 AB = LazyOuterProduct(A,B)
465 @test AB isa TensorMapping{T,2,2} where T 465 @test AB isa LazyTensor{T,2,2} where T
466 @test range_size(AB) == (5,3) 466 @test range_size(AB) == (5,3)
467 @test domain_size(AB) == (5,3) 467 @test domain_size(AB) == (5,3)
468 468
469 v = rand(range_size(AB)...) 469 v = rand(range_size(AB)...)
470 @test AB*v == 6*v 470 @test AB*v == 6*v
471 471
472 ABC = LazyOuterProduct(A,B,C) 472 ABC = LazyOuterProduct(A,B,C)
473 473
474 @test ABC isa TensorMapping{T,4,4} where T 474 @test ABC isa LazyTensor{T,4,4} where T
475 @test range_size(ABC) == (5,3,3,2) 475 @test range_size(ABC) == (5,3,3,2)
476 @test domain_size(ABC) == (5,3,3,2) 476 @test domain_size(ABC) == (5,3,3,2)
477 477
478 @test A⊗B == AB 478 @test A⊗B == AB
479 @test A⊗B⊗C == ABC 479 @test A⊗B⊗C == ABC
494 B̃Ã = LazyOuterProduct(B̃,Ã) 494 B̃Ã = LazyOuterProduct(B̃,Ã)
495 @tullio BAv[k,i] := A[i,j]*B[k,l,m]*v₂[l,m,j] 495 @tullio BAv[k,i] := A[i,j]*B[k,l,m]*v₂[l,m,j]
496 @test B̃Ã*v₂ ≈ BAv 496 @test B̃Ã*v₂ ≈ BAv
497 497
498 @testset "Indentity mapping arguments" begin 498 @testset "Indentity mapping arguments" begin
499 @test LazyOuterProduct(IdentityMapping(3,2), IdentityMapping(1,2)) == IdentityMapping(3,2,1,2) 499 @test LazyOuterProduct(IdentityTensor(3,2), IdentityTensor(1,2)) == IdentityTensor(3,2,1,2)
500 500
501 Ã = LazyLinearMap(A,(1,),(2,)) 501 Ã = LazyLinearMap(A,(1,),(2,))
502 @test LazyOuterProduct(IdentityMapping(3,2), Ã) == InflatedTensorMapping(IdentityMapping(3,2),Ã) 502 @test LazyOuterProduct(IdentityTensor(3,2), Ã) == InflatedLazyTensor(IdentityTensor(3,2),Ã)
503 @test LazyOuterProduct(Ã, IdentityMapping(3,2)) == InflatedTensorMapping(Ã,IdentityMapping(3,2)) 503 @test LazyOuterProduct(Ã, IdentityTensor(3,2)) == InflatedLazyTensor(Ã,IdentityTensor(3,2))
504 504
505 I1 = IdentityMapping(3,2) 505 I1 = IdentityTensor(3,2)
506 I2 = IdentityMapping(4) 506 I2 = IdentityTensor(4)
507 @test I1⊗Ã⊗I2 == InflatedTensorMapping(I1, Ã, I2) 507 @test I1⊗Ã⊗I2 == InflatedLazyTensor(I1, Ã, I2)
508 end 508 end
509 509
510 end 510 end