LOOS  v2.3.2
MatrixUtils.hpp
1 /*
2  MatrixUtils.hpp
3 
4  Matrix utility functions...
5 */
6 
7 /*
8  This file is part of LOOS.
9 
10  LOOS (Lightweight Object-Oriented Structure library)
11  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
12  Department of Biochemistry and Biophysics
13  School of Medicine & Dentistry, University of Rochester
14 
15  This package (LOOS) is free software: you can redistribute it and/or modify
16  it under the terms of the GNU General Public License as published by
17  the Free Software Foundation under version 3 of the License.
18 
19  This package is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27 
28 
29 #if !defined(LOOS_MATRIX_UTILS_HPP)
30 #define LOOS_MATRIX_UTILS_HPP
31 
32 #include <string>
33 #include <vector>
34 
35 #include <loos_defs.hpp>
36 
37 #include <Matrix.hpp>
38 
39 
40 
41 namespace loos {
42 
43  namespace Math {
44 
46 
50  template<class T1, class P1, template<typename> class S1,
51  class T2, class P2, template<typename> class S2>
53  uint m = M.rows();
54  uint n = M.cols();
55  Matrix<T1,P1,S1> B(m, n);
56 
57  for (uint j=0; j<m; ++j)
58  for (uint i=0; i<n; ++i)
59  B(j,i) = M(j, i);
60 
61  A = B;
62  }
63 
65  // matrix's sparseness...
72  template<class T1, class P1, class T2, class P2>
74  uint m = M.rows();
75  uint n = M.cols();
76  static T2 null_value;
78 
79  for (uint j=0; j<m; ++j)
80  for (uint i=0; i<n; ++i) {
81  T2 t = M(j, i);
82  if (t != null_value)
83  B(j, i) = t;
84  }
85  A = B;
86  }
87 
88  template<typename T, template<typename> class S>
89  Matrix<T, RowMajor, S> reinterpretOrder(const Matrix<T, ColMajor, S>& A) {
90  Matrix<T, RowMajor, S> result(A.rows(), A.cols());
91  result.set(A);
92  return(result);
93  };
94 
95  template<typename T, template<typename> class S>
96  Matrix<T, ColMajor, S> reinterpretOrder(const Matrix<T, RowMajor, S>& A) {
97  Matrix<T, ColMajor, S> result(A.rows(), A.cols());
98  result.set(A);
99  return(result);
100  };
101 
102 
103  // These are left as templated functions in case we need to
104  // specialize them in the future for performance reasons...
105 
107  template<typename T, class P, template<typename> class S>
108  std::vector<T> getRow(const Matrix<T,P,S>& M, const uint j) {
109  uint n = M.cols();
110  std::vector<T> row(n);
111  for (uint i=0; i<n; ++i)
112  row[i] = M(j,i);
113 
114  return(row);
115  }
116 
117 
119  template<typename T, class P, template<typename> class S>
120  std::vector<T> getCol(const Matrix<T,P,S>& M, const uint i) {
121  uint m = M.rows();
122  std::vector<T> col(m);
123  for (uint j=0; j<m; ++j)
124  col[j] = M(j,i);
125 
126  return(col);
127  }
128 
129  }
130 
131 }
132 
133 
134 #endif
135 
136 
Simple matrix template class using policy classes to determine behavior.
Definition: MatrixImpl.hpp:53
std::vector< T > getRow(const Matrix< T, P, S > &M, const uint j)
Extract a row from a matrix as a vector of T.
void copyMatrix(Matrix< T1, P1, S1 > &A, const Matrix< T2, P2, S2 > &M)
Copy one matrix into another, converting order/storage along the way.
Definition: MatrixUtils.hpp:52
Namespace for most things not already encapsulated within a class.
std::vector< T > getCol(const Matrix< T, P, S > &M, const uint i)
Extract a column from a matrix as a vector of T.