1 | // utils |
---|
2 | // $Id: utils.hh 2956 2011-06-28 18:54:32Z mabl $ |
---|
3 | // |
---|
4 | // miscellaneous utilities |
---|
5 | // |
---|
6 | #ifndef __UTILS_HH_ |
---|
7 | #define __UTILS_HH_ |
---|
8 | |
---|
9 | #include <stdexcept> |
---|
10 | #include <string> |
---|
11 | #include <algorithm> |
---|
12 | #include <iostream> |
---|
13 | #include <functional> |
---|
14 | #include <locale> |
---|
15 | |
---|
16 | #include "euso.hh" |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | // trim from start |
---|
21 | inline string <rim(string &s) { |
---|
22 | s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); |
---|
23 | return s; |
---|
24 | } |
---|
25 | |
---|
26 | // trim from end |
---|
27 | inline string &rtrim(string &s) { |
---|
28 | s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end()); |
---|
29 | return s; |
---|
30 | } |
---|
31 | |
---|
32 | inline std::string &trim(std::string &s) { |
---|
33 | return ltrim(rtrim(s)); |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | inline double toDouble(const string &s) { |
---|
38 | // trying for a double |
---|
39 | char *p; |
---|
40 | double d=strtod(s.c_str(), &p); |
---|
41 | if(d==0 && s.c_str()==p) // not a double |
---|
42 | return strtod("NaN",0); |
---|
43 | if(s.c_str()+s.size()!=p) // not a double |
---|
44 | return strtod("NaN",0); |
---|
45 | return d; |
---|
46 | } |
---|
47 | |
---|
48 | // solve a x^2 + b x + c = 0 keeping attention to roundoff |
---|
49 | // The result is a pair<int, double*> |
---|
50 | // the first component of the pair is the number of solutions |
---|
51 | // while the second one is an double[2] array containing the solutions. |
---|
52 | pair<int, double*> &findRoots(double a, double b, double c); |
---|
53 | inline int sign(double d) { return (d>0?1:-1); } |
---|
54 | |
---|
55 | // find root of 1d function f(x) = 0 between two points x1 and x2 |
---|
56 | // adapted from Numerical Recipes in C. Chapter 9. Root Finding and |
---|
57 | // Nonlinear Sets of Equations |
---|
58 | // by Dmitry V. Naumov 04/07/2004 |
---|
59 | |
---|
60 | #define FACTOR 1.6 |
---|
61 | #define NTRY 50 |
---|
62 | #define JMAX 40 |
---|
63 | int zbrac(float (*func)(float), float *x1, float *x2); |
---|
64 | float rtbis(float (*func)(float), float x1, float x2, float xacc); |
---|
65 | void write_counter(char*,double,double); |
---|
66 | #endif /* __UTILS_HH_ */ |
---|