eigen.hpp
1 // Copyright © 2020 Thomas Nagler
2 //
3 // This file is part of the wdm library and licensed under the terms of
4 // the MIT license. For a copy, see the LICENSE file in the root directory
5 // or https://github.com/tnagler/wdmcpp/blob/master/LICENSE.
6 
7 #pragma once
8 
9 #include <Eigen/Dense>
10 #include "../wdm.hpp"
11 
12 
13 namespace wdm {
14 
15 namespace utils {
16 
17  inline std::vector<double> convert_vec(const Eigen::VectorXd& x)
18  {
19  std::vector<double> xx(x.size());
20  if (x.size() > 0)
21  Eigen::VectorXd::Map(&xx[0], x.size()) = x;
22  return xx;
23  }
24 
25 }
26 
42 inline double wdm(const Eigen::VectorXd& x,
43  const Eigen::VectorXd& y,
44  std::string method,
45  Eigen::VectorXd weights = Eigen::VectorXd(),
46  bool remove_missing = true)
47 {
48  return wdm(utils::convert_vec(x),
49  utils::convert_vec(y),
50  method,
51  utils::convert_vec(weights),
52  remove_missing);
53 }
54 
70 inline Eigen::MatrixXd wdm(const Eigen::MatrixXd& x,
71  std::string method,
72  Eigen::VectorXd weights = Eigen::VectorXd(),
73  bool remove_missing = true)
74 {
75  size_t d = x.cols();
76  if (d == 1)
77  throw std::runtime_error("x must have at least 2 columns.");
78 
79  Eigen::MatrixXd ms = Eigen::MatrixXd::Identity(d, d);
80  for (size_t i = 0; i < d; i++) {
81  for (size_t j = i + 1; j < d; j++) {
82  ms(i, j) = wdm(utils::convert_vec(x.col(i)),
83  utils::convert_vec(x.col(j)),
84  method,
85  utils::convert_vec(weights),
86  remove_missing);
87  ms(j, i) = ms(i, j);
88  }
89  }
90 
91  return ms;
92 }
93 
94 }
double wdm(const Eigen::VectorXd &x, const Eigen::VectorXd &y, std::string method, Eigen::VectorXd weights=Eigen::VectorXd(), bool remove_missing=true)
Definition: eigen.hpp:42
Weighted dependence measures.
Definition: bbeta.hpp:11