All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
misc.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_MISC_H
24 #define O2SCL_MISC_H
25 /** \file misc.h
26  \brief Miscellaneous functions
27 */
28 
29 #include <cstdlib>
30 #include <iostream>
31 #include <string>
32 // For stringstream for count_words()
33 #include <sstream>
34 #include <vector>
35 // For std::isinf and std::isnan in C++11
36 #include <cmath>
37 
38 #include <o2scl/err_hnd.h>
39 
40 #ifndef DOXYGEN_NO_O2NS
41 namespace o2scl {
42 #endif
43 
44  /** \brief Simple string comparison
45 
46  This struct is used internally by \o2 for the STL routines which
47  require a way to compare strings in the class \ref table and in
48  the I/O classes.
49  */
50  typedef struct {
51  /// Return \c s1<s2
52  bool operator()(const std::string s1, const std::string s2) const {
53  return s1<s2;
54  }
55  } string_comp;
56 
57  /// \name Functions from misc.h
58  //@{
59  /** \brief Return false if x is infinite or not a number
60 
61  This uses the C++ function <tt>std::isfinite</tt>.
62  */
63  bool is_finite(double x);
64 
65  /** \brief Return true if x is not a number
66 
67  This uses the C++ function <tt>std::isnan</tt>.
68  */
69  bool is_nan(double x);
70 
71  /** \brief Return true if x is infinite
72 
73  This uses the C++ function <tt>std::isinf</tt>.
74  */
75  bool is_inf(double x);
76 
77  /** \brief Calculate a Fermi-Dirac distribution function safely
78 
79  \f$ \left[1+\exp\left(E/T-\mu/T\right)\right]^{-1} \f$
80 
81  This calculates a Fermi-Dirac distribution function guaranteeing
82  that numbers larger than \f$ \exp(\mathrm{limit}) \f$ and
83  smaller than \f$ \exp(-\mathrm{limit}) \f$ will be avoided. The
84  default value of <tt>limit=40</tt> ensures accuracy to within 1
85  part in \f$ 10^{17} \f$ compared to the maximum of the
86  distribution (which is unity).
87 
88  Note that this function may return Inf or NAN if \c limit is too
89  large, depending on the machine precision.
90  */
91  double fermi_function(double E, double mu, double T, double limit=40.0);
92 
93  /** \brief Reformat the columns for output of width \c size
94 
95  Given a string array \c in_cols of size \c nin, screenify()
96  reformats the array into columns creating a new string array \c
97  out_cols.
98 
99  For example, for an array of 10 strings
100  \verbatim
101  test1
102  test_of_string2
103  test_of_string3
104  test_of_string4
105  test5
106  test_of_string6
107  test_of_string7
108  test_of_string8
109  test_of_string9
110  test_of_string10
111  \endverbatim
112  screenify() will create an array of 3 new strings:
113  \verbatim
114  test1 test_of_string4 test_of_string7 test_of_string10
115  test_of_string2 test5 test_of_string8
116  test_of_string3 test_of_string6 test_of_string9
117  \endverbatim
118 
119  If the value of \c max_size is less than the length of the
120  longest input string (plus one for a space character), then the
121  output strings may have a larger length than \c max_size.
122  */
123  template<class string_arr_t>
124  void screenify(size_t nin, const string_arr_t &in_cols,
125  std::vector<std::string> &out_cols,
126  size_t max_size=80) {
127 
128  if (nin==0) {
129  O2SCL_ERR("No strings specified in screenify().",exc_efailed);
130  }
131 
132  size_t i,j,lmax,itemp;
133  std::string *in_spaces=new std::string[nin];
134 
135  // Determine size of largest string
136  lmax=0;
137  for(i=0;i<nin;i++) {
138  if (lmax<in_cols[i].size()) lmax=in_cols[i].size();
139  }
140 
141  // Pad with spaces
142  for(i=0;i<nin;i++) {
143  itemp=in_cols[i].size();
144  in_spaces[i]=in_cols[i];
145  for(j=0;j<lmax+1-itemp;j++) {
146  in_spaces[i]+=' ';
147  }
148  }
149 
150  // Determine number of rows and columns
151  size_t row, col;
152  col=max_size/(lmax+1);
153  if (col==0) col=1;
154  if (nin/col*col==nin) row=nin/col;
155  else row=nin/col+1;
156 
157  // Create outc
158  out_cols.reserve(row);
159  for(i=0;i<row;i++) {
160  out_cols.push_back("");
161  for(j=0;j<col;j++) {
162  if (i+j*row<nin) {
163  out_cols[i]+=in_spaces[i+j*row];
164  }
165  }
166  }
167 
168  delete[] in_spaces;
169 
170  return;
171  }
172 
173  /** \brief Count the number of words in the string \c str
174 
175  Words are defined as groups of characters separated by
176  whitespace, where whitespace is any combination of adjacent
177  spaces, tabs, carriage returns, etc. On most systems, whitespace
178  is usually defined as any character corresponding to the
179  integers 9 (horizontal tab), 10 (line feed), 11 (vertical tab),
180  12 (form feed), 13 (carriage return), and 32 (space bar). The
181  test program \c misc_ts enumerates the characters between 0 and
182  255 (inclusive) that count as whitespace for this purpose.
183 
184  \todo Make consistent with split_string().
185  */
186  size_t count_words(std::string str);
187 
188  /** \brief Remove all whitespace from the string \c s
189 
190  This function removes all characters in \c s which correspond to
191  the integer values 9, 10, 11, 12, 13, or 32.
192  */
193  void remove_whitespace(std::string &s);
194 
195  /** \brief Take a string of binary quads and compress them to
196  hexadecimal digits
197 
198  This function proceeds from left to right, ignoring parts of the
199  string that do not consist of squences of four '1's or '0's.
200  */
201  std::string binary_to_hex(std::string s);
202 
203  /** \brief Convert RGB to HSV color
204 
205  Taken from Nathan Schaller's webpage at
206  http://www.cs.rit.edu/~ncs/color/t_convert.html
207 
208  The inputs should be in the ranges \f$ h \in [0,360] \f$, \f$ s
209  \in [0,1] \f$, and \f$ v \in [0,1] \f$. The output values \c r,
210  \c g, and \c b are \f$ \in [0,1] \f$.
211 
212  If s == 0, then h = -1 (undefined)
213  */
214  void RGBtoHSV(double r, double g, double b,
215  double &h, double &s, double &v);
216 
217  /** \brief Convert RGB to HSV color
218 
219  Taken from Nathan Schaller's webpage at
220  http://www.cs.rit.edu/~ncs/color/t_convert.html
221 
222  The inputs should be in the ranges \f$ h \in [0,360] \f$, \f$ s
223  \in [0,1] \f$, and \f$ v \in [0,1] \f$. The output values \c r,
224  \c g, and \c b are \f$ \in [0,1] \f$.
225 
226  If s == 0, then h = -1 (undefined)
227  */
228  void HSVtoRGB(double h, double s, double v,
229  double &r, double &g, double &b);
230  //@}
231 
232  /** \brief Generate number sequence for testing
233 
234  A class which generates \c tot numbers from -1 to 1, making sure
235  to include -1, 1, 0, and numbers near -1, 0 and 1 (so long as \c
236  tot is sufficiently large). If gen() is called more than \c tot
237  times, it just recycles through the list again.
238 
239  This class is used to generate combinations of coefficients for
240  testing the polynomial solvers.
241 
242  For example, the first 15 numbers generated by
243  an object of type gen_test_number<10> are:
244  \verbatim
245  0 -1.000000e+00
246  1 -9.975274e-01
247  2 -8.807971e-01
248  3 -1.192029e-01
249  4 -2.472623e-03
250  5 +0.000000e+00
251  6 +2.472623e-03
252  7 +1.192029e-01
253  8 +8.807971e-01
254  9 +1.000000e+00
255  10 -1.000000e+00
256  11 -9.975274e-01
257  12 -8.807971e-01
258  13 -1.192029e-01
259  14 -2.472623e-03
260  \endverbatim
261 
262  This function is used in <tt>src/other/poly_ts.cpp</tt> which
263  tests the polynomial solvers.
264 
265  \future Document what happens if \c tot is pathologically small.
266  */
267  template<size_t tot> class gen_test_number {
268 
269 #ifndef DOXYGEN_INTERNAL
270 
271  protected:
272 
273  /// Count number of numbers generated
274  int n;
275 
276  /** \brief A constant factor for the argument to
277  <tt>tanh()</tt>, equal to \c tot divided by 20.
278  */
279  double fact;
280 
281 #endif
282 
283  public:
284 
285  gen_test_number() {
286  n=0;
287  fact=((double)tot)/20.0;
288  }
289 
290  /// Return the next number in the sequence
291  double gen() {
292  double x, dtot=((double)tot), dn=((double)n);
293  if (n==0) {
294  x=-1.0;
295  } else if (n==tot/2) {
296  x=0.0;
297  } else if (n==tot-1) {
298  x=1.0;
299  } else if (n==tot) {
300  // Start the sequence over
301  x=-1.0;
302  n=0;
303  } else if (n<((int)tot)/2) {
304  // Since we're in the o2scl namespace, we explicitly
305  // specify std::tanh() here
306  x=(std::tanh((dn-dtot/4.0)/fact)-1.0)/2.0;
307  } else {
308  x=(std::tanh((dn-0.75*dtot)/fact)+1.0)/2.0;
309  }
310  n++;
311  return x;
312  }
313  };
314 
315  /** \brief Return the x value of the extremum of a quadratic defined by
316  three \f$ (x,y) \f$ pairs
317 
318  This function should work for any floating-point data type,
319  but will suffer from problems due to lack of precision in
320  some cases.
321  */
322  template<class data_t>
323  data_t quadratic_extremum_x(const data_t x1, const data_t x2,
324  const data_t x3, const data_t y1,
325  const data_t y2, const data_t y3) {
326 
327  if (x1==x2 || x2==x3 || x1==x3) {
328  O2SCL_ERR2("Two abscissae cannot be equal in function ",
329  "quadratic_extremum_x().",exc_einval);
330  }
331 
332  /*
333  Start with:
334  y1=a x1^2 + b x1 + c
335  y2=a x2^2 + b x2 + c
336  y3=a x3^2 + b x3 + c
337 
338  Eliminate 'c':
339  (y1-y2)=a(x1^2-x2^2)+b(x1-x2)
340  (y3-y2)=a(x3^2-x2^2)+b(x3-x2)
341 
342  Eliminate 'b':
343  (x3-x2)*(y1-y2)=a*(x1^2-x2^2)*(x3-x2)+b*(x1-x2)*(x3-x2)
344  (x1-x2)*(y3-y2)=a*(x3^2-x2^2)*(x1-x2)+b*(x3-x2)*(x1-x2)
345 
346  Alternatively, eliminate 'c' with:
347  (y2-y1)=a(x2^2-x1^2)+b(x2-x1)
348  (y3-y1)=a(x3^2-x1^2)+b(x3-x1)
349 
350  Eliminate 'b':
351  (x3-x1)*(y2-y1)=a(x2^2-x1^2)*(x3-x1)+b(x2-x1)*(x3-x1)
352  (x2-x1)*(y3-y1)=a(x3^2-x1^2)*(x2-x1)+b(x3-x1)*(x2-x1)
353  */
354 
355  data_t a,b,c,den=(x1*x1-x2*x2)*(x3-x2)-(x3*x3-x2*x2)*(x1-x2);
356  if (den==0.0) {
357  den=(x2*x2-x1*x1)*(x3-x1)-(x3*x3-x1*x1)*(x2-x1);
358  a=((x3-x1)*(y2-y1)-(x2-x2)*(y3-y1))/den;
359  } else {
360  a=((x3-x2)*(y1-y2)-(x1-x2)*(y3-y2))/den;
361  }
362  b=(y1-y2-a*(x1*x1-x2*x2))/(x1-x2);
363  c=y2-a*x2*x2-b*x2;
364 
365  return -b/2/a;
366  }
367 
368  /** \brief Return the y value of the extremum of a quadratic defined by
369  three \f$ (x,y) \f$ pairs
370 
371  This function should work for any floating-point data type,
372  but will suffer from problems due to lack of precision in
373  some cases.
374  */
375  template<class data_t>
376  data_t quadratic_extremum_y(const data_t x1, const data_t x2,
377  const data_t x3, const data_t y1,
378  const data_t y2, const data_t y3) {
379 
380  if (x1==x2 || x2==x3 || x1==x3) {
381  O2SCL_ERR2("Two abscissae cannot be equal in function ",
382  "quadratic_extremum_y().",exc_einval);
383  }
384 
385  double a,b,c,den=(x1*x1-x2*x2)*(x3-x2)-(x3*x3-x2*x2)*(x1-x2);
386  if (den==0.0) {
387  den=(x2*x2-x1*x1)*(x3-x1)-(x3*x3-x1*x1)*(x2-x1);
388  a=((x3-x1)*(y2-y1)-(x2-x2)*(y3-y1))/den;
389  } else {
390  a=((x3-x2)*(y1-y2)-(x1-x2)*(y3-y2))/den;
391  }
392  b=(y1-y2-a*(x1*x1-x2*x2))/(x1-x2);
393  c=y2-a*x2*x2-b*x2;
394 
395  return c-b*b/4/a;
396  }
397 
398  /** \brief Return the (x,y) for the extremum of a quadratic defined by
399  three \f$ (x,y) \f$ pairs
400 
401  This function should work for any floating-point data type,
402  but will suffer from problems due to lack of precision in
403  some cases.
404  */
405  template<class data_t>
406  void quadratic_extremum_xy(const data_t x1, const data_t x2,
407  const data_t x3, const data_t y1,
408  const data_t y2, const data_t y3,
409  data_t &x, data_t &y) {
410 
411  if (x1==x2 || x2==x3 || x1==x3) {
412  O2SCL_ERR2("Two abscissae cannot be equal in function ",
413  "quadratic_extremum_xy().",exc_einval);
414  }
415 
416  data_t a,b,c,den=(x1*x1-x2*x2)*(x3-x2)-(x3*x3-x2*x2)*(x1-x2);
417  if (den==0.0) {
418  den=(x2*x2-x1*x1)*(x3-x1)-(x3*x3-x1*x1)*(x2-x1);
419  a=((x3-x1)*(y2-y1)-(x2-x2)*(y3-y1))/den;
420  } else {
421  a=((x3-x2)*(y1-y2)-(x1-x2)*(y3-y2))/den;
422  }
423  b=(y1-y2-a*(x1*x1-x2*x2))/(x1-x2);
424  c=y2-a*x2*x2-b*x2;
425 
426  x=-b/2/a;
427  y=c-b*b/4/a;
428 
429  return;
430  }
431 
432  /** \brief Return the (x,y) for the extremum of a quadratic defined by
433  three \f$ (x,y) \f$ pairs
434 
435  This function should work for any floating-point data type,
436  but will suffer from problems due to lack of precision in
437  some cases.
438  */
439  template<class data_t>
440  void quadratic_extremum_coeffs(const data_t x1, const data_t x2,
441  const data_t x3, const data_t y1,
442  const data_t y2, const data_t y3,
443  data_t &a, data_t &b, data_t &c) {
444 
445  if (x1==x2 || x2==x3 || x1==x3) {
446  O2SCL_ERR2("Two abscissae cannot be equal in function ",
447  "quadratic_extremum_coeffs().",exc_einval);
448  }
449 
450  data_t den=(x1*x1-x2*x2)*(x3-x2)-(x3*x3-x2*x2)*(x1-x2);
451  if (den==0.0) {
452  den=(x2*x2-x1*x1)*(x3-x1)-(x3*x3-x1*x1)*(x2-x1);
453  a=((x3-x1)*(y2-y1)-(x2-x2)*(y3-y1))/den;
454  } else {
455  a=((x3-x2)*(y1-y2)-(x1-x2)*(y3-y2))/den;
456  }
457  b=(y1-y2-a*(x1*x1-x2*x2))/(x1-x2);
458  c=y2-a*x2*x2-b*x2;
459 
460  return;
461  }
462 
463 #ifndef DOXYGEN_NO_O2NS
464 }
465 #endif
466 
467 #endif
468 
std::string binary_to_hex(std::string s)
Take a string of binary quads and compress them to hexadecimal digits.
double fact
A constant factor for the argument to tanh(), equal to tot divided by 20.
Definition: misc.h:279
void remove_whitespace(std::string &s)
Remove all whitespace from the string s.
void HSVtoRGB(double h, double s, double v, double &r, double &g, double &b)
Convert RGB to HSV color.
bool is_finite(double x)
Return false if x is infinite or not a number.
invalid argument supplied by user
Definition: err_hnd.h:59
double fermi_function(double E, double mu, double T, double limit=40.0)
Calculate a Fermi-Dirac distribution function safely.
bool operator()(const std::string s1, const std::string s2) const
Return s1<s2.
Definition: misc.h:52
generic failure
Definition: err_hnd.h:61
bool is_nan(double x)
Return true if x is not a number.
data_t quadratic_extremum_x(const data_t x1, const data_t x2, const data_t x3, const data_t y1, const data_t y2, const data_t y3)
Return the x value of the extremum of a quadratic defined by three pairs.
Definition: misc.h:323
static const double x3[11]
Definition: inte_qng_gsl.h:94
Generate number sequence for testing.
Definition: misc.h:267
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
Definition: err_hnd.h:281
double gen()
Return the next number in the sequence.
Definition: misc.h:291
data_t quadratic_extremum_y(const data_t x1, const data_t x2, const data_t x3, const data_t y1, const data_t y2, const data_t y3)
Return the y value of the extremum of a quadratic defined by three pairs.
Definition: misc.h:376
void screenify(size_t nin, const string_arr_t &in_cols, std::vector< std::string > &out_cols, size_t max_size=80)
Reformat the columns for output of width size.
Definition: misc.h:124
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
Definition: err_hnd.h:273
void RGBtoHSV(double r, double g, double b, double &h, double &s, double &v)
Convert RGB to HSV color.
void quadratic_extremum_coeffs(const data_t x1, const data_t x2, const data_t x3, const data_t y1, const data_t y2, const data_t y3, data_t &a, data_t &b, data_t &c)
Return the (x,y) for the extremum of a quadratic defined by three pairs.
Definition: misc.h:440
void quadratic_extremum_xy(const data_t x1, const data_t x2, const data_t x3, const data_t y1, const data_t y2, const data_t y3, data_t &x, data_t &y)
Return the (x,y) for the extremum of a quadratic defined by three pairs.
Definition: misc.h:406
int n
Count number of numbers generated.
Definition: misc.h:274
size_t count_words(std::string str)
Count the number of words in the string str.
bool is_inf(double x)
Return true if x is infinite.
static const double x2[5]
Definition: inte_qng_gsl.h:66
static const double x1[5]
Definition: inte_qng_gsl.h:48

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..