Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
25:143b19c1fb05
Eurobot 2012 Primary;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narshu 25:143b19c1fb05 1 /*
narshu 25:143b19c1fb05 2 * Tiny Vector Matrix Library
narshu 25:143b19c1fb05 3 * Dense Vector Matrix Libary of Tiny size using Expression Templates
narshu 25:143b19c1fb05 4 *
narshu 25:143b19c1fb05 5 * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
narshu 25:143b19c1fb05 6 *
narshu 25:143b19c1fb05 7 * This library is free software; you can redistribute it and/or
narshu 25:143b19c1fb05 8 * modify it under the terms of the GNU lesser General Public
narshu 25:143b19c1fb05 9 * License as published by the Free Software Foundation; either
narshu 25:143b19c1fb05 10 * version 2.1 of the License, or (at your option) any later version.
narshu 25:143b19c1fb05 11 *
narshu 25:143b19c1fb05 12 * This library is distributed in the hope that it will be useful,
narshu 25:143b19c1fb05 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
narshu 25:143b19c1fb05 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
narshu 25:143b19c1fb05 15 * lesser General Public License for more details.
narshu 25:143b19c1fb05 16 *
narshu 25:143b19c1fb05 17 * You should have received a copy of the GNU lesser General Public
narshu 25:143b19c1fb05 18 * License along with this library; if not, write to the Free Software
narshu 25:143b19c1fb05 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
narshu 25:143b19c1fb05 20 *
narshu 25:143b19c1fb05 21 * $Id: MatrixImpl.h,v 1.31 2007-06-23 15:58:58 opetzold Exp $
narshu 25:143b19c1fb05 22 */
narshu 25:143b19c1fb05 23
narshu 25:143b19c1fb05 24 #ifndef TVMET_MATRIX_IMPL_H
narshu 25:143b19c1fb05 25 #define TVMET_MATRIX_IMPL_H
narshu 25:143b19c1fb05 26
narshu 25:143b19c1fb05 27 #include <iomanip> // setw
narshu 25:143b19c1fb05 28
narshu 25:143b19c1fb05 29 #include <tvmet/Functional.h>
narshu 25:143b19c1fb05 30 #include <tvmet/Io.h>
narshu 25:143b19c1fb05 31
narshu 25:143b19c1fb05 32
narshu 25:143b19c1fb05 33 namespace tvmet {
narshu 25:143b19c1fb05 34
narshu 25:143b19c1fb05 35
narshu 25:143b19c1fb05 36 /*
narshu 25:143b19c1fb05 37 * member operators for i/o
narshu 25:143b19c1fb05 38 */
narshu 25:143b19c1fb05 39 template<class T, std::size_t NRows, std::size_t NCols>
narshu 25:143b19c1fb05 40 std::ostream& Matrix<T, NRows, NCols>::print_xpr(std::ostream& os, std::size_t l) const
narshu 25:143b19c1fb05 41 {
narshu 25:143b19c1fb05 42 os << IndentLevel(l++) << "Matrix[" << ops << "]<"
narshu 25:143b19c1fb05 43 << typeid(T).name() << ", " << Rows << ", " << Cols << ">,"
narshu 25:143b19c1fb05 44 << IndentLevel(--l)
narshu 25:143b19c1fb05 45 << std::endl;
narshu 25:143b19c1fb05 46
narshu 25:143b19c1fb05 47 return os;
narshu 25:143b19c1fb05 48 }
narshu 25:143b19c1fb05 49
narshu 25:143b19c1fb05 50
narshu 25:143b19c1fb05 51 template<class T, std::size_t NRows, std::size_t NCols>
narshu 25:143b19c1fb05 52 std::ostream& Matrix<T, NRows, NCols>::print_on(std::ostream& os) const
narshu 25:143b19c1fb05 53 {
narshu 25:143b19c1fb05 54 enum {
narshu 25:143b19c1fb05 55 complex_type = NumericTraits<value_type>::is_complex
narshu 25:143b19c1fb05 56 };
narshu 25:143b19c1fb05 57
narshu 25:143b19c1fb05 58 std::streamsize w = IoPrintHelper<Matrix>::width(dispatch<complex_type>(), *this);
narshu 25:143b19c1fb05 59
narshu 25:143b19c1fb05 60 os << std::setw(0) << "[\n";
narshu 25:143b19c1fb05 61 for(std::size_t i = 0; i < Rows; ++i) {
narshu 25:143b19c1fb05 62 os << " [";
narshu 25:143b19c1fb05 63 for(std::size_t j = 0; j < (Cols - 1); ++j) {
narshu 25:143b19c1fb05 64 os << std::setw(w) << this->operator()(i, j) << ", ";
narshu 25:143b19c1fb05 65 }
narshu 25:143b19c1fb05 66 os << std::setw(w) << this->operator()(i, Cols - 1)
narshu 25:143b19c1fb05 67 << (i != (Rows-1) ? "],\n" : "]\n");
narshu 25:143b19c1fb05 68 }
narshu 25:143b19c1fb05 69 os << "]";
narshu 25:143b19c1fb05 70
narshu 25:143b19c1fb05 71 return os;
narshu 25:143b19c1fb05 72 }
narshu 25:143b19c1fb05 73
narshu 25:143b19c1fb05 74
narshu 25:143b19c1fb05 75 /*
narshu 25:143b19c1fb05 76 * member operators with scalars, per se element wise
narshu 25:143b19c1fb05 77 */
narshu 25:143b19c1fb05 78 #define TVMET_IMPLEMENT_MACRO(NAME, OP) \
narshu 25:143b19c1fb05 79 template<class T, std::size_t NRows, std::size_t NCols> \
narshu 25:143b19c1fb05 80 inline \
narshu 25:143b19c1fb05 81 Matrix<T, NRows, NCols>& \
narshu 25:143b19c1fb05 82 Matrix<T, NRows, NCols>::operator OP (value_type rhs) { \
narshu 25:143b19c1fb05 83 typedef XprLiteral<value_type> expr_type; \
narshu 25:143b19c1fb05 84 this->M_##NAME(XprMatrix<expr_type, Rows, Cols>(expr_type(rhs))); \
narshu 25:143b19c1fb05 85 return *this; \
narshu 25:143b19c1fb05 86 }
narshu 25:143b19c1fb05 87
narshu 25:143b19c1fb05 88 TVMET_IMPLEMENT_MACRO(add_eq, +=)
narshu 25:143b19c1fb05 89 TVMET_IMPLEMENT_MACRO(sub_eq, -=)
narshu 25:143b19c1fb05 90 TVMET_IMPLEMENT_MACRO(mul_eq, *=)
narshu 25:143b19c1fb05 91 TVMET_IMPLEMENT_MACRO(div_eq, /=)
narshu 25:143b19c1fb05 92 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 93
narshu 25:143b19c1fb05 94
narshu 25:143b19c1fb05 95 #define TVMET_IMPLEMENT_MACRO(NAME, OP) \
narshu 25:143b19c1fb05 96 template<class T, std::size_t NRows, std::size_t NCols> \
narshu 25:143b19c1fb05 97 inline \
narshu 25:143b19c1fb05 98 Matrix<T, NRows, NCols>& \
narshu 25:143b19c1fb05 99 Matrix<T, NRows, NCols>::operator OP (std::size_t rhs) { \
narshu 25:143b19c1fb05 100 typedef XprLiteral<value_type> expr_type; \
narshu 25:143b19c1fb05 101 this->M_##NAME(XprMatrix<expr_type, Rows, Cols>(expr_type(rhs))); \
narshu 25:143b19c1fb05 102 return *this; \
narshu 25:143b19c1fb05 103 }
narshu 25:143b19c1fb05 104
narshu 25:143b19c1fb05 105 TVMET_IMPLEMENT_MACRO(mod_eq, %=)
narshu 25:143b19c1fb05 106 TVMET_IMPLEMENT_MACRO(xor_eq,^=)
narshu 25:143b19c1fb05 107 TVMET_IMPLEMENT_MACRO(and_eq, &=)
narshu 25:143b19c1fb05 108 TVMET_IMPLEMENT_MACRO(or_eq, |=)
narshu 25:143b19c1fb05 109 TVMET_IMPLEMENT_MACRO(shl_eq, <<=)
narshu 25:143b19c1fb05 110 TVMET_IMPLEMENT_MACRO(shr_eq, >>=)
narshu 25:143b19c1fb05 111 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 112
narshu 25:143b19c1fb05 113
narshu 25:143b19c1fb05 114 /*
narshu 25:143b19c1fb05 115 * member functions (operators) with matrizes, for use with +=,-= ... <<=
narshu 25:143b19c1fb05 116 */
narshu 25:143b19c1fb05 117 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 118 template<class T1, std::size_t NRows, std::size_t NCols> \
narshu 25:143b19c1fb05 119 template <class T2> \
narshu 25:143b19c1fb05 120 inline \
narshu 25:143b19c1fb05 121 Matrix<T1, NRows, NCols>& \
narshu 25:143b19c1fb05 122 Matrix<T1, NRows, NCols>::M_##NAME (const Matrix<T2, Rows, Cols>& rhs) { \
narshu 25:143b19c1fb05 123 this->M_##NAME( XprMatrix<typename Matrix<T2, Rows, Cols>::ConstReference, Rows, Cols>(rhs.const_ref()) ); \
narshu 25:143b19c1fb05 124 return *this; \
narshu 25:143b19c1fb05 125 }
narshu 25:143b19c1fb05 126
narshu 25:143b19c1fb05 127 TVMET_IMPLEMENT_MACRO(add_eq)
narshu 25:143b19c1fb05 128 TVMET_IMPLEMENT_MACRO(sub_eq)
narshu 25:143b19c1fb05 129 TVMET_IMPLEMENT_MACRO(mul_eq)
narshu 25:143b19c1fb05 130 TVMET_IMPLEMENT_MACRO(div_eq)
narshu 25:143b19c1fb05 131 TVMET_IMPLEMENT_MACRO(mod_eq)
narshu 25:143b19c1fb05 132 TVMET_IMPLEMENT_MACRO(xor_eq)
narshu 25:143b19c1fb05 133 TVMET_IMPLEMENT_MACRO(and_eq)
narshu 25:143b19c1fb05 134 TVMET_IMPLEMENT_MACRO(or_eq)
narshu 25:143b19c1fb05 135 TVMET_IMPLEMENT_MACRO(shl_eq)
narshu 25:143b19c1fb05 136 TVMET_IMPLEMENT_MACRO(shr_eq)
narshu 25:143b19c1fb05 137 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 138
narshu 25:143b19c1fb05 139
narshu 25:143b19c1fb05 140 /*
narshu 25:143b19c1fb05 141 * member functions (operators) with expressions, for use width +=,-= ... <<=
narshu 25:143b19c1fb05 142 */
narshu 25:143b19c1fb05 143 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 144 template<class T, std::size_t NRows, std::size_t NCols> \
narshu 25:143b19c1fb05 145 template<class E> \
narshu 25:143b19c1fb05 146 inline \
narshu 25:143b19c1fb05 147 Matrix<T, NRows, NCols>& \
narshu 25:143b19c1fb05 148 Matrix<T, NRows, NCols>::M_##NAME (const XprMatrix<E, Rows, Cols>& rhs) { \
narshu 25:143b19c1fb05 149 rhs.assign_to(*this, Fcnl_##NAME<value_type, typename E::value_type>()); \
narshu 25:143b19c1fb05 150 return *this; \
narshu 25:143b19c1fb05 151 }
narshu 25:143b19c1fb05 152
narshu 25:143b19c1fb05 153 TVMET_IMPLEMENT_MACRO(add_eq)
narshu 25:143b19c1fb05 154 TVMET_IMPLEMENT_MACRO(sub_eq)
narshu 25:143b19c1fb05 155 TVMET_IMPLEMENT_MACRO(mul_eq)
narshu 25:143b19c1fb05 156 TVMET_IMPLEMENT_MACRO(div_eq)
narshu 25:143b19c1fb05 157 TVMET_IMPLEMENT_MACRO(mod_eq)
narshu 25:143b19c1fb05 158 TVMET_IMPLEMENT_MACRO(xor_eq)
narshu 25:143b19c1fb05 159 TVMET_IMPLEMENT_MACRO(and_eq)
narshu 25:143b19c1fb05 160 TVMET_IMPLEMENT_MACRO(or_eq)
narshu 25:143b19c1fb05 161 TVMET_IMPLEMENT_MACRO(shl_eq)
narshu 25:143b19c1fb05 162 TVMET_IMPLEMENT_MACRO(shr_eq)
narshu 25:143b19c1fb05 163 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 164
narshu 25:143b19c1fb05 165
narshu 25:143b19c1fb05 166 /*
narshu 25:143b19c1fb05 167 * aliased member functions (operators) with matrizes,
narshu 25:143b19c1fb05 168 * for use with +=,-= ... <<=
narshu 25:143b19c1fb05 169 */
narshu 25:143b19c1fb05 170 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 171 template<class T1, std::size_t NRows, std::size_t NCols> \
narshu 25:143b19c1fb05 172 template <class T2> \
narshu 25:143b19c1fb05 173 inline \
narshu 25:143b19c1fb05 174 Matrix<T1, NRows, NCols>& \
narshu 25:143b19c1fb05 175 Matrix<T1, NRows, NCols>::alias_##NAME (const Matrix<T2, Rows, Cols>& rhs) { \
narshu 25:143b19c1fb05 176 this->alias_##NAME( XprMatrix<typename Matrix<T2, Rows, Cols>::ConstReference, Rows, Cols>(rhs.const_ref()) ); \
narshu 25:143b19c1fb05 177 return *this; \
narshu 25:143b19c1fb05 178 }
narshu 25:143b19c1fb05 179
narshu 25:143b19c1fb05 180 TVMET_IMPLEMENT_MACRO(assign)
narshu 25:143b19c1fb05 181 TVMET_IMPLEMENT_MACRO(add_eq)
narshu 25:143b19c1fb05 182 TVMET_IMPLEMENT_MACRO(sub_eq)
narshu 25:143b19c1fb05 183 TVMET_IMPLEMENT_MACRO(mul_eq)
narshu 25:143b19c1fb05 184 TVMET_IMPLEMENT_MACRO(div_eq)
narshu 25:143b19c1fb05 185 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 186
narshu 25:143b19c1fb05 187
narshu 25:143b19c1fb05 188 /*
narshu 25:143b19c1fb05 189 * aliased member functions (operators) with expressions,
narshu 25:143b19c1fb05 190 * for use width +=,-= ... <<= and aliased(),
narshu 25:143b19c1fb05 191 */
narshu 25:143b19c1fb05 192 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 193 template<class T, std::size_t NRows, std::size_t NCols> \
narshu 25:143b19c1fb05 194 template<class E> \
narshu 25:143b19c1fb05 195 inline \
narshu 25:143b19c1fb05 196 Matrix<T, NRows, NCols>& \
narshu 25:143b19c1fb05 197 Matrix<T, NRows, NCols>::alias_##NAME (const XprMatrix<E, Rows, Cols>& rhs) { \
narshu 25:143b19c1fb05 198 typedef Matrix<T, NRows, NCols> temp_type; \
narshu 25:143b19c1fb05 199 temp_type(rhs).assign_to(*this, Fcnl_##NAME<value_type, typename E::value_type>()); \
narshu 25:143b19c1fb05 200 return *this; \
narshu 25:143b19c1fb05 201 }
narshu 25:143b19c1fb05 202
narshu 25:143b19c1fb05 203 TVMET_IMPLEMENT_MACRO(assign)
narshu 25:143b19c1fb05 204 TVMET_IMPLEMENT_MACRO(add_eq)
narshu 25:143b19c1fb05 205 TVMET_IMPLEMENT_MACRO(sub_eq)
narshu 25:143b19c1fb05 206 TVMET_IMPLEMENT_MACRO(mul_eq)
narshu 25:143b19c1fb05 207 TVMET_IMPLEMENT_MACRO(div_eq)
narshu 25:143b19c1fb05 208 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 209
narshu 25:143b19c1fb05 210
narshu 25:143b19c1fb05 211 } // namespace tvmet
narshu 25:143b19c1fb05 212
narshu 25:143b19c1fb05 213 #endif // TVMET_MATRIX_IMPL_H
narshu 25:143b19c1fb05 214
narshu 25:143b19c1fb05 215 // Local Variables:
narshu 25:143b19c1fb05 216 // mode:C++
narshu 25:143b19c1fb05 217 // tab-width:8
narshu 25:143b19c1fb05 218 // End: