00001 #pragma ident "$Id: Matrix.hpp 70 2006-08-01 18:36:21Z ehagen $"
00002
00003
00004
00010 #ifndef GPSTK_MATRIX_HPP
00011 #define GPSTK_MATRIX_HPP
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "Vector.hpp"
00036 #include "MatrixBase.hpp"
00037
00038 namespace gpstk
00039 {
00040
00043
00044
00045 template <class T> class MatrixRowSlice;
00046 template <class T> class ConstMatrixRowSlice;
00047 template <class T> class MatrixColSlice;
00048 template <class T> class ConstMatrixColSlice;
00049
00050
00058 template <class T>
00059 class Matrix : public RefMatrixBase<T, Matrix<T> >
00060 {
00061 public:
00063 typedef typename Vector<T>::value_type value_type;
00065 typedef typename Vector<T>::reference reference;
00067 typedef typename Vector<T>::const_reference const_reference;
00069 typedef typename Vector<T>::iterator iterator;
00071 typedef typename Vector<T>::const_iterator const_iterator;
00072
00074 Matrix();
00076 Matrix(size_t rows, size_t cols);
00078 Matrix(size_t rows, size_t cols, T initialValue);
00080 Matrix(size_t rows, size_t cols, const T* vec);
00082 template <class BaseClass>
00083 Matrix(size_t rows, size_t cols, const ConstVectorBase<T, BaseClass>& vec)
00084 : v(vec), r(rows), c(cols), s(rows * cols)
00085 {}
00087 template <class BaseClass>
00088 Matrix(const ConstMatrixBase<T, BaseClass>& mat)
00089 : v(mat.size()), r(mat.rows()), c(mat.cols()), s(mat.size())
00090 {
00091 size_t i,j;
00092 for(i = 0; i < r; i++)
00093 for(j = 0; j < c; j++)
00094 (*this)(i,j) = mat(i, j);
00095 }
00096
00098 template <class BaseClass>
00099 Matrix(const ConstMatrixBase<T, BaseClass>& mat, size_t topRow,
00100 size_t topCol, size_t numRows, size_t numCols)
00101 : v((size_t)0), r(0), c(0), s(0)
00102 {
00103
00104 if ( (topCol > mat.cols()) ||
00105 (topRow > mat.rows()) ||
00106 ((topRow + numRows) > mat.rows()) ||
00107 ((topCol + numCols) > mat.cols()) )
00108 {
00109 MatrixException e("Invalid dimensions or size for Matrix(MatrixBase)");
00110 GPSTK_THROW(e);
00111 }
00112
00113
00114 r = numRows;
00115 c = numCols;
00116 s = r * c;
00117 v.resize(r * c);
00118 size_t i, j;
00119 for(i = 0; i < r; i++)
00120 for(j = 0; j < c; j++)
00121 (*this)(i,j) = mat(topRow + i, topCol + j);
00122 }
00123
00125 iterator begin() { return v.begin(); }
00127 const_iterator begin() const { return v.begin(); }
00129 iterator end() { return v.end(); }
00131 const_iterator end() const { return v.end(); }
00133 value_type front() { return v.front(); }
00135 const_reference front() const { return v.front();}
00137 bool empty() const { return s == 0; }
00139 size_t size() const {return s; }
00141 size_t max_size() const { return s; }
00142
00144 inline size_t rows() const { return r; }
00146 inline size_t cols() const { return c; }
00148 inline MatrixRowSlice<T> rowRef(size_t rowNum, const std::slice& s);
00150 inline MatrixRowSlice<T> rowRef(size_t rowNum, size_t colNum = 0);
00152 inline ConstMatrixRowSlice<T> row(size_t rowNum, const std::slice& s) const;
00154 inline ConstMatrixRowSlice<T> row(size_t rowNum, size_t colNum = 0) const;
00155
00157 inline MatrixColSlice<T> colRef(size_t colNum, const std::slice& s);
00159 inline MatrixColSlice<T> colRef(size_t colNum, size_t rowNum = 0);
00161 inline ConstMatrixColSlice<T> col(size_t colNum, const std::slice& s) const;
00163 inline ConstMatrixColSlice<T> col(size_t colNum, size_t rowNum = 0) const;
00164
00166 inline T& operator() (size_t rowNum, size_t colNum)
00167 { return v(rowNum + colNum * r); }
00169 inline T operator() (size_t rowNum, size_t colNum) const
00170 { return v(rowNum + colNum * r); }
00172 inline MatrixRowSlice<T> operator[] (size_t row)
00173 { return rowRef(row); }
00175 inline ConstMatrixRowSlice<T> operator[] (size_t rowNum) const
00176 { return row(rowNum);}
00177
00180 inline Matrix& resize(size_t rows, size_t cols);
00181
00182 inline Matrix& resize(size_t rows, size_t cols,
00183 const T initialValue);
00184
00189 inline Matrix& operator=(const T* array)
00190 { return assignFrom(array); }
00193 inline Matrix& operator=(const std::valarray<T> array)
00194 { return assignFrom(array); }
00196 inline Matrix& operator=(const T t)
00197 { return assignFrom(t); }
00199 inline Matrix& operator=(const Matrix& mat)
00200 { v = mat.v; r = mat.r; c = mat.c; s = mat.s; return *this; }
00202 template <class BaseClass>
00203 inline Matrix& operator=(const ConstMatrixBase<T, BaseClass>& mat)
00204 {
00205 v.resize(mat.size());
00206 r=mat.rows();
00207 c=mat.cols();
00208 s=mat.size();
00209 return assignFrom(mat);
00210 }
00212 template <class BaseClass>
00213 inline Matrix& operator=(const ConstVectorBase<T, BaseClass>& mat)
00214 { return assignFrom(mat); }
00215
00216 private:
00218 Vector<T> v;
00219 size_t r,
00220 c,
00221 s;
00222 };
00223
00227 template <class T>
00228 class MatrixSlice : public RefMatrixSliceBase<T, MatrixSlice<T> >
00229 {
00230 public:
00232 MatrixSlice() : m(NULL), rSlice(std::slice(0,0,0)),
00233 cSlice(std::slice(0,0,0)), s(0)
00234 {}
00235
00237 MatrixSlice(Matrix<T>& mat)
00238 : m(&mat), rSlice(std::slice(0, mat.rows(), 1)),
00239 cSlice(std::slice(0,mat.cols(), 1)), s(mat.size())
00240 {
00241 matSliceCheck(mat.rows(), mat.cols());
00242 }
00243
00245 MatrixSlice(Matrix<T>& mat, const std::slice& rowSlice,
00246 const std::slice& colSlice)
00247 : m(&mat), rSlice(rowSlice), cSlice(colSlice),
00248 s(rSlice.size() * cSlice.size())
00249 {
00250 matSliceCheck(mat.rows(), mat.cols());
00251 }
00252
00254 MatrixSlice(Matrix<T>& mat, size_t topRow, size_t topCol,
00255 size_t numRows, size_t numCols)
00256 : m(&mat), rSlice(std::slice(topRow, numRows, 1)),
00257 cSlice(std::slice(topCol, numCols, 1)),
00258 s(rSlice.size() * cSlice.size())
00259 {
00260 matSliceCheck(mat.rows(), mat.cols());
00261 }
00262
00264 template <class V>
00265 MatrixSlice& operator=(const ConstMatrixBase<T, V>& x)
00266 { return assignFrom(x); }
00267
00269 template <class V>
00270 MatrixSlice& operator=(const ConstVectorBase<T, V>& x)
00271 { return assignFrom(x); }
00272
00274 MatrixSlice& operator=(const std::valarray<T>& x)
00275 { return assignFrom(x); }
00277 MatrixSlice& operator=(const T x)
00278 { return assignFrom(x); }
00280 MatrixSlice& operator=(const T* x)
00281 { return assignFrom(x); }
00282
00284 size_t size() const { return s; }
00286 size_t cols() const { return colSize(); }
00288 size_t rows() const { return rowSize(); }
00290 T& operator() (size_t i, size_t j)
00291 { return (*m)(i * rowStride() + rowStart(),
00292 j * colStride() + colStart()); }
00294 T operator() (size_t i, size_t j) const
00295 { return (*m)(i * rowStride() + rowStart(),
00296 j * colStride() + colStart()); }
00297
00298
00300 size_t rowSize() const { return rSlice.size(); }
00302 size_t rowStart() const{ return rSlice.start(); }
00304 size_t rowStride() const { return rSlice.stride(); }
00306 size_t colSize() const { return cSlice.size(); }
00308 size_t colStart() const { return cSlice.start(); }
00310 size_t colStride() const { return cSlice.stride(); }
00311
00312 private:
00314 Matrix<T>* m;
00315 std::slice rSlice,
00316 cSlice;
00317 size_t s;
00318 };
00319
00323 template <class T>
00324 class ConstMatrixSlice : public ConstMatrixSliceBase<T, ConstMatrixSlice<T> >
00325 {
00326 public:
00328 ConstMatrixSlice(void) : m(NULL), rSlice(std::slice(0,0,0)),
00329 cSlice(std::slice(0,0,0)), s(0)
00330 {}
00331
00333 ConstMatrixSlice(const Matrix<T>& mat)
00334 : m(&mat), rSlice(std::slice(0, mat.rows(), 1)),
00335 cSlice(std::slice(0,mat.cols(), 1)), s(mat.size())
00336 {
00337 matSliceCheck(mat.rows(), mat.cols());
00338 }
00339
00341 ConstMatrixSlice(const Matrix<T>& mat, const std::slice& rowSlice,
00342 const std::slice& colSlice)
00343 : m(&mat), rSlice(rowSlice), cSlice(colSlice),
00344 s(rSlice.size() * cSlice.size())
00345 {
00346 matSliceCheck(mat.rows(), mat.cols());
00347 }
00348
00350 ConstMatrixSlice(const Matrix<T>& mat, size_t topRow, size_t topCol,
00351 size_t numRows, size_t numCols)
00352 : m(&mat), rSlice(std::slice(topRow, numRows, 1)),
00353 cSlice(std::slice(topCol, numCols, 1)),
00354 s(rSlice.size() * cSlice.size())
00355 {
00356 matSliceCheck(mat.rows(), mat.cols());
00357 }
00358
00360 size_t size() const { return s; }
00362 size_t cols() const { return colSize(); }
00364 size_t rows() const { return rowSize(); }
00366 T operator() (size_t i, size_t j) const
00367 { return (*m)(i * rowStride() + rowStart(),
00368 j * colStride() + colStart()); }
00369
00371 size_t rowSize() const { return rSlice.size(); }
00373 size_t rowStart() const{ return rSlice.start(); }
00375 size_t rowStride() const { return rSlice.stride(); }
00377 size_t colSize() const { return cSlice.size(); }
00379 size_t colStart() const { return cSlice.start(); }
00381 size_t colStride() const { return cSlice.stride(); }
00382 private:
00384 const Matrix<T>* m;
00385 std::slice rSlice,
00386 cSlice;
00387 size_t s;
00388 };
00389
00393 template <class T>
00394 class MatrixColSlice : public RefMatrixSliceBase<T, MatrixColSlice<T> >
00395 {
00396 public:
00398 MatrixColSlice() : m(NULL), c(0), rSlice(std::slice(0,0,0)) {}
00400 MatrixColSlice(Matrix<T>& mat, size_t col)
00401 : m(&mat), c(col), rSlice(std::slice(0,mat.rows(),1))
00402 {
00403 matSliceCheck(mat.rows(), mat.cols());
00404 }
00407 MatrixColSlice(Matrix<T>& mat, size_t col, const std::slice& s)
00408 : m(&mat), c(col), rSlice(s)
00409 {
00410
00411 matSliceCheck(mat.rows(), mat.cols());
00412 }
00413
00415 template <class V>
00416 MatrixColSlice& operator=(const ConstMatrixBase<T, V>& x)
00417 { return assignFrom(x); }
00418
00420 template <class V>
00421 MatrixColSlice& operator=(const ConstVectorBase<T, V>& x)
00422 { return assignFrom(x); }
00424 MatrixColSlice& operator=(const std::valarray<T>& x)
00425 { return assignFrom(x); }
00427 MatrixColSlice& operator=(const T x)
00428 { return assignFrom(x); }
00430 MatrixColSlice& operator=(const T* x)
00431 { return assignFrom(x); }
00432
00434 T& operator[] (size_t i)
00435 { return (*m)(rowStart() + i * rowStride(), c); }
00437 T& operator() (size_t i)
00438 { return (*m)(rowStart() + i * rowStride(), c); }
00440 T operator[] (size_t i) const
00441 { return (*m)(rowStart() + i * rowStride(), c); }
00443 T operator() (size_t i) const
00444 { return (*m)(rowStart() + i * rowStride(), c); }
00445
00447 T& operator() (size_t i, size_t j)
00448 { return (*m)(rowStart() + i * rowStride(), j + c); }
00450 T operator() (size_t i, size_t j) const
00451 { return (*m)(rowStart() + i * rowStride(), j + c); }
00452
00454 size_t rows() const {return size();}
00456 size_t cols() const {return 1;}
00458 size_t size() const {return rowSize();}
00459
00461 size_t rowSize() const { return rSlice.size(); }
00463 size_t rowStart() const{ return rSlice.start(); }
00465 size_t rowStride() const { return rSlice.stride(); }
00467 size_t colSize() const { return 1; }
00469 size_t colStart() const { return c; }
00471 size_t colStride() const { return 1; }
00472
00473 private:
00475 Matrix<T>* m;
00477 size_t c;
00479 std::slice rSlice;
00480
00481 };
00482
00486 template <class T>
00487 class ConstMatrixColSlice : public ConstMatrixSliceBase<T, ConstMatrixColSlice<T> >
00488 {
00489 public:
00491 ConstMatrixColSlice()
00492 : m(NULL), c(0), rSlice(std::slice(0,0,0))
00493 {}
00494
00496 ConstMatrixColSlice(const Matrix<T>& mat, size_t col)
00497 : m(&mat), c(col), rSlice(std::slice(0,mat.rows(),1))
00498 { matSliceCheck(mat.rows(), mat.cols()); }
00499
00502 ConstMatrixColSlice(const Matrix<T>& mat, size_t col,
00503 const std::slice& s)
00504 : m(&mat), c(col), rSlice(s)
00505 {
00506
00507 matSliceCheck(mat.rows(), mat.cols());
00508 }
00509
00511 T operator[] (size_t i) const
00512 { return (*m)(rowStart() + i * rowStride(), c); }
00514 T operator() (size_t i) const
00515 { return (*m)(rowStart() + i * rowStride(), c); }
00516
00518 T operator() (size_t i, size_t j) const
00519 { return (*m)(rowStart() + i * rowStride(), j + c); }
00520
00522 size_t rows() const {return rowSize();}
00524 size_t cols() const {return 1;}
00526 size_t size() const {return rowSize();}
00527
00529 size_t rowSize() const { return rSlice.size(); }
00531 size_t rowStart() const{ return rSlice.start(); }
00533 size_t rowStride() const { return rSlice.stride(); }
00535 size_t colSize() const { return 1; }
00537 size_t colStart() const { return c; }
00539 size_t colStride() const { return 1; }
00540 private:
00542 const Matrix<T>* m;
00544 size_t c;
00546 std::slice rSlice;
00547 };
00548
00552 template <class T>
00553 class MatrixRowSlice : public RefMatrixSliceBase<T, MatrixRowSlice<T> >
00554 {
00555 public:
00557 MatrixRowSlice()
00558 : m(NULL), r(0), cSlice(std::slice(0,0,0))
00559 {}
00561 MatrixRowSlice(Matrix<T>& mat, size_t row)
00562 : m(&mat), r(row), cSlice(std::slice(0,mat.cols(),1))
00563 { matSliceCheck(mat.rows(), mat.cols()); }
00564
00566 MatrixRowSlice(Matrix<T>& mat, size_t row,
00567 const std::slice& s)
00568 : m(&mat), r(row), cSlice(s)
00569 {
00570
00571 matSliceCheck(mat.rows(), mat.cols());
00572 }
00573
00575 template <class V>
00576 MatrixRowSlice& operator=(const ConstMatrixBase<T, V>& x)
00577 { return assignFrom(x); }
00579 template <class V>
00580 MatrixRowSlice& operator=(const ConstVectorBase<T, V>& x)
00581 { return assignFrom(x); }
00583 MatrixRowSlice& operator=(const std::valarray<T>& x)
00584 { return assignFrom(x); }
00586 MatrixRowSlice& operator=(const T x)
00587 { return assignFrom(x); }
00589 MatrixRowSlice& operator=(const T* x)
00590 { return assignFrom(x); }
00591
00593 T& operator[] (size_t j)
00594 { return (*m)(r, colStart() + j * colStride()); }
00596 T& operator() (size_t j)
00597 { return (*m)(r, colStart() + j * colStride()); }
00599 T operator[] (size_t j) const
00600 { return (*m)(r, colStart() + j * colStride()); }
00602 T operator() (size_t j) const
00603 { return (*m)(r, colStart() + j * colStride()); }
00605 T& operator() (size_t i, size_t j)
00606 { return (*m)(i + r, colStart() + j * colStride()); }
00608 T operator() (size_t i, size_t j) const
00609 { return (*m)(i + r, colStart() + j * colStride()); }
00610
00612 size_t rows() const {return 1;}
00614 size_t cols() const {return colSize();}
00616 size_t size() const {return colSize();}
00617
00619 size_t rowSize() const { return 1; }
00621 size_t rowStart() const{ return r; }
00623 size_t rowStride() const { return 1; }
00625 size_t colSize() const { return cSlice.size(); }
00627 size_t colStart() const { return cSlice.start(); }
00629 size_t colStride() const { return cSlice.stride(); }
00630
00631 private:
00633 Matrix<T>* m;
00635 size_t r;
00637 std::slice cSlice;
00638 };
00639
00643 template <class T>
00644 class ConstMatrixRowSlice : public ConstMatrixSliceBase<T, ConstMatrixRowSlice<T> >
00645 {
00646 public:
00648 ConstMatrixRowSlice()
00649 : m(NULL), r(0), cSlice(std::slice(0,0,0))
00650 {}
00652 ConstMatrixRowSlice(const Matrix<T>& mat, size_t row)
00653 : m(&mat), r(row), cSlice(std::slice(0,mat.cols(),1))
00654 { matSliceCheck(mat.rows(), mat.cols()); }
00655
00657 ConstMatrixRowSlice(const Matrix<T>& mat, size_t row,
00658 const std::slice& s)
00659 : m(&mat), r(row), cSlice(s)
00660 {
00661
00662 matSliceCheck(mat.rows(), mat.cols());
00663 }
00664
00666 T operator[] (size_t i) const
00667 { return (*m)(r, colStart() + i * colStride()); }
00669 T operator() (size_t i) const
00670 { return (*m)(r, colStart() + i * colStride()); }
00671
00673 T operator() (size_t i, size_t j) const
00674 { return (*m)(i + r, colStart() + j * colStride()); }
00675
00677 size_t rows() const {return 1;}
00679 size_t cols() const {return colSize();}
00681 size_t size() const {return colSize();}
00682
00684 size_t rowSize() const { return 1; }
00686 size_t rowStart() const{ return r; }
00688 size_t rowStride() const { return 1; }
00690 size_t colSize() const { return cSlice.size(); }
00692 size_t colStart() const { return cSlice.start(); }
00694 size_t colStride() const { return cSlice.stride(); }
00695 private:
00697 const Matrix<T>* m;
00699 size_t r;
00701 std::slice cSlice;
00702 };
00703
00705
00706 }
00707
00708 #include "MatrixImplementation.hpp"
00709 #include "MatrixOperators.hpp"
00710 #include "MatrixFunctors.hpp"
00711
00712 #endif