2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* Math.hpp, part of LatAnalyze 3
|
|
|
|
*
|
2016-04-06 20:11:23 +01:00
|
|
|
* Copyright (C) 2013 - 2016 Antonin Portelli
|
2014-02-06 18:52:13 +00:00
|
|
|
*
|
|
|
|
* LatAnalyze 3 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.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 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 LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef Latan_Math_hpp_
|
|
|
|
#define Latan_Math_hpp_
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/Global.hpp>
|
2019-02-10 00:23:36 +00:00
|
|
|
#include <LatAnalyze/Functional/Function.hpp>
|
|
|
|
#include <LatAnalyze/Core/MathInterpreter.hpp>
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2015-01-28 17:15:05 +00:00
|
|
|
BEGIN_LATAN_NAMESPACE
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2014-04-15 17:30:51 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Custom math functions *
|
|
|
|
******************************************************************************/
|
|
|
|
#define MATH_NAMESPACE Math
|
|
|
|
|
|
|
|
namespace MATH_NAMESPACE
|
|
|
|
{
|
|
|
|
// integer power function
|
|
|
|
template <unsigned int n, typename T>
|
2015-06-15 14:20:29 +01:00
|
|
|
typename std::enable_if<(n == 0), T>::type pow(const T x __dumb)
|
2014-04-15 17:30:51 +01:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned int n, typename T>
|
|
|
|
typename std::enable_if<(n == 1), T>::type pow(const T x)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned int n, typename T>
|
|
|
|
typename std::enable_if<(n > 1), T>::type pow(const T x)
|
|
|
|
{
|
|
|
|
return x*pow<n-1>(x);
|
|
|
|
}
|
|
|
|
|
2015-01-28 17:15:26 +00:00
|
|
|
// integral factorial function
|
|
|
|
template <typename T>
|
|
|
|
T factorial(const T n)
|
|
|
|
{
|
|
|
|
static_assert(std::is_integral<T>::value,
|
|
|
|
"factorial must me used with an integral argument");
|
|
|
|
|
|
|
|
T res = n;
|
|
|
|
for (T i = n - 1; i != 0; --i)
|
|
|
|
{
|
|
|
|
res *= i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-04-05 20:56:21 +01:00
|
|
|
// convert variance matrix to correlation matrix
|
|
|
|
DMat varToCorr(const DMat &var);
|
|
|
|
|
2014-04-15 17:30:51 +01:00
|
|
|
// Constants
|
2017-07-09 14:14:36 +01:00
|
|
|
constexpr double pi = 3.1415926535897932384626433832795028841970;
|
|
|
|
constexpr double e = 2.7182818284590452353602874713526624977572;
|
|
|
|
constexpr double inf = std::numeric_limits<double>::infinity();
|
2019-12-12 18:04:57 +00:00
|
|
|
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
|
2014-04-15 17:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-30 19:28:30 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Standard C functions *
|
|
|
|
******************************************************************************/
|
|
|
|
#define STDMATH_NAMESPACE StdMath
|
|
|
|
|
2014-02-12 18:32:08 +00:00
|
|
|
#define DECL_STD_FUNC(name) \
|
2014-01-30 19:28:30 +00:00
|
|
|
namespace STDMATH_NAMESPACE\
|
|
|
|
{\
|
2014-02-12 18:32:08 +00:00
|
|
|
extern DoubleFunction name;\
|
2014-01-30 19:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trigonometric functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DECL_STD_FUNC(cos)
|
|
|
|
DECL_STD_FUNC(sin)
|
|
|
|
DECL_STD_FUNC(tan)
|
|
|
|
DECL_STD_FUNC(acos)
|
|
|
|
DECL_STD_FUNC(asin)
|
|
|
|
DECL_STD_FUNC(atan)
|
|
|
|
DECL_STD_FUNC(atan2)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Hyperbolic functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DECL_STD_FUNC(cosh)
|
|
|
|
DECL_STD_FUNC(sinh)
|
|
|
|
DECL_STD_FUNC(tanh)
|
2014-03-03 18:34:23 +00:00
|
|
|
DECL_STD_FUNC(acosh)
|
|
|
|
DECL_STD_FUNC(asinh)
|
|
|
|
DECL_STD_FUNC(atanh)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Exponential and logarithmic functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DECL_STD_FUNC(exp)
|
|
|
|
DECL_STD_FUNC(log)
|
2014-03-03 18:34:23 +00:00
|
|
|
DECL_STD_FUNC(log10)
|
|
|
|
DECL_STD_FUNC(exp2)
|
|
|
|
DECL_STD_FUNC(expm1)
|
|
|
|
DECL_STD_FUNC(log1p)
|
|
|
|
DECL_STD_FUNC(log2)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Power functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DECL_STD_FUNC(pow)
|
|
|
|
DECL_STD_FUNC(sqrt)
|
2014-03-03 18:34:23 +00:00
|
|
|
DECL_STD_FUNC(cbrt)
|
|
|
|
DECL_STD_FUNC(hypot)
|
|
|
|
|
|
|
|
// Error and gamma functions
|
|
|
|
DECL_STD_FUNC(erf)
|
|
|
|
DECL_STD_FUNC(erfc)
|
|
|
|
DECL_STD_FUNC(tgamma)
|
|
|
|
DECL_STD_FUNC(lgamma)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Rounding and remainder functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DECL_STD_FUNC(ceil)
|
|
|
|
DECL_STD_FUNC(floor)
|
|
|
|
DECL_STD_FUNC(fmod)
|
2014-03-03 18:34:23 +00:00
|
|
|
DECL_STD_FUNC(trunc)
|
|
|
|
DECL_STD_FUNC(round)
|
|
|
|
DECL_STD_FUNC(rint)
|
|
|
|
DECL_STD_FUNC(nearbyint)
|
|
|
|
DECL_STD_FUNC(remainder)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Minimum, maximum, difference functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DECL_STD_FUNC(fdim)
|
|
|
|
DECL_STD_FUNC(fmax)
|
|
|
|
DECL_STD_FUNC(fmin)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Absolute value
|
2014-02-12 18:32:08 +00:00
|
|
|
DECL_STD_FUNC(fabs)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2015-11-02 16:17:59 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Other functions *
|
|
|
|
******************************************************************************/
|
|
|
|
// p-value
|
|
|
|
namespace MATH_NAMESPACE
|
|
|
|
{
|
|
|
|
extern DoubleFunction chi2PValue;
|
2019-12-12 18:04:33 +00:00
|
|
|
extern DoubleFunction chi2Ccdf;
|
2015-11-02 16:17:59 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 17:15:05 +00:00
|
|
|
END_LATAN_NAMESPACE
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2014-02-06 18:52:13 +00:00
|
|
|
#endif // Latan_Math_hpp_
|