41 inline void print_date(){
43 struct tm date = *localtime(&now);
44 std::cout << date.tm_hour <<
":" << date.tm_min <<
" " << date.tm_mday <<
"/" << date.tm_mon <<
"/" << date.tm_year + 1900 << std::endl;
48 const double nXbin=64.;
54 void fill_linear ( std::vector<T> &v,
size_t n, T min, T max ){
56 for (
size_t i = 0; i < n; i++ )
57 v[i] = min + (max - min) * T (i)/T (n-1);
66 for (
size_t i = 0; i < n; i++ )
67 v[i] = exp ( log (min) + ( log (max) - log (min) ) * T (i)/T (n-1) );
76 long locate (
const std::vector<T> &v,
const T x)
88 bool as = (v[n-1] >= v[0]);
92 if ((x >= v[jm]) == as)
105 template <
class T,
class F>
106 long locate (
const std::vector<T> &v,F x,std::function<
bool (F ,
const T &)> less_than)
110 if (less_than(x,v[0]))
112 if (!less_than(x,v[n-1]))
122 if ( less_than(x,v[jm]) )
132 void locate (T *v,
unsigned long n, T x,
unsigned long *index)
145 bool as = (v[n-1] >= v[0]);
149 if ((x >= v[jm]) == as)
168 ,
const std::vector<size_t> &sorted_index
176 if(n != sorted_index.size() ){
177 std::cerr <<
"sorted_index is the wrong size." << std::endl;
178 throw std::invalid_argument(
"wrong size");
181 long imin=0,imax=v.size()-1;
182 double fmax = v[sorted_index[imax]];
183 double fmin = v[sorted_index[imin]];
185 std::swap(imin,imax);
186 std::swap(fmin,fmax);
189 if(value <= fmin)
return sorted_index[imin];
190 if(value >= fmax)
return sorted_index[imax];
192 size_t icurrent = (imax+imin)/2;
193 double fcurrent = v[sorted_index[icurrent]];
195 while ( abs(imax-imin) > 1 ) {
196 if(fcurrent > value){
203 icurrent = (imax+imin)/2;
204 fcurrent = v[sorted_index[icurrent]];
208 return sorted_index[imin];
213 size_t closest (
const std::vector<T> &v,
const T x)
217 if(index == -1)
return 0;
218 if(index == v.size()-1)
return index;
220 return (x - v[index]) < (v[index+1] - x) ? index : index+1;
224 PosType
InterpolateYvec(std:: vector<PosType> &x, std:: vector<PosType> &y,PosType xi);
226 PosType arctanh(PosType x);
228 PosType fmini(PosType a,PosType b);
230 PosType fmaxi(PosType a,PosType b);
233 template <
typename T>
235 size_t size = vec.size();
240 sort(vec.begin(), vec.end());
242 return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
246 template <
typename T>
247 void range(std::vector<T> vec,T &max,T &min){
261 template <
typename T>
262 void polintT(T xa[], T ya[],
int n, T x, T *y, T *dy)
265 T den,dif,dift,ho,hp,w;
274 if ( (dift=fabs(x-xa[i])) < dif) {
283 for (i=1;i<=n-m;i++) {
287 if ( (den=ho-hp) == 0.0)
throw std::runtime_error(
"Error in routine polint");
292 *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--]));
299 template <
typename FunctorType,
typename T =
double>
300 T
trapz(FunctorType &func, T a, T b,
int n, T *s2)
306 return (*s2=0.5*(b-a)*( func(a) + func(b) ));
309 for (it=1,j=1;j<n-1;j++) it <<= 1;
313 for (sum=0.0,j=1;j<=it;j++,x+=del) sum += func(x);
314 *s2=0.5*(*s2+(b-a)*sum/tnm);
320 template <
typename FunctorType,
typename T =
double>
328 const int JMAX = 34,K=6;
329 const int JMAXP = JMAX+1;
334 T s2[JMAXP],h2[JMAXP+1];
338 for (
int j=1;j<=JMAX;j++) {
343 if(fabs(dss) <= tols*fabs(ss))
return ss;
347 std::cout <<
"s2= ";
for(
int j=1;j<=JMAX;j++) std::cout << s2[j] <<
" ";
348 std::cout << std::endl <<
"Too many steps in routine nintegrate<>\n";
349 throw std::invalid_argument(
"Too many steps in routine nintegrate<>");
429 template <
typename FunctorType,
typename T =
double>
437 const int JMAX = 34,K=6;
438 const int JMAXP = JMAX+1;
441 T s2[JMAXP],h2[JMAXP+1];
445 for (
int j=1;j<=JMAX;j++) {
449 if(fabs(dss) <= tols*fabs(ss))
return ss;
453 std::cout <<
"s2= ";
for(
int j=1;j<=JMAX;j++) std::cout << s2[j] <<
" ";
454 std::cout << std::endl <<
"Too many steps in routine nintegrate<>\n";
455 throw std::invalid_argument(
"Too many steps in routine nintegrate<>");
460 template <
class F,
typename T>
461 double bisection_search(
468 int Nmax = 100000000,count=0;
470 T f1 = func(x1) - fvalue;
471 T f2 = func(x2) - fvalue;
473 throw std::invalid_argument(
"not bracketed");
475 while( fabs(x1-x2) > xacc && count < Nmax ){
477 T xnew = (x1 + x2)/2;
478 T fnew = func(xnew) - fvalue;
497 template <
typename T>
498 void vector_maxmin(
const std::vector<T> &v,T &max,T &min ){
502 max = (max > d) ? max : d;
503 min = (min < d) ? min : d;
508 template <
typename T>
509 T vector_mean(
const std::vector<T> &v){
511 for(T a : v) ans += a;
516 template <
typename T>
517 T vector_variance(
const std::vector<T> &v){
518 T m = vector_mean(v);
520 for(T a : v) ans += (a - m)*(a - m);
521 return ans/(v.size() - 1);
525 template <
typename T>
526 T vector_correlation(
const std::vector<T> &v1,
const std::vector<T> &v2){
527 size_t N = v1.size();
529 throw std::invalid_argument(
"vectors are not the same size");
532 T m1 = vector_mean(v1);
533 T m2 = vector_mean(v2);
534 T var1 = vector_variance(v1);
535 T var2 = vector_variance(v2);
538 for(
size_t i = 0 ; i < N ; ++i){
539 ans += (v1[i]-m1)*(v2[i]-m2);
541 ans /= (N-1)*sqrt(var1*var2);
546 template <
typename T>
549 Accumulater(
int size):
550 sum(0),count(0),N(size)
552 products.resize(N*N,0);
557 void add(
const std::vector<T> &v){
558 if(v.size() != N )
throw std::invalid_argument(
"wrong size vector");
559 for(T a : v) sum += a;
561 for(
size_t i = 0 ; i < N ; ++i){
563 for(
size_t j = 0 ; j < N ; ++j){
564 products[j + i*N] += v[i]*v[j];
572 if(1 != N )
throw std::invalid_argument(
"wrong size vector");
583 for(
auto d : sums) sum += d;
587 T mean(
size_t i){
return sums[i]/count;}
588 void means(std::vector<T> &mm){
590 for(T &m : mm) m /= count;
594 T covariance(
size_t i,
size_t j){
595 return (products[j + i*N] - sums[i]*sums[j]/count)/(count - 1);
598 void variances(std::vector<T> &mm){
600 for(
size_t i=0 ; i < N ; ++i){
601 mm[i] = covariance(i,i);
607 for(
size_t i=0 ; i < N ; ++i){
608 ans += covariance(i,i);
614 void cov_matrix(std::vector<T> &mm){
616 for(
size_t i = 0 ; i<N ; ++i){
617 T avi = sums[i]/count;
618 for(
size_t j = i ; j<N ; ++j){
620 mm[k] = (products[k] - avi*sums[j])/(count - 1);
626 void cov_matrix_normalized(std::vector<T> &mm){
631 for(
size_t i = 0 ; i<N ; ++i){
632 T avi = sums[i]/count;
633 for(
size_t j = i ; j<N ; ++j){
635 mm[k] = (products[k] - avi*sums[j])/(count - 1)/sqrt( var[i]*var[j] );
643 std::vector<T> products;
void fill_linear(std::vector< T > &v, size_t n, T min, T max)
Definition utilities.h:54
T trapz(FunctorType &func, T a, T b, int n, T *s2)
used in trapizoidal integral
Definition utilities.h:300
void polintT(T xa[], T ya[], int n, T x, T *y, T *dy)
interpolation
Definition utilities.h:262
size_t closest(const std::vector< T > &v, const T x)
Returns the index of the element of v that is closest to x. v must be sorted.
Definition utilities.h:213
void range(std::vector< T > vec, T &max, T &min)
find the maximum and minimum of a vector
Definition utilities.h:247