ttRounding.cxx 2.9 KB
Newer Older
Baum's avatar
Baum committed
1
// Xerus - A General Purpose Tensor Library
Fuchsi*'s avatar
Fuchsi* committed
2
// Copyright (C) 2014-2019 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 tt_round("TT", "TTTensor_Rounding", [](){
Baum's avatar
Baum committed
28
29
    Index i,j,k;
    
30
    Tensor A1 = Tensor::random({2});
Sebastian Wolf's avatar
Sebastian Wolf committed
31
    Tensor B1;
Baum's avatar
Baum committed
32
33
    TTTensor TTA1(A1, 1e-14);
    B1(i) = TTA1(i);
34
    TEST(approx_equal(B1,A1, 1e-14));
Baum's avatar
Baum committed
35
36
37
    
    TTA1.round(1e-14);
    B1(i) = TTA1(i);
38
    TEST(approx_equal(B1,A1, 1e-14));
Baum's avatar
Baum committed
39
40
41
    
    TTA1.round(1);
    B1(i) = TTA1(i);
42
    TEST(approx_equal(B1,A1, 1e-14));
Baum's avatar
Baum committed
43
44
    
    
45
    Tensor A2 = Tensor::random({2,2});
Sebastian Wolf's avatar
Sebastian Wolf committed
46
    Tensor B2;
Baum's avatar
Baum committed
47
48
    TTTensor TTA2(A2, 1e-14);
    B2(j,i) = TTA2(j,i);
49
    TEST(approx_equal(B2,A2, 1e-14));
Baum's avatar
Baum committed
50
51
52
    
    TTA2.round(1e-14);
    B2(j,i) = TTA2(j,i);
53
    TEST(approx_equal(B2,A2, 1e-14));
Baum's avatar
Baum committed
54
55
56
    
    TTA2.round(2);
    B2(j,i) = TTA2(j,i);
57
    TEST(approx_equal(B2,A2, 1e-14));
Baum's avatar
Baum committed
58
59
    
    
60
    Tensor A3 = Tensor::random({2,7});
Sebastian Wolf's avatar
Sebastian Wolf committed
61
    Tensor B3;
Baum's avatar
Baum committed
62
63
    TTTensor TTA3(A3, 1e-14);
    B3(j,i) = TTA3(j,i);
64
    TEST(approx_equal(B3,A3, 1e-14));
Baum's avatar
Baum committed
65
66
67
    
    TTA3.round(1e-14);
    B3(j,i) = TTA3(j,i);
68
    TEST(approx_equal(B3,A3, 1e-14));
Baum's avatar
Baum committed
69
70
71
    
    TTA3.round(2);
    B3(j,i) = TTA3(j,i);
72
    TEST(approx_equal(B3,A3, 1e-14));
Baum's avatar
Baum committed
73
74
    
    
75
    Tensor A4 = Tensor::random({2,2,2,2,2,2,2,2});
76
77
78
79
80
	for (double eps = 1e-3; eps < 1.0; eps*=1.2) {
		TTTensor B4(A4);
		B4.round(eps);
		MTEST(approx_equal(A4, B4, eps), eps << " vs " << frob_norm(A4-Tensor(B4))/frob_norm(A4));
	}
Baum's avatar
Baum committed
81
82
    
    
83
    Tensor A5 = Tensor::random({5,6,3,1,4,2,8,1});
84
85
86
87
88
	for (double eps = 1e-3; eps < 1.0; eps*=1.2) {
		TTTensor B5(A5);
		B5.round(eps);
		MTEST(approx_equal(A5, B5, eps), eps << " vs " << frob_norm(A5-Tensor(B5))/frob_norm(A5));
	}
89
});
Baum's avatar
Baum committed
90

91

92
static misc::UnitTest tt_noround("TT", "no_rounding", [](){
93
    Index i,j,k;
94
	TTTensor a = TTTensor::random({2,2,2,2,2,2,2}, {2,2,2,2,2,2});
95
96
	TTTensor b(a);
	a.round(2);
Philipp  Trunschke's avatar
Philipp Trunschke committed
97
	TEST(approx_equal(Tensor(a),Tensor(b), 1e-14));
98
	TTTensor c = TTTensor::random({2,2,2,2,2,2,2}, {2,2,2,2,2,2});
99
	a(i&0) = a(i&0) + 0.0*c(i&0);
100
	LOG(unit_test, a.ranks());
101
	a.round(2);
102
	TEST(approx_equal(Tensor(a), Tensor(b), 1e-14));
103
});