00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024 #ifndef SHMATRIXIMPL_HPP
00025 #define SHMATRIXIMPL_HPP
00026
00027 #include <iostream>
00028 #include <algorithm>
00029 #include <string>
00030 #include <sstream>
00031 #include "ShMatrix.hpp"
00032 #include "ShUtility.hpp"
00033
00034 namespace SH {
00035
00036
00037 template<int Rows, int Cols, ShBindingType Binding, typename T>
00038 ShMatrix<Rows, Cols, Binding, T>::ShMatrix()
00039 {
00040 if (Binding != SH_INPUT) {
00041 for (int i = 0; i < std::min(Rows, Cols); i++)
00042 m_data[i][i] = 1.0;
00043 }
00044 }
00045
00046 template<int Rows, int Cols, ShBindingType Binding, typename T>
00047 ShMatrix<Rows, Cols, Binding, T>::ShMatrix(const ShMatrix<Rows, Cols, Binding, T>& other)
00048 {
00049 for (int i = 0; i < Rows; i++)
00050 m_data[i] = other[i];
00051 }
00052
00053 template<int Rows, int Cols, ShBindingType Binding, typename T>
00054 template<ShBindingType Binding2>
00055 ShMatrix<Rows, Cols, Binding, T>::ShMatrix(const ShMatrix<Rows, Cols, Binding2, T>& other)
00056 {
00057 for (int i = 0; i < Rows; i++)
00058 m_data[i] = other[i];
00059 }
00060
00061 template<int Rows, int Cols, ShBindingType Binding, typename T>
00062 ShMatrix<Rows, Cols, Binding, T>::~ShMatrix()
00063 {
00064 }
00065
00066
00067 template<int Rows, int Cols, ShBindingType Binding, typename T>
00068 ShMatrix<Rows, Cols, Binding, T>&
00069 ShMatrix<Rows, Cols, Binding, T>::operator=(const ShMatrix<Rows, Cols, Binding, T>& other)
00070 {
00071 for (int i = 0; i < Rows; i++)
00072 m_data[i] = other[i];
00073 return *this;
00074 }
00075
00076 template<int Rows, int Cols, ShBindingType Binding, typename T>
00077 template<ShBindingType Binding2>
00078 ShMatrix<Rows, Cols, Binding, T>&
00079 ShMatrix<Rows, Cols, Binding, T>::operator=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00080 {
00081 for (int i = 0; i < Rows; i++)
00082 m_data[i] = other[i];
00083 return *this;
00084 }
00085
00086 template<int Rows, int Cols, ShBindingType Binding, typename T>
00087 ShMatrix<Rows, Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator=(const T& scalar)
00088 {
00089 for (int i = 0; i < Rows; i++) {
00090 m_data[i] = fillcast<Cols>(0.0);
00091 if (i < Cols) {
00092 m_data[i][i] = scalar;
00093 }
00094 }
00095 return *this;
00096 }
00097
00098 template<int Rows, int Cols, ShBindingType Binding, typename T>
00099 ShMatrix<Rows, Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator=(const ShGeneric<1, T>& scalar)
00100 {
00101 for (int i = 0; i < Rows; i++) {
00102 m_data[i] = fillcast<Cols>(0.0);
00103 if (i < Cols) {
00104 m_data[i][i] = scalar;
00105 }
00106 }
00107 return *this;
00108 }
00109
00110 template<int Rows, int Cols, ShBindingType Binding, typename T>
00111 ShAttrib<Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator[](int i)
00112 {
00113 return m_data[i];
00114 }
00115
00116 template<int Rows, int Cols, ShBindingType Binding, typename T>
00117 const ShAttrib<Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator[](int i) const
00118 {
00119 return m_data[i];
00120 }
00121
00122 template<int Rows, int Cols, ShBindingType Binding, typename T>
00123 template<ShBindingType Binding2>
00124 ShMatrix<Rows, Cols, Binding, T>&
00125 ShMatrix<Rows, Cols, Binding, T>::operator+=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00126 {
00127 for (int i = 0; i < Rows; i++)
00128 m_data[i] += other[i];
00129 return *this;
00130 }
00131
00132 template<int Rows, int Cols, ShBindingType Binding, typename T>
00133 template<ShBindingType Binding2>
00134 ShMatrix<Rows, Cols, Binding, T>&
00135 ShMatrix<Rows, Cols, Binding, T>::operator-=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00136 {
00137 for (int i = 0; i < Rows; i++)
00138 m_data[i] -= other[i];
00139 return *this;
00140 }
00141
00142 template<int Rows, int Cols, ShBindingType Binding, typename T>
00143 template<ShBindingType Binding2>
00144 ShMatrix<Rows, Cols, Binding, T>&
00145 ShMatrix<Rows, Cols, Binding, T>::operator/=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00146 {
00147 for (int i = 0; i < Rows; i++)
00148 m_data[i] /= other[i];
00149 return *this;
00150 }
00151
00152 template<int Rows, int Cols, ShBindingType Binding, typename T>
00153 template<ShBindingType Binding2>
00154 ShMatrix<Rows, Cols, Binding, T>&
00155 ShMatrix<Rows, Cols, Binding, T>::operator*=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00156 {
00157 ShMatrix<Rows, Cols, SH_TEMP, T> r = *this * other;
00158 *this = r;
00159 return *this;
00160 }
00161
00162 template<int Rows, int Cols, ShBindingType Binding, typename T>
00163 ShMatrix<Rows, Cols, Binding, T>&
00164 ShMatrix<Rows, Cols, Binding, T>::operator-()
00165 {
00166 for (int i = 0; i < Rows; i++)
00167 m_data[i] = -(m_data[i]);
00168 return *this;
00169 }
00170
00171 template<int Rows, int Cols, ShBindingType Binding, typename T>
00172 ShMatrix<Rows, Cols, Binding, T>&
00173 ShMatrix<Rows, Cols, Binding, T>::operator*=(const ShGeneric<1, T>& other)
00174 {
00175 for (int i = 0; i < Rows; i++)
00176 m_data[i] *= other;
00177 return *this;
00178 }
00179
00180 template<int Rows, int Cols, ShBindingType Binding, typename T>
00181 ShMatrix<Rows, Cols, Binding, T>&
00182 ShMatrix<Rows, Cols, Binding, T>::operator/=(const ShGeneric<1, T>& other)
00183 {
00184 for (int i = 0; i < Rows; i++)
00185 m_data[i] /= other;
00186 return *this;
00187 }
00188
00189 template<int Rows, int Cols, ShBindingType Binding, typename T>
00190 ShMatrixRows<Rows, Cols, T>
00191 ShMatrix<Rows, Cols, Binding, T>::operator()() const
00192 {
00193 return ShMatrixRows<Rows, Cols, T>(*this);
00194 }
00195
00196 template<int Rows, int Cols, ShBindingType Binding, typename T>
00197 ShMatrixRows<1, Cols, T>
00198 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0) const
00199 {
00200 return ShMatrixRows<1, Cols, T>(*this, i0);
00201 }
00202
00203 template<int Rows, int Cols, ShBindingType Binding, typename T>
00204 ShMatrixRows<2, Cols, T>
00205 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1) const
00206 {
00207 return ShMatrixRows<2, Cols, T>(*this, i0, i1);
00208 }
00209
00210 template<int Rows, int Cols, ShBindingType Binding, typename T>
00211 ShMatrixRows<3, Cols, T>
00212 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1, int i2) const
00213 {
00214 return ShMatrixRows<3, Cols, T>(*this, i0, i1, i2);
00215 }
00216
00217 template<int Rows, int Cols, ShBindingType Binding, typename T>
00218 ShMatrixRows<4, Cols, T>
00219 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1, int i2, int i3) const
00220 {
00221 return ShMatrixRows<4, Cols, T>(*this, i0, i1, i2, i3);
00222 }
00223
00224 template<int Rows, int Cols, ShBindingType Binding, typename T>
00225 std::ostream& operator<<(std::ostream& out,
00226 const ShMatrix<Rows, Cols, Binding, T>& m)
00227 {
00228 for (int k = 0; k < Rows; k++) {
00229 out << m[k];
00230 out << std::endl;
00231 }
00232
00233 return out;
00234 }
00235
00236
00237 template<int Rows, int Cols, ShBindingType Binding, typename T>
00238 ShMatrix<Rows - 1, Cols -1, SH_TEMP, T>
00239 ShMatrix<Rows, Cols, Binding, T>::subMatrix(int rowToRemove,
00240 int columnToRemove) const
00241 {
00242 ShMatrix<Rows - 1, Cols - 1, SH_TEMP, T> myMatrix;
00243
00244 int indices[Cols - 1];
00245 for (int i = 0; i < columnToRemove; i++)
00246 indices[i] = i;
00247 for (int i = columnToRemove + 1; i < Cols; i++)
00248 indices[i - 1] = i;
00249
00250 for (int i = 0; i < rowToRemove; i++) {
00251 myMatrix[i].clone(m_data[i].template swiz<Cols - 1>(indices));
00252 }
00253
00254 for (int i = rowToRemove + 1; i < Rows; i++) {
00255 myMatrix[i - 1].clone(m_data[i].template swiz<Cols - 1>(indices));
00256 }
00257
00258 return myMatrix;
00259 }
00260
00261 template<int Rows,int Cols,ShBindingType Binding, typename T>
00262 void ShMatrix<Rows, Cols, Binding, T>::setTranslation(const ShGeneric<Rows-1, T>& trans){
00263
00264 for(int i = 0;i<(Rows-1);i++)
00265 m_data[i][(Cols-1)] = trans[i];
00266 }
00267
00268
00269 template<int Rows,int Cols,ShBindingType Binding, typename T>
00270 void ShMatrix<Rows, Cols, Binding, T>::setScaling(const ShGeneric<Rows-1, T>& scale){
00271 for(int i = 0;i<(Rows-1);i++)
00272 m_data[i][i] = scale[i];
00273 }
00274
00276
00278
00279
00280 template<int Rows, int Cols, typename T>
00281 template<int OR, ShBindingType Binding>
00282 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00283 int i0)
00284 {
00285 SH_STATIC_CHECK(Rows == 1, Constructing_Non_1_Row_Matrix_From_1_Row);
00286
00287 m_data[0].clone(source[i0]);
00288 }
00289
00290 template<int Rows, int Cols, typename T>
00291 template<int OR, ShBindingType Binding>
00292 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00293 int i0, int i1)
00294 {
00295 SH_STATIC_CHECK(Rows == 2, Constructing_Non_2_Row_Matrix_From_2_Rows);
00296
00297 m_data[0].clone(source[i0]);
00298 m_data[1].clone(source[i1]);
00299 }
00300
00301 template<int Rows, int Cols, typename T>
00302 template<int OR, ShBindingType Binding>
00303 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00304 int i0, int i1, int i2)
00305 {
00306 SH_STATIC_CHECK(Rows == 3, Constructing_Non_3_Row_Matrix_From_3_Rows);
00307
00308 m_data[0].clone(source[i0]);
00309 m_data[1].clone(source[i1]);
00310 m_data[2].clone(source[i2]);
00311 }
00312
00313 template<int Rows, int Cols, typename T>
00314 template<int OR, ShBindingType Binding>
00315 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00316 int i0, int i1, int i2, int i3)
00317 {
00318 SH_STATIC_CHECK(Rows == 4, Constructing_Non_4_Row_Matrix_From_4_Rows);
00319
00320 m_data[0].clone(source[i0]);
00321 m_data[1].clone(source[i1]);
00322 m_data[2].clone(source[i2]);
00323 m_data[3].clone(source[i3]);
00324 }
00325
00326 template<int Rows, int Cols, typename T>
00327 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrixRows<Rows, Cols, T>& other)
00328 {
00329
00330 for (int i = 0; i < Rows; i++)
00331 m_data[i] = other.m_data[i];
00332 }
00333
00334 template<int Rows, int Cols, typename T>
00335 ShMatrixRows<Rows, Cols, T>&
00336 ShMatrixRows<Rows, Cols, T>::operator=(const ShMatrixRows<Rows, Cols, T>& other)
00337 {
00338
00339 for (int i = 0; i < Rows; i++)
00340 m_data[i] = other.m_data[i];
00341 }
00342
00343 template<int Rows, int Cols, typename T>
00344 ShMatrix<Rows, Cols, SH_TEMP, T>
00345 ShMatrixRows<Rows, Cols, T>::operator()() const
00346 {
00347 ShMatrix<Rows, Cols, SH_TEMP, T> r;
00348 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i]);
00349 return r;
00350 }
00351
00352 template<int Rows, int Cols, typename T>
00353 ShMatrix<Rows, 1, SH_TEMP, T>
00354 ShMatrixRows<Rows, Cols, T>::operator()(int i0) const
00355 {
00356 ShMatrix<Rows, 1, SH_TEMP, T> r;
00357 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0));
00358 return r;
00359 }
00360
00361 template<int Rows, int Cols, typename T>
00362 ShMatrix<Rows, 2, SH_TEMP, T>
00363 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1) const
00364 {
00365 ShMatrix<Rows, 2, SH_TEMP, T> r;
00366 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1));
00367 return r;
00368 }
00369
00370 template<int Rows, int Cols, typename T>
00371 ShMatrix<Rows, 3, SH_TEMP, T>
00372 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1, int i2) const
00373 {
00374 ShMatrix<Rows, 3, SH_TEMP, T> r;
00375 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1, i2));
00376 return r;
00377 }
00378
00379 template<int Rows, int Cols, typename T>
00380 ShMatrix<Rows, 4, SH_TEMP, T>
00381 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1, int i2, int i3) const
00382 {
00383 ShMatrix<Rows, 4, SH_TEMP, T> r;
00384 for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1, i2, i3));
00385 return r;
00386 }
00387
00388 template<int Rows, int Cols, ShBindingType Binding, typename T>
00389 void ShMatrix<Rows, Cols, Binding, T>::range(host_type low, host_type high)
00390 {
00391 for (int i = 0; i < Rows; i++) m_data[i].range(low, high);
00392 }
00393
00394 template<int Rows, int Cols, ShBindingType Binding, typename T>
00395 std::string ShMatrix<Rows, Cols, Binding, T>::name() const
00396 {
00397 return ShMeta::name();
00398 }
00399
00400 template<int Rows, int Cols, ShBindingType Binding, typename T>
00401 void ShMatrix<Rows, Cols, Binding, T>::name(const std::string& n)
00402 {
00403 ShMeta::name(n);
00404
00405 for (int i = 0; i < Rows; i++)
00406 {
00407 std::stringstream s;
00408 s << n << ".row[" << i << "]";
00409 m_data[i].name(s.str());
00410 }
00411 }
00412
00413 template<int Rows, int Cols, ShBindingType Binding, typename T>
00414 bool ShMatrix<Rows, Cols, Binding, T>:: has_name() const
00415 {
00416 return ShMeta::has_name();
00417 }
00418
00419 template<int Rows, int Cols, ShBindingType Binding, typename T>
00420 bool ShMatrix<Rows, Cols, Binding, T>::internal() const
00421 {
00422 return ShMeta::internal();
00423 }
00424
00425 template<int Rows, int Cols, ShBindingType Binding, typename T>
00426 void ShMatrix<Rows, Cols, Binding, T>::internal(bool internal)
00427 {
00428 ShMeta::internal(internal);
00429
00430 for (int i = 0; i < Rows; i++)
00431 {
00432 m_data[i].internal(internal);
00433 }
00434 }
00435
00436 template<int Rows, int Cols, ShBindingType Binding, typename T>
00437 std::string ShMatrix<Rows, Cols, Binding, T>::title() const
00438 {
00439 return ShMeta::title();
00440 }
00441
00442 template<int Rows, int Cols, ShBindingType Binding, typename T>
00443 void ShMatrix<Rows, Cols, Binding, T>::title(const std::string& t)
00444 {
00445 ShMeta::title(t);
00446
00447 for (int i = 0; i < Rows; i++)
00448 {
00449 std::stringstream s;
00450 s << t << ".row[" << i << "]";
00451 m_data[i].title(s.str());
00452 }
00453 }
00454
00455 template<int Rows, int Cols, ShBindingType Binding, typename T>
00456 std::string ShMatrix<Rows, Cols, Binding, T>::description() const
00457 {
00458 return ShMeta::description();
00459 }
00460
00461 template<int Rows, int Cols, ShBindingType Binding, typename T>
00462 void ShMatrix<Rows, Cols, Binding, T>::description(const std::string& d)
00463 {
00464 ShMeta::description(d);
00465
00466 for (int i = 0; i < Rows; i++)
00467 {
00468 std::stringstream s;
00469 s << d << ".row[" << i << "]";
00470 m_data[i].description(s.str());
00471 }
00472 }
00473
00474 template<int Rows, int Cols, ShBindingType Binding, typename T>
00475 std::string ShMatrix<Rows, Cols, Binding, T>::meta(const std::string& key) const
00476 {
00477 return ShMeta::meta(key);
00478 }
00479
00480 template<int Rows, int Cols, ShBindingType Binding, typename T>
00481 void ShMatrix<Rows, Cols, Binding, T>::meta(const std::string& key, const std::string& value)
00482 {
00483 ShMeta::meta(key, value);
00484
00485 for (int i = 0; i < Rows; i++)
00486 {
00487 std::stringstream s;
00488 s << value << ".row[" << i << "]";
00489 m_data[i].meta(key, s.str());
00490 }
00491 }
00492
00493 template<int Rows, int Cols, ShBindingType Binding, typename T>
00494 void ShMatrix<Rows, Cols, Binding, T>::getValues(host_type dest[]) const
00495 {
00496 int x = 0;
00497 for (int i=0; i < Rows; i++) {
00498 for (int j=0; j < Cols; j++) {
00499 typedef ShDataVariant<T, SH_HOST> VariantType;
00500 typedef ShPointer<VariantType> VariantTypePtr;
00501 VariantTypePtr c = variant_cast<T, SH_HOST>(m_data[i].getVariant());
00502 dest[x] = (*c)[j];
00503 x++;
00504 }
00505 }
00506 }
00507
00508 }
00509
00510 #endif