fullTensor_assignment.cxx 10.6 KB
Newer Older
Baum's avatar
Baum committed
1
// Xerus - A General Purpose Tensor Library
2
// Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf. 
Baum's avatar
Baum committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 
// Xerus is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// 
// Xerus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// 
// You should have received a copy of the GNU Affero General Public License
// along with Xerus. If not, see <http://www.gnu.org/licenses/>.
//
// For further information on Xerus visit https://libXerus.org 
// or contact us at contact@libXerus.org.

20 21 22

#include<xerus.h>

23
#include "../../include/xerus/test/test.h"
24
#include "../../include/xerus/misc/internal.h"
25
using namespace xerus;
Baum's avatar
Baum committed
26

27
static misc::UnitTest tensor_assign_triv("Tensor", "Assignment_Trivia", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
28 29 30 31
    Tensor A({2,2,3,1,2});
    Tensor res({2,2,3,1,2});
    Tensor res2({2,3,2,1,2});
    Tensor res3({2,3,1,2,2});
Baum's avatar
Baum committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

    Index i,j,k,l,m;
    
    A[{0,0,0,0,0}]=1;
    A[{0,0,0,0,1}]=2;
    A[{0,0,1,0,0}]=3;
    A[{0,0,1,0,1}]=4;
    A[{0,0,2,0,0}]=5;
    A[{0,0,2,0,1}]=6;
    A[{0,1,0,0,0}]=7;
    A[{0,1,0,0,1}]=8;
    A[{0,1,1,0,0}]=9;
    A[{0,1,1,0,1}]=10;
    A[{0,1,2,0,0}]=11;
    A[{0,1,2,0,1}]=12;
    A[{1,0,0,0,0}]=13;
    A[{1,0,0,0,1}]=14;
    A[{1,0,1,0,0}]=15;
    A[{1,0,1,0,1}]=16;
    A[{1,0,2,0,0}]=17;
    A[{1,0,2,0,1}]=18;
    A[{1,1,0,0,0}]=19;
    A[{1,1,0,0,1}]=20;
    A[{1,1,1,0,0}]=21;
    A[{1,1,1,0,1}]=22;
    A[{1,1,2,0,0}]=23;
    A[{1,1,2,0,1}]=24;
    
    res(i,j,k,l,m) = A(i,j,k,l,m);
61
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
62
    res(i&0) = A(i&0);
63
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
64
    res(i&0) = A(i^5);
65
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
66
    res(i^5) = A(i&0);
67
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
68
    res(i^3, j&3) = A(i&2,j^2);
69
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
70 71
    
    res(j,i,k,l,m) = A(j,i,k,l,m);
72
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
73
    res(i,j,k,l,m) = A(j,i,k,l,m);
74
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,13,14,15,16,17,18,7,8,9,10,11,12,19,20,21,22,23,24}));
Baum's avatar
Baum committed
75
    res(j,i,k,l,m) = A(i,j,k,l,m);
76
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,13,14,15,16,17,18,7,8,9,10,11,12,19,20,21,22,23,24}));
Baum's avatar
Baum committed
77 78
    
    res2(i,k,j,l,m) = A(i,j,k,l,m);
79
    TEST(approx_entrywise_equal(res2, {1,2,7,8,3,4,9,10,5,6,11,12,13,14,19,20,15,16,21,22,17,18,23,24}));
Baum's avatar
Baum committed
80
    res2(i,j,k,l,m) = A(i,k,j,l,m);
81
    TEST(approx_entrywise_equal(res2, {1,2,7,8,3,4,9,10,5,6,11,12,13,14,19,20,15,16,21,22,17,18,23,24}));
Baum's avatar
Baum committed
82 83
    
    res(i,j,k,l,m) = A(i,j,l,k,m);
84
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
85
    res(i,j,l,k,m) = A(i,j,k,l,m);
86
    TEST(approx_entrywise_equal(res, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
87
});
Baum's avatar
Baum committed
88

89
static misc::UnitTest tensor_assign_const("Tensor", "Assignment_Const", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
90 91
    Tensor A({2,2,3,1,2});
    Tensor res({2,2,3,1,2});
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    Index i,j,k,l,m;
    
    A[{0,0,0,0,0}]=1;
    A[{0,0,0,0,1}]=2;
    A[{0,0,1,0,0}]=3;
    A[{0,0,1,0,1}]=4;
    A[{0,0,2,0,0}]=5;
    A[{0,0,2,0,1}]=6;
    A[{0,1,0,0,0}]=7;
    A[{0,1,0,0,1}]=8;
    A[{0,1,1,0,0}]=9;
    A[{0,1,1,0,1}]=10;
    A[{0,1,2,0,0}]=11;
    A[{0,1,2,0,1}]=12;
    A[{1,0,0,0,0}]=13;
    A[{1,0,0,0,1}]=14;
    A[{1,0,1,0,0}]=15;
    A[{1,0,1,0,1}]=16;
    A[{1,0,2,0,0}]=17;
    A[{1,0,2,0,1}]=18;
    A[{1,1,0,0,0}]=19;
    A[{1,1,0,0,1}]=20;
    A[{1,1,1,0,0}]=21;
    A[{1,1,1,0,1}]=22;
    A[{1,1,2,0,0}]=23;
    A[{1,1,2,0,1}]=24;
    
    res(i,j,k,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
121
    const Tensor resC1(res);
122
    TEST(approx_entrywise_equal(resC1, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
123 124 125
    TEST(misc::approx_equal(resC1[{0,0,0,0,0}], 1.0));
    TEST(misc::approx_equal(resC1[{0,1,2,0,0}], 11.0));
    TEST(misc::approx_equal(resC1[{1,1,2,0,0}], 23.0));
126 127
    
    res(j,i,k,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
128
    const Tensor resC2(res);
129
    TEST(approx_entrywise_equal(resC2, {1,2,3,4,5,6,13,14,15,16,17,18,7,8,9,10,11,12,19,20,21,22,23,24}));
130 131 132
    TEST(misc::approx_equal(resC2[{0,0,0,0,0}], 1.0));
    TEST(misc::approx_equal(resC2[{0,1,2,0,0}], 17.0));
    TEST(misc::approx_equal(resC2[{1,1,2,0,0}], 23.0));
133
});
134

135
static misc::UnitTest tensor_assign_overwriting_dim("Tensor", "Assignment_Overwriting_Dimensions", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
136 137 138 139 140 141 142 143 144 145 146 147 148
    Tensor A({2,2,3,1,2});
    Tensor res1({2,2,3,1,2});
    Tensor res2({1,3,5,1,7});
    Tensor res3({2,3,2,2,3});
    Tensor res4({13,9,2,5,3});
    Tensor res5({1,1,1,1,1});
    Tensor res6;
    Tensor res7({1,2,3,4,5});
    Tensor res8({5,4,3,2,1});
    Tensor res9({3,2,3,2,3});
    Tensor res10({4,5,3,1,3});
    Tensor res11;
    Tensor res12({1,5,3,1,3});
Baum's avatar
Baum committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    Index i,j,k,l,m;
    
    A[{0,0,0,0,0}]=1;
    A[{0,0,0,0,1}]=2;
    A[{0,0,1,0,0}]=3;
    A[{0,0,1,0,1}]=4;
    A[{0,0,2,0,0}]=5;
    A[{0,0,2,0,1}]=6;
    A[{0,1,0,0,0}]=7;
    A[{0,1,0,0,1}]=8;
    A[{0,1,1,0,0}]=9;
    A[{0,1,1,0,1}]=10;
    A[{0,1,2,0,0}]=11;
    A[{0,1,2,0,1}]=12;
    A[{1,0,0,0,0}]=13;
    A[{1,0,0,0,1}]=14;
    A[{1,0,1,0,0}]=15;
    A[{1,0,1,0,1}]=16;
    A[{1,0,2,0,0}]=17;
    A[{1,0,2,0,1}]=18;
    A[{1,1,0,0,0}]=19;
    A[{1,1,0,0,1}]=20;
    A[{1,1,1,0,0}]=21;
    A[{1,1,1,0,1}]=22;
    A[{1,1,2,0,0}]=23;
    A[{1,1,2,0,1}]=24;
    
    res1(i,j,k,l,m) = A(i,j,k,l,m);
178
    TEST(approx_entrywise_equal(res1, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
179
    res2(i&0) = A(i&0);
180
    TEST(approx_entrywise_equal(res2, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
181
    res3(i&0) = A(i^5);
182
    TEST(approx_entrywise_equal(res3, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
183
    res4(i^5) = A(i&0);
184
    TEST(approx_entrywise_equal(res4, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
185
    res5(i^3, j&3) = A(i&2,j^2);
186
    TEST(approx_entrywise_equal(res5, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
187 188
    
    res6(j,i,k,l,m) = A(j,i,k,l,m);
189
    TEST(approx_entrywise_equal(res6, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
190
    res7(i,j,k,l,m) = A(j,i,k,l,m);
191
    TEST(approx_entrywise_equal(res7, {1,2,3,4,5,6,13,14,15,16,17,18,7,8,9,10,11,12,19,20,21,22,23,24}));
Baum's avatar
Baum committed
192
    res8(j,i,k,l,m) = A(i,j,k,l,m);
193
    TEST(approx_entrywise_equal(res8, {1,2,3,4,5,6,13,14,15,16,17,18,7,8,9,10,11,12,19,20,21,22,23,24}));
Baum's avatar
Baum committed
194 195
    
    res9(i,k,j,l,m) = A(i,j,k,l,m);
196
    TEST(approx_entrywise_equal(res9, {1,2,7,8,3,4,9,10,5,6,11,12,13,14,19,20,15,16,21,22,17,18,23,24}));
Baum's avatar
Baum committed
197
    res10(i,j,k,l,m) = A(i,k,j,l,m);
198
    TEST(approx_entrywise_equal(res10, {1,2,7,8,3,4,9,10,5,6,11,12,13,14,19,20,15,16,21,22,17,18,23,24}));
Baum's avatar
Baum committed
199 200
    
    res11(i,j,k,l,m) = A(i,j,l,k,m);
201
    TEST(approx_entrywise_equal(res11, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
202
    res12(i,j,l,k,m) = A(i,j,k,l,m);
203
    TEST(approx_entrywise_equal(res12, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}));
204
});
Baum's avatar
Baum committed
205

206 207
static misc::UnitTest tensor_assign_eq("Tensor", "Assignment_LHS_Equals_RHS", [](){
	Tensor B({2,2});
Sebastian Wolf's avatar
Sebastian Wolf committed
208
    Tensor C({2,2});
Baum's avatar
Baum committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222

    Index i, J, K;
    
    B[{0,0}]=1;
    B[{0,1}]=2;
    B[{1,0}]=3;
    B[{1,1}]=4;
    
    C[{0,0}]=5;
    C[{0,1}]=6;
    C[{1,0}]=7;
    C[{1,1}]=8;
    
    B(i,J) = B(i,J);
223
    TEST(approx_entrywise_equal(B, {1,2,3,4}));
Baum's avatar
Baum committed
224
    B(i,J) = B(J,i);
225
    TEST(approx_entrywise_equal(B, {1,3,2,4}));
226
});
Baum's avatar
Baum committed
227

228
static misc::UnitTest tensor_assign_fixed_idx("Tensor", "Assignment_Fixed_Indices", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
229 230 231 232
    Tensor A({2,2,3,1,2});
    Tensor res1({2,3,1,2});
    Tensor res2;
    Tensor res3;
Baum's avatar
Baum committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    
    Index i,j,k,l,m;
    
    A[{0,0,0,0,0}]=1;
    A[{0,0,0,0,1}]=2;
    A[{0,0,1,0,0}]=3;
    A[{0,0,1,0,1}]=4;
    A[{0,0,2,0,0}]=5;
    A[{0,0,2,0,1}]=6;
    A[{0,1,0,0,0}]=7;
    A[{0,1,0,0,1}]=8;
    A[{0,1,1,0,0}]=9;
    A[{0,1,1,0,1}]=10;
    A[{0,1,2,0,0}]=11;
    A[{0,1,2,0,1}]=12;
    A[{1,0,0,0,0}]=13;
    A[{1,0,0,0,1}]=14;
    A[{1,0,1,0,0}]=15;
    A[{1,0,1,0,1}]=16;
    A[{1,0,2,0,0}]=17;
    A[{1,0,2,0,1}]=18;
    A[{1,1,0,0,0}]=19;
    A[{1,1,0,0,1}]=20;
    A[{1,1,1,0,0}]=21;
    A[{1,1,1,0,1}]=22;
    A[{1,1,2,0,0}]=23;
    A[{1,1,2,0,1}]=24;
    
    res1(i,j,k,l) = A(0,i,j,k,l);
262
    TEST(approx_entrywise_equal(res1, {1,2,3,4,5,6,7,8,9,10,11,12}));
Baum's avatar
Baum committed
263
    res1(i,j,k,l) = A(1,i,j,k,l);
264
    TEST(approx_entrywise_equal(res1, {13,14,15,16,17,18,19,20,21,22,23,24}));
Baum's avatar
Baum committed
265 266
    
    res1(i,j,k,l) = A(i,0,j,k,l);
267
    TEST(approx_entrywise_equal(res1, {1,2,3,4,5,6,13,14,15,16,17,18}));
Baum's avatar
Baum committed
268
    res1(i,j,k,l) = A(i,1,j,k,l);
269
    TEST(approx_entrywise_equal(res1, {7,8,9,10,11,12,19,20,21,22,23,24}));
Baum's avatar
Baum committed
270 271
    
    res1(i,j,k,l) = A(i,j,k,l,0);
272
    TEST(approx_entrywise_equal(res1, {1,3,5,7,9,11,13,15,17,19,21,23}));
Baum's avatar
Baum committed
273
    res1(i,j,k,l) = A(i,j,k,l,1);
274
    TEST(approx_entrywise_equal(res1, {2,4,6,8,10,12,14,16,18,20,22,24}));
Baum's avatar
Baum committed
275 276
    
    res2(i,j,k) = A(i,j,1,k,1);
277
    TEST(approx_entrywise_equal(res2, {4,10,16,22}));
Baum's avatar
Baum committed
278
    res2(i,j,k) = A(i,j,2,k,1);
279
    TEST(approx_entrywise_equal(res2, {6,12,18,24}));
Baum's avatar
Baum committed
280
    res2(i,j,k) = A(j,i,2,k,1);
281
    TEST(approx_entrywise_equal(res2, {6,18,12,24}));
Baum's avatar
Baum committed
282 283
    
    res2(i,k,j) = A(j,i,2,k,1);
284
    TEST(approx_entrywise_equal(res2, {6,18,12,24}));
Baum's avatar
Baum committed
285 286
    
    res3(i,j) = A(j,i,2,0,1);
287
    TEST(approx_entrywise_equal(res3, {6,18,12,24}));
Baum's avatar
Baum committed
288
    res3(i,j) = A(1,i,2,j,1);
289
    TEST(approx_entrywise_equal(res3, {18,24}));
290
});
Baum's avatar
Baum committed
291

292
static misc::UnitTest tensor_assign_neg("Tensor", "Assignment_Negatives", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
293 294 295 296 297 298
    Tensor A({2,2,2,2});
    Tensor A2({2,2,2,2});
    Tensor B({2,2,2});
    Tensor C;
    Tensor D({2,2});
    Tensor E({});
Baum's avatar
Baum committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    
    Index i,j,k,l;
        
    FAILTEST(A(i) = A(i));
    FAILTEST(A(i&1) = A(i&1));
    FAILTEST(A(i^3) = A(i^3));
    FAILTEST(A(i,j^3) = A(i^3,j));
    FAILTEST(A(i^5) = A(i&0));
    FAILTEST(A(i^4,j&5) = A(i^4,j&5));
    FAILTEST(A2(i) = A(i));
    FAILTEST(A2(i&1) = A(i&1));
    FAILTEST(A2(i^3) = A(i^3));
    FAILTEST(A2(i,j^3) = A(i^3,j));
    FAILTEST(A2(i^5) = A(i&0));
    FAILTEST(A2(i^4,j&5) = A(i^4,j&5));
    FAILTEST(A(i,j,k^2) = B(i,k^2));
    FAILTEST(A(i,j,k,l) = B(i,j,k));
    FAILTEST(A(i,j,k,l) = B(j,k,i));
    FAILTEST(C(i,j) = D(i^2));
    FAILTEST(C(i,j) = D(i^2));
	static_assert(!std::is_assignable<decltype(D(i,j) * D(j,k)), decltype(D(i,k))>::value,"");
320
});
Baum's avatar
Baum committed
321 322 323