2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* Math.cpp, 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/Math.hpp>
|
|
|
|
#include <LatAnalyze/includes.hpp>
|
2015-11-02 16:17:59 +00:00
|
|
|
#include <gsl/gsl_cdf.h>
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
2016-04-05 20:56:21 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Custom math functions *
|
|
|
|
******************************************************************************/
|
|
|
|
DMat MATH_NAMESPACE::varToCorr(const DMat &var)
|
|
|
|
{
|
|
|
|
DMat res = var, invDiag = res.diagonal();
|
|
|
|
|
|
|
|
invDiag = invDiag.cwiseInverse().cwiseSqrt();
|
|
|
|
res = (invDiag*invDiag.transpose()).cwiseProduct(res);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-01-30 19:28:30 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Standard C functions *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-02-12 18:32:08 +00:00
|
|
|
#define DEF_STD_FUNC_1ARG(name) \
|
2014-03-03 12:42:27 +00:00
|
|
|
auto name##VecFunc = [](const double arg[1]){return (name)(arg[0]);};\
|
2015-02-24 17:00:19 +00:00
|
|
|
DoubleFunction STDMATH_NAMESPACE::name(name##VecFunc, 1);
|
2014-01-30 19:28:30 +00:00
|
|
|
|
2014-02-12 18:32:08 +00:00
|
|
|
#define DEF_STD_FUNC_2ARG(name) \
|
2014-03-03 12:42:27 +00:00
|
|
|
auto name##VecFunc = [](const double arg[2]){return (name)(arg[0], arg[1]);};\
|
2015-02-24 17:00:19 +00:00
|
|
|
DoubleFunction STDMATH_NAMESPACE::name(name##VecFunc, 2);
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Trigonometric functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DEF_STD_FUNC_1ARG(cos)
|
|
|
|
DEF_STD_FUNC_1ARG(sin)
|
|
|
|
DEF_STD_FUNC_1ARG(tan)
|
|
|
|
DEF_STD_FUNC_1ARG(acos)
|
|
|
|
DEF_STD_FUNC_1ARG(asin)
|
|
|
|
DEF_STD_FUNC_1ARG(atan)
|
|
|
|
DEF_STD_FUNC_2ARG(atan2)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Hyperbolic functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DEF_STD_FUNC_1ARG(cosh)
|
|
|
|
DEF_STD_FUNC_1ARG(sinh)
|
|
|
|
DEF_STD_FUNC_1ARG(tanh)
|
2014-03-03 18:34:23 +00:00
|
|
|
DEF_STD_FUNC_1ARG(acosh)
|
|
|
|
DEF_STD_FUNC_1ARG(asinh)
|
|
|
|
DEF_STD_FUNC_1ARG(atanh)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Exponential and logarithmic functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DEF_STD_FUNC_1ARG(exp)
|
|
|
|
DEF_STD_FUNC_1ARG(log)
|
2014-03-03 18:34:23 +00:00
|
|
|
DEF_STD_FUNC_1ARG(log10)
|
|
|
|
DEF_STD_FUNC_1ARG(exp2)
|
|
|
|
DEF_STD_FUNC_1ARG(expm1)
|
|
|
|
DEF_STD_FUNC_1ARG(log1p)
|
|
|
|
DEF_STD_FUNC_1ARG(log2)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Power functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DEF_STD_FUNC_2ARG(pow)
|
|
|
|
DEF_STD_FUNC_1ARG(sqrt)
|
2014-03-03 18:34:23 +00:00
|
|
|
DEF_STD_FUNC_1ARG(cbrt)
|
|
|
|
DEF_STD_FUNC_2ARG(hypot)
|
|
|
|
|
|
|
|
// Error and gamma functions
|
|
|
|
DEF_STD_FUNC_1ARG(erf)
|
|
|
|
DEF_STD_FUNC_1ARG(erfc)
|
|
|
|
DEF_STD_FUNC_1ARG(tgamma)
|
|
|
|
DEF_STD_FUNC_1ARG(lgamma)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Rounding and remainder functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DEF_STD_FUNC_1ARG(ceil)
|
|
|
|
DEF_STD_FUNC_1ARG(floor)
|
|
|
|
DEF_STD_FUNC_2ARG(fmod)
|
2014-03-03 18:34:23 +00:00
|
|
|
DEF_STD_FUNC_1ARG(trunc)
|
|
|
|
DEF_STD_FUNC_1ARG(round)
|
|
|
|
DEF_STD_FUNC_1ARG(rint)
|
|
|
|
DEF_STD_FUNC_1ARG(nearbyint)
|
|
|
|
DEF_STD_FUNC_2ARG(remainder)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Minimum, maximum, difference functions
|
2014-02-12 18:32:08 +00:00
|
|
|
DEF_STD_FUNC_2ARG(fdim)
|
|
|
|
DEF_STD_FUNC_2ARG(fmax)
|
|
|
|
DEF_STD_FUNC_2ARG(fmin)
|
2014-01-30 19:28:30 +00:00
|
|
|
|
|
|
|
// Absolute value
|
2014-02-12 18:32:08 +00:00
|
|
|
DEF_STD_FUNC_1ARG(fabs)
|
2015-11-02 16:17:59 +00:00
|
|
|
|
|
|
|
// p-value
|
|
|
|
auto chi2PValueVecFunc = [](const double arg[2])
|
|
|
|
{
|
|
|
|
return gsl_cdf_chisq_Q(arg[0], arg[1]);
|
|
|
|
};
|
|
|
|
|
|
|
|
DoubleFunction MATH_NAMESPACE::chi2PValue(chi2PValueVecFunc, 2);
|