Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
matrix_operation.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
3/*********************************************************************** * This program is free software; you can
4 *redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free
5 *Software Foundation; either version 2 of the License, or * (at your option) any later version. *
6 * *
7 ***************************************************************************/
8
9#ifndef __MATRIX_OPERATIONS_H_INCLUDED__
10#define __MATRIX_OPERATIONS_H_INCLUDED__
11
12#include <algorithm>
13#include <boost/numeric/ublas/io.hpp>
14#include <boost/numeric/ublas/matrix_sparse.hpp>
15#include <boost/numeric/ublas/operation.hpp>
16#include <boost/numeric/ublas/vector.hpp>
17
18namespace quetzal
19{
20
21namespace utils
22{
23
24template <typename matrix_type> auto divide_terms_by_row_sum(matrix_type const &A)
25{
26 using namespace boost::numeric::ublas;
27 auto v = prod(scalar_vector<double>(A.size2(), 1.0), trans(A));
28 vector<double> w(v.size());
29 for (unsigned int i = 0; i < v.size(); ++i)
30 {
31 double a = v(i);
32 assert(a > 0);
33 w(i) = 1.0 / a;
34 }
35 compressed_matrix<double> S(w.size(), w.size(), 0.0);
36 assert(S.size2() == A.size1());
37 for (unsigned int i = 0; i < v.size(); ++i)
38 {
39 S(i, i) = w(i);
40 }
41 matrix_type M(A.size1(), A.size2());
42 axpy_prod(S, A, M);
43 return M;
44}
45
46} // namespace utils
47} // namespace quetzal
48
49#endif
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21