MatrixT.tcc 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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 (rows() < v.size() )
  384. {
  385. _THROW_EMatrix("Matrix setRow: target matrix too small.");
  386. }
  387. // FIXME use IPP?
  388. for (unsigned int j = 0; j < v.size(); j++)
  389. {
  390. (*this)(row, j) = v[j];
  391. }
  392. }
  393. template<typename ElementType>
  394. void MatrixT<ElementType>::exchangeRows(const uint & r1, const uint & r2)
  395. {
  396. //is r1 a proper row index?
  397. if ( r1 > this->rows() )
  398. {
  399. _THROW_EMatrix("Matrix exchangeRows: r1 does not specify a proper row index.");
  400. }
  401. //is r2 a proper row index?
  402. if ( r2 > this->rows() )
  403. {
  404. _THROW_EMatrix("Matrix exchangeRows: r2 does not specify a proper row index.");
  405. }
  406. NICE::VectorT<ElementType> tmp (this->getRow(r1));
  407. this->setRow(r1, this->getRow(r2));
  408. this->setRow(r1, tmp);
  409. }
  410. template<typename ElementType>
  411. void MatrixT<ElementType>::addToBlock(uint top, uint left,
  412. MatrixT<ElementType> m)
  413. {
  414. if (rows() < m.rows() + top || cols() < m.cols() + left)
  415. {
  416. _THROW_EMatrix("Matrix setBlock: target matrix too small.");
  417. }
  418. // FIXME use IPP?
  419. for (unsigned int j = 0; j < m.cols(); j++)
  420. {
  421. for (unsigned int i = 0; i < m.rows(); i++)
  422. {
  423. (*this)(i + top, j + left) += m(i, j);
  424. }
  425. }
  426. }
  427. template<typename ElementType>
  428. void MatrixT<ElementType>::addIdentity(double scale)
  429. {
  430. unsigned int m = std::min(rows(), cols());
  431. for (unsigned int i = 0; i < m; i++)
  432. {
  433. (*this)(i, i) += scale;
  434. }
  435. }
  436. template<typename ElementType>
  437. void MatrixT<ElementType>::addDiagonal( const VectorT<ElementType> & D )
  438. {
  439. unsigned int m = std::min(rows(), cols());
  440. if ( D.size() != m )
  441. _THROW_EMatrix("Matrix addDiagonal: inconsistent size of vector and diagonal elements.");
  442. for (unsigned int i = 0; i < m; i++)
  443. {
  444. (*this)(i, i) += D[i];
  445. }
  446. }
  447. template<typename ElementType>
  448. void MatrixT<ElementType>::addScaledMatrix( const ElementType & scale , const MatrixT<ElementType>& m)
  449. {
  450. if ( (rows() != m.rows()) || (cols() != m.cols()) )
  451. {
  452. _THROW_EMatrix("Matrix addScaledMatrix: target matrix not of the same size.");
  453. }
  454. for (unsigned int j = 0; j < m.cols(); j++)
  455. {
  456. for (unsigned int i = 0; i < m.rows(); i++)
  457. {
  458. (*this)(i, j) += scale * m(i, j);
  459. }
  460. }
  461. }
  462. template<typename ElementType>
  463. void MatrixT<ElementType>::tensorProduct(const VectorT<ElementType>& v,
  464. const VectorT<ElementType>& w)
  465. {
  466. if (v.size() != w.size())
  467. {
  468. _THROW_EMatrix("Matrix tensorProduct: inconsistent sizes of factors.");
  469. }
  470. if (rows() == 0 && cols() == 0)
  471. {
  472. resize(v.size(), w.size());
  473. }
  474. if (rows() != v.size() || cols() != w.size())
  475. {
  476. _THROW_EMatrix("Matrix multiplication: inconsistent size of result matrix.");
  477. }
  478. // FIXME use IPP?
  479. for (unsigned int j = 0; j < cols(); j++)
  480. {
  481. for (unsigned int i = 0; i < rows(); i++)
  482. {
  483. (*this)(i, j) = v[i] * w[j];
  484. }
  485. }
  486. }
  487. template<typename ElementType>
  488. void MatrixT<ElementType>::addTensorProduct(double lambda, const VectorT<ElementType>& v,
  489. const VectorT<ElementType>& w)
  490. {
  491. if (v.size() != w.size())
  492. {
  493. _THROW_EMatrix("Matrix tensorProduct: inconsistent sizes of factors.");
  494. }
  495. if (rows() == 0 && cols() == 0)
  496. {
  497. resize(v.size(), w.size());
  498. set(0.0);
  499. }
  500. if (rows() != v.size() || cols() != w.size())
  501. {
  502. _THROW_EMatrix("Matrix multiplication: inconsistent size of result matrix.");
  503. }
  504. for (unsigned int j = 0; j < cols(); j++)
  505. {
  506. for (unsigned int i = 0; i < rows(); i++)
  507. {
  508. (*this)(i, j) += lambda * v[i] * w[j];
  509. }
  510. }
  511. }
  512. template<typename ElementType>
  513. void MatrixT<ElementType>::multiply(const MatrixT<ElementType>& a,
  514. const MatrixT<ElementType>& b,
  515. bool atranspose,
  516. bool btranspose)
  517. {
  518. if (this == &a || this == &b)
  519. {
  520. _THROW_EMatrix(
  521. "Matrix multiplication: a and b must not be the same object as this.");
  522. }
  523. size_t arows, acols;
  524. if (atranspose)
  525. {
  526. arows = a.cols();
  527. acols = a.rows();
  528. }
  529. else
  530. {
  531. arows = a.rows();
  532. acols = a.cols();
  533. }
  534. size_t brows, bcols;
  535. if (btranspose)
  536. {
  537. brows = b.cols();
  538. bcols = b.rows();
  539. }
  540. else
  541. {
  542. brows = b.rows();
  543. bcols = b.cols();
  544. }
  545. if (acols != brows)
  546. {
  547. _THROW_EMatrix("Matrix multiplication: inconsistent sizes of factors.");
  548. }
  549. if (rows() == 0 && cols() == 0)
  550. {
  551. resize(arows, bcols);
  552. }
  553. if (rows() != arows || cols() != bcols)
  554. {
  555. _THROW_EMatrix("Matrix multiplication: inconsistent size of result matrix.");
  556. }
  557. #ifdef NICE_USELIB_IPP
  558. size_t tsize = sizeof(ElementType);
  559. IppStatus ret;
  560. if (atranspose)
  561. {
  562. if (btranspose)
  563. {
  564. ret = ippmMul_tt(b.getDataPointer(), b.rows() * tsize, tsize, b.rows(), b.cols(),
  565. a.getDataPointer(), a.rows() * tsize, tsize, a.rows(), a.cols(),
  566. this->getDataPointer(), rows() * tsize, tsize);
  567. }
  568. else
  569. {
  570. ret = ippmMul_mt(b.getDataPointer(), b.rows() * tsize, tsize, b.rows(), b.cols(),
  571. a.getDataPointer(), a.rows() * tsize, tsize, a.rows(), a.cols(),
  572. this->getDataPointer(), rows() * tsize, tsize);
  573. }
  574. }
  575. else
  576. {
  577. if (btranspose)
  578. {
  579. ret = ippmMul_tm(b.getDataPointer(), b.rows() * tsize, tsize, b.rows(), b.cols(),
  580. a.getDataPointer(), a.rows() * tsize, tsize, a.rows(), a.cols(),
  581. this->getDataPointer(), rows() * tsize, tsize);
  582. }
  583. else
  584. {
  585. ret = ippmMul_mm(b.getDataPointer(), b.rows() * tsize, tsize, b.rows(), b.cols(),
  586. a.getDataPointer(), a.rows() * tsize, tsize, a.rows(), a.cols(),
  587. this->getDataPointer(), rows() * tsize, tsize);
  588. }
  589. }
  590. if (ret != ippStsNoErr)
  591. _THROW_EMatrix(ippGetStatusString(ret));
  592. #else
  593. // FIXME not very efficient
  594. ElementType ela, elb;
  595. for (unsigned int j = 0; j < cols(); j++)
  596. {
  597. for (unsigned int i = 0; i < rows(); i++)
  598. {
  599. ElementType sum = ElementType(0);
  600. for (unsigned int k = 0; k < acols; k++)
  601. {
  602. ela = atranspose ? a(k, i) : a(i, k);
  603. elb = btranspose ? b(j, k) : b(k, j);
  604. sum += ela * elb;
  605. }
  606. (*this)(i, j) = sum;
  607. }
  608. }
  609. #endif
  610. }
  611. template<typename ElementType>
  612. bool MatrixT<ElementType>::containsNaN() const
  613. {
  614. for (unsigned int c = 0; c < cols(); c++)
  615. {
  616. for (unsigned int r = 0; r < rows(); r++)
  617. {
  618. if (isNaN((*this)(r, c)))
  619. {
  620. return true;
  621. }
  622. }
  623. }
  624. return false;
  625. }
  626. template<typename ElementType>
  627. inline bool MatrixT<ElementType>::isEqual(const MatrixT<ElementType> &a, ElementType threshold) const
  628. {
  629. if (this->rows() != a.rows() || this->cols() != a.cols())
  630. _THROW_EMatrix("MatrixT: different data size");
  631. for (unsigned int j = 0; j < cols(); j++)
  632. {
  633. for (unsigned int i = 0; i < rows(); i++)
  634. {
  635. if (fabs((*this)(i, j) - a(i, j)) > threshold)
  636. return false;
  637. }
  638. }
  639. return true;
  640. }
  641. template<typename ElementType>
  642. ElementType MatrixT<ElementType>::squaredFrobeniusNorm() const
  643. {
  644. double sum = ElementType(0);
  645. for (unsigned int j = 0; j < cols(); j++)
  646. {
  647. for (unsigned int i = 0; i < rows(); i++)
  648. {
  649. sum += square((*this)(i, j));
  650. }
  651. }
  652. return static_cast<ElementType>(sum);
  653. }
  654. template<typename ElementType>
  655. ElementType MatrixT<ElementType>::trace() const
  656. {
  657. double sum = ElementType(0);
  658. for (unsigned int j = 0; j < std::min(cols(),rows()); j++)
  659. {
  660. sum += (*this)(j, j);
  661. }
  662. return static_cast<ElementType>(sum);
  663. }
  664. template<typename ElementType>
  665. ElementType MatrixT<ElementType>::bilinear(const VectorT<ElementType> & v) const
  666. {
  667. double sum = ElementType(0);
  668. if ((this->rows() != this->cols()) || (v.size() != this->rows()))
  669. _THROW_EMatrix("MatrixT: different data size");
  670. for (unsigned int j = 0; j < cols(); j++)
  671. {
  672. for (unsigned int i = 0; i < rows(); i++)
  673. {
  674. sum += (*this)(i, j) * v[i] * v[j];
  675. }
  676. }
  677. return static_cast<ElementType>(sum);
  678. }
  679. template<typename ElementType>
  680. void MatrixT<ElementType>::normalizeColumnsL1()
  681. {
  682. for (unsigned int j = 0 ; j < cols() ; j++)
  683. {
  684. ElementType sum = 0.0;
  685. for (unsigned int i = 0 ; i < rows() ; i++)
  686. sum += fabs((double)(*this)(i, j));
  687. if (sum > 1e-20)
  688. for (unsigned int i = 0 ; i < rows() ; i++)
  689. (*this)(i, j) /= sum;
  690. }
  691. }
  692. template<typename ElementType>
  693. void MatrixT<ElementType>::normalizeRowsL1()
  694. {
  695. for (unsigned int i = 0 ; i < rows() ; i++)
  696. {
  697. ElementType sum = 0.0;
  698. for (unsigned int j = 0 ; j < cols() ; j++)
  699. sum += fabs((double)(*this)(i, j));
  700. if (sum > 1e-20)
  701. for (unsigned int j = 0 ; j < cols() ; j++)
  702. (*this)(i, j) /= sum;
  703. }
  704. }
  705. /** get sub-matrix */
  706. template<class ElementType>
  707. MatrixT<ElementType> MatrixT<ElementType>::operator()(const uint row_tl,
  708. const uint col_tl,
  709. const uint row_br,
  710. const uint col_br) const
  711. {
  712. if ( (row_tl>row_br+1)||(row_br>=rows())||
  713. (col_tl>col_br+1)||(col_br>=cols()) )
  714. _THROW_EMatrix("MatrixT: wrong specification of sub-matrix");
  715. MatrixT<ElementType> tm(row_br-row_tl+1,col_br-col_tl+1);
  716. for (uint i=row_tl;i<=row_br;i++)
  717. for (uint j=col_tl;j<=col_br;j++) {
  718. tm(i-row_tl,j-col_tl) = (*this)(i,j);
  719. }
  720. return tm;
  721. }
  722. /** Matrix addition */
  723. template<class ElementType>
  724. MatrixT<ElementType> operator+(const MatrixT<ElementType> & a, const MatrixT<ElementType> & b)
  725. {
  726. MatrixT<ElementType> dst(a);
  727. dst += b;
  728. return dst;
  729. }
  730. /** Matrix substraction */
  731. template<class ElementType>
  732. MatrixT<ElementType> operator-(const MatrixT<ElementType> & a, const MatrixT<ElementType> & b)
  733. {
  734. MatrixT<ElementType> dst(a);
  735. dst -= b;
  736. return dst;
  737. }
  738. /** Matrix (right) multiplication with a scalar */
  739. template<class ElementType>
  740. MatrixT<ElementType> operator*(const MatrixT<ElementType> & a, const double s)
  741. {
  742. MatrixT<ElementType> dst(a);
  743. dst *= s;
  744. return dst;
  745. }
  746. /** Matrix (left) multiplication with a scalar */
  747. template<class ElementType>
  748. MatrixT<ElementType> operator*(const double s, const MatrixT<ElementType> & a)
  749. {
  750. MatrixT<ElementType> dst(a);
  751. dst *= s;
  752. return dst;
  753. }
  754. /** Matrix multiplication with a vector */
  755. template<class ElementType>
  756. VectorT<ElementType> operator*(const MatrixT<ElementType> & a, const VectorT<ElementType> & b)
  757. {
  758. VectorT<ElementType> dst;
  759. dst.multiply(a, b);
  760. return dst;
  761. }
  762. /** Matrix multiplication with a matrix */
  763. template<class ElementType>
  764. MatrixT<ElementType> operator*(const MatrixT<ElementType> & a, const MatrixT<ElementType> & b)
  765. {
  766. MatrixT<ElementType> dst;
  767. dst.multiply(a, b);
  768. return dst;
  769. }
  770. template<class ElementType>
  771. void kroneckerProduct (const MatrixT<ElementType>& A, const MatrixT<ElementType>& B, MatrixT<ElementType>& dst)
  772. {
  773. dst.resize ( A.rows() * B.rows(), A.cols() * B.cols() );
  774. for (uint iA = 0 ; iA < A.rows(); iA++ )
  775. for (uint jA = 0 ; jA < A.cols(); jA++ )
  776. for (uint iB = 0 ; iB < B.rows(); iB++ )
  777. for (uint jB = 0 ; jB < B.cols(); jB++ )
  778. dst( iA*B.rows() + iB, jA*B.cols() + jB ) = A(iA,jA) * B(iB,jB);
  779. }
  780. template <typename T>
  781. void kroneckerProductMultiplication ( const MatrixT<T> & A, const MatrixT<T> & B, const Vector & x, Vector & y )
  782. {
  783. if ( x.size() != A.cols() * B.cols() )
  784. fthrow(Exception, "The size of matrix A and B do not fit to the size of the given vector.");
  785. y.resize ( A.rows() * B.rows() );
  786. Matrix X ( A.cols(), B.cols() );
  787. uint k = 0;
  788. for ( uint i = 0 ; i < A.cols(); i++ )
  789. for ( uint j = 0 ; j < B.cols(); j++,k++ )
  790. X(i,j) = x[k];
  791. // result in matrix form Y^T = (B * X^T) * A^T
  792. // Y = A X B^T
  793. Matrix Y;
  794. Matrix XBt;
  795. XBt.multiply ( X, B, false, true );
  796. Y.multiply ( A, XBt );
  797. // Y has size B.rows() and A.rows()
  798. k = 0;
  799. for ( uint i = 0 ; i < A.rows(); i++ )
  800. for ( uint j = 0 ; j < B.rows(); j++,k++ )
  801. y[k] = Y(i,j);
  802. }
  803. template <typename T>
  804. void blockwiseMultiplication ( const MatrixT<T> & A, uint blockSize, const Vector & x, Vector & y )
  805. {
  806. if ( x.size() != A.cols() * blockSize )
  807. fthrow(Exception, "The size of matrix A and the block size do not fit to the size of the given vector.");
  808. y.resize ( A.rows() * blockSize );
  809. Matrix X ( A.cols(), blockSize );
  810. uint k = 0;
  811. for ( uint i = 0 ; i < A.cols(); i++ )
  812. for ( uint j = 0 ; j < blockSize; j++,k++ )
  813. X(i,j) = x[k];
  814. // result in matrix form Y^T = X^T * A^T
  815. // Y = A X
  816. Matrix Y;
  817. Y.multiply ( A, X );
  818. // Y has size blockSize and A.rows()
  819. k = 0;
  820. for ( uint i = 0 ; i < A.rows(); i++ )
  821. for ( uint j = 0 ; j < blockSize; j++,k++ )
  822. y[k] = Y(i,j);
  823. }
  824. /**
  825. * @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
  826. * @author Alexander Lütz
  827. * @date 07/10/2011
  828. * @param int index of col which shall be deleted (const, reference)
  829. */
  830. template <typename T>
  831. void MatrixT<T>::deleteRow ( const int & index)
  832. {
  833. if ( (0 > index) || ((*this).rows() <= (uint)index))
  834. fthrow(Exception, "MatrixT::deleteRow -- The given index is not valid.");
  835. MatrixT<T> tmp((*this).rows()-1,(*this).cols());
  836. for (uint i = 0; i < (*this).rows(); i++)
  837. {
  838. for (uint j = 0; j < (*this).cols(); j++)
  839. {
  840. if (i < (uint) index)
  841. {
  842. tmp(i,j) = (*this)(i,j);
  843. } else if (i > (uint) index)
  844. {
  845. tmp(i-1,j) = (*this)(i,j);
  846. }
  847. }
  848. }
  849. (*this).resize(tmp.rows(), tmp.cols());
  850. (*this) = tmp;
  851. }
  852. /**
  853. * @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
  854. * @author Alexander Lütz
  855. * @date 07/10/2011
  856. * @param int index of col which shall be deleted (const, reference)
  857. */
  858. template <typename T>
  859. void MatrixT<T>::deleteCol ( const int & index)
  860. {
  861. if ( (0 > index) || ((*this).cols() <= (uint) index))
  862. fthrow(Exception, "MatrixT::deleteCol -- The given index is not valid.");
  863. MatrixT<T> tmp((*this).rows(),(*this).cols()-1);
  864. for (uint i = 0; i < (*this).rows(); i++)
  865. {
  866. for (uint j = 0; j < (*this).cols(); j++)
  867. {
  868. if (j < (uint) index)
  869. {
  870. tmp(i,j) = (*this)(i,j);
  871. } else if (j > (uint) index)
  872. {
  873. tmp(i,j-1) = (*this)(i,j);
  874. }
  875. }
  876. }
  877. (*this).resize(tmp.rows(), tmp.cols());
  878. (*this) = tmp;
  879. }
  880. /**
  881. * @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
  882. * @author Alexander Lütz
  883. * @date 07/10/2011
  884. * @param std::vector<int> indices of cols which shall be deleted (not const, reference, sorted afterwards)
  885. */
  886. //not const, because they get sorted
  887. template <typename T>
  888. void MatrixT<T>::deleteRows ( std::vector<int> & indices)
  889. {
  890. for (std::vector<int>::const_iterator it = indices.begin(); it != indices.end(); it++)
  891. {
  892. if ( (0 > *it) || ((*this).rows() <= (uint) *it))
  893. fthrow(Exception, "MatrixT::deleteRows -- The given indices are not valid.");
  894. }
  895. sort (indices.begin(), indices.begin() + indices.size());
  896. int row_counter(0);
  897. MatrixT<T> tmp((*this).rows()-indices.size(),(*this).cols());
  898. for (uint i = 0; i < (*this).rows(); i++)
  899. {
  900. for (uint j = 0; j < (*this).cols(); j++)
  901. {
  902. if (i == (uint) indices[row_counter])
  903. {
  904. row_counter++;
  905. break; //break inner j-loop
  906. }
  907. else
  908. tmp(i-row_counter,j) = (*this)(i,j);
  909. }
  910. }
  911. (*this).resize(tmp.rows(), tmp.cols());
  912. (*this) = tmp;
  913. }
  914. /**
  915. * @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
  916. * @author Alexander Lütz
  917. * @date 07/10/2011
  918. * @param std::vector<int> indices of rows which shall be deleted (not const, reference, sorted afterwards)
  919. */
  920. //not const, because they get sorted
  921. template <typename T>
  922. void MatrixT<T>::deleteCols ( std::vector<int> & indices)
  923. {
  924. for (std::vector<int>::const_iterator it = indices.begin(); it != indices.end(); it++)
  925. {
  926. if ( (0 > *it) || ((*this).cols() <= (uint) *it))
  927. fthrow(Exception, "MatrixT::deleteCols -- The given indices are not valid.");
  928. }
  929. sort (indices.begin(), indices.begin() + indices.size());
  930. int col_counter(0);
  931. MatrixT<T> tmp((*this).rows(),(*this).cols()-indices.size());
  932. //TODO check wether this is sufficient according to the way we keep the matrices in the storage!
  933. for (uint j = 0; j < (*this).cols(); j++)
  934. {
  935. for (uint i = 0; i < (*this).rows(); i++)
  936. {
  937. if (j == (uint) indices[col_counter])
  938. {
  939. col_counter++;
  940. break; //break inner j-loop
  941. }
  942. else
  943. tmp(i,j-col_counter) = (*this)(i,j);
  944. }
  945. }
  946. (*this).resize(tmp.rows(), tmp.cols());
  947. (*this) = tmp;
  948. }
  949. } // namespace