ToPS
|
00001 /* 00002 * util.hpp 00003 * 00004 * Copyright 2011 Andre Yoshiaki Kashiwabara <akashiwabara@usp.br> 00005 * Ígor Bonádio <ibonadio@ime.usp.br> 00006 * Vitor Onuchic <vitoronuchic@gmail.com> 00007 * Alan Mitchell Durham <aland@usp.br> 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 3 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00022 * MA 02110-1301, USA. 00023 */ 00024 00025 #ifndef UTIL_H 00026 #define UTIL_H 00027 #define NDEBUG 00028 00029 #include "crossplatform.hpp" 00030 00031 #define _USE_MATH_DEFINES 00032 00033 #include <iostream> 00034 #include <fstream> 00035 #include <boost/regex.hpp> 00036 #include <vector> 00037 #include <string> 00038 #include <cmath> 00039 #include "Sequence.hpp" 00040 #include "SequenceEntry.hpp" 00041 #include <boost/numeric/ublas/matrix.hpp> 00042 #include <boost/numeric/ublas/matrix_sparse.hpp> 00043 #include <boost/math/special_functions/erf.hpp> 00044 00045 #ifdef WIN32 00046 #define _min _cpp_min 00047 #define _max _ccp_max 00048 #else 00049 #define _min std::min 00050 #define _max std::max 00051 #endif 00052 00053 #define _erf boost::math::erf 00054 00055 namespace tops { 00056 00057 typedef std::vector <double> DoubleVector; 00058 typedef std::vector <int> IntVector; 00059 typedef boost::numeric::ublas::matrix<double> Matrix; 00060 typedef boost::numeric::ublas::matrix<float> fMatrix; 00061 typedef boost::numeric::ublas::mapped_matrix<double> MMatrix; 00062 typedef boost::numeric::ublas::compressed_matrix<float,boost::numeric::ublas::row_major> CMatrix; 00063 typedef boost::numeric::ublas::matrix<int> IntMatrix; 00064 typedef std::vector <std::string> StringVector; 00065 typedef std::map <std::string, std::string> StringMap; 00066 00068 void trim_spaces (std::string & s); 00069 00071 void split_regex (const std::string & s, std::vector <std::string> & result, const boost::regex & re); 00072 00074 DLLEXPORT double log_sum( double log_a, double log_b); 00075 00076 double lookup (double x); 00077 00078 00080 double safe_division(double a, double b); 00081 00083 bool close(double a, double b, double tolerance) ; 00084 00086 int mod(int D, int d); 00087 00089 void readSequencesFromFile(SequenceEntryList & s, AlphabetPtr alphabet, std::string file_name) ; 00090 00092 void readSequencesFromFile(SequenceList & s, AlphabetPtr alphabet, std::string file_name) ; 00093 00094 00095 /* Sheather and Jones bandwidth */ 00096 double sj_bandwidth(const DoubleVector &data); 00097 double kernel_density_estimation(double x, double bw, const DoubleVector &data); 00098 double kernel_density_estimation_gaussian(double x, double bw, const DoubleVector &data); 00099 00100 /* Epanechnikov kernel */ 00101 double epanechnikov(double x, double h); 00102 /* normal kernel */ 00103 double kernel_normal(double x, double h); 00104 // code from R-1.7.0/src/appl/bandwidths.c 00105 #define abs9(a) (a > 0 ? a:-a) 00106 void band_den_bin(int n, int nb, double *d, const DoubleVector &x, DoubleVector &cnt); 00107 void band_phi6_bin(int n, int nb, double d, DoubleVector &x, double h, double *u); 00108 void band_phi4_bin(int n, int nb, double d, DoubleVector x, double h, double *u); 00109 00110 double mean(const DoubleVector &data); 00111 00112 double var(const DoubleVector &data); 00113 /* quantile */ 00114 double quantile (DoubleVector data, double q); 00115 00116 /* interquantile */ 00117 double iqr (const DoubleVector &data); 00118 00120 void readMapFromFile(std::map<std::string, double> & s, std::string file_name); 00121 00122 00123 } 00124 #endif