NLToolbox
src/include/utils.h
00001 /*
00002  * utils.h
00003  *
00004  *  Created on:
00005  *      Author: romain
00006  */
00007 
00008 #ifndef UTILS_H_
00009 #define UTILS_H_
00010 
00011 #include "iostream"
00012 #include "string"
00013 #include "fstream"
00014 #include "iomanip"
00015 #include "stdio.h"
00016 #include "vector"
00017 #include "math.h"
00018 #include "stdlib.h"
00019 #include "assert.h"
00020 #include <gmpxx.h>
00021 #include <armadillo>
00022 
00023 
00024 namespace nltool {
00025 #define PI 3.14159265
00026 
00027 #ifdef GMP_RAT
00028 typedef mpq_class Number;
00029 #else
00030 typedef double Number;
00031 #endif
00032 
00033 const Number EPSILON=1e-10;
00034 
00035 typedef arma::Mat<Number> Matrix;
00036 typedef arma::Col<Number> Vector;
00037 typedef std::vector<Vector> Vertices;
00038 typedef std::vector<Vector> Template;
00039 typedef std::vector<double> doubleVector;
00040 typedef std::vector<Number> stdVector;
00041 typedef std::vector<std::vector<Number> > stdMatrix;
00042 
00043 extern bool useBinomTable;
00044 extern int** binomialCoefficientTable;
00045 
00046 
00047 
00048 //class Simplex:public Set{
00049 //public:
00050 //      Simplex(int dim, stdMatrix& vert):dimension(dim),verticesData(vert){}
00051 //      Simplex():dimension(0),verticesData(stdMatrix(0)){};
00052 //      int dim(){return dimension;}
00053 //      stdMatrix vertices(){
00054 //               return getVertices();
00055 //      }
00056 //      stdMatrix getVertices(){return verticesData;}
00057 //      unsigned int nb_vertices(){return verticesData.size();}
00058 //
00059 //private:
00060 //      stdMatrix verticesData;
00061 //      int dimension;
00062 //};
00063 
00064 // ###############################
00065 // Matrix and vector operations
00066 // ###############################
00067 
00068 inline arma::Mat<Number> template2Matrix(const Template& temp) {
00069         arma::Mat<Number> result(temp.size(), (temp[0].n_elem));
00070         for (unsigned int i = 0; i < temp.size(); ++i) {
00071                 for (unsigned int j = 0; j < temp[0].n_elem; ++j) {
00072                         result(i, j) = temp[i][j];
00073                 }
00074         }
00075         return result;
00076 }
00077 
00078 inline Template matrixToTemplate(const Matrix& mat) {
00079         Template result(mat.n_rows, Vector(mat.n_cols));
00080         for (unsigned int i = 0; i < mat.n_rows; ++i) {
00081                 for (unsigned int j = 0; j < mat.n_cols; ++j) {
00082                         result[i][j] = mat(i,j);
00083                 }
00084         }
00085         return result;
00086 }
00087 
00088 inline Number infinityNorm(stdVector v) {
00089         Number res = fabs(v[0]);
00090         for(unsigned i=1; i<v.size();i++)
00091                 if (res<fabs(v[i]))
00092                         res = fabs(v[i]);
00093         return res;
00094 }
00095 
00096 stdMatrix matrixTranspose(const stdMatrix m1);
00097 
00098 inline stdVector operator*(stdMatrix A, const stdVector v) {
00099         assert((A.size()>0) && (A[0].size()==v.size()));
00100         stdVector result(A.size(), 0);
00101         for (unsigned i = 0; i < A.size(); i++)
00102                 for (unsigned j = 0; j < v.size(); j++)
00103                         result[i] += A[i][j] * v[j];
00104         return result;
00105 }
00106 
00107 inline stdVector operator*(Number n, const stdVector v) {
00108         stdVector result(v.size());
00109         for (unsigned i = 0; i < v.size(); i++)
00110                 result[i] = n * v[i];
00111         return result;
00112 }
00113 
00114 inline stdMatrix operator*(Number n, const stdMatrix m) {
00115         stdMatrix result(m.size());
00116         for (unsigned i = 0; i < m.size(); i++)
00117                 result[i] = n * m[i];
00118         return result;
00119 }
00120 
00121 inline stdVector operator+(stdVector v1, const stdVector v2) {
00122         assert(v1.size()==v2.size());
00123         stdVector result(v1.size());
00124         for (unsigned i = 0; i < v1.size(); i++)
00125                 result[i] = v1[i] + v2[i];
00126         return result;
00127 }
00128 
00129 inline stdVector operator-(stdVector v1, const stdVector v2) {
00130         assert(v1.size()==v2.size());
00131         stdVector result(v1.size());
00132         for (unsigned i = 0; i < v1.size(); i++)
00133                 result[i] = v1[i] - v2[i];
00134         return result;
00135 }
00136 
00137 inline stdMatrix operator+(stdMatrix m1, const stdMatrix m2) {
00138         assert(m1.size()==m2.size());
00139         stdMatrix result(m1.size());
00140         for (unsigned i = 0; i < m1.size(); i++) {
00141                 assert(m1[i].size()==m2[i].size());
00142                 result[i] = m1[i] + m2[i];
00143         }
00144         return result;
00145 }
00146 
00147 inline bool operator<=(stdVector v1, const stdVector v2) {
00148         assert(v1.size()==v2.size());
00149         for (unsigned i = 0; i < v1.size(); i++)
00150                 if (v1[i] > v2[i])
00151                         return false;
00152         return true;
00153 }
00154 
00155 inline Number dot(stdVector v1, stdVector v2) {
00156         Number result = 0.0;
00157         for (unsigned int i = 0; i < v1.size(); ++i) {
00158                 result += v1[i] * v2[i];
00159         }
00160         return result;
00161 }
00162 
00163 inline bool notNul(stdVector v) {
00164         for (unsigned int i = 0; i < v.size(); i++)
00165                 if (v[i] != 0)
00166                         return true;
00167         return false;
00168 }
00169 
00170 inline int fact(const int n) {
00171         int result = 1;
00172         for (int i = 2; i <= n; i++)
00173                 result *= i;
00174         return result;
00175 }
00176 
00177 inline Number max(const stdVector& v){
00178         assert(v.size()>0);
00179         Number maximum=v[0];
00180         for(unsigned i=1; i<v.size(); i++)
00181                 if(maximum<v[i])
00182                         maximum = v[i];
00183         return maximum;
00184 }
00185 
00186 inline Number min(const stdVector& v){
00187         assert(v.size()>0);
00188         Number maximum=v[0];
00189         for(unsigned i=1; i<v.size(); i++)
00190                 if(maximum>v[i])
00191                         maximum = v[i];
00192         return maximum;
00193 }
00194 
00195 inline bool closeToZero(Number nb){
00196         return (fabs(nb)<EPSILON);
00197 }
00198 
00199 
00200 double binomialCoef(const int n, const int k);
00201 
00202 void createBinomialCoefficientTable(const int max);
00203 
00204 inline std::ostream& operator<<(std::ostream& out, stdVector v) {
00205         for (unsigned int i = 0; i < v.size(); i++)
00206                 out << v[i] << " ";
00207         out << std::endl;
00208         return out;
00209 }
00210 
00211 inline std::ostream& operator<<(std::ostream& out, stdMatrix t) {
00212         for (unsigned int i = 0; i < t.size(); i++)
00213                 out << t[i];
00214         return out;
00215 }
00216 
00217 inline std::ostream& operator<<(std::ostream& out, Template t) {
00218         for (unsigned int i = 0; i < t.size(); i++)
00219                 out << t[i].t()<<std::endl;
00220         return out;
00221 }
00222 
00223 
00224 stdVector setUnitVector(const Vector v);
00225 
00226 Matrix matrixExponential(Matrix A, Number dt);
00227 
00228 
00229 
00230 // ###############################
00231 // LP solver
00232 // ###############################
00233 
00234 Number simplex(Vector obj, Template M, Vector N, Vector& solution, bool max = true);
00235 
00236 Number simplex(Vector obj, Template M, Vector N, bool max = true);
00237 
00238 Number simplex(stdVector obj, stdMatrix Aineq, stdVector Bineq, stdMatrix Aeq,
00239                 stdVector Beq, stdVector bl, stdVector bu, int &err);
00240 
00241 Number simplify(const Number& num, const Number& precision);
00242 
00243 Vector simplify(const Vector& vec, const Number& precision);
00244 
00245 //Number simplex(stdVector obj, Template M, stdVector N, stdVector bl,
00246 //              stdVector bu, int &err);
00247 //
00248 //Number simplex(stdVector obj, stdMatrix Aineq, stdVector Bineq, stdMatrix Aeq,
00249 //              stdVector Beq, stdVector bl, stdVector bu, int &err);
00250 
00251 //bool adjacencyTest(doubleMatrix M, doubleVector N, int face1, int face2);
00252 
00253 inline void wait(){
00254         getchar();
00255 }
00256 
00257 // ###############################
00258 // Multi index operation
00259 // ###############################
00260 
00261 bool increaseMultiIndice(std::vector<int>& m, const std::vector<int> d);
00262 
00263 
00264 class Chrono{
00265 public:
00266         Chrono();
00267         ~Chrono();
00268          void start();
00269          Number stop();
00270 private :
00271          clock_t startClock;
00272 };
00273 
00274 }  // namespace nltool
00275 
00276 //using namespace std;
00277 
00278 #endif /* UTILS_H_ */
 All Classes Namespaces Functions