MPQC  3.0.0-alpha
matrix.hpp
1 #ifndef MPQC_MATRIX_HPP
2 #define MPQC_MATRIX_HPP
3 
4 #include "mpqc/range.hpp"
5 #include "math/scmat/matrix.h"
6 
7 #define EIGEN_DONT_PARALLELIZE
8 
9 #include <Eigen/Eigen>
10 // #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
11 // #include <Eigen/Sparse>
12 
13 namespace mpqc {
14 
17 
18 
20  template<typename T, int Order = Eigen::ColMajor>
21  struct matrix :
22  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Order>
23  {
24  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Order> EigenType;
25 
26  matrix() : EigenType() {}
27 
32  matrix(size_t m, size_t n) : EigenType(m,n) {}
33 
35  template<class A>
36  matrix(const Eigen::EigenBase<A> &a) : EigenType(a) {}
37 
40  this->resize(a.nrow(), a.ncol());
41  apply(assign(), this->rows(), this->cols(), *this, a);
42  }
43 
46  this->resize(a.n(), a.n());
47  apply(assign(), this->rows(), this->cols(), *this, a);
48  }
49 
54  struct Assignable {
55  virtual ~Assignable() {}
57  virtual void assign_to(matrix &m) const = 0;
58  };
59 
61  matrix(const Assignable &a) {
62  a.assign_to(*this);
63  }
64 
65 #ifdef DOXYGEN
66 
68  Type operator()(i, j);
69 
70 #else // DOXYGEN
71 
72  using EigenType::operator();
73 
75  template<typename T_, typename U_>
76  struct is_index : boost::mpl::and_<
77  boost::is_integral<T_>,
78  boost::is_integral<U_> > {};
79 
81  template<class Ri, class Rj>
82  typename boost::disable_if<
83  is_index<Ri, Rj>,
84  Eigen::Block<EigenType>
85  >::type
86  operator()(const Ri &i, const Rj &j) {
87  range ri = range_cast(i);
88  range rj = range_cast(j);
89  // printf("i=(%i,%i),j=(%i:%i)\n",
90  // *ri.begin(), *ri.end(),
91  // *rj.begin(), *rj.end());
92  return this->block(*ri.begin(), *rj.begin(), ri.size(), rj.size());
93  }
94 
95  template<class Ri, class Rj>
96  typename boost::disable_if<
97  is_index<Ri, Rj>,
98  Eigen::Block<const EigenType>
99  >::type
100  operator()(const Ri &i, const Rj &j) const {
101  range ri = range_cast(i);
102  range rj = range_cast(j);
103  // printf("i=(%i,%i),j=(%i:%i)\n",
104  // *i.begin(), *i.end(),
105  // *j.begin(), *j.end());
106  return this->block(*ri.begin(), *rj.begin(), ri.size(), rj.size());
107  }
108 
109 #endif // DOXYGEN
110 
111  void reshape(int m, int n);
112 
113  private:
114  template<class F, class A, class B>
115  static void apply(const F &f, size_t m, size_t n, A &a, B &b) {
116  for (int j = 0; j < n; ++j) {
117  for (int i = 0; i < m; ++i) {
118  f(a(i, j), b(i, j));
119  }
120  }
121  }
122  struct assign {
123  template<class A, class B>
124  void operator()(A &a, const B &b) const {
125  a = b;
126  }
127  };
128  };
129 
132  template<typename T>
133  struct vector: Eigen::Matrix<T, Eigen::Dynamic, 1> {
134 
136  typedef Eigen::Matrix<T, Eigen::Dynamic, 1> EigenType;
137 
141  explicit vector(size_t m = 0) : EigenType(m) {}
142 
144  template<class A>
145  vector(const Eigen::EigenBase<A> &a) : EigenType(a) {}
146 
148  vector(const T *begin, const T *end) {
149  EigenType::resize(end - begin, 1);
150  std::copy(begin, end, EigenType::data());
151  }
152 
154  using EigenType::operator();
155 
157  Eigen::Block<EigenType> operator()(range i) {
158  return this->block(*i.begin(), 0, i.size(), 1);
159  }
160 
162  Eigen::Block<const EigenType> operator()(range i) const {
163  return this->block(*i.begin(), 0, i.size(), 1);
164  }
165 
166  };
167 
168 
173 
174  // typedef Eigen::SparseMatrix<double> Sparse;
175 
178  template<class E>
179  double absmax(const E &e) {
180  return std::max(fabs(e.maxCoeff()), fabs(e.minCoeff()));
181  }
182 
185  template<class T>
186  T dot(const matrix<T> &a, const matrix<T> &b) {
187  T q = 0;
188  MPQC_ASSERT(a.cols() == b.cols());
189  for (size_t j = 0; j < a.cols(); ++j) {
190  q += a.col(j).dot(b.col(j));
191  }
192  return q;
193  }
194 
198  template<class T>
199  Eigen::SelfAdjointEigenSolver<Matrix::EigenType> symmetric(const matrix<T> &a) {
200  Eigen::SelfAdjointEigenSolver<Matrix::EigenType> es(a);
201  if (es.info() != Eigen::Success)
202  throw std::runtime_error("Eigen solver failed");
203  return es;
204  }
205 
208  template<class T>
209  T norm(const matrix<T> &a) {
210  return a.norm();
211  }
212 
215  template<class T>
216  void normalize(matrix<T> &a) {
217  a *= 1/a.norm();
218  }
219 
223  template<class T>
224  void orthonormalize(matrix<T> &d, const matrix<T> &b) {
225  T db = dot(d, b);
226  T bb = 1; //dot(b,b); // should be normalized already
227  d = d - (db/bb)*b;
228  normalize(d);
229  //d /= sqrt(d.norm());
230  }
231 
233 
234 } // namespace mpqc
235 
236 namespace sc {
237  using mpqc::Vector;
238  using mpqc::Matrix;
239  using mpqc::dot;
240 }
241 
242 #endif /* MPQC_MATRIX_HPP */
mpqc::absmax
double absmax(const E &e)
absolute max of an Eigen type
Definition: matrix.hpp:179
mpqc::orthonormalize
void orthonormalize(matrix< T > &d, const matrix< T > &b)
orthormalize matrix d wrt to normalized matrix b d = normalize(d - (<d|b>*b))
Definition: matrix.hpp:224
mpqc
Contains new MPQC code since version 3.
Definition: integralenginepool.hpp:37
mpqc::vector::vector
vector(size_t m=0)
Construct unititialized vector.
Definition: matrix.hpp:141
sc::RefSymmSCMatrix
The RefSymmSCMatrix class is a smart pointer to an SCSymmSCMatrix specialization.
Definition: matrix.h:265
mpqc::matrix::matrix
matrix(size_t m, size_t n)
Construct unititialized matrix.
Definition: matrix.hpp:32
mpqc::range
Definition: range.hpp:23
sc::RefSCMatrix
The RefSCMatrix class is a smart pointer to an SCMatrix specialization.
Definition: matrix.h:135
mpqc::matrix
Matrix class derived from Eigen::Matrix with additional MPQC integration.
Definition: matrix.hpp:21
mpqc::range_cast
range range_cast(const range &r)
Cast range to range, return argument unchanged.
Definition: range.hpp:123
mpqc::Matrix
matrix< double > Matrix
Convience double matrix type.
Definition: matrix.hpp:170
mpqc::matrix::Assignable
An interface to enable matrix assignment from other containers.
Definition: matrix.hpp:54
mpqc::matrix::matrix
matrix(const sc::RefSymmSCMatrix &a)
Construct full matrix from sc::RefSCMatrix matrix.
Definition: matrix.hpp:45
mpqc::norm
T norm(const matrix< T > &a)
Matrix norm.
Definition: matrix.hpp:209
mpqc::matrix::operator()
Type operator()(i, j)
Operators to access matrix element/block, see here.
mpqc::vector::vector
vector(const T *begin, const T *end)
Construct vector from iterator range.
Definition: matrix.hpp:148
mpqc::normalize
void normalize(matrix< T > &a)
Normalize matrix.
Definition: matrix.hpp:216
mpqc::matrix::Assignable::assign_to
virtual void assign_to(matrix &m) const =0
Assign data to matrix.
mpqc::vector::EigenType
Eigen::Matrix< T, Eigen::Dynamic, 1 > EigenType
Eigen base type.
Definition: matrix.hpp:136
mpqc::vector::operator()
Eigen::Block< const EigenType > operator()(range i) const
const range operator.
Definition: matrix.hpp:162
mpqc::dot
T dot(const matrix< T > &a, const matrix< T > &b)
element-wise dot product of two matrices
Definition: matrix.hpp:186
mpqc::matrix::matrix
matrix(const Eigen::EigenBase< A > &a)
Construct matrix from Eigen type.
Definition: matrix.hpp:36
mpqc::Vector
vector< double > Vector
Convience double vector type.
Definition: matrix.hpp:172
mpqc::vector
Vector class derived from Eigen::Matrix with additional MPQC integration.
Definition: matrix.hpp:133
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14
mpqc::vector::operator()
Eigen::Block< EigenType > operator()(range i)
range operator.
Definition: matrix.hpp:157
mpqc::matrix::matrix
matrix(const Assignable &a)
Constructs matrix from an Assignable container.
Definition: matrix.hpp:61
mpqc::symmetric
Eigen::SelfAdjointEigenSolver< Matrix::EigenType > symmetric(const matrix< T > &a)
Computes (Eigen::SelfAdjointEigenSolver) eigensystem of a matrix.
Definition: matrix.hpp:199
mpqc::vector::vector
vector(const Eigen::EigenBase< A > &a)
Construct vector from Eigen type.
Definition: matrix.hpp:145
mpqc::matrix::matrix
matrix(sc::RefSCMatrix a)
Construct matrix from sc::RefSCMatrix matrix.
Definition: matrix.hpp:39

Generated at Sun Jan 26 2020 23:24:01 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.