Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TMATH_UTIL_H
Toshihiro Shimizu 890ddd
#define TMATH_UTIL_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "tcommon.h"
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <numeric></numeric>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef __sgi
Toshihiro Shimizu 890ddd
#include <cmath></cmath>
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TNZCORE_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
enum TMathError { INFINITE_SOLUTIONS = -1 };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class DVAPI TMathException final : public TException {
Shinya Kitaoka 120a6e
  TString m_msg;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TMathException(std::string msg);
Shinya Kitaoka 120a6e
  virtual ~TMathException() {}
Shinya Kitaoka 473e70
Shinya Kitaoka 473e70
  TString getMessage() const override { return m_msg; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
/*!
Toshihiro Shimizu 890ddd
  Decompose a square matrix [A] dim(A)=[n x n]  using the LU method.
Toshihiro Shimizu 890ddd
  A is a matrix stored for row.
Toshihiro Shimizu 890ddd
  \par A array of coeff
Shinya Kitaoka 120a6e
  \par n size of matrix
Toshihiro Shimizu 890ddd
  \par indx array with permutation from pivoting
luz paz 6454c4
  \par d +1/-1 and depend whether the number of row interchanges (even or odd)
Toshihiro Shimizu 890ddd
  \note If error a TException is thrown.
Toshihiro Shimizu 890ddd
  \include mathutils_ex2.cpp
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DVAPI void tLUDecomposition(double *A, int n, int *indx, double &d);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
/*!
Toshihiro Shimizu 890ddd
  Use the back-substitution method to solve the linear
Toshihiro Shimizu 890ddd
  system A*x=b.
Toshihiro Shimizu 890ddd
  \par A matrix exited from tLUDecomposition
Toshihiro Shimizu 890ddd
  \par n size of matrix
Toshihiro Shimizu 890ddd
  \par indx array exited from tLUDecomposition
Toshihiro Shimizu 890ddd
  \par b in input is array of element
Toshihiro Shimizu 890ddd
  \ret b in output contains result
Toshihiro Shimizu 890ddd
  \note A is the square matrix [n x n] returned from tLUDecomposition routine.
Toshihiro Shimizu 890ddd
  \sa tLUDecomposition
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DVAPI void tbackSubstitution(double *A, int n, int *indx, double *b);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Solve the system A*x=b.
Toshihiro Shimizu 890ddd
  It`s needed to insert b in res.
Toshihiro Shimizu 890ddd
  A and res are modifyed in the routine.
Shinya Kitaoka 120a6e
  \par A matrix
Shinya Kitaoka 120a6e
  \par n size of matrix
Toshihiro Shimizu 890ddd
  \note A is square [n x n]
Toshihiro Shimizu 890ddd
  \include mathutils_ex1.cpp
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DVAPI void tsolveSistem(double *A, int n, double *res);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Solve the system A*x=b.
Toshihiro Shimizu 890ddd
  It`s needed to insert b in res.
Toshihiro Shimizu 890ddd
  A and res are modifyed in the routine.
Toshihiro Shimizu 890ddd
  \note A is square [n x n]
Toshihiro Shimizu 890ddd
  \include mathutils_ex1.cpp
Toshihiro Shimizu 890ddd
 */
Shinya Kitaoka 120a6e
inline void tsolveSistem(std::vector<double> &A, std::vector<double> &res) {</double></double>
Shinya Kitaoka 120a6e
  assert(res.size() * res.size() == A.size());
Shinya Kitaoka 120a6e
  tsolveSistem(&A[0], res.size(), &res[0]);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Find determinant of square matrix A
Shinya Kitaoka 120a6e
  \par A is matrix to test
Toshihiro Shimizu 890ddd
  \par n size of matrix
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DVAPI double tdet(double *A, int n);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Shinya Kitaoka 120a6e
  Find determinant of square matrix A [n x n], using information
Toshihiro Shimizu 890ddd
  from tLUDecomposition decomposition.
Toshihiro Shimizu 890ddd
  \par LUa output matrix from tLUDecomposition
Toshihiro Shimizu 890ddd
  \par n size of matrix
Toshihiro Shimizu 890ddd
  \par d parameter from tLUDecomposition
Toshihiro Shimizu 890ddd
  \sa tLUDecomposition
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DVAPI double tdet(double *LUa, int n, double d);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Find real root of a polynomious
Toshihiro Shimizu 890ddd
  \par poly is a vector with coeff of polynomious in crescent order.
Toshihiro Shimizu 890ddd
  \ret sol is the vector of solution and contain real found solution.
Toshihiro Shimizu 890ddd
 */
Shinya Kitaoka 120a6e
DVAPI int rootFinding(const std::vector<double> &poly,</double>
Shinya Kitaoka 120a6e
                      std::vector<double> &sol);</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Check if val is nearest to epsilon.
Toshihiro Shimizu 890ddd
  \par val value to test
Toshihiro Shimizu 890ddd
  \par eps tolerance required
Toshihiro Shimizu 890ddd
  \ret true if value is nearest to zero
Toshihiro Shimizu 890ddd
 */
Shinya Kitaoka 120a6e
inline bool isAlmostZero(double val, double eps = TConsts::epsilon) {
Shinya Kitaoka 120a6e
  return -eps < val && val < eps;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Find number of roots of a poly in interval min max.
Toshihiro Shimizu 890ddd
  \par order degree of polynomious
Toshihiro Shimizu 890ddd
  \par coeff of poly in crescent order
Toshihiro Shimizu 890ddd
  \par lesser extreme
Toshihiro Shimizu 890ddd
  \par greater extreme
Toshihiro Shimizu 890ddd
  \ret number of root
Toshihiro Shimizu 890ddd
 */
Shinya Kitaoka 120a6e
DVAPI int numberOfRootsInInterval(int order, const double *polyH, double min,
Shinya Kitaoka 120a6e
                                  double max);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Find the real root of the poly a*x^3+b*x^2+c*x+d in [0,1]
Toshihiro Shimizu 890ddd
  \par a coeff of x^3
Toshihiro Shimizu 890ddd
  \par b coeff of x^2
Toshihiro Shimizu 890ddd
  \par c coeff of x^1
Toshihiro Shimizu 890ddd
  \par d coeff of x^0
Toshihiro Shimizu 890ddd
  \ret the real root of the poly, or 1.0 if the root is not in [0,1]
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
DVAPI double cubicRoot(double a, double b, double c, double d);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Find the root of the poly a*x^2+b*x+c
Toshihiro Shimizu 890ddd
  \par a coeff of x^2
Toshihiro Shimizu 890ddd
  \par b coeff of x^1
Toshihiro Shimizu 890ddd
  \par c coeff of x^0
Toshihiro Shimizu 890ddd
  \ret the root of the poly, or 1.0 if it does not exist
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
DVAPI double quadraticRoot(double a, double b, double c);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // #ifndef TMATH_UTIL_H
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------