library of assembled shared sources

http://lass.cocamware.com

matrix_expressions.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 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_MATRIX_EXPRESSIONS_H
00046 #define LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_MATRIX_EXPRESSIONS_H
00047 
00048 #include "../num_common.h"
00049 
00050 namespace lass
00051 {
00052 namespace num
00053 {
00054 namespace impl
00055 {
00056 
00057 /** @internal
00058 */
00059 template <typename T>
00060 class MStorage
00061 {
00062 public:
00063     enum { lvalue = true };
00064     typedef typename util::CallTraits<T>::TValue TValue;
00065     typedef typename util::CallTraits<T>::TParam TParam;
00066     typedef typename util::CallTraits<T>::TReference TReference;
00067     typedef typename util::CallTraits<T>::TConstReference TConstReference;
00068     typedef size_t TSize;
00069 
00070     MStorage(): storage_(), rows_(0), cols_(0) {}
00071     MStorage(TSize iRows, TSize iCols): storage_(iRows * iCols, T()), rows_(iRows), cols_(iCols) {}
00072     TConstReference operator()(TSize iI, TSize iJ) const { return storage_[iI * cols_ + iJ]; }
00073     TReference operator()(TSize iI, TSize iJ) { return storage_[iI * cols_ + iJ]; }
00074     TSize rows() const { return rows_; }
00075     TSize columns() const { return cols_; }
00076 
00077     // special MStorage members:
00078     //
00079     void resize(TSize iRows, TSize iCols)
00080     {
00081         storage_.resize(iRows * iCols, T());
00082         rows_ = iRows;
00083         cols_ = iCols;
00084     }
00085     void swap(MStorage<T>& iOther)
00086     {
00087         storage_.swap(iOther.storage_);
00088         std::swap(rows_, iOther.rows_);
00089         std::swap(cols_, iOther.cols_);
00090     }
00091 
00092     typename std::vector<T>::iterator rowMajor() { return storage_.begin(); } 
00093     typename std::vector<T>::const_iterator rowMajor() const { return storage_.begin(); } 
00094 
00095 private:
00096     std::vector<T> storage_;
00097     TSize rows_;
00098     TSize cols_;
00099 };
00100 
00101 /** @internal
00102 */
00103 template <typename T>
00104 class MScalar
00105 {
00106 public:
00107     enum { lvalue = false };
00108     typedef typename util::CallTraits<T>::TValue TValue;
00109     typedef typename util::CallTraits<T>::TParam TParam;
00110     typedef size_t TSize;
00111 
00112     MScalar(TParam iValue, TSize iRows, TSize iCols): value_(iValue), rows_(iRows), cols_(iCols) {}
00113     TParam operator()(TSize iI, TSize iJ) const
00114     {
00115         LASS_ASSERT(iI < rows_ && iJ < cols_);
00116         return value_;
00117     }
00118     TSize rows() const { return rows_; }
00119     TSize columns() const { return cols_; }
00120 private:
00121     TValue value_;
00122     TSize rows_;
00123     TSize cols_;
00124 };
00125 
00126 
00127 
00128 template <typename ExpressionType>
00129 struct MatrixExpressionTraits
00130 {
00131     typedef const ExpressionType& TStorage;
00132 };
00133 
00134 template <typename T>
00135 struct MatrixExpressionTraits<MScalar<T> >
00136 {
00137     typedef MScalar<T> TStorage;
00138 };
00139 
00140 
00141 
00142 #define LASS_NUM_MATRIX_BINARY_EXPRESSION(i_name, c_operator)\
00143 template <typename T, typename Operand1, typename Operand2>\
00144 class LASS_CONCATENATE(M, i_name)\
00145 {\
00146 public:\
00147     enum { lvalue = false };\
00148     typedef typename util::CallTraits<T>::TValue TValue;\
00149     typedef size_t TSize;\
00150     LASS_CONCATENATE(M, i_name)(const Operand1& iA, const Operand2& iB):\
00151         operand1_(iA), operand2_(iB)\
00152     {\
00153         LASS_ASSERT(operand1_.rows() == operand2_.rows() &&\
00154             operand1_.columns() == operand2_.columns());\
00155     }\
00156     TValue operator()(TSize iI, TSize iJ) const\
00157     {\
00158         return operand1_(iI, iJ) c_operator operand2_(iI, iJ);\
00159     }\
00160     TSize rows() const { return operand1_.rows(); }\
00161     TSize columns() const { return operand1_.columns(); }\
00162 private:\
00163     typename MatrixExpressionTraits<Operand1>::TStorage operand1_;\
00164     typename MatrixExpressionTraits<Operand2>::TStorage operand2_;\
00165 }
00166 
00167 LASS_NUM_MATRIX_BINARY_EXPRESSION(Add, +);
00168 LASS_NUM_MATRIX_BINARY_EXPRESSION(Sub, -);
00169 LASS_NUM_MATRIX_BINARY_EXPRESSION(Mul, *);
00170 
00171 
00172 
00173 #define LASS_NUM_MATRIX_UNARY_EXPRESSION(i_name, c_operator)\
00174 template <typename T, typename Operand1>\
00175 class LASS_CONCATENATE(M, i_name)\
00176 {\
00177 public:\
00178     enum { lvalue = false };\
00179     typedef typename util::CallTraits<T>::TValue TValue;\
00180     typedef size_t TSize;\
00181     LASS_CONCATENATE(M, i_name)(const Operand1& iA):\
00182         operand1_(iA)\
00183     {\
00184     }\
00185     TValue operator()(TSize iRow, TSize iCol) const\
00186     {\
00187         return c_operator operand1_(iRow, iCol);\
00188     }\
00189     TSize rows() const { return operand1_.rows(); }\
00190     TSize columns() const { return operand1_.columns(); }\
00191 private:\
00192     typename MatrixExpressionTraits<Operand1>::TStorage operand1_;\
00193 }
00194 
00195 LASS_NUM_MATRIX_UNARY_EXPRESSION(Neg, -);
00196 LASS_NUM_MATRIX_UNARY_EXPRESSION(Rec, ::lass::num::NumTraits<T>::one /);
00197 
00198 
00199 
00200 /** @internal
00201 *  matrix product
00202 */
00203 template <typename T, typename Operand1, typename Operand2>
00204 class MProd
00205 {
00206 public:
00207     enum { lvalue = false };
00208     typedef typename util::CallTraits<T>::TValue TValue;
00209     typedef size_t TSize;
00210     MProd(const Operand1& iA, const Operand2& iB):
00211         operand1_(iA), operand2_(iB), loopSize_(iA.columns())
00212     {
00213         LASS_ASSERT(operand1_.columns() == operand2_.rows());
00214     }
00215     TValue operator()(TSize iI, TSize iJ) const
00216     {
00217         TValue result = TValue();
00218         for (TSize k = 0; k < loopSize_; ++k)
00219         {
00220             result += operand1_(iI, k) * operand2_(k, iJ);
00221         }
00222         return result;
00223     }
00224     TSize rows() const { return operand1_.rows(); }
00225     TSize columns() const { return operand2_.columns(); }
00226 private:
00227     typename MatrixExpressionTraits<Operand1>::TStorage operand1_;
00228     typename MatrixExpressionTraits<Operand2>::TStorage operand2_;
00229     TSize loopSize_;
00230 };
00231 
00232 
00233 /** @internal
00234 *  transpose matrix
00235 */
00236 template <typename T, typename Operand1>
00237 class MTrans
00238 {
00239 public:
00240     enum { lvalue = false };
00241     typedef typename util::CallTraits<T>::TParam TParam;
00242     typedef size_t TSize;
00243     MTrans(const Operand1& iA): operand1_(iA) {}
00244     TParam operator()(TSize iI, TSize iJ) const { return operand1_(iJ, iI);
00245 }
00246     TSize rows() const { return operand1_.columns(); }
00247     TSize columns() const { return operand1_.rows(); }
00248 private:
00249     typename MatrixExpressionTraits<Operand1>::TStorage operand1_;
00250 };
00251 
00252 
00253 
00254 /** @internal
00255 *  apply function
00256 */
00257 template <typename T, typename Operand1>
00258 class MFun
00259 {
00260 public:
00261     enum { lvalue = false };
00262     typedef T (*TOperator)(T);
00263     typedef typename util::CallTraits<T>::TValue TValue;
00264     typedef size_t TSize;
00265     MFun(const Operand1& iA, TOperator iOperator): operand1_(iA),
00266 operator_(iOperator) {}
00267     TValue operator()(TSize iI, TSize iJ) const { return
00268 operator_(operand1_(iI, iJ)); }
00269     TSize rows() const { return operand1_.rows(); }\
00270     TSize columns() const { return operand1_.columns(); }\
00271 private:
00272     typename MatrixExpressionTraits<Operand1>::TStorage operand1_;
00273     TOperator operator_;
00274 };
00275 
00276 
00277 
00278 }
00279 
00280 }
00281 
00282 }
00283 
00284 #endif
00285 
00286 // 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