Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 124 lines (109 sloc) 3.499 kb
ef815c9 @moritz move math functions and definitons to math-utils.ht
authored
1 #ifndef __MATH_UTILS_H
2 #define __MATH_UTILS_H
3
4 #include <complex>
5
6 #include <Eigen/Core>
7 #include <Eigen/Sparse>
8
9 #include <boost/numeric/ublas/matrix.hpp>
10 #include <boost/numeric/ublas/matrix_sparse.hpp>
11 #include <boost/numeric/ublas/io.hpp>
12
843eeaf the MUMPS backend actually runs now.
Moritz Lenz authored
13 // for bzero
14 #include <strings.h>
d8f460c rip out useless, non-working MUMPS stuff
Moritz Lenz authored
15 // for debugging
16 #include <iostream>
17 using std::cout;
18 using std::endl;
843eeaf the MUMPS backend actually runs now.
Moritz Lenz authored
19
ef815c9 @moritz move math functions and definitons to math-utils.ht
authored
20 namespace ub = boost::numeric::ublas;
21
22 typedef double num;
23 typedef std::complex<num> cnum;
24 typedef ub::matrix<cnum> cmatrix;
25 typedef ub::compressed_matrix<cnum, ub::row_major> sparse_cm;
26 typedef Eigen::SparseMatrix< cnum , Eigen::RowMajor> esm;
27 typedef Eigen::RandomSetter< esm > ers;
28 typedef Eigen::SparseLU<esm, Eigen::SuperLU> eslu;
29
b0b8f3d @moritz more comments for math-utils.h
authored
30 // calculate the (dense) inverse of a sparse, complex matrix
ef815c9 @moritz move math functions and definitons to math-utils.ht
authored
31 void sparse_inverse(const esm &m, cmatrix &inv) {
32 assert(m.rows() == m.cols());
33 int size = m.rows();
34 eslu slu(m);
35 Eigen::VectorXcd base(size), invCol(size);
36 for (int i=0; i<size; ++i) {
37 base = Eigen::VectorXcd::Unit(size, i);
38 slu.solve(base, &invCol);
39 for (int j=0; j<size; ++j) {
40 inv(j, i) = invCol[j];
41 }
42 }
43 }
44
b0b8f3d @moritz more comments for math-utils.h
authored
45 // convert a sparse boost::numeric::ublas to an Eigen::SparseMatrix
ef815c9 @moritz move math functions and definitons to math-utils.ht
authored
46 void ublas_to_eigen(const sparse_cm &m, esm &result) {
47 result.setZero();
48 ers setter(result);
49 sparse_cm::const_iterator1 x = m.begin1();
50 sparse_cm::const_iterator1 x_end = m.end1();
51 for (; x != x_end; ++x) {
52 sparse_cm::const_iterator2 y = x.begin();
53 sparse_cm::const_iterator2 y_end = x.end();
54 for (; y != y_end; y++) {
55 setter(y.index1(), y.index2()) = *y;
56 }
57 }
58 return;
59 }
60
b0b8f3d @moritz more comments for math-utils.h
authored
61 // convert an Eigen::SparseMatrix to a boost::numeric::ublas matrix
ef815c9 @moritz move math functions and definitons to math-utils.ht
authored
62 void eigen_to_ublas(const esm &m, sparse_cm &result) {
63 result.clear();
64 for (int k=0; k<m.outerSize(); ++k) {
65 for (esm::InnerIterator it(m,k); it; ++it) {
66 result(it.row(), it.col()) = it.value();
67 }
68 }
69 }
70
b0b8f3d @moritz more comments for math-utils.h
authored
71 // given a sparse LU decompostion slu of a matrix A and a right-hand side
72 // rhs, solve the equation A * x = rhs or A^\dagger * x = rhs (if the
73 // `adjoint' flag is true
ef815c9 @moritz move math functions and definitons to math-utils.ht
authored
74 void pseudo_sparse_solve(const eslu * const slu,
75 const esm &rhs,
76 esm &result,
77 const bool adjoint = false) {
78 assert( rhs.cols() == rhs.rows() );
79 int n = rhs.cols();
80
81 ers setter(result);
82 Eigen::VectorXcd *invCol = new Eigen::VectorXcd(n);
83 int transpose_flag;
84 if (adjoint) {
85 transpose_flag = Eigen::SvAdjoint;
86 } else {
87 transpose_flag = Eigen::SvNoTrans;
88 }
89 for (int k=0; k<rhs.outerSize(); ++k) {
90 Eigen::VectorXcd base(n);
91 int i = 0;
92 for (esm::InnerIterator it(rhs,k); it; ++it) {
93 base(it.col()) = it.value();
94 i++;
95 }
96 if (i != 0) {
97 slu->solve(base, invCol, transpose_flag);
98 for (int j = 0; j < n; j++) {
99 if (abs((*invCol)[j]) > 1e-18) {
100 setter(j, k) = (*invCol)[j];
101 }
102 }
103 }
104 }
105 delete invCol;
106 }
107
21f421a @moritz move r_prod_trace to math-utils.h
authored
108 // real(trace(a * b))
109 num r_prod_trace(const esm &a, const esm &b) {
110 num sum = 0.0;
111 cnum null = cnum(0, 0);
112 for (int k = 0; k < a.outerSize(); ++k) {
113 for (esm::InnerIterator it(a,k); it; ++it) {
114 cnum y = b.coeff(it.col(), it.row());
115 sum += real(it.value() * y);
116 }
117 }
118
119 return sum;
120 }
ef815c9 @moritz move math functions and definitons to math-utils.ht
authored
121
122 #endif /* __MATH_UTILS_H */
843eeaf the MUMPS backend actually runs now.
Moritz Lenz authored
123 // vim: ts=4 sw=4 expandtab
Something went wrong with that request. Please try again.