OpenWAM
Classes | Typedefs | Functions
Math_wam.h File Reference
#include <cstdlib>
#include <vector>
#include <cmath>
#include <limits>
#include <iostream>
#include "Globales.h"
Include dependency graph for Math_wam.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  stPolar
 
struct  stRectan
 
struct  Base_interp
 
struct  Linear_interp
 
struct  Hermite_interp
 
struct  Step_interp
 
struct  LUdcmp
 

Typedefs

typedef unsigned int Uint
 Unsigned integer.
 
typedef std::vector< double > dVector
 Double vector.
 
typedef std::vector< std::vector< double > > dMatrix
 2-dimensional double matrix
 
typedef std::vector< int > iVector
 Integer vector.
 
typedef std::vector< std::vector< int > > iMatrix
 2-dimensional integer matrix
 
typedef std::vector< bool > bVector
 Boolean vector.
 
typedef std::vector< std::vector< bool > > bMatrix
 2-dimensional boolean matrix
 

Functions

double Interpola (double vizq, double vder, double axid, double xif)
 
template<class T >
pow2 (T x)
 Returns x to the power of 2. More...
 
template<class T >
pow3 (T x)
 Returns x to the power of 3. More...
 
template<class T >
pow4 (T x)
 Returns x to the power of 4. More...
 
template<class T >
pow025 (T x)
 Returns x to the power of 0.25. More...
 
template<class T >
pow150 (T x)
 Returns x to the power of 1.5. More...
 
template<class T >
pow075 (T x)
 Returns x to the power of 0.75. More...
 
template<class T , class U >
poww (T x, U y)
 
template<class T >
sqrtw (T x)
 
template<class T >
bool DoubEqZero (T x)
 
double QuadraticEqP (double A, double B, double C)
 
double QuadraticEqN (double A, double B, double C)
 
template<class T >
SQR (const T a)
 
template<class T >
const T & Max (const T &a, const T &b)
 
float Max (const double &a, const float &b)
 
float Max (const float &a, const double &b)
 
template<class T >
const T & Min (const T &a, const T &b)
 
float Min (const double &a, const float &b)
 
float Min (const float &a, const double &b)
 
template<class T >
Sign (const T &a, const T &b)
 
float Sign (const float &a, const double &b)
 
float Sign (const double &a, const float &b)
 
template<class T >
void Swap (T &a, T &b)
 
template<class T >
MaxComponent (std::vector< T > &x)
 
template<class T >
MinComponent (std::vector< T > &x)
 
template<class T >
double zbrent (T &func, const double &x1, const double &x2, const double &tol)
 Solves a function using Brent's method. More...
 
template<class T >
double FindRoot (T &func, const double x1, const double x2)
 Finds the root of a function. More...
 
template<class T >
bool zbrac (T &func, double &x1, double &x2)
 
template<class T >
bool zbrac2 (T &func, double &x1, double &x2, const double &min, const double &max)
 
template<class T >
void zbrak (T &fx, const double x1, const double x2, const int n, dVector &xb1, dVector &xb2, int &nroot)
 
template<class T >
double rtbis (T &func, const double x1, const double x2, const double xacc)
 
template<class T >
double rtflsp (T &func, const double x1, const double x2, const double xacc)
 
template<class T >
double rtsec (T &func, const double x1, const double x2, const double xacc)
 
template<class T >
double zriddr (T &func, const double x1, const double x2, const double xacc)
 
template<class T >
double rtnewt (T &funcd, const double x1, const double x2, const double xacc)
 
template<class T >
double rtsafe (T &funcd, const double x1, const double x2, const double xacc)
 

Detailed Description

Author
Francisco Jose Arnau farna.nosp@m.u@mo.nosp@m.t.upv.nosp@m..es
Luis Miguel Garcia-Cuevas Gonzalez luiga.nosp@m.12@m.nosp@m.ot.up.nosp@m.v.es

LICENSE

This file is part of OpenWAM.

OpenWAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

OpenWAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with OpenWAM. If not, see http://www.gnu.org/licenses/.

DESCRIPTION

This file declares several auxiliary math functions, as well as some typedefs.

Definition in file Math_wam.h.

Function Documentation

◆ FindRoot()

template<class T >
double FindRoot ( T &  func,
const double  x1,
const double  x2 
)
inline

Finds the root of a function.

Finds the root of a function using Brent's method. It uses a combination of the bisection method, the secant method and inverse quadratic interpolation, and finds the root in an interval. The function must have different signs at each bound of the interval. If the signs are the same, returns one of the bounds, whichever is minimum. It uses a default tolerance of 1e-10.

Parameters
funcThe function.
x1The lower bound of the interval.
x2The upper bound of the interval.
Returns
The interpolated value.

Definition at line 459 of file Math_wam.h.

459  {
460  return zbrent(func, x1, x2, 1e-10);
461 }

References zbrent().

Here is the call graph for this function:

◆ pow025()

template<class T >
T pow025 ( x)
inline

Returns x to the power of 0.25.

Computes $ x ^ {0.25} $

Parameters
xThe value.
Returns
$ x ^ {0.25} $

Definition at line 127 of file Math_wam.h.

127  {
128  return sqrt(sqrt(x));
129 }

◆ pow075()

template<class T >
T pow075 ( x)
inline

Returns x to the power of 0.75.

Computes $ x ^ {0.75} $

Parameters
xThe value.
Returns
$ x ^ {0.75} $

Definition at line 153 of file Math_wam.h.

153  {
154  return sqrt(pow150(x));
155 }

References pow150().

Here is the call graph for this function:

◆ pow150()

template<class T >
T pow150 ( x)
inline

Returns x to the power of 1.5.

Computes $ x ^ {1.5} $

Parameters
xThe value.
Returns
$ x ^ {1.5} $

Definition at line 140 of file Math_wam.h.

140  {
141  return sqrt(pow3(x));
142 }

References pow3().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pow2()

template<class T >
T pow2 ( x)
inline

Returns x to the power of 2.

Computes $ x ^ 2 $

Parameters
xThe value.
Returns
$ x ^ 2 $

Definition at line 88 of file Math_wam.h.

88  {
89  return x * x;
90 }
Here is the caller graph for this function:

◆ pow3()

template<class T >
T pow3 ( x)
inline

Returns x to the power of 3.

Computes $ x ^ 3 $

Parameters
xThe value.
Returns
$ x ^ 3 $

Definition at line 101 of file Math_wam.h.

101  {
102  return x * x * x;
103 }
Here is the caller graph for this function:

◆ pow4()

template<class T >
T pow4 ( x)
inline

Returns x to the power of 4.

Computes $ x ^ 4 $

Parameters
xThe value.
Returns
$ x ^ 4 $

Definition at line 114 of file Math_wam.h.

114  {
115  return x * x * x * x;
116 }

◆ zbrent()

template<class T >
double zbrent ( T &  func,
const double &  x1,
const double &  x2,
const double &  tol 
)
inline

Solves a function using Brent's method.

Finds the root of a function using Brent's method. It uses a combination of the bisection method, the secant method and inverse quadratic interpolation, and finds the root in an interval. The function must have different signs at each bound of the interval. If the signs are the same, returns one of the bounds, whichever is minimum.

Parameters
funcThe function.
x1The lower bound of the interval.
x2The upper bound of the interval.
tolThe tolerance of the method.
Returns
The interpolated value.

Definition at line 352 of file Math_wam.h.

352  {
353  const int ITMAX = 100;
354  const double EPS = std::numeric_limits<double>::epsilon();
355  double a = x1;
356  double b = x2;
357  double c = x2;
358  double d;
359  double e;
360  double fa = func(a);
361  double fb = func(b);
362  double fc;
363  double p;
364  double q;
365  double r;
366  double s;
367  double tol1;
368  double xm;
369 
370  if((fa > 0.0 && fb > 0.0) || (fa < 0.0 && fb < 0.0)) {
371  if(fabs(fa) < fabs(fb)) {
372  fa = func(a);
373  return a;
374  } /* Condicion original if((fabs(fa) < fabs(fb)) && fabs(fa) < tol) */
375  else if(fabs(fa) > fabs(fb)) {
376  fb = func(b);
377  return b;
378  } /* Original if((fabs(fa) > fabs(fb)) && fabs(fb) < tol) */
379  // return 0;
380  /* throw("Root must be bracketed in zbrent"); */
381  }
382 
383  fc = fb;
384  for(int iter = 0; iter < ITMAX; iter++) {
385  if((fb > 0.0 && fc > 0.0) || (fb < 0.0 && fc < 0.0)) {
386  c = a;
387  fc = fa;
388  e = d = b - a;
389  }
390  if(fabs(fc) < fabs(fb)) {
391  a = b;
392  b = c;
393  c = a;
394  fa = fb;
395  fb = fc;
396  fc = fa;
397  }
398  tol1 = 2.0 * EPS * fabs(b) + 0.5 * tol;
399  xm = 0.5 * (c - b);
400  if(fabs(xm) <= tol1 || fb == 0.0) {
401  /* std::cout << iter << std::endl; */
402  return b;
403  }
404  if(fabs(e) >= tol1 && fabs(fa) > fabs(fb)) {
405  s = fb / fa;
406  if(a == c) {
407  p = 2.0 * xm * s;
408  q = 1.0 - s;
409  } else {
410  q = fa / fc;
411  r = fb / fc;
412  p = s * (2.0 * xm * q * (q - r) - (b - a) * (r - 1.0));
413  q = (q - 1.0) * (r - 1.0) * (s - 1.0);
414  }
415  if(p > 0.0)
416  q = -q;
417  p = fabs(p);
418  double min1 = 3.0 * xm * q - fabs(tol1 * q);
419  double min2 = fabs(e * q);
420  if(2.0 * p < (min1 < min2 ? min1 : min2)) {
421  e = d;
422  d = p / q;
423  } else {
424  d = xm;
425  e = d;
426  }
427 
428  } else {
429  d = xm;
430  e = d;
431  }
432  a = b;
433  fa = fb;
434  if(fabs(d) > tol1)
435  b += d;
436  else
437  b += Sign(tol1, xm);
438  fb = func(b);
439  }
440  return b;
441 }
Here is the caller graph for this function:
zbrent
double zbrent(T &func, const double &x1, const double &x2, const double &tol)
Solves a function using Brent's method.
Definition: Math_wam.h:352
pow150
T pow150(T x)
Returns x to the power of 1.5.
Definition: Math_wam.h:140
pow3
T pow3(T x)
Returns x to the power of 3.
Definition: Math_wam.h:101