Kstars

tools/math_tools.h
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: 2014-2017 Max Planck Society.
3  All rights reserved.
4 
5  SPDX-License-Identifier: BSD-3-Clause
6 */
7 
8 /**
9  * @file
10  * @date 2014-2017
11  * @copyright Max Planck Society
12  *
13  * @author Edgar D. Klenske <[email protected]>
14  * @author Stephan Wenninger <[email protected]>
15  * @author Raffi Enficiaud <[email protected]>
16  *
17  * @brief Provides mathematical tools needed for the Gaussian process toolbox.
18  */
19 
20 #ifndef GP_MATH_TOOLS_H
21 #define GP_MATH_TOOLS_H
22 
23 #define MINIMAL_THETA 1e-7
24 
25 #include <Eigen/Dense>
26 #include <unsupported/Eigen/FFT>
27 #include <limits>
28 #include <vector>
29 #include <stdexcept>
30 #include <cstdint>
31 #include <cmath>
32 
33 // M_PI not part of the standard
34 #ifndef M_PI
35 #define M_PI 3.14159265358979323846
36 #endif
37 
38 namespace math_tools
39 {
40  /*!
41  * The squareDistance is the pairwise distance between all points of the passed
42  * vectors. I.e. it generates a symmetric matrix when two vectors are passed.
43  *
44  * The first dimension (rows) is the dimensionality of the input space, the
45  * second dimension (columns) is the number of datapoints. The number of
46  * rows must be identical.
47 
48  @param a
49  a Matrix of size Dxn
50 
51  @param b
52  a Matrix of size Dxm
53 
54  @result
55  a Matrix of size nxm containing all pairwise squared distances
56  */
57  Eigen::MatrixXd squareDistance(
58  const Eigen::MatrixXd& a,
59  const Eigen::MatrixXd& b);
60 
61  /*!
62  * Single-input version of squaredDistance. For single inputs, it is assumed
63  * that the pairwise distance matrix should be computed between the passed
64  * vector itself.
65  */
66  Eigen::MatrixXd squareDistance(const Eigen::MatrixXd& a);
67 
68  /*!
69  * Generates a uniformly distributed random matrix of values between 0 and 1.
70  */
71  Eigen::MatrixXd generate_uniform_random_matrix_0_1(const size_t n, const size_t m);
72 
73  /*!
74  * Apply the Box-Muller transform, which transforms uniform random samples
75  * to Gaussian distributed random samples.
76  */
77  Eigen::MatrixXd box_muller(const Eigen::VectorXd& vRand);
78 
79  /*!
80  * Generates normal random samples. First it gets some uniform random samples
81  * and then uses the Box-Muller transform to get normal samples out of it
82  */
83  Eigen::MatrixXd generate_normal_random_matrix(const size_t n, const size_t m);
84 
85  /*!
86  * Checks if a value is NaN by comparing it with itself.
87  *
88  * The rationale is that NaN is the only value that does not evaluate to true
89  * when comparing it with itself.
90  * Taken from:
91  * http://stackoverflow.com/questions/3437085/check-nan-number
92  * http://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c
93  */
94  inline bool isNaN(double x)
95  {
96  return x != x;
97  }
98 
99  static const double NaN = std::numeric_limits<double>::quiet_NaN();
100 
101  /*!
102  * Checks if a double is infinite via std::numeric_limits<double> functions.
103  *
104  * Taken from:
105  * http://bytes.com/topic/c/answers/588254-how-check-double-inf-nan#post2309761
106  */
107  inline bool isInf(double x)
108  {
109  return x == std::numeric_limits<double>::infinity() ||
110  x == -std::numeric_limits<double>::infinity();
111  }
112 
113  /*!
114  * Calculates the spectrum of a data vector.
115  *
116  * Does pre-and postprocessing:
117  * - The data is zero-padded until the desired resolution is reached.
118  * - ditfft2 is called to compute the FFT.
119  * - The frequencies from the padding are removed.
120  * - The constant coefficient is removed.
121  * - A list of frequencies is generated.
122  */
123  std::pair< Eigen::VectorXd, Eigen::VectorXd > compute_spectrum(Eigen::VectorXd& data, int N = 0);
124 
125  /*!
126  * Computes a Hamming window (used to reduce spectral leakage of subsequent DFT).
127  */
128  Eigen::VectorXd hamming_window(int N);
129 
130  /*!
131  * Computes the standard deviation of a vector... which is not part of Eigen.
132  */
133  double stdandard_deviation(Eigen::VectorXd& input);
134 
135 } // namespace math_tools
136 
137 #endif // define GP_MATH_TOOLS_H
double stdandard_deviation(Eigen::VectorXd &input)
Eigen::MatrixXd squareDistance(const Eigen::MatrixXd &a, const Eigen::MatrixXd &b)
std::pair< Eigen::VectorXd, Eigen::VectorXd > compute_spectrum(Eigen::VectorXd &data, int N)
Eigen::MatrixXd box_muller(const Eigen::VectorXd &vRand)
Eigen::VectorXd hamming_window(int N)
Eigen::MatrixXd generate_normal_random_matrix(const size_t n, const size_t m)
Eigen::MatrixXd generate_uniform_random_matrix_0_1(const size_t n, const size_t m)
bool isNaN(double x)
bool isInf(double x)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Oct 1 2023 04:02:41 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.