SourceForge Logo Tiny Vector Matrix library using Expression Templates Sourceforge Project Page

include/tvmet/MatrixImpl.h

Go to the documentation of this file.
00001 /*
00002  * Tiny Vector Matrix Library
00003  * Dense Vector Matrix Libary of Tiny size using Expression Templates
00004  *
00005  * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * $Id: MatrixImpl.h,v 1.31 2007-06-23 15:58:58 opetzold Exp $
00022  */
00023 
00024 #ifndef TVMET_MATRIX_IMPL_H
00025 #define TVMET_MATRIX_IMPL_H
00026 
00027 #include <iomanip>      // setw
00028 
00029 #include <tvmet/Functional.h>
00030 #include <tvmet/Io.h>
00031 
00032 
00033 namespace tvmet {
00034 
00035 
00036 /*
00037  * member operators for i/o
00038  */
00039 template<class T, std::size_t NRows, std::size_t NCols>
00040 std::ostream& Matrix<T, NRows, NCols>::print_xpr(std::ostream& os, std::size_t l) const
00041 {
00042   os << IndentLevel(l++) << "Matrix[" << ops << "]<"
00043      << typeid(T).name() << ", " << Rows << ", " << Cols << ">,"
00044      << IndentLevel(--l)
00045      << std::endl;
00046 
00047   return os;
00048 }
00049 
00050 
00051 template<class T, std::size_t NRows, std::size_t NCols>
00052 std::ostream& Matrix<T, NRows, NCols>::print_on(std::ostream& os) const
00053 {
00054   enum {
00055     complex_type = NumericTraits<value_type>::is_complex
00056   };
00057 
00058   std::streamsize w = IoPrintHelper<Matrix>::width(dispatch<complex_type>(), *this);
00059 
00060   os << std::setw(0) << "[\n";
00061   for(std::size_t i = 0; i < Rows; ++i) {
00062     os << " [";
00063     for(std::size_t j = 0; j < (Cols - 1); ++j) {
00064       os << std::setw(w) << this->operator()(i, j) << ", ";
00065     }
00066     os << std::setw(w) << this->operator()(i, Cols - 1)
00067        << (i != (Rows-1) ? "],\n" : "]\n");
00068     }
00069   os << "]";
00070 
00071   return os;
00072 }
00073 
00074 
00075 /*
00076  * member operators with scalars, per se element wise
00077  */
00078 #define TVMET_IMPLEMENT_MACRO(NAME, OP)           \
00079 template<class T, std::size_t NRows, std::size_t NCols>       \
00080 inline                    \
00081 Matrix<T, NRows, NCols>&              \
00082 Matrix<T, NRows, NCols>::operator OP (value_type rhs) {       \
00083   typedef XprLiteral<value_type>      expr_type;  \
00084   this->M_##NAME(XprMatrix<expr_type, Rows, Cols>(expr_type(rhs))); \
00085   return *this;                 \
00086 }
00087 
00088 TVMET_IMPLEMENT_MACRO(add_eq, +=)
00089 TVMET_IMPLEMENT_MACRO(sub_eq, -=)
00090 TVMET_IMPLEMENT_MACRO(mul_eq, *=)
00091 TVMET_IMPLEMENT_MACRO(div_eq, /=)
00092 #undef TVMET_IMPLEMENT_MACRO
00093 
00094 
00095 #define TVMET_IMPLEMENT_MACRO(NAME, OP)           \
00096 template<class T, std::size_t NRows, std::size_t NCols>       \
00097 inline                    \
00098 Matrix<T, NRows, NCols>&              \
00099 Matrix<T, NRows, NCols>::operator OP (std::size_t rhs) {      \
00100   typedef XprLiteral<value_type>      expr_type;  \
00101   this->M_##NAME(XprMatrix<expr_type, Rows, Cols>(expr_type(rhs))); \
00102   return *this;                 \
00103 }
00104 
00105 TVMET_IMPLEMENT_MACRO(mod_eq, %=)
00106 TVMET_IMPLEMENT_MACRO(xor_eq,^=)
00107 TVMET_IMPLEMENT_MACRO(and_eq, &=)
00108 TVMET_IMPLEMENT_MACRO(or_eq, |=)
00109 TVMET_IMPLEMENT_MACRO(shl_eq, <<=)
00110 TVMET_IMPLEMENT_MACRO(shr_eq, >>=)
00111 #undef TVMET_IMPLEMENT_MACRO
00112 
00113 
00114 /*
00115  *  member functions (operators) with matrizes, for use with +=,-= ... <<=
00116  */
00117 #define TVMET_IMPLEMENT_MACRO(NAME)                      \
00118 template<class T1, std::size_t NRows, std::size_t NCols>                 \
00119 template <class T2>                          \
00120 inline                               \
00121 Matrix<T1, NRows, NCols>&                        \
00122 Matrix<T1, NRows, NCols>::M_##NAME (const Matrix<T2, Rows, Cols>& rhs) {             \
00123   this->M_##NAME( XprMatrix<typename Matrix<T2, Rows, Cols>::ConstReference, Rows, Cols>(rhs.const_ref()) ); \
00124   return *this;                            \
00125 }
00126 
00127 TVMET_IMPLEMENT_MACRO(add_eq)
00128 TVMET_IMPLEMENT_MACRO(sub_eq)
00129 TVMET_IMPLEMENT_MACRO(mul_eq)
00130 TVMET_IMPLEMENT_MACRO(div_eq)
00131 TVMET_IMPLEMENT_MACRO(mod_eq)
00132 TVMET_IMPLEMENT_MACRO(xor_eq)
00133 TVMET_IMPLEMENT_MACRO(and_eq)
00134 TVMET_IMPLEMENT_MACRO(or_eq)
00135 TVMET_IMPLEMENT_MACRO(shl_eq)
00136 TVMET_IMPLEMENT_MACRO(shr_eq)
00137 #undef TVMET_IMPLEMENT_MACRO
00138 
00139 
00140 /*
00141  * member functions (operators) with expressions, for use width +=,-= ... <<=
00142  */
00143 #define TVMET_IMPLEMENT_MACRO(NAME)            \
00144 template<class T, std::size_t NRows, std::size_t NCols>        \
00145 template<class E>                \
00146 inline                     \
00147 Matrix<T, NRows, NCols>&               \
00148 Matrix<T, NRows, NCols>::M_##NAME (const XprMatrix<E, Rows, Cols>& rhs) {  \
00149   rhs.assign_to(*this, Fcnl_##NAME<value_type, typename E::value_type>()); \
00150   return *this;                  \
00151 }
00152 
00153 TVMET_IMPLEMENT_MACRO(add_eq)
00154 TVMET_IMPLEMENT_MACRO(sub_eq)
00155 TVMET_IMPLEMENT_MACRO(mul_eq)
00156 TVMET_IMPLEMENT_MACRO(div_eq)
00157 TVMET_IMPLEMENT_MACRO(mod_eq)
00158 TVMET_IMPLEMENT_MACRO(xor_eq)
00159 TVMET_IMPLEMENT_MACRO(and_eq)
00160 TVMET_IMPLEMENT_MACRO(or_eq)
00161 TVMET_IMPLEMENT_MACRO(shl_eq)
00162 TVMET_IMPLEMENT_MACRO(shr_eq)
00163 #undef TVMET_IMPLEMENT_MACRO
00164 
00165 
00166 /*
00167  * aliased member functions (operators) with matrizes,
00168  * for use with +=,-= ... <<=
00169  */
00170 #define TVMET_IMPLEMENT_MACRO(NAME)                    \
00171 template<class T1, std::size_t NRows, std::size_t NCols>               \
00172 template <class T2>                        \
00173 inline                             \
00174 Matrix<T1, NRows, NCols>&                      \
00175 Matrix<T1, NRows, NCols>::alias_##NAME (const Matrix<T2, Rows, Cols>& rhs) {           \
00176   this->alias_##NAME( XprMatrix<typename Matrix<T2, Rows, Cols>::ConstReference, Rows, Cols>(rhs.const_ref()) ); \
00177   return *this;                          \
00178 }
00179 
00180 TVMET_IMPLEMENT_MACRO(assign)
00181 TVMET_IMPLEMENT_MACRO(add_eq)
00182 TVMET_IMPLEMENT_MACRO(sub_eq)
00183 TVMET_IMPLEMENT_MACRO(mul_eq)
00184 TVMET_IMPLEMENT_MACRO(div_eq)
00185 #undef TVMET_IMPLEMENT_MACRO
00186 
00187 
00188 /*
00189  * aliased member functions (operators) with expressions,
00190  * for use width +=,-= ... <<= and aliased(),
00191  */
00192 #define TVMET_IMPLEMENT_MACRO(NAME)                 \
00193 template<class T, std::size_t NRows, std::size_t NCols>             \
00194 template<class E>                     \
00195 inline                          \
00196 Matrix<T, NRows, NCols>&                    \
00197 Matrix<T, NRows, NCols>::alias_##NAME (const XprMatrix<E, Rows, Cols>& rhs) {       \
00198   typedef Matrix<T, NRows, NCols>       temp_type;          \
00199   temp_type(rhs).assign_to(*this, Fcnl_##NAME<value_type, typename E::value_type>()); \
00200   return *this;                       \
00201 }
00202 
00203 TVMET_IMPLEMENT_MACRO(assign)
00204 TVMET_IMPLEMENT_MACRO(add_eq)
00205 TVMET_IMPLEMENT_MACRO(sub_eq)
00206 TVMET_IMPLEMENT_MACRO(mul_eq)
00207 TVMET_IMPLEMENT_MACRO(div_eq)
00208 #undef TVMET_IMPLEMENT_MACRO
00209 
00210 
00211 } // namespace tvmet
00212 
00213 #endif // TVMET_MATRIX_IMPL_H
00214 
00215 // Local Variables:
00216 // mode:C++
00217 // tab-width:8
00218 // End:

Author: