kmatrix_impl.hpp
Go to the documentation of this file.
1 // This file is part of kfilter.
2 // kfilter is a C++ variable-dimension extended kalman filter library.
3 //
4 // Copyright (C) 2004 Vincent Zalzal, Sylvain Marleau
5 // Copyright (C) 2001, 2004 Richard Gourdeau
6 // Copyright (C) 2004 GRPR and DGE's Automation sector
7 // �cole Polytechnique de Montr�al
8 //
9 // Code adapted from algorithms presented in :
10 // Bierman, G. J. "Factorization Methods for Discrete Sequential
11 // Estimation", Academic Press, 1977.
12 //
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2.1 of the License, or (at your option) any later version.
17 //
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 
27 #ifndef KMATRIX_IMPL_HPP
28 #define KMATRIX_IMPL_HPP
29 
32 
33 #include KSSTREAM_HEADER
34 
35 namespace Kalman {
36 
38 
46  public:
47 
49 
57  KMatrixContextImpl(std::string elemDelim = " ",
58  std::string rowDelim = "\n",
59  std::string startDelim = std::string(),
60  std::string endDelim = std::string(),
61  unsigned prec = 4)
62  : elemDelim_(elemDelim), rowDelim_(rowDelim), startDelim_(startDelim),
63  endDelim_(endDelim), precision_(prec), width_(8+prec) {
64 
65  std::string ws(" \t\n");
67  ( elemDelim_.find_first_not_of(ws) != std::string::npos);
68  skipRowDelim_ =
69  ( rowDelim_.find_first_not_of(ws) != std::string::npos);
71  (startDelim_.find_first_not_of(ws) != std::string::npos);
72  skipEndDelim_ =
73  ( endDelim_.find_first_not_of(ws) != std::string::npos);
74  }
75 
76  std::string elemDelim_;
77  std::string rowDelim_;
78  std::string startDelim_;
79  std::string endDelim_;
80  unsigned precision_;
81  unsigned width_;
86  };
87 
89 
93 
103  template<typename T, K_UINT_32 BEG, bool DBG>
105 
106  if (m != 0 && n != 0) {
107 
108  // non-empty matrix
109  vimpl_.resize(m);
110  T* ptr = &Mimpl_[0] - BEG;
111  T** M = &vimpl_[0];
112  T** end = M + m;
113 
114  while (M != end) {
115  *M++ = ptr;
116  ptr += n;
117  }
118 
119  M_ = &vimpl_[0] - BEG;
120  m_ = m;
121  n_ = n;
122 
123  } else {
124 
125  // empty matrix
126  M_ = 0;
127  m_ = 0;
128  n_ = 0;
129 
130  }
131 
132  }
133 
134  template<typename T, K_UINT_32 BEG, bool DBG>
136  init(0, 0);
137  }
138 
143  template<typename T, K_UINT_32 BEG, bool DBG>
145  : Mimpl_(m*n) {
146  init(m, n);
147  }
148 
154  template<typename T, K_UINT_32 BEG, bool DBG>
156  : Mimpl_(m*n, a) {
157  init(m, n);
158  }
159 
171  template<typename T, K_UINT_32 BEG, bool DBG>
173  : Mimpl_(v, v + m*n) {
174  init(m, n);
175  }
176 
179  template<typename T, K_UINT_32 BEG, bool DBG>
181  : Mimpl_(M.Mimpl_) {
182  init(M.m_, M.n_);
183  }
184 
185  template<typename T, K_UINT_32 BEG, bool DBG>
187 
193  template<typename T, K_UINT_32 BEG, bool DBG>
195  K_UINT_32 j) {
196  if (DBG) {
197 
198  if(BEG!=0)
199  {
200 // if (i < BEG || i >= m_ + BEG || j < BEG || j >= n_ + BEG)
201 // {
202 // KOSTRINGSTREAM oss;
203 // oss << "Trying to access element (" << i << ", " << j
204 // << ") not included in [" << BEG << ", " << m_ + BEG - 1 << "]["
205 // << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
206 // throw OutOfBoundError(oss.str());
207 // }
208  }else{
209  if (i >= m_ + BEG || j >= n_ + BEG)
210  {
211  KOSTRINGSTREAM oss;
212  oss << "Trying to access element (" << i << ", " << j
213  << ") not included in [" << BEG << ", " << m_ + BEG - 1 << "]["
214  << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
215  throw OutOfBoundError(oss.str());
216  }
217  }
218  }
219  return M_[i][j];
220  }
221 
227  template<typename T, K_UINT_32 BEG, bool DBG>
229  K_UINT_32 j) const {
230  if (DBG) {
231  if(BEG!=0)
232  {
233 // if (i < BEG || i >= m_ + BEG || j < BEG || j >= n_ + BEG) {
234 // KOSTRINGSTREAM oss;
235 // oss << "Trying to access element (" << i << ", " << j
236 // << ") not included in [" << BEG << ", " << m_ + BEG - 1 << "]["
237 // << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
238 // throw OutOfBoundError(oss.str());
239 // }
240  }else
241  {
242  if (i >= m_ + BEG || j >= n_ + BEG) {
243  KOSTRINGSTREAM oss;
244  oss << "Trying to access element (" << i << ", " << j
245  << ") not included in [" << BEG << ", " << m_ + BEG - 1 << "]["
246  << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
247  throw OutOfBoundError(oss.str());
248  }
249  }
250  }
251  return M_[i][j];
252  }
253 
256  template<typename T, K_UINT_32 BEG, bool DBG>
258  return m_;
259  }
260 
263  template<typename T, K_UINT_32 BEG, bool DBG>
265  return n_;
266  }
267 
276  template<typename T, K_UINT_32 BEG, bool DBG>
278 
279  if (m == m_ && n == n_) {
280  return;
281  }
282 
283  Mimpl_.resize(m*n);
284  init(m, n);
285  }
286 
289  template<typename T, K_UINT_32 BEG, bool DBG>
291 
292  T* ptr = &Mimpl_[0];
293  const T* end = ptr + Mimpl_.size();
294 
295  while (ptr != end) {
296  *ptr++ = a;
297  }
298  return *this;
299  }
300 
304  template<typename T, K_UINT_32 BEG, bool DBG>
305  inline KMatrix<T, BEG, DBG>&
307  KMatrix temp(M);
308  swap(temp);
309  return *this;
310  }
311 
319  template<typename T, K_UINT_32 BEG, bool DBG>
321  const T* v) {
322  KMatrix temp(m, n, v);
323  swap(temp);
324  }
325 
331  template<typename T, K_UINT_32 BEG, bool DBG>
333  vimpl_.swap(M.vimpl_);
334  Mimpl_.swap(M.Mimpl_);
335  Util::swap(M_, M.M_);
336  Util::swap(m_, M.m_);
337  Util::swap(n_, M.n_);
338  }
339 
346  template<typename T, K_UINT_32 BEG, bool DBG>
347  inline void KMatrix<T, BEG, DBG>::get(std::istream& is) {
348 
349  T* ptr = &Mimpl_[0];
350  std::string tmp;
351  K_UINT_32 i, j;
352 
354  is >> tmp;
355  }
356 
358 
360 
361  for (i = 0; i < m_-1; ++i) {
362  for (j = 0; j < n_; ++j) {
363  is >> *ptr++ >> tmp;
364  }
365  }
366 
367  for (j = 0; j < n_-1; ++j) {
368  is >> *ptr++ >> tmp;
369  }
370 
371  is >> *ptr;
372 
373  } else {
374 
375  for (i = 0; i < m_-1; ++i) {
376  for (j = 0; j < n_; ++j) {
377  is >> *ptr++;
378  }
379  is >> tmp;
380  }
381 
382  for (j = 0; j < n_; ++j) {
383  is >> *ptr++;
384  }
385 
386  }
387 
388  } else {
389 
391 
392  for (i = 0; i < m_; ++i) {
393  for (j = 0; j < n_-1; ++j) {
394  is >> *ptr++ >> tmp;
395  }
396  is >> *ptr++;
397  }
398 
399  } else {
400 
401  for (i = 0; i < m_; ++i) {
402  for (j = 0; j < n_; ++j) {
403  is >> *ptr++;
404  }
405  }
406 
407  }
408 
409  }
410 
412  is >> tmp;
413  }
414  }
415 
422  template<typename T, K_UINT_32 BEG, bool DBG>
423  inline void KMatrix<T, BEG, DBG>::put(std::ostream& os) const {
424  if (m_ == 0 || n_ == 0) {
425  return;
426  }
427 
428  const T* ptr = &Mimpl_[0];
429  K_UINT_32 i, j;
430 
431  std::ios::fmtflags f = os.setf(std::ios::scientific, std::ios::floatfield);
432  os.setf(std::ios::showpoint);
433  std::streamsize p = os.precision(currentMatrixContext->precision_);
434 
436 
437  for (i = 0; i < m_-1; ++i) {
438  for (j = 0; j < n_-1; ++j) {
439  os.width(currentMatrixContext->width_);
440  os << *ptr++ << currentMatrixContext->elemDelim_;
441  }
442  os.width(currentMatrixContext->width_);
443  os << *ptr++ << currentMatrixContext->rowDelim_;
444  }
445 
446  for (j = 0; j < n_-1; ++j) {
447  os.width(currentMatrixContext->width_);
448  os << *ptr++ << currentMatrixContext->elemDelim_;
449  }
450 
451  os.width(currentMatrixContext->width_);
452  os << *ptr++ << currentMatrixContext->endDelim_;
453 
454  os.precision(p);
455  os.flags(f);
456  }
457 
462  template<typename T, K_UINT_32 BEG, bool DBG>
463  inline std::istream& operator>>(std::istream& is,
465  M.get(is);
466  return is;
467  }
468 
473  template<typename T, K_UINT_32 BEG, bool DBG>
474  inline std::ostream& operator<<(std::ostream& os,
475  const KMatrix<T, BEG, DBG>& M) {
476  M.put(os);
477  return os;
478  }
479 
480 }
481 
482 #endif
K_UINT_32 nrow() const
Returns m_, the number of rows of the matrix.
void get(std::istream &is)
Reads a matrix from a stream.
std::string endDelim_
Ending string after last matrix element.
void swap(KMatrix &M)
Constant-time swap function between two matrices.
#define KEND_OF_STREAM
Definition: ktypes.hpp:52
unsigned precision_
Number of significant digits to output.
KMatrix()
Default constructor. Creates an empty matrix.
Contains necessary informations to print a formatted KMatrix.
K_UINT_32 n_
Number of columns of matrix.
Definition: kmatrix.hpp:162
void swap(T &a, T &b)
Swaps objects a and b.
Definition: ktypes.hpp:104
KMatrix & operator=(const T &a)
Assigns a copy of a to all elements of the matrix.
bool skipElemDelim_
Must we skip a word between elements ?
void assign(K_UINT_32 m, K_UINT_32 n, const T *v)
Copies a C-style array of instances of T in an m by n matrix.
void init(K_UINT_32 m, K_UINT_32 n)
Helper function to initialize matrix.
#define KOSTRINGSTREAM
Definition: ktypes.hpp:51
std::string startDelim_
Starting string before first matrix element.
std::vector< T * > vimpl_
Array of pointers to rows of Mimpl_.
Definition: kmatrix.hpp:151
K_UINT_32 m_
Number of rows of matrix.
Definition: kmatrix.hpp:161
unsigned long int K_UINT_32
Unsigned 32-bits integral type.
Definition: ktypes.hpp:68
T & operator()(K_UINT_32 i, K_UINT_32 j)
Returns the element (i,j).
bool skipRowDelim_
Must we skip a word at the end of the row ?
std::string rowDelim_
Delimiter string at the end of each row.
bool skipEndDelim_
Must we skip a word at end of vector ?
std::string elemDelim_
Delimiter string between matrix elements.
void resize(K_UINT_32 m, K_UINT_32 n)
Resizes the matrix. Resulting matrix contents are undefined.
bool skipStartDelim_
Must we skip a word at start of vector ?
Minimalist matrix template class.
Definition: kmatrix.hpp:72
std::ostream & operator<<(std::ostream &os, const KMatrix< T, BEG, DBG > &M)
Writes a matrix to a stream.
unsigned width_
Width of output field for nice alignment.
void put(std::ostream &os) const
Writes a matrix to a stream.
K_UINT_32 ncol() const
Returns n_, the number of columns of the matrix.
std::vector< T > Mimpl_
Underlying vector implementation.
Definition: kmatrix.hpp:152
Exception class for access to out-of-bound elements.
Definition: ktypes.hpp:85
KMatrixContextImpl * currentMatrixContext
Refers to the currently selected matrix printing context.
Definition: kstatics.cpp:116
std::istream & operator>>(std::istream &is, KMatrix< T, BEG, DBG > &M)
Reads a matrix from a stream.
T ** M_
Pointer to the start of vimpl_.
Definition: kmatrix.hpp:160
~KMatrix()
Destructor.
KMatrixContextImpl(std::string elemDelim=" ", std::string rowDelim="\n", std::string startDelim=std::string(), std::string endDelim=std::string(), unsigned prec=4)
Constructor.


kfilter
Author(s): Jorge Almeida, Vincent Zalzal, Sylvain Marleau, Richard Gourdeau
autogenerated on Mon Mar 2 2015 01:31:45