fml  0.1-0
Fused Matrix Library
cov.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_MPI_STATS_COV_H
6 #define FML_MPI_STATS_COV_H
7 #pragma once
8 
9 
10 #include <stdexcept>
11 
12 #include "../mpimat.hh"
13 #include "../dimops.hh"
14 
15 #include "../linalg/crossprod.hh"
16 #include "../linalg/matmult.hh"
17 
18 
19 namespace fml
20 {
21 namespace stats
22 {
38  template <typename REAL>
40  {
41  dimops::scale(true, false, x);
42 
43  const REAL alpha = 1. / ((REAL) (x.nrows()-1));
44  linalg::crossprod(alpha, x, cov);
45  }
46 
47 
48 
50  template <typename REAL>
52  {
53  if (x.nrows() != y.nrows())
54  throw std::runtime_error("non-conformable arguments");
55 
56  dimops::scale(true, false, x);
57  dimops::scale(true, false, y);
58 
59  const REAL alpha = 1. / ((REAL) (x.nrows()-1));
60  linalg::matmult(true, false, alpha, x, y, cov);
61  }
62 }
63 }
64 
65 
66 #endif
fml::linalg::crossprod
void crossprod(const REAL alpha, const cpumat< REAL > &x, cpumat< REAL > &ret)
Computes lower triangle of alpha*x^T*x.
Definition: crossprod.hh:37
fml::mpimat
Matrix class for data distributed over MPI in the 2-d block cyclic format.
Definition: mpimat.hh:40
fml::unimat::nrows
len_t nrows() const
Number of rows.
Definition: unimat.hh:36
fml::dimops::scale
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:376
fml
Core namespace.
Definition: dimops.hh:10
fml::stats::cov
void cov(cpumat< REAL > &x, cpumat< REAL > &cov)
Covariance.
Definition: cov.hh:43
fml::linalg::matmult
void matmult(const bool transx, const bool transy, const REAL alpha, const cpumat< REAL > &x, const cpumat< REAL > &y, cpumat< REAL > &ret)
Computes ret = alpha*op(x)*op(y) where op(A) is A or A^T.
Definition: matmult.hh:43