00001
00002
00004
00005 #ifndef EVENTPRIMITIVES_EVENTPRIMITIVESHELPERS_H
00006 #define EVENTPRIMITIVES_EVENTPRIMITIVESHELPERS_H
00007
00008 #include "EventPrimitives/EventPrimitives.h"
00009 #include "cmath"
00010 #include <iostream>
00011 #include <vector>
00012
00020 namespace Amg {
00021
00025 inline double error(const Amg::MatrixX& mat, int index) {
00026 return sqrt(mat(index, index));
00027 }
00028
00029 template<int N>
00030 inline double error(const AmgSymMatrix(N)& mat, int index ) {
00031 assert(index<N);
00032 return sqrt(mat(index,index));
00033 }
00034
00035
00036 template<int N>
00037 struct CalculateCompressedSize {
00038 static const int value = N + CalculateCompressedSize<N - 1>::value;
00039 };
00040
00041 template<>
00042 struct CalculateCompressedSize<1> {
00043 static const int value = 1;
00044 };
00045
00046 template<int N>
00047 inline void compress(const AmgSymMatrix(N)& covMatrix, std::vector<float>& vec ) {
00048 vec.reserve(CalculateCompressedSize<N>::value);
00049 for (unsigned int i = 0; i < N ; ++i)
00050 for (unsigned int j = 0; j <= i; ++j)
00051 vec.push_back(covMatrix(i,j));
00052 }
00053 inline void compress(const MatrixX& covMatrix, std::vector<float>& vec) {
00054 int rows = covMatrix.rows();
00055 for (int i = 0; i < rows; ++i) {
00056 for (int j = 0; j <= i; ++j) {
00057 vec.push_back(covMatrix(i, j));
00058 }
00059 }
00060 }
00061
00062 template<int N>
00063 inline void expand(std::vector<float>::const_iterator it,
00064 std::vector<float>::const_iterator, AmgSymMatrix(N)& covMatrix ) {
00065 for (unsigned int i = 0; i < N; ++i) {
00066 for (unsigned int j = 0; j <= i; ++j) {
00067 covMatrix.fillSymmetric(i,j, *it);
00068 ++it;
00069 }
00070 }
00071 }
00072
00073 inline void expand(std::vector<float>::const_iterator it,
00074 std::vector<float>::const_iterator it_end, MatrixX& covMatrix) {
00075 unsigned int dist = std::distance(it, it_end);
00076 unsigned int n;
00077 for (n = 1; dist > n; ++n) {
00078 dist = dist - n;
00079 }
00080 covMatrix = MatrixX(n, n);
00081 for (unsigned int i = 0; i < n; ++i) {
00082 for (unsigned int j = 0; j <= i; ++j) {
00083 covMatrix.fillSymmetric(i, j, *it);
00084 ++it;
00085 }
00086 }
00087 }
00088
00092 template<int N>
00093 double largestDifference(const AmgSymMatrix(N)& m1, const AmgSymMatrix(N)& m2,bool relative = false ) {
00094 if( N < 1 ) return 0;
00095 double max = relative ? 0.5*fabs(m1(0,0)-m2(0,0))/(m1(0,0)+m2(0,0)) : fabs(m1(0,0)-m2(0,0));
00096 for (int i = 0; i < N; ++i) {
00097 for (int j = 0; j < N; ++j) {
00098 double val = fabs(m1(i,j)-m2(i,j));
00099 if( relative ) {
00100 val = 0.5*val/(m1(i,j)+m2(i,j));
00101 }
00102 if( val > max ) max = val;
00103 }
00104 }
00105 return max;
00106 }
00107
00111 template<int N>
00112 int largestDifference(const AmgVector(N)& m1, const AmgVector(N)& m2, bool relative = false ) {
00113 if( N < 1 ) return 0;
00114 double max = relative ? 0.5*fabs(m1(0)-m2(0))/(m1(0)+m2(0)) : fabs(m1(0)-m2(0));
00115 for (int i = 0; i < N; ++i) {
00116 for (int j = 0; j < N; ++j) {
00117 double val = fabs(m1(i)-m2(i));
00118 if( relative ) {
00119 val = 0.5*val/(m1(i)+m2(i));
00120 }
00121 if( val > max ) max = val;
00122 }
00123 }
00124 return max;
00125 }
00126
00130 template<int N>
00131 std::pair<int, int> compare(const AmgSymMatrix(N)& m1, const AmgSymMatrix(N)& m2, double precision = 1e-9, bool relative = false ) {
00132 for (int i = 0; i < N; ++i) {
00133 for (int j = 0; j < N; ++j) {
00134 if( relative ) {
00135 if( 0.5*fabs(m1(i,j)-m2(i,j))/(m1(i,j)+m2(i,j)) > precision ) return std::make_pair(i,j);
00136 } else {
00137 if( fabs(m1(i,j)-m2(i,j)) > precision ) return std::make_pair(i,j);
00138 }
00139 }
00140 }
00141 return std::make_pair(-1,-1);
00142 }
00143
00147 template<int N>
00148 int compare(const AmgVector(N)& m1, const AmgVector(N)& m2, double precision = 1e-9, bool relative = false ) {
00149 for (int i = 0; i < N; ++i) {
00150 if( relative ) {
00151 if( 0.5*fabs(m1(i)-m2(i))/(m1(i)+m2(i)) > precision ) return i;
00152 } else {
00153 if( fabs(m1(i)-m2(i)) > precision ) return i;
00154 }
00155 }
00156 return -1;
00157 }
00158
00159 template<int N>
00160 bool isSymMatrix(const AmgSymMatrix(N)& m ) {
00161 for (int i = 0; i < N; ++i) {
00162 for (int j = 0; j <= i; ++j) {
00163
00164 if( i==j ) {
00165 if( m(i,j) < 0. ) return false;
00166 } else {
00167
00168 if( fabs(m(i,j)-m(j,i)) > 1e-9 ) return false;
00169 }
00170 }
00171 }
00172 return true;
00173 }
00174
00175 }
00176
00177 #endif