library of assembled shared sources

http://lass.cocamware.com

matrix.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @class lass::num::Matrix
00046  *  @brief a dynamic sized n-dimensional matrix with expression templates
00047  *  @author Bram de Greve [BdG]
00048  */
00049 
00050 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_MATRIX_H
00051 #define LASS_GUARDIAN_OF_INCLUSION_NUM_MATRIX_H
00052 
00053 #include "num_common.h"
00054 #include "num_traits.h"
00055 #include "impl/matrix_expressions.h"
00056 #include "../util/call_traits.h"
00057 #include "../util/scoped_ptr.h"
00058 
00059 namespace lass
00060 {
00061 namespace num
00062 {
00063 
00064 template
00065 <
00066     typename T,
00067     typename S = impl::MStorage<T>
00068 >
00069 class Matrix
00070 {
00071 public:
00072 
00073     typedef Matrix<T, S> TSelf;
00074     typedef S TStorage;
00075     typedef typename TStorage::TSize TSize;
00076 
00077     typedef typename util::CallTraits<T>::TValue TValue;
00078     typedef typename util::CallTraits<T>::TParam TParam;
00079     typedef typename util::CallTraits<T>::TReference TReference;
00080     typedef typename util::CallTraits<T>::TConstReference TConstReference;
00081     typedef num::NumTraits<T> TNumTraits;
00082     typedef TValue* TPointer;
00083 
00084     template <typename U> struct Rebind
00085     {
00086         typedef Matrix<U, S> Type;
00087     };
00088 
00089     class Row
00090     {
00091     public:
00092         T& operator[](size_t iJ) { return matrix_(row_, iJ); }
00093         const T& operator[](size_t iJ) const { return matrix_(row_, iJ); }
00094         T& at(size_t iJ) { return matrix_.at(row_, iJ); }
00095         const T& at(size_t iJ) const { return matrix_.at(row_, iJ); }
00096         const TSize size() const { return matrix_.columns(); }
00097     private:
00098         friend class Matrix<T, S>;
00099         Row(Matrix<T, S>& iMatrix, TSize iRow): matrix_(iMatrix), row_(iRow) {}
00100         Matrix<T, S>& matrix_;
00101         TSize row_;
00102     };
00103 
00104     class ConstRow
00105     {
00106     public:
00107         ConstRow(const Row& iOther): matrix_(iOther.matrix_), row_(iOther.row_) {}
00108         const T& operator[](size_t iJ) const { return matrix_(row_, iJ); }
00109         const T& at(size_t iJ) const { return matrix_.at(row_, iJ); }
00110         const TSize size() const { return matrix_.columns(); }
00111     private:
00112         friend class Matrix<T, S>;
00113         ConstRow(const Matrix<T, S>& iMatrix, TSize iRow): matrix_(iMatrix), row_(iRow) {}
00114         const Matrix<T, S>& matrix_;
00115         TSize row_;
00116     };
00117 
00118     class Column
00119     {
00120     public:
00121         T& operator[](size_t iI) { return matrix_(iI, column_); }
00122         const T& operator[](size_t iI) const { return matrix_(iI, column_); }
00123         T& at(size_t iI) { return matrix_.at(iI, column_); }
00124         const T& at(size_t iI) const { return matrix_.at(iI, column_); }
00125         const TSize size() const { return matrix_.rows(); }
00126     private:
00127         friend class Matrix<T, S>;
00128         Column(Matrix<T, S>& iMatrix, TSize iColumn): matrix_(iMatrix), column_(iColumn) {}
00129         Matrix<T, S>& matrix_;
00130         TSize column_;
00131     };
00132 
00133     class ConstColumn
00134     {
00135     public:
00136         ConstColumn(const Column& iOther): matrix_(iOther.matrix_), column_(iOther.column_) {}
00137         const T& operator[](size_t iI) const { return matrix_(iI, column_); }
00138         const T& at(size_t iI) const { return matrix_.at(iI, column_); }
00139         const TSize size() const { return matrix_.rows(); }
00140     private:
00141         friend class Matrix<T, S>;
00142         ConstColumn(const Matrix<T, S>& iMatrix, TSize iColumn): matrix_(iMatrix), column_(iColumn) {}
00143         const Matrix<T, S>& matrix_;
00144         TSize column_;
00145     };
00146 
00147     Matrix();
00148     explicit Matrix(TSize iRows, TSize iCols);
00149     explicit Matrix(const TStorage& iStorage);
00150     template <typename T2, typename S2> Matrix(const Matrix<T2, S2>& iOther);
00151 
00152     template <typename T2, typename S2> Matrix<T, S>& operator=(const Matrix<T2, S2>& iOther);
00153 
00154     const TSize rows() const;
00155     const TSize columns() const;
00156 
00157     const TValue operator()(TSize iRow, TSize iCol) const;
00158     TReference operator()(TSize iRow, TSize iCol);
00159     const TValue at(signed iRow, signed iCol) const;
00160     TReference at(signed iRow, signed iCol);
00161 
00162     ConstRow row(signed iRow) const;
00163     Row row(signed iRow);
00164     ConstColumn column(signed iColumn) const;
00165     Column column(signed iColumn);
00166 
00167     const Matrix<T, S>& operator+() const;
00168     const Matrix<T, impl::MNeg<T, S> > operator-() const;
00169 
00170     template <typename S2> Matrix<T, S>& operator+=(const Matrix<T, S2>& iB);
00171     template <typename S2> Matrix<T, S>& operator-=(const Matrix<T, S2>& iB);
00172     Matrix<T, S>& operator*=(TParam iB);
00173     Matrix<T, S>& operator/=(TParam iB);
00174 
00175     void setZero(TSize iRows, TSize iCols);
00176     void setIdentity(TSize iSize);
00177 
00178     bool isEmpty() const;
00179     bool isZero() const;
00180     bool isIdentity() const;
00181     bool isDiagonal() const;
00182     bool isSquare() const;
00183 
00184     const Matrix<T, impl::MTrans<T, S> > transpose() const;
00185 
00186     bool invert();
00187 
00188     const TStorage& storage() const;
00189     TStorage& storage();
00190     void swap(Matrix<T, S>& iOther);
00191 
00192 private:
00193 
00194     TStorage storage_;
00195 };
00196 
00197 template <typename T, typename S1, typename S2>
00198 bool operator==(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00199 template <typename T, typename S1, typename S2>
00200 inline bool operator!=(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00201 
00202 template <typename T, typename S1, typename S2>
00203 const Matrix<T, impl::MAdd<T, S1, S2> > operator+(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00204 template <typename T, typename S1, typename S2>
00205 const Matrix<T, impl::MSub<T, S1, S2> > operator-(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00206 template <typename T, typename S1, typename S2>
00207 const Matrix<T, impl::MProd<T, S1, S2> > operator*(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00208 
00209 template <typename T, typename S>
00210 const Matrix<T, impl::MAdd<T, impl::MScalar<T>, S> > operator+(const T& iA, const Matrix<T, S>& iB);
00211 template <typename T, typename S>
00212 const Matrix<T, impl::MSub<T, impl::MScalar<T>, S> > operator-(const T& iA, const Matrix<T, S>& iB);
00213 template <typename T, typename S>
00214 const Matrix<T, impl::MMul<T, impl::MScalar<T>, S> > operator*(const T& iA, const Matrix<T, S>& iB);
00215 
00216 template <typename T, typename S>
00217 const Matrix<T, impl::MAdd<T, S, impl::MScalar<T> > > operator+(const Matrix<T, S>& iA, const T& iB);
00218 template <typename T, typename S>
00219 const Matrix<T, impl::MAdd<T, S, impl::MScalar<T> > > operator-(const Matrix<T, S>& iA, const T& iB);
00220 template <typename T, typename S>
00221 const Matrix<T, impl::MMul<T, S, impl::MScalar<T> > > operator*(const Matrix<T, S>& iA, const T& iB);
00222 template <typename T, typename S>
00223 const Matrix<T, impl::MMul<T, S, impl::MScalar<T> > > operator/(const Matrix<T, S>& iA, const T& iB);
00224 
00225 template <typename T, typename S, typename S2>
00226 bool solve(const Matrix<T, S>& iA, Matrix<T, S2>& ioB);
00227 
00228 
00229 template <typename T, typename S, typename Char, typename Traits>
00230 std::basic_ostream<Char, Traits>&
00231 operator<<(std::basic_ostream<Char, Traits>& iS, const Matrix<T, S>& iA);
00232 
00233 }
00234 
00235 }
00236 
00237 #include "matrix.inl"
00238 
00239 #ifdef LASS_GUARDIAN_OF_INCLUSION_NUM_VECTOR_H
00240 #include "matrix_vector.h"
00241 #endif
00242 
00243 #endif
00244 
00245 // EOF

Generated on Mon Nov 10 14:20:31 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo