sparseTensor_assignment_cpy.cxx 11.9 KB
Newer Older
1
// Xerus - A General Purpose Tensor Library
Fuchsi*'s avatar
Fuchsi* committed
2
// Copyright (C) 2014-2019 Benjamin Huber and Sebastian Wolf. 
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
using namespace xerus;
25

Fuchsi*'s avatar
Fuchsi* committed
26
static misc::UnitTest sparse_assign_triv("SparseTensor", "Assignment_Trivia", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
27 28 29 30
    Tensor A({2,2,3,1,2}, Tensor::Representation::Sparse);
    Tensor res({2,2,3,1,2}, Tensor::Representation::Sparse);
    Tensor res2({2,3,2,1,2}, Tensor::Representation::Sparse);
    Tensor res3({2,3,1,2,2}, Tensor::Representation::Sparse);
31 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

    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;
    
Fuchsi*'s avatar
Fuchsi* committed
59 60
	A.use_sparse_representation();
	
61
    res(i,j,k,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
62
    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}));
63
    res(i&0) = A(i&0);
Sebastian Wolf's avatar
Sebastian Wolf committed
64
    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}));
65
    res(i&0) = A(i^5);
Sebastian Wolf's avatar
Sebastian Wolf committed
66
    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}));
67
    res(i^5) = A(i&0);
Sebastian Wolf's avatar
Sebastian Wolf committed
68
    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}));
69
    res(i^3, j&3) = A(i&2,j^2);
Sebastian Wolf's avatar
Sebastian Wolf committed
70
    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}));
71 72
    
    res(j,i,k,l,m) = A(j,i,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
73
    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}));
74
    res(i,j,k,l,m) = A(j,i,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
75
    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}));
76
    res(j,i,k,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
77
    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}));
78 79
    
    res2(i,k,j,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
80
    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}));
81
    res2(i,j,k,l,m) = A(i,k,j,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
82
    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}));
83 84
    
    res(i,j,k,l,m) = A(i,j,l,k,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
85
    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}));
86
    res(i,j,l,k,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
87
    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}));
Fuchsi*'s avatar
Fuchsi* committed
88
});
89

90

Fuchsi*'s avatar
Fuchsi* committed
91
static misc::UnitTest sparse_assign_const("SparseTensor", "Assignment_Const", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
92 93
    Tensor A({2,2,3,1,2}, Tensor::Representation::Sparse);
    Tensor res({2,2,3,1,2}, Tensor::Representation::Sparse);
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;
Fuchsi*'s avatar
Fuchsi* committed
121
	A.use_sparse_representation();
122 123
    
    res(i,j,k,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
124
    const Tensor resC1(res.sparse_copy());
Sebastian Wolf's avatar
Sebastian Wolf committed
125
    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}));
126 127 128
    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));
129 130
    TEST(misc::approx_equal(resC1[{1,0,0,0,1}], 14.0));
    TEST(misc::approx_equal(resC1[{1,0,1,0,1}], 16.0));
131 132
    
    res(j,i,k,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
133
    const Tensor resC2(res.sparse_copy());
Sebastian Wolf's avatar
Sebastian Wolf committed
134
    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}));
135 136 137
    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));
138 139
    TEST(misc::approx_equal(resC2[{1,0,0,0,1}], 8.0));
    TEST(misc::approx_equal(resC2[{1,0,1,0,1}], 10.0));
Fuchsi*'s avatar
Fuchsi* committed
140
});
141

Fuchsi*'s avatar
Fuchsi* committed
142
static misc::UnitTest sparse_assign_overwriting_dim("SparseTensor", "Assignment_Overwriting_Dimensions", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
143 144 145 146 147 148 149 150 151 152 153 154 155
    Tensor A({2,2,3,1,2}, Tensor::Representation::Sparse);
    Tensor res1({2,2,3,1,2}, Tensor::Representation::Sparse);
    Tensor res2({1,3,5,1,7}, Tensor::Representation::Sparse);
    Tensor res3({2,3,2,2,3}, Tensor::Representation::Sparse);
    Tensor res4({13,9,2,5,3}, Tensor::Representation::Sparse);
    Tensor res5({1,1,1,1,1}, Tensor::Representation::Sparse);
    Tensor res6( Tensor::Representation::Sparse );
    Tensor res7({1,2,3,4,5}, Tensor::Representation::Sparse);
    Tensor res8({5,4,3,2,1}, Tensor::Representation::Sparse);
    Tensor res9({3,2,3,2,3}, Tensor::Representation::Sparse);
    Tensor res10({4,5,3,1,3}, Tensor::Representation::Sparse);
    Tensor res11( Tensor::Representation::Sparse );
    Tensor res12({1,5,3,1,3}, Tensor::Representation::Sparse);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

    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;
Fuchsi*'s avatar
Fuchsi* committed
183
	A.use_sparse_representation();
184 185
    
    res1(i,j,k,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
186
    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}));
187
    res2(i&0) = A(i&0);
Sebastian Wolf's avatar
Sebastian Wolf committed
188
    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}));
189
    res3(i&0) = A(i^5);
Sebastian Wolf's avatar
Sebastian Wolf committed
190
    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}));
191
    res4(i^5) = A(i&0);
Sebastian Wolf's avatar
Sebastian Wolf committed
192
    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}));
193
    res5(i^3, j&3) = A(i&2,j^2);
Sebastian Wolf's avatar
Sebastian Wolf committed
194
    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}));
195 196
    
    res6(j,i,k,l,m) = A(j,i,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
197
    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}));
198
    res7(i,j,k,l,m) = A(j,i,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
199
    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}));
200
    res8(j,i,k,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
201
    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}));
202 203
    
    res9(i,k,j,l,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
204
    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}));
205
    res10(i,j,k,l,m) = A(i,k,j,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
206
    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}));
207 208
    
    res11(i,j,k,l,m) = A(i,j,l,k,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
209
    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}));
210
    res12(i,j,l,k,m) = A(i,j,k,l,m);
Sebastian Wolf's avatar
Sebastian Wolf committed
211
    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}));
Fuchsi*'s avatar
Fuchsi* committed
212
});
213

Fuchsi*'s avatar
Fuchsi* committed
214
static misc::UnitTest sparse_assign_eq("SparseTensor", "Assignment_LHS_Equals_RHS", [](){ 
Sebastian Wolf's avatar
Sebastian Wolf committed
215 216
    Tensor B({2,2}, Tensor::Representation::Sparse);
    Tensor C({2,2}, Tensor::Representation::Sparse);
217

218
    Index i, j, k;
219 220 221 222 223 224 225 226 227 228 229
    
    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;
    
230
    B(i,j) = B(i,j);
Sebastian Wolf's avatar
Sebastian Wolf committed
231
    TEST(approx_entrywise_equal(B, {1,2,3,4}));
232
    B(i,j) = B(j,i);
Sebastian Wolf's avatar
Sebastian Wolf committed
233
    TEST(approx_entrywise_equal(B, {1,3,2,4}));
Fuchsi*'s avatar
Fuchsi* committed
234
});
235

Fuchsi*'s avatar
Fuchsi* committed
236
static misc::UnitTest sparse_assign_fixed("SparseTensor", "Assignment_Fixed_Indices", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
237 238 239 240
    Tensor A({2,2,3,1,2}, Tensor::Representation::Sparse);
    Tensor res1({2,3,1,2}, Tensor::Representation::Sparse);
    Tensor res2( Tensor::Representation::Sparse );
    Tensor res3( Tensor::Representation::Sparse );
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    
    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;
Fuchsi*'s avatar
Fuchsi* committed
268
	A.use_sparse_representation();
269 270
    
    res1(i,j,k,l) = A(0,i,j,k,l);
Sebastian Wolf's avatar
Sebastian Wolf committed
271
    TEST(approx_entrywise_equal(res1, {1,2,3,4,5,6,7,8,9,10,11,12}));
272
    res1(i,j,k,l) = A(1,i,j,k,l);
Sebastian Wolf's avatar
Sebastian Wolf committed
273
    TEST(approx_entrywise_equal(res1, {13,14,15,16,17,18,19,20,21,22,23,24}));
274 275
    
    res1(i,j,k,l) = A(i,0,j,k,l);
Sebastian Wolf's avatar
Sebastian Wolf committed
276
    TEST(approx_entrywise_equal(res1, {1,2,3,4,5,6,13,14,15,16,17,18}));
277
    res1(i,j,k,l) = A(i,1,j,k,l);
Sebastian Wolf's avatar
Sebastian Wolf committed
278
    TEST(approx_entrywise_equal(res1, {7,8,9,10,11,12,19,20,21,22,23,24}));
279 280
    
    res1(i,j,k,l) = A(i,j,k,l,0);
Sebastian Wolf's avatar
Sebastian Wolf committed
281
    TEST(approx_entrywise_equal(res1, {1,3,5,7,9,11,13,15,17,19,21,23}));
282
    res1(i,j,k,l) = A(i,j,k,l,1);
Sebastian Wolf's avatar
Sebastian Wolf committed
283
    TEST(approx_entrywise_equal(res1, {2,4,6,8,10,12,14,16,18,20,22,24}));
284 285
    
    res2(i,j,k) = A(i,j,1,k,1);
Sebastian Wolf's avatar
Sebastian Wolf committed
286
    TEST(approx_entrywise_equal(res2, {4,10,16,22}));
287
    res2(i,j,k) = A(i,j,2,k,1);
Sebastian Wolf's avatar
Sebastian Wolf committed
288
    TEST(approx_entrywise_equal(res2, {6,12,18,24}));
289
    res2(i,j,k) = A(j,i,2,k,1);
Sebastian Wolf's avatar
Sebastian Wolf committed
290
    TEST(approx_entrywise_equal(res2, {6,18,12,24}));
291 292
    
    res2(i,k,j) = A(j,i,2,k,1);
Sebastian Wolf's avatar
Sebastian Wolf committed
293
    TEST(approx_entrywise_equal(res2, {6,18,12,24}));
294 295
    
    res3(i,j) = A(j,i,2,0,1);
Sebastian Wolf's avatar
Sebastian Wolf committed
296
    TEST(approx_entrywise_equal(res3, {6,18,12,24}));
297
    res3(i,j) = A(1,i,2,j,1);
Sebastian Wolf's avatar
Sebastian Wolf committed
298
    TEST(approx_entrywise_equal(res3, {18,24}));
Fuchsi*'s avatar
Fuchsi* committed
299
});
300

Fuchsi*'s avatar
Fuchsi* committed
301
static misc::UnitTest sparse_assign_neg("SparseTensor", "Assignment_Negatives", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
302 303 304 305 306 307
    Tensor A({2,2,2,2}, Tensor::Representation::Sparse);
    Tensor A2({2,2,2,2}, Tensor::Representation::Sparse);
    Tensor B({2,2,2}, Tensor::Representation::Sparse);
    Tensor C( Tensor::Representation::Sparse );
    Tensor D({2,2}, Tensor::Representation::Sparse);
    Tensor E({}, Tensor::Representation::Sparse);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    
    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,"");
Fuchsi*'s avatar
Fuchsi* committed
329
});
330 331


332