Peano
Loading...
Searching...
No Matches
CompressedLinearAlgebra.cpph
Go to the documentation of this file.
2
3
4template<int Size>
5std::vector<unsigned char> toolbox::multiprecision::compress(const tarch::la::Vector<Size,double>& vector, double maxAbsoluteError) {
6 std::vector<unsigned char> result;
7 unsigned char bytesForMantissa = findMostAgressiveCompression( vector.data(), Size, maxAbsoluteError );
8 result.push_back(bytesForMantissa);
9
10 if (bytesForMantissa>0) {
11 for (int i=0; i<Size; i++) {
12 char exponent;
13 long int mantissa;
14 double error;
15 char* pMantissa = reinterpret_cast<char*>( &(mantissa) );
16
17 decompose( vector(i), exponent, mantissa, bytesForMantissa );
18
19 result.push_back(exponent);
20 for (int j=0; j<bytesForMantissa; j++) {
21 result.push_back(pMantissa[j]);
22 }
23 }
24 }
25 return result;
26}
27
28
29template<int Rows, int Cols>
30std::vector<unsigned char> toolbox::multiprecision::compress(const tarch::la::Matrix<Rows,Cols,double>& matrix, double maxAbsoluteError) {
31 std::vector<unsigned char> result;
32 unsigned char bytesForMantissa = findMostAgressiveCompression( matrix.data(), Rows*Cols, maxAbsoluteError );
33 result.push_back(bytesForMantissa);
34 if (bytesForMantissa>0) {
35 for (int row=0; row<Rows; row++)
36 for (int col=0; col<Cols; col++) {
37 char exponent;
38 long int mantissa;
39 double error;
40 char* pMantissa = reinterpret_cast<char*>( &(mantissa) );
41
42 decompose( matrix(row,col), exponent, mantissa, bytesForMantissa );
43
44 result.push_back(exponent);
45 for (int j=0; j<bytesForMantissa; j++) {
46 result.push_back(pMantissa[j]);
47 }
48 }
49 }
50 return result;
51}
52
53
54
55template<int Size>
58
59 int index = 0;
60 unsigned char bytesForMantissa = stream[index]; index++;
61
62 for (int i=0; i<Size; i++) {
63 if (bytesForMantissa>0) {
64 char exponent;
65 long int mantissa;
66 char* pMantissa = reinterpret_cast<char*>( &(mantissa) );
67
68 exponent = stream[index]; index++;
69 for (int j=0; j<bytesForMantissa; j++) {
70 pMantissa[j] = stream[index]; index++;
71 }
72
73 result(i) = compose( exponent, mantissa, bytesForMantissa );
74 }
75 else {
76 result(i) = 0;
77 }
78 }
79
80 assertionEquals(index,stream.size());
81
82 return result;
83}
84
85
86template<int Rows, int Cols>
89
90 int index = 0;
91 unsigned char bytesForMantissa = stream[index]; index++;
92
93 for (int row=0; row<Rows; row++)
94 for (int col=0; col<Cols; col++) {
95 if (bytesForMantissa>0) {
96 char exponent;
97 long int mantissa = 0;
98 char* pMantissa = reinterpret_cast<char*>( &(mantissa) );
99
100 exponent = stream[index]; index++;
101 for (int j=0; j<bytesForMantissa; j++) {
102 pMantissa[j] = stream[index]; index++;
103 }
104
105 result(row,col) = compose( exponent, mantissa, bytesForMantissa );
106 }
107 else {
108 result(row,col) = 0;
109 }
110 }
111
112 assertionEquals(index,stream.size());
113
114 return result;
115}
116
#define assertionEquals(lhs, rhs)
Static (i.e.
Definition Matrix.h:31
Scalar * data()
This routine returns a pointer to the first data element.
Definition Matrix.h:115
void decompose(const double &value, char &exponent, T &mantissa)
Takes a double and returns the exponent and the mantissa.
tarch::la::Matrix< Rows, Cols, double > uncompressMatrix(const std::vector< unsigned char > &stream)
tarch::la::Vector< Size, double > uncompressVector(const std::vector< unsigned char > &stream)
Can't call it decompress only, as the result data type is not part of the signature,...
std::vector< unsigned char > compress(const tarch::la::Vector< Size, double > &vector, double maxAbsoluteError)
Take a vector and compress it into a byte stream.
int findMostAgressiveCompression(double value, double maxAbsoluteError)
Analyses the handed data and determines the most aggressive compression.
Simple vector class.
Definition Vector.h:134
Scalar * data()
This routine returns a pointer to the first data element.
Definition Vector.h:254