fml  0.1-0
Fused Matrix Library
stats.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML_CPU_STATS_H
6 #define FML_CPU_STATS_H
7 #pragma once
8 
9 
10 #include "dimops.hh"
11 #include "linalg.hh"
12 
13 
15 namespace stats
16 {
35  template <typename REAL>
36  void pca(const bool rm_mean, const bool rm_sd, cpumat<REAL> &x, cpuvec<REAL> &sdev, cpumat<REAL> &rot)
37  {
38  dimops::scale(rm_mean, rm_sd, x);
39 
40  cpumat<REAL> u;
41  cpumat<REAL> trot;
42  linalg::svd(x, sdev, u, trot);
43 
44  const REAL d = 1.0 / sqrt((REAL)std::max(x.nrows()-1, 1));
45  REAL *sdev_d = sdev.data_ptr();
46 
47  #pragma omp for simd
48  for (len_t i=0; i<sdev.size(); i++)
49  sdev_d[i] *= d;
50 
51  linalg::xpose(trot, rot);
52  }
53 }
54 
55 
56 #endif
Vector class for data held on a single CPU.
Definition: cpuvec.hh:27
T * data_ptr()
Pointer to the internal array.
Definition: univec.hh:26
len_t nrows() const
Number of rows.
Definition: unimat.hh:31
void scale(const bool rm_mean, const bool rm_sd, cpumat< REAL > &x)
Remove the mean and/or the sd from a matrix.
Definition: dimops.hh:232
len_t size() const
Number of elements in the vector.
Definition: univec.hh:24
void svd(cpumat< REAL > &x, cpuvec< REAL > &s)
Computes the singular value decomposition.
Definition: linalg.hh:457
Matrix class for data held on a single CPU.
Definition: cpumat.hh:32
Statistics kernels.
Definition: stats.hh:15
void pca(const bool rm_mean, const bool rm_sd, cpumat< REAL > &x, cpuvec< REAL > &sdev, cpumat< REAL > &rot)
Principal components analysis.
Definition: stats.hh:36
void xpose(const cpumat< REAL > &x, cpumat< REAL > &tx)
Computes the transpose out-of-place (i.e. in a copy).
Definition: linalg.hh:281