sparseTensor_add_sub_cpy.cxx 2.97 KB
Newer Older
1
// Xerus - A General Purpose Tensor Library
2
// Copyright (C) 2014-2017 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

26
static misc::UnitTest sparse_sum2("SparseTensor", "sum_matrix_2x2", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
27 28 29
    Tensor res({2,2}, Tensor::Representation::Sparse);
    Tensor B({2,2}, Tensor::Representation::Sparse);
    Tensor C({2,2}, Tensor::Representation::Sparse);
30 31 32 33 34 35 36 37 38 39 40 41 42

    Index i, J;
    
    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;
    
43 44 45
	B.use_sparse_representation();
	C.use_sparse_representation();
	
46
    res(i,J) = B(i,J) + C(i,J);
Sebastian Wolf's avatar
Sebastian Wolf committed
47
    TEST(approx_entrywise_equal(res, {6,8,10,12}));
48
    res(i,J) = B(i,J) + C(J,i);
Sebastian Wolf's avatar
Sebastian Wolf committed
49
    TEST(approx_entrywise_equal(res, {6,9,9,12}));
50
});
51
 
52
static misc::UnitTest sparse_sum_eq("SparseTensor", "sum_lhs_equals_rhs", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
53 54
    Tensor B({2,2}, Tensor::Representation::Sparse);
    Tensor C({2,2}, Tensor::Representation::Sparse);
55 56 57 58 59 60 61 62 63 64 65 66 67

    Index i, J;
    
    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;
    
68 69
	B.use_sparse_representation();
	C.use_sparse_representation();
70
    B(i,J) = B(i,J) + C(i,J);
Sebastian Wolf's avatar
Sebastian Wolf committed
71
	TEST(approx_entrywise_equal(B, {6,8,10,12}));
72
    B(i,J) = B(i,J) + B(J,i);
Sebastian Wolf's avatar
Sebastian Wolf committed
73
    TEST(approx_entrywise_equal(B, {12,18,18,24}));
74
});
75 76


77
static misc::UnitTest sparse_dyadicsum("SparseTensor", "sum_dyadic", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
78 79 80
    Tensor res({2,2}, Tensor::Representation::Sparse);
    Tensor B({2}, Tensor::Representation::Sparse);
    Tensor C({2}, Tensor::Representation::Sparse);
81 82 83 84

    Index i, J, K;
    
    FAILTEST(res(i,J) = B(i) + C(J));
85
});
86

87
static misc::UnitTest sparse_sum_three("SparseTensor", "sum_threefold", [](){
Sebastian Wolf's avatar
Sebastian Wolf committed
88 89 90 91
    Tensor res({2}, Tensor::Representation::Sparse);
    Tensor B({2}, Tensor::Representation::Sparse);
    Tensor C({2}, Tensor::Representation::Sparse);
    Tensor D({2}, Tensor::Representation::Sparse);
92 93 94

    Index i, J, K;
    
95 96
    B[0]=1;
    B[1]=2;
97
    
98 99
    C[0]=5;
    C[1]=9;
100
    
101 102
    D[0]=7;
    D[1]=13;
103
    
104 105 106 107
	B.use_sparse_representation();
	C.use_sparse_representation();
	D.use_sparse_representation();
	
108
    res(i) = B(i) + C(i) + D(i);
Sebastian Wolf's avatar
Sebastian Wolf committed
109
    TEST(approx_entrywise_equal(res, {13,24}));
110
});