00001 #ifndef CORRCOEF_H 00002 #define CORRCOEF_H 00003 00004 #include <cassert> 00005 #include <functional> 00006 #include <cmath> 00007 00008 namespace mimas { 00027 template <class Arg1, class Arg2 = Arg1 > 00028 struct corrcoef: public std::binary_function< Arg1, Arg2, double > 00029 { 00030 double operator()( const Arg1 &a, 00031 const Arg2 &b ) { 00032 double sa = 0.0, sb = 0.0, saa = 0.0, sbb = 0.0, sab = 0.0; 00033 assert( a.size() == b.size() ); 00034 typename Arg1::const_iterator i; 00035 typename Arg2::const_iterator j; 00036 for ( i = a.begin(), j = b.begin(); i != a.end(); i++, j++ ) { 00037 sa += *i; 00038 sb += *j; 00039 saa += *i * *i; 00040 sbb += *j * *j; 00041 sab += *i * *j; 00042 }; 00043 int n = a.size(); 00044 return ( n * sab - sa*sb ) / 00045 sqrt( ( n * saa - sa * sa ) * 00046 ( n * sbb - sb * sb ) ); 00047 } 00048 }; 00049 00050 } 00051 #endif