LOOS  v2.3.2
MatrixImpl.hpp
1 /*
2  Matrix.hpp
3 
4  A simple matrix wrapper class. This is not meant to be a mathematical matrix class!
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_IMPL_HPP)
30 #define LOOS_MATRIX_IMPL_HPP
31 
32 #include <iostream>
33 #include <ostream>
34 #include <string>
35 
36 #include <loos_defs.hpp>
37 
38 #include <boost/format.hpp>
39 
40 
41 #include <MatrixOrder.hpp> // Order (layout) policies
42 #include <MatrixStorage.hpp> // Storage (physical) policies
43 
44 namespace loos {
45 
46  namespace Math {
47 
49  typedef std::pair<uint,uint> Range;
50 
51  // Forward declarations...
52  template <typename T, class P, template<typename> class S>
53  class Matrix;
54 
55  // Forward declarations (required for friend status) of conversion
56  // functions...
57  template<typename T, template<typename> class S>
59 
60  template<typename T, template<typename> class S>
62 
63 
64 
66 
147  template<typename T, class OrderPolicy = ColMajor, template<typename> class StoragePolicy = SharedArray>
148  class Matrix : public OrderPolicy, public StoragePolicy<T> {
149  public:
150 
151  typedef T element_type;
152 
154  Matrix() : meta("") { }
155 
157 
160  Matrix(T* p, const uint b, const uint a) : OrderPolicy(b, a),
161  StoragePolicy<T>(p, OrderPolicy::size()),
162  meta("") { }
163 
165  Matrix(const uint b, const uint a) : OrderPolicy(b, a),
166  StoragePolicy<T>(OrderPolicy::size()),
167  meta("") { }
168 
171  Matrix<T,OrderPolicy,StoragePolicy> M(OrderPolicy::m, OrderPolicy::n);
172  M.copyData(*this);
173 
174  return(M);
175  }
176 
177  uint rows(void) const { return(OrderPolicy::m); }
178  uint cols(void) const { return(OrderPolicy::n); }
179 
181  T& operator()(const uint y, const uint x) {
182  ulong i = OrderPolicy::index(y,x);
183  return(StoragePolicy<T>::operator[](i));
184  }
185 
186  const T& operator()(const uint y, const uint x) const {
187  ulong i = OrderPolicy::index(y,x);
188  return(StoragePolicy<T>::operator[](i));
189  }
190 
191  void metaData(const std::string& s) { meta = s; }
192  std::string metaData(void) const { return(meta); }
193 
195  void reset(void) { OrderPolicy::setSize(0,0); StoragePolicy<T>::reset(); }
196 
197 
200 
203 
204  private:
205  std::string meta;
206 
207  };
208 
209 
210  template<typename T, class P, template<typename> class S>
211  std::ostream& operator<<(std::ostream& os, const Matrix<T,P,S>& M) {
212 
213  uint m = M.rows();
214  uint n = M.cols();
215  os << boost::format("# %d %d (0)\n") % m % n;
216  for (uint j=0; j<m; ++j) {
217  for (uint i=0; i<n; ++i)
218  os << M(j, i) << " ";
219  os << std::endl;
220  }
221  return(os);
222  }
223 
224  }
225 
226 
227 }
228 
229 #endif
Simple matrix template class using policy classes to determine behavior.
Definition: MatrixImpl.hpp:53
Matrix(const uint b, const uint a)
Create a new block of data for the requested Matrix.
Definition: MatrixImpl.hpp:165
Matrix()
Unitialized matrix.
Definition: MatrixImpl.hpp:154
Storage policy for a block of memory wrapped in a boost::shared_array pointer.
Matrix(T *p, const uint b, const uint a)
Wrap an existing block of data with a Matrix.
Definition: MatrixImpl.hpp:160
std::pair< uint, uint > Range
Specify a range for columns/rows [first,second)
Definition: MatrixImpl.hpp:49
friend Matrix< T, RowMajor, StoragePolicy > reinterpretOrder(const Matrix< T, ColMajor, StoragePolicy > &)
Convert a Col-major to Row-major format.
T & operator()(const uint y, const uint x)
Return the appropriate element (y-rows, x-cols)
Definition: MatrixImpl.hpp:181
Namespace for most things not already encapsulated within a class.
Matrix< T, OrderPolicy, StoragePolicy > copy(void) const
Deep copy...
Definition: MatrixImpl.hpp:170
void reset(void)
Deallocate data...
Definition: MatrixImpl.hpp:195