Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mathtools.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Various mathematical tools
22 //=============================================================================
23 
24 
25 #ifndef MATHTOOLS_H
26 #define MATHTOOLS_H
27 
28 #include <vector>
29 #include <cmath>
30 #include <functional>
31 #include <algorithm>
32 #include <limits>
33 #include "../system/prerequisites.h"
34 
35 namespace MathTools {
36 
37 const double PI = 3.14159265358979323846;
38 const double TWO_PI = 2 * PI;
39 const double LOG2 = 0.69314718055994530942;
40 
42 template<class T>
43 int roundToInteger (T x) { return static_cast<int>(lround(x)); }
44 
46 double computeMoment (const std::vector<double> &v, const int n);
47 
49 void normalize (std::vector<double> &vec);
50 
52 double computeNorm (std::vector<double> &vec);
53 
56 void coarseGrainSpectrum (const std::vector<double> &X,
57  std::vector<double> &Y,
58  std::function<double(double y)> f,
59  double exponent=0);
60 
62 double computeEntropy (const std::vector<double> &v);
63 
65 double computeRenyiEntropy (const std::vector<double> &v, const double q);
66 
68 int findMaximum (const std::vector<double> &X, int i, int j);
69 int findMaximum (const std::vector<double> &X);
70 
72 double findSmoothedMaximum (const std::vector<double> &x);
73 
75 double weightedArithmetricMean(const std::vector<double> &Y, size_t start = 0, size_t end = std::numeric_limits<size_t>::max());
76 
78 double restrictToInterval (double x, double xmin, double xmax);
79 
81 template <typename T>
82 void transformVector (const std::vector<T> &v, std::vector<T> &w,
83  std::function<T(T)> f)
84 { w.resize(v.size()); for (size_t i=0; i<v.size(); i++) w[i]=f(v[i]); }
85 
86 template <typename T>
87 std::vector<T> transformVector (const std::vector<T> &v, std::function<T(T)> f)
88 { std::vector<T> w(v.size()); for (size_t i=0; i<v.size(); i++) w[i]=f(v[i]); return w; }
89 
90 } // MathTools
91 
92 #endif // MATHTOOLS_H
double restrictToInterval(double x, double xmin, double xmax)
Restrict floating point value to an interval.
Definition: mathtools.cpp:196
double computeMoment(const std::vector< double > &v, const int n)
Determine the n-th moment of a distribution stored in a vector.
Definition: mathtools.cpp:38
const double LOG2
Definition: mathtools.h:39
double computeRenyiEntropy(const std::vector< double > &v, const double q)
Compute the Renyi entropy of a normalized probability distribution.
Definition: mathtools.cpp:69
double weightedArithmetricMean(const std::vector< double > &Y, size_t start=0, size_t end=std::numeric_limits< size_t >::max())
Computes the weighted arithmetric mean index of the given Y data.
Definition: mathtools.cpp:178
int roundToInteger(T x)
Round a floating point number to an integer.
Definition: mathtools.h:43
void transformVector(const std::vector< T > &v, std::vector< T > &w, std::function< T(T)> f)
Map a vector to a different one by a unary map.
Definition: mathtools.h:82
void coarseGrainSpectrum(const std::vector< double > &X, std::vector< double > &Y, std::function< double(double y)> f, double exponent=0)
Definition: mathtools.cpp:120
int findMaximum(const std::vector< double > &X, int i, int j)
Find the component where the vector has its maximum.
Definition: mathtools.cpp:151
const double PI
Definition: mathtools.h:37
double findSmoothedMaximum(const std::vector< double > &x)
Use a parabola to fit the maximum.
Definition: mathtools.cpp:164
double computeEntropy(const std::vector< double > &v)
Compute the Shannon entropy of a normalized probability distribution.
Definition: mathtools.cpp:56
void normalize(std::vector< double > &vec)
Normalize a distribution stored in a vector.
Definition: mathtools.cpp:92
const double TWO_PI
Definition: mathtools.h:38
double computeNorm(std::vector< double > &vec)
Compute the norm of a vector.
Definition: mathtools.cpp:82