MatrixT.tcc 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. #include <string>
  2. #include <sstream>
  3. #include <stdexcept>
  4. #include <limits>
  5. #include "core/basics/Exception.h"
  6. #define _THROW_EMatrix(string) fthrow(Exception, string)
  7. #include "core/vector/ippwrapper.h"
  8. #include "core/vector/MatrixT.h"
  9. #include "vector"
  10. #include <algorithm>
  11. namespace NICE
  12. {
  13. template<typename ElementType>
  14. inline MatrixT<ElementType>::MatrixT()
  15. {
  16. setDataPointer(NULL, 0, 0, false);
  17. }
  18. template<typename ElementType>
  19. inline MatrixT<ElementType>::MatrixT(const size_t rows, const size_t cols)
  20. {
  21. setDataPointer(new ElementType[rows * cols], rows, cols, false);
  22. }
  23. #ifdef NICE_USELIB_LINAL
  24. template<typename ElementType>
  25. inline MatrixT<ElementType>::MatrixT(const LinAl::Matrix<ElementType>& m)
  26. {
  27. setDataPointer(new ElementType[m.rows() * m.cols()],
  28. m.rows(), m.cols(), false);
  29. for (unsigned int j = 0; j < cols(); j++)
  30. {
  31. for (unsigned int i = 0; i < rows(); i++)
  32. {
  33. (*this)(i, j) = m(i, j);
  34. }
  35. }
  36. }
  37. template<typename ElementType>
  38. inline MatrixT<ElementType>&
  39. MatrixT<ElementType>::operator=(const LinAl::Matrix<ElementType>& v)
  40. {
  41. if (rows() * cols() == 0 && !externalStorage && getDataPointer() == NULL)
  42. {
  43. setDataPointer(new ElementType[v.rows() * v.cols()],
  44. v.rows(), v.cols(), false);
  45. }
  46. else if (this->rows() != (unsigned int) v.rows()
  47. || this->cols() != (unsigned int) v.cols())
  48. {
  49. this->resize(v.rows(), v.cols());
  50. }
  51. for (unsigned int j = 0; j < cols(); j++)
  52. {
  53. for (unsigned int i = 0; i < rows(); i++)
  54. {
  55. (*this)(i, j) = v(i, j);
  56. }
  57. }
  58. return *this;
  59. }
  60. #endif // NICE_USELIB_LINAL
  61. template<typename ElementType>
  62. inline MatrixT<ElementType>::MatrixT(const size_t rows, const size_t cols,
  63. const ElementType& element)
  64. {
  65. setDataPointer(new ElementType[rows * cols], rows, cols, false);
  66. ippsSet(element, getDataPointer(), rows * cols);
  67. }
  68. template<typename ElementType>
  69. inline MatrixT<ElementType>::MatrixT(const ElementType* _data,
  70. const size_t rows, const size_t cols)
  71. {
  72. setDataPointer(new ElementType[rows * cols], rows, cols, false);
  73. ippsCopy(_data, getDataPointer(), rows * cols);
  74. }
  75. template<typename ElementType>
  76. inline MatrixT<ElementType>::MatrixT(ElementType* _data,
  77. const size_t rows, const size_t cols,
  78. const MatrixBase::Mode mode)
  79. {
  80. switch (mode)
  81. {
  82. case external:
  83. setDataPointer(_data, rows, cols, true);
  84. break;
  85. case copy:
  86. setDataPointer(new ElementType[rows * cols], rows, cols, false);
  87. ippsCopy(_data, getDataPointer(), rows * cols);
  88. break;
  89. default:
  90. setDataPointer(NULL, 0, 0, false);
  91. _THROW_EMatrix("Unknown Mode-enum.");
  92. }
  93. }
  94. //template <class ElementType>
  95. //inline MatrixT<ElementType>::MatrixT(std::istream& input) {
  96. // input >> dataSize;
  97. // setDataPointer(new ElementType[dataSize], dataSize, false);
  98. //
  99. // char c;
  100. // input >> c;
  101. // if (c != '<') {
  102. // _THROW_EMatrix("syntax error reading MatrixT");
  103. // }
  104. //
  105. // unsigned int i = -1;
  106. // while (true) {
  107. // std::string s;
  108. // input >> s;
  109. // if (s == ">") {
  110. // break;
  111. // }
  112. // i++;
  113. // if (i > dataSize) {
  114. // _THROW_EMatrix("syntax error reading MatrixT");
  115. // }
  116. // std::stringstream st(s);
  117. // ElementType x;
  118. // st >> x;
  119. // getDataPointer()[i] = x;
  120. // }
  121. //}
  122. template<typename ElementType>
  123. MatrixT<ElementType>::MatrixT(const MatrixT<ElementType>& v)
  124. {
  125. setDataPointer(new ElementType[v.rows() * v.cols()],
  126. v.rows(), v.cols(), false);
  127. ippsCopy(v.getDataPointer(), getDataPointer(), v.rows() * v.cols());
  128. }
  129. template<typename ElementType>
  130. inline MatrixT<ElementType>::~MatrixT()
  131. {
  132. if (!externalStorage && data != NULL)
  133. {
  134. delete[] data;
  135. setDataPointer(NULL, 0, 0, false);
  136. }
  137. }
  138. template<typename ElementType>
  139. void MatrixT<ElementType>::resize(size_t _rows, size_t _cols)
  140. {
  141. if (rows() * cols() == _rows * _cols)
  142. {
  143. m_rows = _rows;
  144. m_cols = _cols;
  145. return;
  146. }
  147. if (externalStorage)
  148. {
  149. if (_rows * _cols < rows() * cols())
  150. {
  151. m_rows = _rows;
  152. m_cols = _cols;
  153. return;
  154. }
  155. _THROW_EMatrix("Cannot resize MatrixT (external storage used)");
  156. }
  157. if (getDataPointer() != NULL)
  158. {
  159. size_t oldRows = rows();
  160. size_t oldCols = cols();
  161. ElementType* tmp = getDataPointer();
  162. setDataPointer(new ElementType[_rows * _cols], _rows, _cols, false);
  163. ippsCopy(tmp, getDataPointer(), std::min(_rows * _cols, oldRows * oldCols));
  164. delete[] tmp;
  165. }
  166. else
  167. {
  168. setDataPointer(new ElementType[_rows * _cols], _rows, _cols, false);
  169. }
  170. }
  171. template<class ElementType>
  172. inline const MatrixT<ElementType>*
  173. MatrixT<ElementType>::createConst(const ElementType* _data,
  174. const size_t rows, const size_t cols)
  175. {
  176. MatrixT<ElementType>* result = new MatrixT<ElementType>;
  177. result->setConstDataPointer(_data, rows, cols);
  178. return result;
  179. }
  180. template<class ElementType>
  181. inline const VectorT<ElementType> MatrixT<ElementType>::getColumnRef(uint i) const
  182. {
  183. const VectorT<ElementType> column(constData[i * rows()], rows(), VectorBase::external);
  184. return column;
  185. }
  186. template<class ElementType>
  187. inline VectorT<ElementType> MatrixT<ElementType>::getColumnRef(uint i)
  188. {
  189. VectorT<ElementType> column(data + (i * rows()), rows(), VectorBase::external);
  190. return column;
  191. }
  192. template<class ElementType>
  193. inline VectorT<ElementType> MatrixT<ElementType>::getColumn(uint i) const
  194. {
  195. VectorT<ElementType> column(constData + i * rows(), rows());
  196. return column;
  197. }
  198. template<class ElementType>
  199. inline VectorT<ElementType> MatrixT<ElementType>::getRow(uint i) const
  200. {
  201. VectorT<ElementType> row(cols());
  202. for (uint j = 0;j < cols();j++)
  203. {
  204. row(j) = (*this)(i, j);
  205. }
  206. return row;
  207. }
  208. template<class ElementType>
  209. inline ElementType MatrixT<ElementType>::Max() const
  210. {
  211. ElementType max = - std::numeric_limits<ElementType>::max();
  212. for (uint i = 0 ; i < rows(); i++)
  213. for (uint j = 0 ; j < cols(); j++)
  214. if ((*this)(i, j) > max)
  215. max = (*this)(i, j);
  216. return max;
  217. }
  218. template<class ElementType>
  219. inline ElementType MatrixT<ElementType>::Min() const
  220. {
  221. ElementType min = std::numeric_limits<ElementType>::max();
  222. for (uint i = 0 ; i < rows(); i++)
  223. for (uint j = 0 ; j < cols(); j++)
  224. if ((*this)(i, j) < min)
  225. min = (*this)(i, j);
  226. return min;
  227. }
  228. template<class ElementType>
  229. inline bool
  230. MatrixT<ElementType>::operator==(const MatrixT<ElementType>& v) const
  231. {
  232. if (this->rows() != v.rows() || this->cols() != v.cols())
  233. {
  234. throw std::invalid_argument(
  235. "MatrixT::operator==(): v.rows/cols() != rows/cols()");
  236. }
  237. else if (rows() == 0 || cols() == 0)
  238. {
  239. return true;
  240. }
  241. for (unsigned int j = 0; j < cols(); j++)
  242. {
  243. for (unsigned int i = 0; i < rows(); i++)
  244. {
  245. if (!(operator()(i, j) == v(i, j)))
  246. {
  247. return false;
  248. }
  249. }
  250. }
  251. return true;
  252. }
  253. template<class ElementType>
  254. inline bool
  255. MatrixT<ElementType>::operator!=(const MatrixT<ElementType>& v) const
  256. {
  257. if (this->rows() != v.rows() || this->cols() != v.cols())
  258. {
  259. throw std::invalid_argument(
  260. "MatrixT::operator==(): v.rows/cols() != rows/cols()");
  261. }
  262. else if (rows() == 0 || cols() == 0)
  263. {
  264. return false;
  265. }
  266. for (unsigned int j = 0; j < cols(); j++)
  267. {
  268. for (unsigned int i = 0; i < rows(); i++)
  269. {
  270. if (!(operator()(i, j) == v(i, j)))
  271. {
  272. return true;
  273. }
  274. }
  275. }
  276. return false;
  277. }
  278. template<typename ElementType>
  279. inline MatrixT<ElementType>&
  280. MatrixT<ElementType>::operator=(const MatrixT<ElementType>& v)
  281. {
  282. if (rows() * cols() == 0 && !externalStorage && getDataPointer() == NULL)
  283. {
  284. setDataPointer(new ElementType[v.rows() * v.cols()],
  285. v.rows(), v.cols(), false);
  286. }
  287. else if (this->rows() != v.rows() || this->cols() != v.cols())
  288. {
  289. this->resize(v.rows(), v.cols());
  290. }
  291. ippsCopy(v.getDataPointer(), getDataPointer(), v.rows() * v.cols());
  292. return *this;
  293. }
  294. template<typename ElementType>
  295. inline MatrixT<ElementType>&
  296. MatrixT<ElementType>::operator=(const ElementType& element)
  297. {
  298. ippsSet(element, getDataPointer(), this->rows() * this->cols());
  299. return *this;
  300. }
  301. template<typename ElementType>
  302. void MatrixT<ElementType>::transposeInplace()
  303. {
  304. if (rows() != cols())
  305. {
  306. _THROW_EMatrix("transposeInplace(): matrix has to be quadratic");
  307. }
  308. for (unsigned int j = 1; j < cols(); j++)
  309. {
  310. for (unsigned int i = 0; i < j; i++)
  311. {
  312. std::swap((*this)(i, j), (*this)(j, i));
  313. }
  314. }
  315. }
  316. template<typename ElementType>
  317. MatrixT<ElementType> MatrixT<ElementType>::transpose() const
  318. {
  319. MatrixT<ElementType> result(cols(), rows());
  320. // ifdef NICE_USELIB_IPP
  321. size_t tsize = sizeof(ElementType);
  322. IppStatus ret = ippmTranspose_m(getDataPointer(), tsize, rows() * tsize,
  323. cols(), rows(), result.getDataPointer(),
  324. tsize, cols() * tsize);
  325. if (ret != ippStsNoErr)
  326. {
  327. _THROW_EMatrix(ippGetStatusString(ret));
  328. }
  329. // #else
  330. // for (unsigned int j = 0; j < cols(); j++) {
  331. // for (unsigned int i = 0; i < rows(); i++) {
  332. // result(j, i) = (*this)(i, j);
  333. // }
  334. // }
  335. // #endif
  336. return result;
  337. }
  338. #define _DEFINE_EMATRIX_AUGMENTED_ASSIGNMENT(_Op, _Name) \
  339. template<class ElementType> \
  340. inline MatrixT<ElementType> & \
  341. MatrixT<ElementType>::operator _Op##= (const ElementType& v) { \
  342. ipps##_Name##C_I(v, getDataPointer(), this->rows() * this->cols()); \
  343. return *this; \
  344. }
  345. _DEFINE_EMATRIX_AUGMENTED_ASSIGNMENT( + , Add)
  346. _DEFINE_EMATRIX_AUGMENTED_ASSIGNMENT(-, Sub)
  347. _DEFINE_EMATRIX_AUGMENTED_ASSIGNMENT(*, Mul)
  348. _DEFINE_EMATRIX_AUGMENTED_ASSIGNMENT( / , Div)
  349. #define _DEFINE_EMATRIX_ASSIGNMENT(_Op, _Name) \
  350. template<class ElementType> \
  351. inline MatrixT<ElementType> & \
  352. MatrixT<ElementType>::operator _Op##= (const MatrixT<ElementType>& v) { \
  353. if(this->rows() != v.rows() || this->cols() != v. cols()) \
  354. _THROW_EMatrix("MatrixT: different data size"); \
  355. ipps##_Name##_I(v.getDataPointer(), getDataPointer(), \
  356. this->rows() * this->cols()); \
  357. return *this; \
  358. }
  359. _DEFINE_EMATRIX_ASSIGNMENT( + , Add)
  360. _DEFINE_EMATRIX_ASSIGNMENT(-, Sub)
  361. //_DEFINE_EMATRIX_ASSIGNMENT(*, Mul)
  362. //_DEFINE_EMATRIX_ASSIGNMENT(/, Div)
  363. template<typename ElementType>
  364. void MatrixT<ElementType>::setBlock(uint top, uint left,
  365. MatrixT<ElementType> m)
  366. {
  367. if (rows() < m.rows() + top || cols() < m.cols() + left)
  368. {
  369. _THROW_EMatrix("Matrix setBlock: target matrix too small.");
  370. }
  371. // FIXME use IPP?
  372. for (unsigned int j = 0; j < m.cols(); j++)
  373. {
  374. for (unsigned int i = 0; i < m.rows(); i++)
  375. {
  376. (*this)(i + top, j + left) = m(i, j);
  377. }
  378. }
  379. }
  380. template<typename ElementType>
  381. void MatrixT<ElementType>::setRow(const uint & row, const NICE::VectorT<ElementType> & v)
  382. {
  383. if (cols() < v.size() )
  384. {
  385. _THROW_EMatrix("Matrix setRow: target matrix too small.");
  386. }
  387. if (row >= rows() )
  388. {
  389. _THROW_EMatrix("Matrix setRow: row does not specify a proper row index.");
  390. }
  391. // FIXME use IPP?
  392. for (unsigned int j = 0; j < v.size(); j++)
  393. {
  394. (*this)(row, j) = v[j];
  395. }
  396. }
  397. template<typename ElementType>
  398. void MatrixT<ElementType>::exchangeRows(const uint & r1, const uint & r2)
  399. {
  400. //is r1 a proper row index?
  401. if ( r1 > this->rows() )
  402. {
  403. _THROW_EMatrix("Matrix exchangeRows: r1 does not specify a proper row index.");
  404. }
  405. //is r2 a proper row index?
  406. if ( r2 > this->rows() )
  407. {
  408. _THROW_EMatrix("Matrix exchangeRows: r2 does not specify a proper row index.");
  409. }
  410. NICE::VectorT<ElementType> tmp (this->getRow(r1));
  411. this->setRow(r1, this->getRow(r2));
  412. this->setRow(r1, tmp);
  413. }
  414. template<typename ElementType>
  415. void MatrixT<ElementType>::addToBlock(uint top, uint left,
  416. MatrixT<ElementType> m)
  417. {
  418. if (rows() < m.rows() + top || cols() < m.cols() + left)
  419. {
  420. _THROW_EMatrix("Matrix setBlock: target matrix too small.");
  421. }
  422. // FIXME use IPP?
  423. for (unsigned int j = 0; j < m.cols(); j++)
  424. {
  425. for (unsigned int i = 0; i < m.rows(); i++)
  426. {
  427. (*this)(i + top, j + left) += m(i, j);
  428. }
  429. }
  430. }
  431. template<typename ElementType>
  432. void MatrixT<ElementType>::addIdentity(double scale)
  433. {
  434. unsigned int m = std::min(rows(), cols());
  435. for (unsigned int i = 0; i < m; i++)
  436. {
  437. (*this)(i, i) += scale;
  438. }
  439. }
  440. template<typename ElementType>
  441. void MatrixT<ElementType>::addDiagonal( const VectorT<ElementType> & D )
  442. {
  443. unsigned int m = std::min(rows(), cols());
  444. if ( D.size() != m )
  445. _THROW_EMatrix("Matrix addDiagonal: inconsistent size of vector and diagonal elements.");
  446. for (unsigned int i = 0; i < m; i++)
  447. {
  448. (*this)(i, i) += D[i];
  449. }
  450. }
  451. template<typename ElementType>
  452. void MatrixT<ElementType>::addScaledMatrix( const ElementType & scale , const MatrixT<ElementType>& m)
  453. {
  454. if ( (rows() != m.rows()) || (cols() != m.cols()) )
  455. {
  456. _THROW_EMatrix("Matrix addScaledMatrix: target matrix not of the same size.");
  457. }
  458. for (unsigned int j = 0; j < m.cols(); j++)
  459. {
  460. for (unsigned int i = 0; i < m.rows(); i++)
  461. {
  462. (*this)(i, j) += scale * m(i, j);
  463. }
  464. }
  465. }
  466. template<typename ElementType>
  467. void MatrixT<ElementType>::tensorProduct(const VectorT<ElementType>& v,
  468. const VectorT<ElementType>& w)
  469. {
  470. if (v.size() != w.size())
  471. {
  472. _THROW_EMatrix("Matrix tensorProduct: inconsistent sizes of factors.");
  473. }
  474. if (rows() == 0 && cols() == 0)
  475. {
  476. resize(v.size(), w.size());
  477. }
  478. if (rows() != v.size() || cols() != w.size())
  479. {
  480. _THROW_EMatrix("Matrix multiplication: inconsistent size of result matrix.");
  481. }
  482. // FIXME use IPP?
  483. for (unsigned int j = 0; j < cols(); j++)
  484. {
  485. for (unsigned int i = 0; i < rows(); i++)
  486. {
  487. (*this)(i, j) = v[i] * w[j];
  488. }
  489. }
  490. }
  491. template<typename ElementType>
  492. void MatrixT<ElementType>::addTensorProduct(double lambda, const VectorT<ElementType>& v,
  493. const VectorT<ElementType>& w)
  494. {
  495. if (v.size() != w.size())
  496. {
  497. _THROW_EMatrix("Matrix tensorProduct: inconsistent sizes of factors.");
  498. }
  499. if (rows() == 0 && cols() == 0)
  500. {
  501. resize(v.size(), w.size());
  502. set(0.0);
  503. }
  504. if (rows() != v.size() || cols() != w.size())
  505. {
  506. _THROW_EMatrix("Matrix multiplication: inconsistent size of result matrix.");
  507. }
  508. for (unsigned int j = 0; j < cols(); j++)
  509. {
  510. for (unsigned int i = 0; i < rows(); i++)
  511. {
  512. (*this)(i, j) += lambda * v[i] * w[j];
  513. }
  514. }
  515. }
  516. template<typename ElementType>
  517. void MatrixT<ElementType>::multiply(const MatrixT<ElementType>& a,
  518. const MatrixT<ElementType>& b,
  519. bool atranspose,
  520. bool btranspose)
  521. {
  522. if (this == &a || this == &b)
  523. {
  524. _THROW_EMatrix(
  525. "Matrix multiplication: a and b must not be the same object as this.");
  526. }
  527. size_t arows, acols;
  528. if (atranspose)
  529. {
  530. arows = a.cols();
  531. acols = a.rows();
  532. }
  533. else
  534. {
  535. arows = a.rows();
  536. acols = a.cols();
  537. }
  538. size_t brows, bcols;
  539. if (btranspose)
  540. {
  541. brows = b.cols();
  542. bcols = b.rows();
  543. }
  544. else
  545. {
  546. brows = b.rows();
  547. bcols = b.cols();
  548. }
  549. if (acols != brows)
  550. {
  551. _THROW_EMatrix("Matrix multiplication: inconsistent sizes of factors.");
  552. }
  553. if (rows() == 0 && cols() == 0)
  554. {
  555. resize(arows, bcols);
  556. }
  557. if (rows() != arows || cols() != bcols)
  558. {
  559. _THROW_EMatrix("Matrix multiplication: inconsistent size of result matrix.");
  560. }
  561. #ifdef NICE_USELIB_IPP
  562. size_t tsize = sizeof(ElementType);
  563. IppStatus ret;
  564. if (atranspose)
  565. {
  566. if (btranspose)
  567. {
  568. ret = ippmMul_tt(b.getDataPointer(), b.rows() * tsize, tsize, b.rows(), b.cols(),
  569. a.getDataPointer(), a.rows() * tsize, tsize, a.rows(), a.cols(),
  570. this->getDataPointer(), rows() * tsize, tsize);
  571. }
  572. else
  573. {
  574. ret = ippmMul_mt(b.getDataPointer(), b.rows() * tsize, tsize, b.rows(), b.cols(),
  575. a.getDataPointer(), a.rows() * tsize, tsize, a.rows(), a.cols(),
  576. this->getDataPointer(), rows() * tsize, tsize);
  577. }
  578. }
  579. else
  580. {
  581. if (btranspose)
  582. {
  583. ret = ippmMul_tm(b.getDataPointer(), b.rows() * tsize, tsize, b.rows(), b.cols(),
  584. a.getDataPointer(), a.rows() * tsize, tsize, a.rows(), a.cols(),
  585. this->getDataPointer(), rows() * tsize, tsize);
  586. }
  587. else
  588. {
  589. ret = ippmMul_mm(b.getDataPointer(), b.rows() * tsize, tsize, b.rows(), b.cols(),
  590. a.getDataPointer(), a.rows() * tsize, tsize, a.rows(), a.cols(),
  591. this->getDataPointer(), rows() * tsize, tsize);
  592. }
  593. }
  594. if (ret != ippStsNoErr)
  595. _THROW_EMatrix(ippGetStatusString(ret));
  596. #else
  597. // FIXME not very efficient
  598. ElementType ela, elb;
  599. for (unsigned int j = 0; j < cols(); j++)
  600. {
  601. for (unsigned int i = 0; i < rows(); i++)
  602. {
  603. ElementType sum = ElementType(0);
  604. for (unsigned int k = 0; k < acols; k++)
  605. {
  606. ela = atranspose ? a(k, i) : a(i, k);
  607. elb = btranspose ? b(j, k) : b(k, j);
  608. sum += ela * elb;
  609. }
  610. (*this)(i, j) = sum;
  611. }
  612. }
  613. #endif
  614. }
  615. template<typename ElementType>
  616. bool MatrixT<ElementType>::containsNaN() const
  617. {
  618. for (unsigned int c = 0; c < cols(); c++)
  619. {
  620. for (unsigned int r = 0; r < rows(); r++)
  621. {
  622. if (NICE::isNaN((*this)(r, c)))
  623. {
  624. return true;
  625. }
  626. }
  627. }
  628. return false;
  629. }
  630. template<typename ElementType>
  631. inline bool MatrixT<ElementType>::isEqual(const MatrixT<ElementType> &a, ElementType threshold) const
  632. {
  633. if (this->rows() != a.rows() || this->cols() != a.cols())
  634. _THROW_EMatrix("MatrixT: different data size");
  635. for (unsigned int j = 0; j < cols(); j++)
  636. {
  637. for (unsigned int i = 0; i < rows(); i++)
  638. {
  639. if (fabs((*this)(i, j) - a(i, j)) > threshold)
  640. return false;
  641. }
  642. }
  643. return true;
  644. }
  645. template<typename ElementType>
  646. ElementType MatrixT<ElementType>::squaredFrobeniusNorm() const
  647. {
  648. double sum = ElementType(0);
  649. for (unsigned int j = 0; j < cols(); j++)
  650. {
  651. for (unsigned int i = 0; i < rows(); i++)
  652. {
  653. sum += square((*this)(i, j));
  654. }
  655. }
  656. return static_cast<ElementType>(sum);
  657. }
  658. template<typename ElementType>
  659. ElementType MatrixT<ElementType>::trace() const
  660. {
  661. double sum = ElementType(0);
  662. for (unsigned int j = 0; j < std::min(cols(),rows()); j++)
  663. {
  664. sum += (*this)(j, j);
  665. }
  666. return static_cast<ElementType>(sum);
  667. }
  668. template<typename ElementType>
  669. ElementType MatrixT<ElementType>::bilinear(const VectorT<ElementType> & v) const
  670. {
  671. double sum = ElementType(0);
  672. if ((this->rows() != this->cols()) || (v.size() != this->rows()))
  673. _THROW_EMatrix("MatrixT: different data size");
  674. for (unsigned int j = 0; j < cols(); j++)
  675. {
  676. for (unsigned int i = 0; i < rows(); i++)
  677. {
  678. sum += (*this)(i, j) * v[i] * v[j];
  679. }
  680. }
  681. return static_cast<ElementType>(sum);
  682. }
  683. template<typename ElementType>
  684. void MatrixT<ElementType>::normalizeColumnsL1()
  685. {
  686. for (unsigned int j = 0 ; j < cols() ; j++)
  687. {
  688. ElementType sum = 0.0;
  689. for (unsigned int i = 0 ; i < rows() ; i++)
  690. sum += fabs((double)(*this)(i, j));
  691. if (sum > 1e-20)
  692. for (unsigned int i = 0 ; i < rows() ; i++)
  693. (*this)(i, j) /= sum;
  694. }
  695. }
  696. template<typename ElementType>
  697. void MatrixT<ElementType>::normalizeRowsL1()
  698. {
  699. for (unsigned int i = 0 ; i < rows() ; i++)
  700. {
  701. ElementType sum = 0.0;
  702. for (unsigned int j = 0 ; j < cols() ; j++)
  703. sum += fabs((double)(*this)(i, j));
  704. if (sum > 1e-20)
  705. for (unsigned int j = 0 ; j < cols() ; j++)
  706. (*this)(i, j) /= sum;
  707. }
  708. }
  709. /** get sub-matrix */
  710. template<class ElementType>
  711. MatrixT<ElementType> MatrixT<ElementType>::operator()(const uint row_tl,
  712. const uint col_tl,
  713. const uint row_br,
  714. const uint col_br) const
  715. {
  716. if ( (row_tl>row_br+1)||(row_br>=rows())||
  717. (col_tl>col_br+1)||(col_br>=cols()) )
  718. _THROW_EMatrix("MatrixT: wrong specification of sub-matrix");
  719. MatrixT<ElementType> tm(row_br-row_tl+1,col_br-col_tl+1);
  720. for (uint i=row_tl;i<=row_br;i++)
  721. for (uint j=col_tl;j<=col_br;j++) {
  722. tm(i-row_tl,j-col_tl) = (*this)(i,j);
  723. }
  724. return tm;
  725. }
  726. /** Matrix addition */
  727. template<class ElementType>
  728. MatrixT<ElementType> operator+(const MatrixT<ElementType> & a, const MatrixT<ElementType> & b)
  729. {
  730. MatrixT<ElementType> dst(a);
  731. dst += b;
  732. return dst;
  733. }
  734. /** Matrix substraction */
  735. template<class ElementType>
  736. MatrixT<ElementType> operator-(const MatrixT<ElementType> & a, const MatrixT<ElementType> & b)
  737. {
  738. MatrixT<ElementType> dst(a);
  739. dst -= b;
  740. return dst;
  741. }
  742. /** Matrix (right) multiplication with a scalar */
  743. template<class ElementType>
  744. MatrixT<ElementType> operator*(const MatrixT<ElementType> & a, const double s)
  745. {
  746. MatrixT<ElementType> dst(a);
  747. dst *= s;
  748. return dst;
  749. }
  750. /** Matrix (left) multiplication with a scalar */
  751. template<class ElementType>
  752. MatrixT<ElementType> operator*(const double s, const MatrixT<ElementType> & a)
  753. {
  754. MatrixT<ElementType> dst(a);
  755. dst *= s;
  756. return dst;
  757. }
  758. /** Matrix multiplication with a vector */
  759. template<class ElementType>
  760. VectorT<ElementType> operator*(const MatrixT<ElementType> & a, const VectorT<ElementType> & b)
  761. {
  762. VectorT<ElementType> dst;
  763. dst.multiply(a, b);
  764. return dst;
  765. }
  766. /** Matrix multiplication with a matrix */
  767. template<class ElementType>
  768. MatrixT<ElementType> operator*(const MatrixT<ElementType> & a, const MatrixT<ElementType> & b)
  769. {
  770. MatrixT<ElementType> dst;
  771. dst.multiply(a, b);
  772. return dst;
  773. }
  774. template<class ElementType>
  775. void kroneckerProduct (const MatrixT<ElementType>& A, const MatrixT<ElementType>& B, MatrixT<ElementType>& dst)
  776. {
  777. dst.resize ( A.rows() * B.rows(), A.cols() * B.cols() );
  778. for (uint iA = 0 ; iA < A.rows(); iA++ )
  779. for (uint jA = 0 ; jA < A.cols(); jA++ )
  780. for (uint iB = 0 ; iB < B.rows(); iB++ )
  781. for (uint jB = 0 ; jB < B.cols(); jB++ )
  782. dst( iA*B.rows() + iB, jA*B.cols() + jB ) = A(iA,jA) * B(iB,jB);
  783. }
  784. template <typename T>
  785. void kroneckerProductMultiplication ( const MatrixT<T> & A, const MatrixT<T> & B, const Vector & x, Vector & y )
  786. {
  787. if ( x.size() != A.cols() * B.cols() )
  788. fthrow(Exception, "The size of matrix A and B do not fit to the size of the given vector.");
  789. y.resize ( A.rows() * B.rows() );
  790. Matrix X ( A.cols(), B.cols() );
  791. uint k = 0;
  792. for ( uint i = 0 ; i < A.cols(); i++ )
  793. for ( uint j = 0 ; j < B.cols(); j++,k++ )
  794. X(i,j) = x[k];
  795. // result in matrix form Y^T = (B * X^T) * A^T
  796. // Y = A X B^T
  797. Matrix Y;
  798. Matrix XBt;
  799. XBt.multiply ( X, B, false, true );
  800. Y.multiply ( A, XBt );
  801. // Y has size B.rows() and A.rows()
  802. k = 0;
  803. for ( uint i = 0 ; i < A.rows(); i++ )
  804. for ( uint j = 0 ; j < B.rows(); j++,k++ )
  805. y[k] = Y(i,j);
  806. }
  807. template <typename T>
  808. void blockwiseMultiplication ( const MatrixT<T> & A, uint blockSize, const Vector & x, Vector & y )
  809. {
  810. if ( x.size() != A.cols() * blockSize )
  811. fthrow(Exception, "The size of matrix A and the block size do not fit to the size of the given vector.");
  812. y.resize ( A.rows() * blockSize );
  813. Matrix X ( A.cols(), blockSize );
  814. uint k = 0;
  815. for ( uint i = 0 ; i < A.cols(); i++ )
  816. for ( uint j = 0 ; j < blockSize; j++,k++ )
  817. X(i,j) = x[k];
  818. // result in matrix form Y^T = X^T * A^T
  819. // Y = A X
  820. Matrix Y;
  821. Y.multiply ( A, X );
  822. // Y has size blockSize and A.rows()
  823. k = 0;
  824. for ( uint i = 0 ; i < A.rows(); i++ )
  825. for ( uint j = 0 ; j < blockSize; j++,k++ )
  826. y[k] = Y(i,j);
  827. }
  828. /**
  829. * @brief Delete one specified row by copying every data but of this row to a temporal storage, resizing M and assigning the tmp data to the resized matrix
  830. * @author Alexander Lütz
  831. * @date 07/10/2011
  832. * @param int index of col which shall be deleted (const, reference)
  833. */
  834. template <typename T>
  835. void MatrixT<T>::deleteRow ( const int & index)
  836. {
  837. if ( (0 > index) || ((*this).rows() <= (uint)index))
  838. fthrow(Exception, "MatrixT::deleteRow -- The given index is not valid.");
  839. MatrixT<T> tmp((*this).rows()-1,(*this).cols());
  840. for (uint i = 0; i < (*this).rows(); i++)
  841. {
  842. for (uint j = 0; j < (*this).cols(); j++)
  843. {
  844. if (i < (uint) index)
  845. {
  846. tmp(i,j) = (*this)(i,j);
  847. } else if (i > (uint) index)
  848. {
  849. tmp(i-1,j) = (*this)(i,j);
  850. }
  851. }
  852. }
  853. (*this).resize(tmp.rows(), tmp.cols());
  854. (*this) = tmp;
  855. }
  856. /**
  857. * @brief Delete one specified col by copying every data but of this col to a temporal storage, resizing M and assigning the tmp data to the resized matrix
  858. * @author Alexander Lütz
  859. * @date 07/10/2011
  860. * @param int index of col which shall be deleted (const, reference)
  861. */
  862. template <typename T>
  863. void MatrixT<T>::deleteCol ( const int & index)
  864. {
  865. if ( (0 > index) || ((*this).cols() <= (uint) index))
  866. fthrow(Exception, "MatrixT::deleteCol -- The given index is not valid.");
  867. MatrixT<T> tmp((*this).rows(),(*this).cols()-1);
  868. for (uint i = 0; i < (*this).rows(); i++)
  869. {
  870. for (uint j = 0; j < (*this).cols(); j++)
  871. {
  872. if (j < (uint) index)
  873. {
  874. tmp(i,j) = (*this)(i,j);
  875. } else if (j > (uint) index)
  876. {
  877. tmp(i,j-1) = (*this)(i,j);
  878. }
  879. }
  880. }
  881. (*this).resize(tmp.rows(), tmp.cols());
  882. (*this) = tmp;
  883. }
  884. /**
  885. * @brief Delete multiple specified rows by copying every data but of this rows to a temporal storage, resizing M and assigning the tmp data to the resized matrix
  886. * @author Alexander Lütz
  887. * @date 07/10/2011
  888. * @param std::vector<int> indices of cols which shall be deleted (not const, reference, sorted afterwards)
  889. */
  890. //not const, because they get sorted
  891. template <typename T>
  892. void MatrixT<T>::deleteRows ( std::vector<int> & indices)
  893. {
  894. for (std::vector<int>::const_iterator it = indices.begin(); it != indices.end(); it++)
  895. {
  896. if ( (0 > *it) || ((*this).rows() <= (uint) *it))
  897. fthrow(Exception, "MatrixT::deleteRows -- The given indices are not valid.");
  898. }
  899. sort (indices.begin(), indices.begin() + indices.size());
  900. int row_counter(0);
  901. MatrixT<T> tmp((*this).rows()-indices.size(),(*this).cols());
  902. for (uint i = 0; i < (*this).rows(); i++)
  903. {
  904. for (uint j = 0; j < (*this).cols(); j++)
  905. {
  906. if (i == (uint) indices[row_counter])
  907. {
  908. row_counter++;
  909. break; //break inner j-loop
  910. }
  911. else
  912. tmp(i-row_counter,j) = (*this)(i,j);
  913. }
  914. }
  915. (*this).resize(tmp.rows(), tmp.cols());
  916. (*this) = tmp;
  917. }
  918. /**
  919. * @brief Delete multiple specified cols by copying every data but of this cols to a temporal storage, resizing M and assigning the tmp data to the resized matrix
  920. * @author Alexander Lütz
  921. * @date 07/10/2011
  922. * @param std::vector<int> indices of rows which shall be deleted (not const, reference, sorted afterwards)
  923. */
  924. //not const, because they get sorted
  925. template <typename T>
  926. void MatrixT<T>::deleteCols ( std::vector<int> & indices)
  927. {
  928. for (std::vector<int>::const_iterator it = indices.begin(); it != indices.end(); it++)
  929. {
  930. if ( (0 > *it) || ((*this).cols() <= (uint) *it))
  931. fthrow(Exception, "MatrixT::deleteCols -- The given indices are not valid.");
  932. }
  933. sort (indices.begin(), indices.begin() + indices.size());
  934. int col_counter(0);
  935. MatrixT<T> tmp((*this).rows(),(*this).cols()-indices.size());
  936. //TODO check wether this is sufficient according to the way we keep the matrices in the storage!
  937. for (uint j = 0; j < (*this).cols(); j++)
  938. {
  939. for (uint i = 0; i < (*this).rows(); i++)
  940. {
  941. if (j == (uint) indices[col_counter])
  942. {
  943. col_counter++;
  944. break; //break inner j-loop
  945. }
  946. else
  947. tmp(i,j-col_counter) = (*this)(i,j);
  948. }
  949. }
  950. (*this).resize(tmp.rows(), tmp.cols());
  951. (*this) = tmp;
  952. }
  953. } // namespace