@@ -13,8 +13,6 @@ typelist = (Float32, Float64, ComplexF32, ComplexF64)
13
13
@test C1 ≈ C2
14
14
@test C2 == C3
15
15
@test C1 ≈ ncon (Any[A], Any[[- 2 , - 4 , - 1 , - 3 ]])
16
- @test_throws IndexError tensorcopy (1 : 4 , A, 1 : 3 )
17
- @test_throws IndexError tensorcopy (1 : 4 , A, [1 , 2 , 2 , 4 ])
18
16
end
19
17
20
18
@testset " tensoradd" begin
@@ -26,45 +24,29 @@ typelist = (Float32, Float64, ComplexF32, ComplexF64)
26
24
C3 = @inferred tensoradd (A, ((1 : 4 ... ,), ()), false , B, (p, ()), false , 1 , 1 , b)
27
25
@test C1 ≈ C2
28
26
@test C2 == C3
29
- @test C1 ≈ A + ncon (Any[B], Any[[- 2 , - 4 , - 1 , - 3 ]]; backend= b)
30
- @test_throws DimensionMismatch tensoradd (A, 1 : 4 , B, 1 : 4 )
31
27
end
32
28
33
29
@testset " tensortrace" begin
34
30
A = randn (Float64, (50 , 100 , 100 ))
35
- C1 = zeros (50 )
36
- for i in 1 : 50
37
- for j in 1 : 100
38
- C1[i] += A[i, j, j]
39
- end
40
- end
31
+ C1 = tensortrace (A, [:a , :b , :b ])
41
32
C2 = tensortrace (A, [:a , :b , :b ]; backend= b)
42
33
C3 = ncon (Any[A], Any[[- 1 , 1 , 1 ]]; backend= b)
43
34
@test C1 ≈ C2
44
35
@test C2 == C3
45
36
A = randn (Float64, (3 , 20 , 5 , 3 , 20 , 4 , 5 ))
46
- C1 = zeros (4 , 3 , 3 )
47
- for i1 in 1 : 4 , i2 in 1 : 3 , i3 in 1 : 3
48
- for j1 in 1 : 20 , j2 in 1 : 5
49
- C1[i1, i2, i3] += A[i2, j1, j2, i3, j1, i1, j2]
50
- end
51
- end
37
+ C1 = tensortrace ((:e , :a , :d ), A, (:a , :b , :c , :d , :b , :e , :c ))
52
38
C2 = @inferred tensortrace ((:e , :a , :d ), A, (:a , :b , :c , :d , :b , :e , :c ); backend= b)
53
39
C3 = @inferred tensortrace (A, ((6 , 1 , 4 ), ()), ((2 , 3 ), (5 , 7 )), false , 1.0 , b)
54
40
C4 = ncon (Any[A], Any[[- 2 , 1 , 2 , - 3 , 1 , - 1 , 2 ]]; backend= b)
55
41
@test C1 ≈ C2
56
42
@test C2 == C3 == C4
57
- @test_throws IndexError tensortrace (randn (2 , 2 , 2 , 2 , 2 , 2 , 2 ), ((1 ,), (3 , 2 )),
58
- ((1 , 5 ), (2 , 6 )), false )
59
43
end
60
44
61
45
@testset " tensorcontract" begin
62
46
A = randn (T, (3 , 20 , 5 , 3 , 4 ))
63
47
B = randn (T, (5 , 6 , 20 , 3 ))
64
- C1 = zeros (T, (3 , 3 , 4 , 3 , 6 ))
65
- for a in 1 : 3 , b in 1 : 20 , c in 1 : 5 , d in 1 : 3 , e in 1 : 4 , f in 1 : 6 , g in 1 : 3
66
- C1[a, g, e, d, f] += A[a, b, c, d, e] * B[c, f, b, g]
67
- end
48
+ C1 = tensorcontract ((:a , :g , :e , :d , :f ), A, (:a , :b , :c , :d , :e ), B,
49
+ (:c , :f , :b , :g ))
68
50
C2 = @inferred tensorcontract ((:a , :g , :e , :d , :f ),
69
51
A, (:a , :b , :c , :d , :e ), B, (:c , :f , :b , :g );
70
52
backend= b)
@@ -78,8 +60,6 @@ typelist = (Float32, Float64, ComplexF32, ComplexF64)
78
60
79
61
@test C1 ≈ C2
80
62
@test C2 == C3 == C4 == C5
81
- @test_throws IndexError tensorcontract (A, [:a , :b , :c , :d ], B, [:c , :f , :b , :g ])
82
- @test_throws IndexError tensorcontract (A, [:a , :b , :c , :a , :e ], B, [:c , :f , :b , :g ])
83
63
end
84
64
85
65
@testset " tensorproduct" begin
@@ -90,17 +70,10 @@ typelist = (Float32, Float64, ComplexF32, ComplexF64)
90
70
A, (1 , 2 , 3 , 4 ), B, (5 , 6 , 7 , 8 ); backend= b)),
91
71
(5 * 5 * 5 * 5 , 5 * 5 * 5 * 5 ))
92
72
@test C1 ≈ C2
93
- @test_throws IndexError tensorproduct (A, [:a , :b , :c , :d ],
94
- B, [:d , :e , :f , :g ])
95
- @test_throws IndexError tensorproduct ([:a , :b , :c , :d , :e , :f , :g , :i ],
96
- A, [:a , :b , :c , :d ], B, [:e , :f , :g , :h ])
97
73
98
74
A = rand (1 , 2 )
99
75
B = rand (4 , 5 )
100
- C1 = zeros (T, (2 , 4 , 1 , 5 ))
101
- for i in axes (C1, 1 ), j in axes (C1, 2 ), k in axes (C1, 3 ), l in axes (C1, 4 )
102
- C1[i, j, k, l] = A[k, i] * B[j, l]
103
- end
76
+ C1 = tensorcontract ((- 1 , - 2 , - 3 , - 4 ), A, (- 3 , - 1 ), false , B, (- 2 , - 4 ), false )
104
77
C2 = tensorcontract ((- 1 , - 2 , - 3 , - 4 ), A, (- 3 , - 1 ), false , B, (- 2 , - 4 ), false ;
105
78
backend= b)
106
79
C3 = tensorproduct (A, ((1 , 2 ), ()), false , B, ((), (1 , 2 )), false ,
@@ -141,12 +114,11 @@ end
141
114
p = (3 , 1 , 4 , 2 )
142
115
Cbig = zeros (ComplexF32, (50 , 50 , 50 , 50 ))
143
116
C = view (Cbig, 13 .+ (0 : 6 ), 11 .+ 4 * (0 : 9 ), 15 .+ 4 * (0 : 8 ), 4 .+ 3 * (0 : 6 ))
144
- Acopy = tensorcopy (p, A, 1 : 4 )
145
117
Ccopy = tensorcopy (1 : 4 , C, 1 : 4 )
146
118
α = randn (ComplexF32)
147
119
β = randn (ComplexF32)
148
120
tensoradd! (C, A, (p, ()), false , α, β, b)
149
- Ccopy = β * Ccopy + α * Acopy
121
+ tensoradd! ( Ccopy, A, (p, ()), false , α, β) # default backend
150
122
@test C ≈ Ccopy
151
123
@test_throws IndexError tensoradd! (C, A, ((1 , 2 , 3 ), ()), false , 1.2 , 0.5 , b)
152
124
@test_throws DimensionMismatch tensoradd! (C, A, ((1 , 2 , 3 , 4 ), ()), false , 1.2 , 0.5 ,
164
136
α = randn (Float64)
165
137
β = randn (Float64)
166
138
tensortrace! (B, A, ((2 , 3 ), ()), ((1 ,), (4 ,)), true , α, β, b)
167
- Bcopy = β * Bcopy
168
- for i in 1 .+ (0 : 8 )
169
- Bcopy += α * conj (view (A, i, :, :, i))
170
- end
139
+ tensortrace! (Bcopy, A, ((2 , 3 ), ()), ((1 ,), (4 ,)), true , α, β) # default backend
171
140
@test B ≈ Bcopy
172
141
@test_throws IndexError tensortrace! (B, A, ((1 ,), ()), ((2 ,), (3 ,)), false , α, β, b)
173
142
@test_throws DimensionMismatch tensortrace! (B, A, ((1 , 4 ), ()), ((2 ,), (3 ,)), false ,
178
147
α, β, b)
179
148
end
180
149
150
+ bref = TensorOperations. DefaultBackend () # reference backend
181
151
@testset " tensorcontract! with allocator = $allocator " for allocator in
182
152
(DefaultAllocator (),
183
153
ManualAllocator ())
@@ -192,36 +162,24 @@ end
192
162
Ccopy = tensorcopy (C, 1 : 3 )
193
163
α = randn (ComplexF64)
194
164
β = randn (ComplexF64)
195
- Ccopy = β * Ccopy
196
- for d in 1 .+ (0 : 8 ), a in 1 .+ (0 : 8 ), e in 1 .+ (0 : 7 )
197
- for b in 1 .+ (0 : 14 ), c in 1 .+ (0 : 6 )
198
- Ccopy[d, a, e] += α * A[a, b, c, d] * conj (B[c, e, b])
199
- end
200
- end
201
165
tensorcontract! (C, A, ((4 , 1 ), (2 , 3 )), false , B, ((3 , 1 ), (2 ,)), true ,
202
166
((1 , 2 , 3 ), ()), α, β, b, allocator)
167
+ tensorcontract! (Ccopy, A, ((4 , 1 ), (2 , 3 )), false , B, ((3 , 1 ), (2 ,)), true ,
168
+ ((1 , 2 , 3 ), ()), α, β, bref, allocator)
203
169
@test C ≈ Ccopy
204
170
205
171
Ccopy = tensorcopy (C, 1 : 3 )
206
- Ccopy = β * Ccopy
207
- for d in 1 .+ (0 : 8 ), a in 1 .+ (0 : 8 ), e in 1 .+ (0 : 7 )
208
- for b in 1 .+ (0 : 14 ), c in 1 .+ (0 : 6 )
209
- Ccopy[d, a, e] += α * conj (A[a, b, c, d]) * conj (B[c, e, b])
210
- end
211
- end
212
172
tensorcontract! (C, A, ((4 , 1 ), (2 , 3 )), true , B, ((3 , 1 ), (2 ,)), true ,
213
173
((1 , 2 , 3 ), ()), α, β, b, allocator)
174
+ tensorcontract! (Ccopy, A, ((4 , 1 ), (2 , 3 )), true , B, ((3 , 1 ), (2 ,)), true ,
175
+ ((1 , 2 , 3 ), ()), α, β, bref, allocator)
214
176
@test C ≈ Ccopy
215
177
216
178
Ccopy = tensorcopy (C, 1 : 3 )
217
- Ccopy = β * Ccopy
218
- for d in 1 .+ (0 : 8 ), a in 1 .+ (0 : 8 ), e in 1 .+ (0 : 7 )
219
- for b in 1 .+ (0 : 14 ), c in 1 .+ (0 : 6 )
220
- Ccopy[d, a, e] += α * conj (A[a, b, c, d]) * B[c, e, b]
221
- end
222
- end
223
179
tensorcontract! (C, A, ((4 , 1 ), (2 , 3 )), true , B, ((3 , 1 ), (2 ,)), false ,
224
180
((1 , 2 , 3 ), ()), α, β, b, allocator)
181
+ tensorcontract! (Ccopy, A, ((4 , 1 ), (2 , 3 )), true , B, ((3 , 1 ), (2 ,)), false ,
182
+ ((1 , 2 , 3 ), ()), α, β, bref, allocator)
225
183
@test C ≈ Ccopy
226
184
227
185
@test_throws IndexError tensorcontract! (C,
0 commit comments