Matrix.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: Matrix.hpp 3320 2012-09-20 00:41:47Z yanweignss $"
00002 
00003 
00004 
00010 #ifndef GPSTK_MATRIX_HPP
00011 #define GPSTK_MATRIX_HPP
00012 
00013 //============================================================================
00014 //
00015 //  This file is part of GPSTk, the GPS Toolkit.
00016 //
00017 //  The GPSTk is free software; you can redistribute it and/or modify
00018 //  it under the terms of the GNU Lesser General Public License as published
00019 //  by the Free Software Foundation; either version 2.1 of the License, or
00020 //  any later version.
00021 //
00022 //  The GPSTk is distributed in the hope that it will be useful,
00023 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 //  GNU Lesser General Public License for more details.
00026 //
00027 //  You should have received a copy of the GNU Lesser General Public
00028 //  License along with GPSTk; if not, write to the Free Software Foundation,
00029 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
00030 //  
00031 //  Copyright 2004, The University of Texas at Austin
00032 //
00033 //============================================================================
00034 
00035 #include "Vector.hpp"
00036 #include "MatrixBase.hpp"
00037 
00038 namespace gpstk
00039 {
00040 
00043  
00044 // forward declarations
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(rows*cols), r(rows), c(cols), s(rows * cols)
00085       { assignFrom(vec); }
00086 
00088       template <class BaseClass>
00089       Matrix(const ConstMatrixBase<T, BaseClass>& mat) 
00090             : v(mat.size()), r(mat.rows()), c(mat.cols()), s(mat.size())
00091          {
00092             size_t i,j;
00093             for(i = 0; i < r; i++)
00094                for(j = 0; j < c; j++)
00095                   (*this)(i,j) = mat(i, j);
00096          }
00097 
00099       template <class BaseClass>
00100       Matrix(const ConstMatrixBase<T, BaseClass>& mat, size_t topRow, 
00101           size_t topCol, size_t numRows, size_t numCols) 
00102             : v((size_t)0), r(0), c(0), s(0)
00103          {
00104                // sanity checks...
00105             if ( (topCol > mat.cols()) || 
00106                  (topRow > mat.rows()) ||
00107                  ((topRow + numRows) > mat.rows()) ||
00108                  ((topCol + numCols) > mat.cols()) )
00109             {
00110                MatrixException e("Invalid dimensions or size for Matrix(MatrixBase)");
00111                GPSTK_THROW(e);
00112             }
00113          
00114                // seems ok - make the valarray and copy column by column
00115             r = numRows;
00116             c = numCols;
00117             s = r * c;
00118             v.resize(r * c);
00119             size_t i, j;
00120             for(i = 0; i < r; i++)
00121                for(j = 0; j < c; j++)
00122                   (*this)(i,j) = mat(topRow + i, topCol + j);
00123          }
00124 
00126       iterator begin() { return v.begin(); }
00128       const_iterator begin() const { return v.begin(); }
00130       iterator end() { return v.end(); }
00132       const_iterator end() const { return v.end(); }
00134       value_type front() { return v.front(); }
00136       const_reference front() const { return v.front();}
00138       bool empty() const { return s == 0; }
00140       size_t size() const {return s; }
00142       size_t max_size() const { return s; }
00143 
00145       inline size_t rows() const { return r; }
00147       inline size_t cols() const { return c; }
00149       inline MatrixRowSlice<T> rowRef(size_t rowNum, const std::slice& s);
00151       inline MatrixRowSlice<T> rowRef(size_t rowNum, size_t colNum = 0);
00153       inline ConstMatrixRowSlice<T> row(size_t rowNum, const std::slice& s) const;
00155       inline ConstMatrixRowSlice<T> row(size_t rowNum, size_t colNum = 0) const;
00156 
00158       inline MatrixColSlice<T> colRef(size_t colNum, const std::slice& s);
00160       inline MatrixColSlice<T> colRef(size_t colNum, size_t rowNum = 0);
00162       inline ConstMatrixColSlice<T> col(size_t colNum, const std::slice& s) const;
00164       inline ConstMatrixColSlice<T> col(size_t colNum, size_t rowNum = 0) const;
00165 
00167       inline T& operator() (size_t rowNum, size_t colNum)
00168          { return v(rowNum + colNum * r); }
00170       inline T operator() (size_t rowNum, size_t colNum) const
00171          { return v(rowNum + colNum * r); }
00173       inline MatrixRowSlice<T> operator[] (size_t row)
00174          { return rowRef(row); }
00176       inline ConstMatrixRowSlice<T> operator[] (size_t rowNum) const 
00177          { return row(rowNum);}
00178 
00181       inline Matrix& resize(size_t rows, size_t cols);
00182 
00183       inline Matrix& resize(size_t rows, size_t cols, 
00184                          const T initialValue);
00185 
00190       inline Matrix& operator=(const T* array)
00191          { return this->assignFrom(array); }
00194       inline Matrix& operator=(const std::valarray<T> array)
00195          { return this->assignFrom(array); }
00197       inline Matrix& operator=(const T t)
00198          { return this->assignFrom(t); }
00200       inline Matrix& operator=(const Matrix& mat)
00201          { v = mat.v; r = mat.r; c = mat.c; s = mat.s; return *this; }
00203       template <class BaseClass>
00204       inline Matrix& operator=(const ConstMatrixBase<T, BaseClass>& mat)
00205          { 
00206             v.resize(mat.size()); 
00207             r=mat.rows(); 
00208             c=mat.cols(); 
00209             s=mat.size();
00210             return this->assignFrom(mat);
00211          }
00213       template <class BaseClass>
00214       inline Matrix& operator=(const ConstVectorBase<T, BaseClass>& mat)
00215          { return this->assignFrom(mat); }
00216 
00217    private:
00219       Vector<T> v;
00220       size_t r,  
00221          c,  
00222          s;  
00223    };
00224 
00228    template <class T>
00229    class MatrixSlice : public RefMatrixSliceBase<T, MatrixSlice<T> >
00230    {
00231    public:
00233       MatrixSlice() : m(NULL), rSlice(std::slice(0,0,0)), 
00234          cSlice(std::slice(0,0,0)), s(0)
00235          {}
00236 
00238       MatrixSlice(Matrix<T>& mat)
00239             : m(&mat), rSlice(std::slice(0, mat.rows(), 1)),
00240               cSlice(std::slice(0,mat.cols(), 1)), s(mat.size())
00241          {
00242             this->matSliceCheck(mat.rows(), mat.cols());
00243          }
00244 
00246       MatrixSlice(Matrix<T>& mat, const std::slice& rowSlice,
00247                const std::slice& colSlice)
00248             : m(&mat), rSlice(rowSlice), cSlice(colSlice),
00249               s(rSlice.size() * cSlice.size())
00250          {
00251             this->matSliceCheck(mat.rows(), mat.cols());
00252          }
00253 
00255       MatrixSlice(Matrix<T>& mat, size_t topRow, size_t topCol, 
00256                size_t numRows, size_t numCols)
00257             : m(&mat), rSlice(std::slice(topRow, numRows, 1)),
00258               cSlice(std::slice(topCol, numCols, 1)),
00259               s(rSlice.size() * cSlice.size())
00260          {
00261             this->matSliceCheck(mat.rows(), mat.cols());
00262          }
00263       
00265       template <class V>
00266       MatrixSlice& operator=(const ConstMatrixBase<T, V>& x)
00267          { return this->assignFrom(x); }
00268 
00270       template <class V>
00271       MatrixSlice& operator=(const ConstVectorBase<T, V>& x)
00272          { return this->assignFrom(x); }
00273 
00275       MatrixSlice& operator=(const std::valarray<T>& x)
00276          { return this->assignFrom(x); }
00278       MatrixSlice& operator=(const T x)
00279          { return this->assignFrom(x); }
00281       MatrixSlice& operator=(const T* x)
00282          { return this->assignFrom(x); }
00283 
00285       size_t size() const { return s; }
00287       size_t cols() const { return colSize(); }
00289       size_t rows() const { return rowSize(); }
00291       T& operator() (size_t i, size_t j)
00292          { return (*m)(i * rowStride() + rowStart(), 
00293                        j * colStride() + colStart()); }
00295       T operator() (size_t i, size_t j) const
00296          { return (*m)(i * rowStride() + rowStart(), 
00297                        j * colStride() + colStart()); }
00298 
00299 
00301       size_t rowSize() const { return rSlice.size(); }
00303       size_t rowStart() const{ return rSlice.start(); }
00305       size_t rowStride() const { return rSlice.stride(); }
00307       size_t colSize() const { return cSlice.size(); }
00309       size_t colStart() const { return cSlice.start(); }
00311       size_t colStride() const { return cSlice.stride(); }
00312 
00313    private:
00315       Matrix<T>* m;
00316       std::slice rSlice, 
00317          cSlice; 
00318       size_t s; 
00319    };
00320 
00324    template <class T>
00325    class ConstMatrixSlice : public ConstMatrixSliceBase<T, ConstMatrixSlice<T> >
00326    {
00327    public:
00329       ConstMatrixSlice(void) : m(NULL), rSlice(std::slice(0,0,0)), 
00330          cSlice(std::slice(0,0,0)), s(0)
00331          {}
00332 
00334       ConstMatrixSlice(const Matrix<T>& mat)
00335             : m(&mat), rSlice(std::slice(0, mat.rows(), 1)),
00336               cSlice(std::slice(0,mat.cols(), 1)), s(mat.size())
00337          {
00338             this->matSliceCheck(mat.rows(), mat.cols());
00339          }
00340 
00342       ConstMatrixSlice(const Matrix<T>& mat, const std::slice& rowSlice,
00343                const std::slice& colSlice)
00344             : m(&mat), rSlice(rowSlice), cSlice(colSlice),
00345               s(rSlice.size() * cSlice.size())
00346          {
00347             this->matSliceCheck(mat.rows(), mat.cols());
00348          }
00349 
00351       ConstMatrixSlice(const Matrix<T>& mat, size_t topRow, size_t topCol, 
00352                size_t numRows, size_t numCols)
00353             : m(&mat), rSlice(std::slice(topRow, numRows, 1)),
00354               cSlice(std::slice(topCol, numCols, 1)),
00355               s(rSlice.size() * cSlice.size())
00356          {
00357             this->matSliceCheck(mat.rows(), mat.cols());
00358          }
00359 
00361       size_t size() const { return s; }
00363       size_t cols() const { return colSize(); }
00365       size_t rows() const { return rowSize(); }
00367       T operator() (size_t i, size_t j) const 
00368          { return (*m)(i * rowStride() + rowStart(), 
00369                        j * colStride() + colStart()); }
00370 
00372       size_t rowSize() const { return rSlice.size(); }
00374       size_t rowStart() const{ return rSlice.start(); }
00376       size_t rowStride() const { return rSlice.stride(); }
00378       size_t colSize() const { return cSlice.size(); }
00380       size_t colStart() const { return cSlice.start(); }
00382       size_t colStride() const { return cSlice.stride(); }
00383    private:
00385       const Matrix<T>* m;
00386       std::slice rSlice, 
00387          cSlice; 
00388       size_t s; 
00389    };
00390 
00394    template <class T>
00395    class MatrixColSlice : public RefMatrixSliceBase<T, MatrixColSlice<T> >
00396    {
00397    public:
00399       MatrixColSlice() : m(NULL), c(0), rSlice(std::slice(0,0,0)) {}
00401       MatrixColSlice(Matrix<T>& mat, size_t col)
00402             : m(&mat), c(col), rSlice(std::slice(0,mat.rows(),1))
00403          { 
00404             this->matSliceCheck(mat.rows(), mat.cols()); 
00405          }
00408       MatrixColSlice(Matrix<T>& mat, size_t col, const std::slice& s)
00409             : m(&mat), c(col), rSlice(s)
00410          { 
00411                // decide if the input is reasonable
00412             this->matSliceCheck(mat.rows(), mat.cols());
00413          }
00414 
00416       template <class V>
00417       MatrixColSlice& operator=(const ConstMatrixBase<T, V>& x)
00418          { return this->assignFrom(x); }
00419 
00421       template <class V>
00422       MatrixColSlice& operator=(const ConstVectorBase<T, V>& x)
00423          { return this->assignFrom(x); }
00425       MatrixColSlice& operator=(const std::valarray<T>& x)
00426          { return this->assignFrom(x); }
00428       MatrixColSlice& operator=(const T x)
00429          { return this->assignFrom(x); }
00431       MatrixColSlice& operator=(const T* x)
00432          { return this->assignFrom(x); }
00433 
00435       T& operator[] (size_t i) 
00436          { return (*m)(rowStart() + i * rowStride(), c); }
00438       T& operator() (size_t i) 
00439          { return (*m)(rowStart() + i * rowStride(), c); }
00441       T operator[] (size_t i) const
00442          { return (*m)(rowStart() + i * rowStride(), c); }
00444       T operator() (size_t i) const
00445          { return (*m)(rowStart() + i * rowStride(), c); }
00446 
00448       T& operator() (size_t i, size_t j) 
00449          { return (*m)(rowStart() + i * rowStride(), j + c); }
00451       T operator() (size_t i, size_t j) const
00452          { return (*m)(rowStart() + i * rowStride(), j + c); }
00453 
00455       size_t rows() const {return size();}
00457       size_t cols() const {return 1;}
00459       size_t size() const {return rowSize();}
00460 
00462       size_t rowSize() const { return rSlice.size(); }
00464       size_t rowStart() const{ return rSlice.start(); }
00466       size_t rowStride() const { return rSlice.stride(); }
00468       size_t colSize() const { return 1; }
00470       size_t colStart() const { return c; }
00472       size_t colStride() const { return 1; }
00473 
00474    private:
00476       Matrix<T>* m;
00478       size_t c;
00480       std::slice rSlice;
00481 
00482    };
00483 
00487    template <class T>
00488    class ConstMatrixColSlice : public ConstMatrixSliceBase<T, ConstMatrixColSlice<T> >
00489    {
00490    public:
00492       ConstMatrixColSlice() 
00493             : m(NULL), c(0), rSlice(std::slice(0,0,0)) 
00494          {}
00495 
00497       ConstMatrixColSlice(const Matrix<T>& mat, size_t col)
00498             : m(&mat), c(col), rSlice(std::slice(0,mat.rows(),1))
00499          { this->matSliceCheck(mat.rows(), mat.cols()); }
00500 
00503       ConstMatrixColSlice(const Matrix<T>& mat, size_t col, 
00504                        const std::slice& s)
00505             : m(&mat), c(col), rSlice(s)
00506          { 
00507                // decide if the input is reasonable
00508             this->matSliceCheck(mat.rows(), mat.cols());
00509          }
00510 
00512       T operator[] (size_t i) const
00513          { return (*m)(rowStart() + i * rowStride(), c); }
00515       T operator() (size_t i) const
00516          { return (*m)(rowStart() + i * rowStride(), c); }
00517 
00519       T operator() (size_t i, size_t j) const
00520          { return (*m)(rowStart() + i * rowStride(), j + c); }
00521 
00523       size_t rows() const {return rowSize();}
00525       size_t cols() const {return 1;}
00527       size_t size() const {return rowSize();}
00528 
00530       size_t rowSize() const { return rSlice.size(); }
00532       size_t rowStart() const{ return rSlice.start(); }
00534       size_t rowStride() const { return rSlice.stride(); }
00536       size_t colSize() const { return 1; }
00538       size_t colStart() const { return c; }
00540       size_t colStride() const { return 1; }
00541    private:
00543       const Matrix<T>* m;
00545       size_t c;
00547       std::slice rSlice;
00548    };
00549 
00553    template <class T>
00554    class MatrixRowSlice : public RefMatrixSliceBase<T, MatrixRowSlice<T> >
00555    {
00556    public:
00558       MatrixRowSlice() 
00559             : m(NULL), r(0), cSlice(std::slice(0,0,0)) 
00560          {}
00562       MatrixRowSlice(Matrix<T>& mat, size_t row)
00563             : m(&mat), r(row), cSlice(std::slice(0,mat.cols(),1))
00564          { this->matSliceCheck(mat.rows(), mat.cols()); }
00565 
00567       MatrixRowSlice(Matrix<T>& mat, size_t row, 
00568                   const std::slice& s)
00569             : m(&mat), r(row), cSlice(s)
00570          { 
00571                // decide if the input is reasonable
00572             this->matSliceCheck(mat.rows(), mat.cols());
00573          }   
00574 
00576       template <class V>
00577       MatrixRowSlice& operator=(const ConstMatrixBase<T, V>& x)
00578          { return this->assignFrom(x); }
00580       template <class V>
00581       MatrixRowSlice& operator=(const ConstVectorBase<T, V>& x)
00582          { return this->assignFrom(x); }
00584       MatrixRowSlice& operator=(const std::valarray<T>& x)
00585          { return this->assignFrom(x); }
00587       MatrixRowSlice& operator=(const T x)
00588          { return this->assignFrom(x); }
00590       MatrixRowSlice& operator=(const T* x)
00591          { return this->assignFrom(x); }
00592 
00594       T& operator[] (size_t j)
00595          { return (*m)(r, colStart() + j * colStride()); }
00597       T& operator() (size_t j)
00598          { return (*m)(r, colStart() + j * colStride()); }
00600       T operator[] (size_t j) const
00601          { return (*m)(r, colStart() + j * colStride()); }
00603       T operator() (size_t j) const
00604          { return (*m)(r, colStart() + j * colStride()); }
00606       T& operator() (size_t i, size_t j) 
00607          { return (*m)(i + r, colStart() + j * colStride()); }
00609       T operator() (size_t i, size_t j) const
00610          { return (*m)(i + r, colStart() + j * colStride()); }
00611 
00613       size_t rows() const {return 1;}
00615       size_t cols() const {return colSize();}
00617       size_t size() const {return colSize();}
00618 
00620       size_t rowSize() const { return 1; }
00622       size_t rowStart() const{ return r; }
00624       size_t rowStride() const { return 1; }
00626       size_t colSize() const { return cSlice.size(); }
00628       size_t colStart() const { return cSlice.start(); }
00630       size_t colStride() const { return cSlice.stride(); }
00631 
00632    private:
00634       Matrix<T>* m;
00636       size_t r;
00638       std::slice cSlice;
00639    };
00640 
00644    template <class T>
00645    class ConstMatrixRowSlice : public ConstMatrixSliceBase<T, ConstMatrixRowSlice<T> >
00646    {
00647    public:
00649       ConstMatrixRowSlice() 
00650             : m(NULL), r(0), cSlice(std::slice(0,0,0)) 
00651          {}
00653       ConstMatrixRowSlice(const Matrix<T>& mat, size_t row)
00654             : m(&mat), r(row), cSlice(std::slice(0,mat.cols(),1))
00655          { this->matSliceCheck(mat.rows(), mat.cols()); }
00656 
00658       ConstMatrixRowSlice(const Matrix<T>& mat, size_t row, 
00659                        const std::slice& s)
00660             : m(&mat), r(row), cSlice(s)
00661          { 
00662                // decide if the input is reasonable
00663             this->matSliceCheck(mat.rows(), mat.cols());
00664          }   
00665 
00667       T operator[] (size_t i) const
00668          { return (*m)(r, colStart() + i * colStride()); }
00670       T operator() (size_t i) const
00671          { return (*m)(r, colStart() + i * colStride()); }
00672 
00674       T operator() (size_t i, size_t j) const
00675          { return (*m)(i + r, colStart() + j * colStride()); }
00676 
00678       size_t rows() const {return 1;}
00680       size_t cols() const {return colSize();}
00682       size_t size() const {return colSize();}
00683 
00685       size_t rowSize() const { return 1; }
00687       size_t rowStart() const{ return r; }
00689       size_t rowStride() const { return 1; }
00691       size_t colSize() const { return cSlice.size(); }
00693       size_t colStart() const { return cSlice.start(); }
00695       size_t colStride() const { return cSlice.stride(); }
00696    private:
00698       const Matrix<T>* m;
00700       size_t r;
00702       std::slice cSlice;
00703    };
00704 
00706 
00707 }  // namespace
00708 
00709 #include "MatrixImplementation.hpp"
00710 #include "MatrixOperators.hpp"
00711 #include "MatrixFunctors.hpp"
00712 
00713 #endif

Generated on Mon May 20 03:31:07 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1