6 #ifndef THEORETICA_DATASET_H
7 #define THEORETICA_DATASET_H
9 #ifndef THEORETICA_NO_PRINT
28 template<
typename Vector, enable_vector<Vector> = true>
37 for(
unsigned int i = 1; i < X.size(); i++)
45 template<
typename Vector>
48 if(X.size() != Y.size() || !X.size()) {
53 auto res = X[0] * Y[0];
54 for(
unsigned int i = 1; i < X.size(); i++)
62 template<
typename Vector>
65 if(X.size() != Y.size() || !X.size()) {
66 TH_MATH_ERROR(
"product_sum_squares", X.size(), INVALID_ARGUMENT);
70 auto res = (X[0] * X[0]) * (Y[0] * Y[0]);
72 for(
unsigned int i = 1; i < X.size(); i++)
73 res += (X[i] * X[i]) * (Y[i] * Y[i]);
80 template<
typename Vector>
81 inline auto product_sum(
const Vector& X,
const Vector& Y,
const Vector& Z) {
83 if(X.size() != Y.size() || X.size() != Z.size() || !X.size()) {
88 auto res = X[0] * Y[0] * Z[0];
89 for(
unsigned int i = 1; i < X.size(); i++)
90 res += X[i] * Y[i] * Z[i];
97 template<
typename Vector>
100 if(X.size() != Y.size()) {
110 auto res = X[0] / Y[0];
111 for(
unsigned int i = 1; i < X.size(); i++) {
126 template<
typename Vector>
134 auto res = X[0] * X[0];
135 for(
unsigned int i = 1; i < X.size(); i++)
147 template<
typename Vector>
156 for (
unsigned int i = 0; i < X.size(); i++) {
162 ? ((
sum - temp) + X[i])
163 : ((X[i] - temp) +
sum);
181 template<
typename Vector>
183 const Vector& X,
size_t begin = 0,
184 size_t end = 0,
size_t base_size = 128) {
192 if((end - begin) <= base_size) {
194 for (
size_t i = begin; i < end; ++i)
200 const size_t m = (end - begin) / 2;
201 const size_t cutoff = begin + m;
217 std::enable_if_t<has_real_elements<Vector>::value> =
true
219 inline auto sum(
const Vector& X) {
227 template<
typename Vector>
228 inline auto sum(
const Vector& X) {
235 for(
unsigned int i = 1; i < X.size(); i++)
249 template<
typename Vector,
typename Function>
250 inline Vector&
apply(Function f, Vector& X) {
252 for (
unsigned int i = 0; i < X.size(); i++)
265 template<
typename Vector1,
typename Vector2 = Vector1,
typename Function>
266 inline Vector2&
map(Function f,
const Vector1& src, Vector2& dest) {
268 if(src.size() != dest.size()) {
270 dest = Vector2(
nan());
274 for (
unsigned int i = 0; i < src.size(); i++)
286 template<
typename Vector2,
typename Vector1,
typename Function>
287 inline Vector2
map(Function f,
const Vector1& X) {
290 res.resize(X.size());
292 for (
unsigned int i = 0; i < X.size(); i++)
304 template<
typename Vector,
typename Function>
305 inline Vector
map(Function f,
const Vector& X) {
306 return map<Vector, Vector, Function>(f, X);
311 template<
typename Vector1,
typename Vector2,
typename Vector3 = Vector1>
312 inline Vector3
concatenate(
const Vector1& v1,
const Vector2& v2) {
315 res.resize(v1.size() + v2.size());
316 const unsigned int offset = v1.size();
318 for (
unsigned int i = 0; i < offset; ++i)
321 for (
unsigned int i = 0; i < v2.size(); ++i)
322 res[i + offset] = v2[i];
329 template<
typename Vector>
330 inline auto max(
const Vector& X) {
341 for (
unsigned int i = 1; i < X.size(); ++i)
350 template<
typename Vector>
351 inline auto min(
const Vector& X) {
362 for (
unsigned int i = 1; i < X.size(); ++i)
374 template<
typename Dataset>
383 return sum(data) / (
real) data.size();
388 template<
typename Dataset>
398 for (
unsigned int i = 0; i < data.size(); ++i) {
405 res += 1.0 / data[i];
408 return static_cast<real>(data.size()) / res;
414 template<
typename Dataset>
422 template<
typename Dataset1,
typename Dataset2>
432 template<
typename Dataset>
436 TH_MATH_ERROR(
"quadratic_mean", data.size(), INVALID_ARGUMENT);
Mathematical constants and default algorithm parameters.
#define TH_CONSTIF
Enable constexpr in if statements if C++17 is supported.
Definition: constants.h:159
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
real weighted_mean(const Dataset1 &data, const Dataset2 &weights)
Compute the weighted mean of a set of values <data> and <weights> must have the same size.
Definition: dataset.h:423
real arithmetic_mean(const Dataset &data)
Compute the arithmetic mean of a set of values.
Definition: dataset.h:375
auto min(const Vector &X)
Finds the minimum value inside a dataset.
Definition: dataset.h:351
real harmonic_mean(const Dataset &data)
Compute the harmonic mean of a set of values.
Definition: dataset.h:389
real sum_compensated(const Vector &X)
Compute the sum of a set of values using the compensated Neumaier-Kahan-Babushka summation algorithm ...
Definition: dataset.h:148
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition: dual2_functions.h:54
Vector3 concatenate(const Vector1 &v1, const Vector2 &v2)
Concatenate two datasets to form a single one.
Definition: dataset.h:312
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:183
real sum_pairwise(const Vector &X, size_t begin=0, size_t end=0, size_t base_size=128)
Compute the sum of a set of values using pairwise summation to reduce round-off error.
Definition: dataset.h:182
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition: core_traits.h:134
auto sum(const Vector &X)
Compute the sum of a vector of real values using pairwise summation to reduce round-off error.
Definition: dataset.h:219
Vector & apply(Function f, Vector &X)
Apply a function to a set of values element-wise.
Definition: dataset.h:250
real geometric_mean(const Dataset &data)
Compute the geometric mean of a set of values as .
Definition: dataset.h:415
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition: dataset.h:330
auto product_sum(const Vector &X, const Vector &Y)
Sum the products of two sets of values.
Definition: dataset.h:46
auto product_sum_squares(const Vector &X, const Vector &Y)
Sum the products of the squares of two sets of data.
Definition: dataset.h:63
real quadratic_mean(const Dataset &data)
Compute the quadratic mean (Root Mean Square) of a set of values .
Definition: dataset.h:433
auto quotient_sum(const Vector &X, const Vector &Y)
Sum the quotients of two sets of values.
Definition: dataset.h:98
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
real root(real x, int n)
Compute the n-th root of x.
Definition: real_analysis.h:813
Vector2 & map(Function f, const Vector1 &src, Vector2 &dest)
Get a new vector obtained by applying the function element-wise.
Definition: dataset.h:266
auto product(const Vector &X)
Compute the product of a set of values.
Definition: dataset.h:29
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
auto sum_squares(const Vector &X)
Sum the squares of a set of values.
Definition: dataset.h:127
Type trait to check whether a type represents a real number.
Definition: core_traits.h:30