You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2399 lines
92 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2015 Yves Renard
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /**@file gmm_blas.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 13, 2002.
  29. @brief Basic linear algebra functions.
  30. */
  31. #ifndef GMM_BLAS_H__
  32. #define GMM_BLAS_H__
  33. // This Version of GMM was modified for StoRM.
  34. // To detect whether the usage of TBB is possible, this include is neccessary
  35. #include "storm-config.h"
  36. #ifdef STORM_HAVE_INTELTBB
  37. # include <new> // This fixes a potential dependency ordering problem between GMM and TBB
  38. # include "tbb/tbb.h"
  39. # include <iterator>
  40. #endif
  41. #include "gmm_scaled.h"
  42. #include "gmm_transposed.h"
  43. #include "gmm_conjugated.h"
  44. namespace gmm {
  45. /* ******************************************************************** */
  46. /* */
  47. /* Generic algorithms */
  48. /* */
  49. /* ******************************************************************** */
  50. /* ******************************************************************** */
  51. /* Miscellaneous */
  52. /* ******************************************************************** */
  53. /** clear (fill with zeros) a vector or matrix. */
  54. template <typename L> inline void clear(L &l)
  55. { linalg_traits<L>::do_clear(l); }
  56. /** @cond DOXY_SHOW_ALL_FUNCTIONS
  57. skip all these redundant definitions in doxygen documentation..
  58. */
  59. template <typename L> inline void clear(const L &l)
  60. { linalg_traits<L>::do_clear(linalg_const_cast(l)); }
  61. ///@endcond
  62. /** count the number of non-zero entries of a vector or matrix. */ template <typename L> inline size_type nnz(const L& l)
  63. { return nnz(l, typename linalg_traits<L>::linalg_type()); }
  64. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  65. template <typename L> inline size_type nnz(const L& l, abstract_vector) {
  66. typename linalg_traits<L>::const_iterator it = vect_const_begin(l),
  67. ite = vect_const_end(l);
  68. size_type res(0);
  69. for (; it != ite; ++it) ++res;
  70. return res;
  71. }
  72. template <typename L> inline size_type nnz(const L& l, abstract_matrix) {
  73. return nnz(l, typename principal_orientation_type<typename
  74. linalg_traits<L>::sub_orientation>::potype());
  75. }
  76. template <typename L> inline size_type nnz(const L& l, row_major) {
  77. size_type res(0);
  78. for (size_type i = 0; i < mat_nrows(l); ++i)
  79. res += nnz(mat_const_row(l, i));
  80. return res;
  81. }
  82. template <typename L> inline size_type nnz(const L& l, col_major) {
  83. size_type res(0);
  84. for (size_type i = 0; i < mat_ncols(l); ++i)
  85. res += nnz(mat_const_col(l, i));
  86. return res;
  87. }
  88. ///@endcond
  89. /** fill a vector or matrix with x. */
  90. template <typename L> inline
  91. void fill(L& l, typename gmm::linalg_traits<L>::value_type x) {
  92. typedef typename gmm::linalg_traits<L>::value_type T;
  93. if (x == T(0)) gmm::clear(l);
  94. fill(l, x, typename linalg_traits<L>::linalg_type());
  95. }
  96. template <typename L> inline
  97. void fill(const L& l, typename gmm::linalg_traits<L>::value_type x) {
  98. fill(linalg_const_cast(l), x);
  99. }
  100. template <typename L> inline // to be optimized for dense vectors ...
  101. void fill(L& l, typename gmm::linalg_traits<L>::value_type x,
  102. abstract_vector) {
  103. for (size_type i = 0; i < vect_size(l); ++i) l[i] = x;
  104. }
  105. template <typename L> inline // to be optimized for dense matrices ...
  106. void fill(L& l, typename gmm::linalg_traits<L>::value_type x,
  107. abstract_matrix) {
  108. for (size_type i = 0; i < mat_nrows(l); ++i)
  109. for (size_type j = 0; j < mat_ncols(l); ++j)
  110. l(i,j) = x;
  111. }
  112. /** fill a vector or matrix with random value (uniform [-1,1]). */
  113. template <typename L> inline void fill_random(L& l)
  114. { fill_random(l, typename linalg_traits<L>::linalg_type()); }
  115. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  116. template <typename L> inline void fill_random(const L& l) {
  117. fill_random(linalg_const_cast(l),
  118. typename linalg_traits<L>::linalg_type());
  119. }
  120. template <typename L> inline void fill_random(L& l, abstract_vector) {
  121. for (size_type i = 0; i < vect_size(l); ++i)
  122. l[i] = gmm::random(typename linalg_traits<L>::value_type());
  123. }
  124. template <typename L> inline void fill_random(L& l, abstract_matrix) {
  125. for (size_type i = 0; i < mat_nrows(l); ++i)
  126. for (size_type j = 0; j < mat_ncols(l); ++j)
  127. l(i,j) = gmm::random(typename linalg_traits<L>::value_type());
  128. }
  129. ///@endcond
  130. /** fill a vector or matrix with random value.
  131. @param l a vector or matrix.
  132. @param cfill probability of a non-zero value.
  133. */
  134. template <typename L> inline void fill_random(L& l, double cfill)
  135. { fill_random(l, cfill, typename linalg_traits<L>::linalg_type()); }
  136. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  137. template <typename L> inline void fill_random(const L& l, double cfill) {
  138. fill_random(linalg_const_cast(l), cfill,
  139. typename linalg_traits<L>::linalg_type());
  140. }
  141. template <typename L> inline
  142. void fill_random(L& l, double cfill, abstract_vector) {
  143. typedef typename linalg_traits<L>::value_type T;
  144. size_type ntot = std::min(vect_size(l),
  145. size_type(double(vect_size(l))*cfill) + 1);
  146. for (size_type nb = 0; nb < ntot;) {
  147. size_type i = gmm::irandom(vect_size(l));
  148. if (l[i] == T(0)) {
  149. l[i] = gmm::random(typename linalg_traits<L>::value_type());
  150. ++nb;
  151. }
  152. }
  153. }
  154. template <typename L> inline
  155. void fill_random(L& l, double cfill, abstract_matrix) {
  156. fill_random(l, cfill, typename principal_orientation_type<typename
  157. linalg_traits<L>::sub_orientation>::potype());
  158. }
  159. template <typename L> inline
  160. void fill_random(L& l, double cfill, row_major) {
  161. for (size_type i=0; i < mat_nrows(l); ++i) fill_random(mat_row(l,i),cfill);
  162. }
  163. template <typename L> inline
  164. void fill_random(L& l, double cfill, col_major) {
  165. for (size_type j=0; j < mat_ncols(l); ++j) fill_random(mat_col(l,j),cfill);
  166. }
  167. /* resize a vector */
  168. template <typename V> inline
  169. void resize(V &v, size_type n, linalg_false)
  170. { linalg_traits<V>::resize(v, n); }
  171. template <typename V> inline
  172. void resize(V &, size_type , linalg_modifiable)
  173. { GMM_ASSERT1(false, "You cannot resize a reference"); }
  174. template <typename V> inline
  175. void resize(V &, size_type , linalg_const)
  176. { GMM_ASSERT1(false, "You cannot resize a reference"); }
  177. ///@endcond
  178. /** resize a vector. */
  179. template <typename V> inline
  180. void resize(V &v, size_type n) {
  181. resize(v, n, typename linalg_traits<V>::is_reference());
  182. }
  183. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  184. /** resize a matrix **/
  185. template <typename M> inline
  186. void resize(M &v, size_type m, size_type n, linalg_false) {
  187. linalg_traits<M>::resize(v, m, n);
  188. }
  189. template <typename M> inline
  190. void resize(M &, size_type, size_type, linalg_modifiable)
  191. { GMM_ASSERT1(false, "You cannot resize a reference"); }
  192. template <typename M> inline
  193. void resize(M &, size_type, size_type, linalg_const)
  194. { GMM_ASSERT1(false, "You cannot resize a reference"); }
  195. ///@endcond
  196. /** resize a matrix */
  197. template <typename M> inline
  198. void resize(M &v, size_type m, size_type n)
  199. { resize(v, m, n, typename linalg_traits<M>::is_reference()); }
  200. ///@cond
  201. template <typename M> inline
  202. void reshape(M &v, size_type m, size_type n, linalg_false)
  203. { linalg_traits<M>::reshape(v, m, n); }
  204. template <typename M> inline
  205. void reshape(M &, size_type, size_type, linalg_modifiable)
  206. { GMM_ASSERT1(false, "You cannot reshape a reference"); }
  207. template <typename M> inline
  208. void reshape(M &, size_type, size_type, linalg_const)
  209. { GMM_ASSERT1(false, "You cannot reshape a reference"); }
  210. ///@endcond
  211. /** reshape a matrix */
  212. template <typename M> inline
  213. void reshape(M &v, size_type m, size_type n)
  214. { reshape(v, m, n, typename linalg_traits<M>::is_reference()); }
  215. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  216. /* ******************************************************************** */
  217. /* Scalar product */
  218. /* ******************************************************************** */
  219. ///@endcond
  220. /** scalar product between two vectors */
  221. template <typename V1, typename V2> inline
  222. typename strongest_value_type<V1,V2>::value_type
  223. vect_sp(const V1 &v1, const V2 &v2) {
  224. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch, "
  225. << vect_size(v1) << " !=" << vect_size(v2));
  226. return vect_sp(v1, v2,
  227. typename linalg_traits<V1>::storage_type(),
  228. typename linalg_traits<V2>::storage_type());
  229. }
  230. /** scalar product between two vectors, using a matrix.
  231. @param ps the matrix of the scalar product.
  232. @param v1 the first vector
  233. @param v2 the second vector
  234. */
  235. template <typename MATSP, typename V1, typename V2> inline
  236. typename strongest_value_type3<V1,V2,MATSP>::value_type
  237. vect_sp(const MATSP &ps, const V1 &v1, const V2 &v2) {
  238. return vect_sp_with_mat(ps, v1, v2,
  239. typename linalg_traits<MATSP>::sub_orientation());
  240. }
  241. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  242. template <typename MATSP, typename V1, typename V2> inline
  243. typename strongest_value_type3<V1,V2,MATSP>::value_type
  244. vect_sp_with_mat(const MATSP &ps, const V1 &v1, const V2 &v2, row_major) {
  245. return vect_sp_with_matr(ps, v1, v2,
  246. typename linalg_traits<V2>::storage_type());
  247. }
  248. template <typename MATSP, typename V1, typename V2> inline
  249. typename strongest_value_type3<V1,V2,MATSP>::value_type
  250. vect_sp_with_matr(const MATSP &ps, const V1 &v1, const V2 &v2,
  251. abstract_sparse) {
  252. GMM_ASSERT2(vect_size(v1) == mat_ncols(ps) &&
  253. vect_size(v2) == mat_nrows(ps), "dimensions mismatch");
  254. size_type nr = mat_nrows(ps);
  255. typename linalg_traits<V2>::const_iterator
  256. it = vect_const_begin(v2), ite = vect_const_end(v2);
  257. typename strongest_value_type3<V1,V2,MATSP>::value_type res(0);
  258. for (; it != ite; ++it)
  259. res += vect_sp(mat_const_row(ps, it.index()), v1)* (*it);
  260. return res;
  261. }
  262. template <typename MATSP, typename V1, typename V2> inline
  263. typename strongest_value_type3<V1,V2,MATSP>::value_type
  264. vect_sp_with_matr(const MATSP &ps, const V1 &v1, const V2 &v2,
  265. abstract_skyline)
  266. { return vect_sp_with_matr(ps, v1, v2, abstract_sparse()); }
  267. template <typename MATSP, typename V1, typename V2> inline
  268. typename strongest_value_type3<V1,V2,MATSP>::value_type
  269. vect_sp_with_matr(const MATSP &ps, const V1 &v1, const V2 &v2,
  270. abstract_dense) {
  271. GMM_ASSERT2(vect_size(v1) == mat_ncols(ps) &&
  272. vect_size(v2) == mat_nrows(ps), "dimensions mismatch");
  273. typename linalg_traits<V2>::const_iterator
  274. it = vect_const_begin(v2), ite = vect_const_end(v2);
  275. typename strongest_value_type3<V1,V2,MATSP>::value_type res(0);
  276. for (size_type i = 0; it != ite; ++i, ++it)
  277. res += vect_sp(mat_const_row(ps, i), v1) * (*it);
  278. return res;
  279. }
  280. template <typename MATSP, typename V1, typename V2> inline
  281. typename strongest_value_type3<V1,V2,MATSP>::value_type
  282. vect_sp_with_mat(const MATSP &ps, const V1 &v1,const V2 &v2,row_and_col)
  283. { return vect_sp_with_mat(ps, v1, v2, row_major()); }
  284. template <typename MATSP, typename V1, typename V2> inline
  285. typename strongest_value_type3<V1,V2,MATSP>::value_type
  286. vect_sp_with_mat(const MATSP &ps, const V1 &v1, const V2 &v2,col_major){
  287. return vect_sp_with_matc(ps, v1, v2,
  288. typename linalg_traits<V1>::storage_type());
  289. }
  290. template <typename MATSP, typename V1, typename V2> inline
  291. typename strongest_value_type3<V1,V2,MATSP>::value_type
  292. vect_sp_with_matc(const MATSP &ps, const V1 &v1, const V2 &v2,
  293. abstract_sparse) {
  294. GMM_ASSERT2(vect_size(v1) == mat_ncols(ps) &&
  295. vect_size(v2) == mat_nrows(ps), "dimensions mismatch");
  296. typename linalg_traits<V1>::const_iterator
  297. it = vect_const_begin(v1), ite = vect_const_end(v1);
  298. typename strongest_value_type3<V1,V2,MATSP>::value_type res(0);
  299. for (; it != ite; ++it)
  300. res += vect_sp(mat_const_col(ps, it.index()), v2) * (*it);
  301. return res;
  302. }
  303. template <typename MATSP, typename V1, typename V2> inline
  304. typename strongest_value_type3<V1,V2,MATSP>::value_type
  305. vect_sp_with_matc(const MATSP &ps, const V1 &v1, const V2 &v2,
  306. abstract_skyline)
  307. { return vect_sp_with_matc(ps, v1, v2, abstract_sparse()); }
  308. template <typename MATSP, typename V1, typename V2> inline
  309. typename strongest_value_type3<V1,V2,MATSP>::value_type
  310. vect_sp_with_matc(const MATSP &ps, const V1 &v1, const V2 &v2,
  311. abstract_dense) {
  312. GMM_ASSERT2(vect_size(v1) == mat_ncols(ps) &&
  313. vect_size(v2) == mat_nrows(ps), "dimensions mismatch");
  314. typename linalg_traits<V1>::const_iterator
  315. it = vect_const_begin(v1), ite = vect_const_end(v1);
  316. typename strongest_value_type3<V1,V2,MATSP>::value_type res(0);
  317. for (size_type i = 0; it != ite; ++i, ++it)
  318. res += vect_sp(mat_const_col(ps, i), v2) * (*it);
  319. return res;
  320. }
  321. template <typename MATSP, typename V1, typename V2> inline
  322. typename strongest_value_type3<V1,V2,MATSP>::value_type
  323. vect_sp_with_mat(const MATSP &ps, const V1 &v1,const V2 &v2,col_and_row)
  324. { return vect_sp_with_mat(ps, v1, v2, col_major()); }
  325. template <typename MATSP, typename V1, typename V2> inline
  326. typename strongest_value_type3<V1,V2,MATSP>::value_type
  327. vect_sp_with_mat(const MATSP &ps, const V1 &v1, const V2 &v2,
  328. abstract_null_type) {
  329. typename temporary_vector<V1>::vector_type w(mat_nrows(ps));
  330. GMM_WARNING2("Warning, a temporary is used in scalar product\n");
  331. mult(ps, v1, w);
  332. return vect_sp(w, v2);
  333. }
  334. template <typename IT1, typename IT2> inline
  335. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  336. typename std::iterator_traits<IT2>::value_type>::T
  337. vect_sp_dense_(IT1 it, IT1 ite, IT2 it2) {
  338. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  339. typename std::iterator_traits<IT2>::value_type>::T res(0);
  340. for (; it != ite; ++it, ++it2) res += (*it) * (*it2);
  341. return res;
  342. }
  343. template <typename IT1, typename V> inline
  344. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  345. typename linalg_traits<V>::value_type>::T
  346. vect_sp_sparse_(IT1 it, IT1 ite, const V &v) {
  347. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  348. typename linalg_traits<V>::value_type>::T res(0);
  349. for (; it != ite; ++it) res += (*it) * v[it.index()];
  350. return res;
  351. }
  352. template <typename V1, typename V2> inline
  353. typename strongest_value_type<V1,V2>::value_type
  354. vect_sp(const V1 &v1, const V2 &v2, abstract_dense, abstract_dense) {
  355. return vect_sp_dense_(vect_const_begin(v1), vect_const_end(v1),
  356. vect_const_begin(v2));
  357. }
  358. template <typename V1, typename V2> inline
  359. typename strongest_value_type<V1,V2>::value_type
  360. vect_sp(const V1 &v1, const V2 &v2, abstract_skyline, abstract_dense) {
  361. typename linalg_traits<V1>::const_iterator it1 = vect_const_begin(v1),
  362. ite = vect_const_end(v1);
  363. typename linalg_traits<V2>::const_iterator it2 = vect_const_begin(v2);
  364. return vect_sp_dense_(it1, ite, it2 + it1.index());
  365. }
  366. template <typename V1, typename V2> inline
  367. typename strongest_value_type<V1,V2>::value_type
  368. vect_sp(const V1 &v1, const V2 &v2, abstract_dense, abstract_skyline) {
  369. typename linalg_traits<V2>::const_iterator it1 = vect_const_begin(v2),
  370. ite = vect_const_end(v2);
  371. typename linalg_traits<V1>::const_iterator it2 = vect_const_begin(v1);
  372. return vect_sp_dense_(it1, ite, it2 + it1.index());
  373. }
  374. template <typename V1, typename V2> inline
  375. typename strongest_value_type<V1,V2>::value_type
  376. vect_sp(const V1 &v1, const V2 &v2, abstract_skyline, abstract_skyline) {
  377. typedef typename strongest_value_type<V1,V2>::value_type T;
  378. typename linalg_traits<V1>::const_iterator it1 = vect_const_begin(v1),
  379. ite1 = vect_const_end(v1);
  380. typename linalg_traits<V2>::const_iterator it2 = vect_const_begin(v2),
  381. ite2 = vect_const_end(v2);
  382. size_type n = std::min(ite1.index(), ite2.index());
  383. size_type l = std::max(it1.index(), it2.index());
  384. if (l < n) {
  385. size_type m = l - it1.index(), p = l - it2.index(), q = m + n - l;
  386. return vect_sp_dense_(it1+m, it1+q, it2 + p);
  387. }
  388. return T(0);
  389. }
  390. template <typename V1, typename V2> inline
  391. typename strongest_value_type<V1,V2>::value_type
  392. vect_sp(const V1 &v1, const V2 &v2,abstract_sparse,abstract_dense) {
  393. return vect_sp_sparse_(vect_const_begin(v1), vect_const_end(v1), v2);
  394. }
  395. template <typename V1, typename V2> inline
  396. typename strongest_value_type<V1,V2>::value_type
  397. vect_sp(const V1 &v1, const V2 &v2, abstract_sparse, abstract_skyline) {
  398. return vect_sp_sparse_(vect_const_begin(v1), vect_const_end(v1), v2);
  399. }
  400. template <typename V1, typename V2> inline
  401. typename strongest_value_type<V1,V2>::value_type
  402. vect_sp(const V1 &v1, const V2 &v2, abstract_skyline, abstract_sparse) {
  403. return vect_sp_sparse_(vect_const_begin(v2), vect_const_end(v2), v1);
  404. }
  405. template <typename V1, typename V2> inline
  406. typename strongest_value_type<V1,V2>::value_type
  407. vect_sp(const V1 &v1, const V2 &v2, abstract_dense,abstract_sparse) {
  408. return vect_sp_sparse_(vect_const_begin(v2), vect_const_end(v2), v1);
  409. }
  410. template <typename V1, typename V2> inline
  411. typename strongest_value_type<V1,V2>::value_type
  412. vect_sp_sparse_sparse(const V1 &v1, const V2 &v2, linalg_true) {
  413. typename linalg_traits<V1>::const_iterator it1 = vect_const_begin(v1),
  414. ite1 = vect_const_end(v1);
  415. typename linalg_traits<V2>::const_iterator it2 = vect_const_begin(v2),
  416. ite2 = vect_const_end(v2);
  417. typename strongest_value_type<V1,V2>::value_type res(0);
  418. while (it1 != ite1 && it2 != ite2) {
  419. if (it1.index() == it2.index())
  420. { res += (*it1) * *it2; ++it1; ++it2; }
  421. else if (it1.index() < it2.index()) ++it1; else ++it2;
  422. }
  423. return res;
  424. }
  425. template <typename V1, typename V2> inline
  426. typename strongest_value_type<V1,V2>::value_type
  427. vect_sp_sparse_sparse(const V1 &v1, const V2 &v2, linalg_false) {
  428. return vect_sp_sparse_(vect_const_begin(v1), vect_const_end(v1), v2);
  429. }
  430. template <typename V1, typename V2> inline
  431. typename strongest_value_type<V1,V2>::value_type
  432. vect_sp(const V1 &v1, const V2 &v2,abstract_sparse,abstract_sparse) {
  433. return vect_sp_sparse_sparse(v1, v2,
  434. typename linalg_and<typename linalg_traits<V1>::index_sorted,
  435. typename linalg_traits<V2>::index_sorted>::bool_type());
  436. }
  437. /* ******************************************************************** */
  438. /* Hermitian product */
  439. /* ******************************************************************** */
  440. ///@endcond
  441. /** Hermitian product. */
  442. template <typename V1, typename V2>
  443. inline typename strongest_value_type<V1,V2>::value_type
  444. vect_hp(const V1 &v1, const V2 &v2)
  445. { return vect_sp(v1, conjugated(v2)); }
  446. /** Hermitian product with a matrix. */
  447. template <typename MATSP, typename V1, typename V2> inline
  448. typename strongest_value_type3<V1,V2,MATSP>::value_type
  449. vect_hp(const MATSP &ps, const V1 &v1, const V2 &v2) {
  450. return vect_sp(ps, v1, gmm::conjugated(v2));
  451. }
  452. /* ******************************************************************** */
  453. /* Trace of a matrix */
  454. /* ******************************************************************** */
  455. /** Trace of a matrix */
  456. template <typename M>
  457. typename linalg_traits<M>::value_type
  458. mat_trace(const M &m) {
  459. typedef typename linalg_traits<M>::value_type T;
  460. T res(0);
  461. for (size_type i = 0; i < std::min(mat_nrows(m), mat_ncols(m)); ++i)
  462. res += m(i,i);
  463. return res;
  464. }
  465. /* ******************************************************************** */
  466. /* Euclidean norm */
  467. /* ******************************************************************** */
  468. /** squared Euclidean norm of a vector. */
  469. template <typename V>
  470. typename number_traits<typename linalg_traits<V>::value_type>
  471. ::magnitude_type
  472. vect_norm2_sqr(const V &v) {
  473. typedef typename linalg_traits<V>::value_type T;
  474. typedef typename number_traits<T>::magnitude_type R;
  475. typename linalg_traits<V>::const_iterator
  476. it = vect_const_begin(v), ite = vect_const_end(v);
  477. R res(0);
  478. for (; it != ite; ++it) res += gmm::abs_sqr(*it);
  479. return res;
  480. }
  481. /** Euclidean norm of a vector. */
  482. template <typename V> inline
  483. typename number_traits<typename linalg_traits<V>::value_type>
  484. ::magnitude_type
  485. vect_norm2(const V &v)
  486. { return sqrt(vect_norm2_sqr(v)); }
  487. /** squared Euclidean distance between two vectors */
  488. template <typename V1, typename V2> inline
  489. typename number_traits<typename linalg_traits<V1>::value_type>
  490. ::magnitude_type
  491. vect_dist2_sqr(const V1 &v1, const V2 &v2) { // not fully optimized
  492. typedef typename linalg_traits<V1>::value_type T;
  493. typedef typename number_traits<T>::magnitude_type R;
  494. typename linalg_traits<V1>::const_iterator
  495. it1 = vect_const_begin(v1), ite1 = vect_const_end(v1);
  496. typename linalg_traits<V2>::const_iterator
  497. it2 = vect_const_begin(v2), ite2 = vect_const_end(v2);
  498. size_type k1(0), k2(0);
  499. R res(0);
  500. while (it1 != ite1 && it2 != ite2) {
  501. size_type i1 = index_of_it(it1, k1,
  502. typename linalg_traits<V1>::storage_type());
  503. size_type i2 = index_of_it(it2, k2,
  504. typename linalg_traits<V2>::storage_type());
  505. if (i1 == i2) {
  506. res += gmm::abs_sqr(*it2 - *it1); ++it1; ++k1; ++it2; ++k2;
  507. }
  508. else if (i1 < i2) {
  509. res += gmm::abs_sqr(*it1); ++it1; ++k1;
  510. }
  511. else {
  512. res += gmm::abs_sqr(*it2); ++it2; ++k2;
  513. }
  514. }
  515. while (it1 != ite1) { res += gmm::abs_sqr(*it1); ++it1; }
  516. while (it2 != ite2) { res += gmm::abs_sqr(*it2); ++it2; }
  517. return res;
  518. }
  519. /** Euclidean distance between two vectors */
  520. template <typename V1, typename V2> inline
  521. typename number_traits<typename linalg_traits<V1>::value_type>
  522. ::magnitude_type
  523. vect_dist2(const V1 &v1, const V2 &v2)
  524. { return sqrt(vect_dist2_sqr(v1, v2)); }
  525. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  526. template <typename M>
  527. typename number_traits<typename linalg_traits<M>::value_type>
  528. ::magnitude_type
  529. mat_euclidean_norm_sqr(const M &m, row_major) {
  530. typename number_traits<typename linalg_traits<M>::value_type>
  531. ::magnitude_type res(0);
  532. for (size_type i = 0; i < mat_nrows(m); ++i)
  533. res += vect_norm2_sqr(mat_const_row(m, i));
  534. return res;
  535. }
  536. template <typename M>
  537. typename number_traits<typename linalg_traits<M>::value_type>
  538. ::magnitude_type
  539. mat_euclidean_norm_sqr(const M &m, col_major) {
  540. typename number_traits<typename linalg_traits<M>::value_type>
  541. ::magnitude_type res(0);
  542. for (size_type i = 0; i < mat_ncols(m); ++i)
  543. res += vect_norm2_sqr(mat_const_col(m, i));
  544. return res;
  545. }
  546. ///@endcond
  547. /** squared Euclidean norm of a matrix. */
  548. template <typename M> inline
  549. typename number_traits<typename linalg_traits<M>::value_type>
  550. ::magnitude_type
  551. mat_euclidean_norm_sqr(const M &m) {
  552. return mat_euclidean_norm_sqr(m,
  553. typename principal_orientation_type<typename
  554. linalg_traits<M>::sub_orientation>::potype());
  555. }
  556. /** Euclidean norm of a matrix. */
  557. template <typename M> inline
  558. typename number_traits<typename linalg_traits<M>::value_type>
  559. ::magnitude_type
  560. mat_euclidean_norm(const M &m)
  561. { return gmm::sqrt(mat_euclidean_norm_sqr(m)); }
  562. /* ******************************************************************** */
  563. /* vector norm1 */
  564. /* ******************************************************************** */
  565. /** 1-norm of a vector */
  566. template <typename V>
  567. typename number_traits<typename linalg_traits<V>::value_type>
  568. ::magnitude_type
  569. vect_norm1(const V &v) {
  570. typename linalg_traits<V>::const_iterator
  571. it = vect_const_begin(v), ite = vect_const_end(v);
  572. typename number_traits<typename linalg_traits<V>::value_type>
  573. ::magnitude_type res(0);
  574. for (; it != ite; ++it) res += gmm::abs(*it);
  575. return res;
  576. }
  577. /* ******************************************************************** */
  578. /* vector Infinity norm */
  579. /* ******************************************************************** */
  580. /** Infinity norm of a vector. */
  581. template <typename V>
  582. typename number_traits<typename linalg_traits<V>::value_type>
  583. ::magnitude_type
  584. vect_norminf(const V &v) {
  585. typename linalg_traits<V>::const_iterator
  586. it = vect_const_begin(v), ite = vect_const_end(v);
  587. typename number_traits<typename linalg_traits<V>::value_type>
  588. ::magnitude_type res(0);
  589. for (; it != ite; ++it) res = std::max(res, gmm::abs(*it));
  590. return res;
  591. }
  592. /* ******************************************************************** */
  593. /* matrix norm1 */
  594. /* ******************************************************************** */
  595. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  596. template <typename M>
  597. typename number_traits<typename linalg_traits<M>::value_type>
  598. ::magnitude_type
  599. mat_norm1(const M &m, col_major) {
  600. typename number_traits<typename linalg_traits<M>::value_type>
  601. ::magnitude_type res(0);
  602. for (size_type i = 0; i < mat_ncols(m); ++i)
  603. res = std::max(res, vect_norm1(mat_const_col(m,i)));
  604. return res;
  605. }
  606. template <typename M>
  607. typename number_traits<typename linalg_traits<M>::value_type>
  608. ::magnitude_type
  609. mat_norm1(const M &m, row_major) {
  610. typedef typename linalg_traits<M>::value_type T;
  611. typedef typename number_traits<T>::magnitude_type R;
  612. typedef typename linalg_traits<M>::storage_type store_type;
  613. std::vector<R> aux(mat_ncols(m));
  614. for (size_type i = 0; i < mat_nrows(m); ++i) {
  615. typedef typename linalg_traits<M>::const_sub_row_type row_type;
  616. row_type row = mat_const_row(m, i);
  617. typename linalg_traits<row_type>::const_iterator
  618. it = vect_const_begin(row), ite = vect_const_end(row);
  619. for (size_type k = 0; it != ite; ++it, ++k)
  620. aux[index_of_it(it, k, store_type())] += gmm::abs(*it);
  621. }
  622. return vect_norminf(aux);
  623. }
  624. template <typename M>
  625. typename number_traits<typename linalg_traits<M>::value_type>
  626. ::magnitude_type
  627. mat_norm1(const M &m, col_and_row)
  628. { return mat_norm1(m, col_major()); }
  629. template <typename M>
  630. typename number_traits<typename linalg_traits<M>::value_type>
  631. ::magnitude_type
  632. mat_norm1(const M &m, row_and_col)
  633. { return mat_norm1(m, col_major()); }
  634. ///@endcond
  635. /** 1-norm of a matrix */
  636. template <typename M>
  637. typename number_traits<typename linalg_traits<M>::value_type>
  638. ::magnitude_type
  639. mat_norm1(const M &m) {
  640. return mat_norm1(m, typename linalg_traits<M>::sub_orientation());
  641. }
  642. /* ******************************************************************** */
  643. /* matrix Infinity norm */
  644. /* ******************************************************************** */
  645. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  646. template <typename M>
  647. typename number_traits<typename linalg_traits<M>::value_type>
  648. ::magnitude_type
  649. mat_norminf(const M &m, row_major) {
  650. typename number_traits<typename linalg_traits<M>::value_type>
  651. ::magnitude_type res(0);
  652. for (size_type i = 0; i < mat_nrows(m); ++i)
  653. res = std::max(res, vect_norm1(mat_const_row(m,i)));
  654. return res;
  655. }
  656. template <typename M>
  657. typename number_traits<typename linalg_traits<M>::value_type>
  658. ::magnitude_type
  659. mat_norminf(const M &m, col_major) {
  660. typedef typename linalg_traits<M>::value_type T;
  661. typedef typename number_traits<T>::magnitude_type R;
  662. typedef typename linalg_traits<M>::storage_type store_type;
  663. std::vector<R> aux(mat_nrows(m));
  664. for (size_type i = 0; i < mat_ncols(m); ++i) {
  665. typedef typename linalg_traits<M>::const_sub_col_type col_type;
  666. col_type col = mat_const_col(m, i);
  667. typename linalg_traits<col_type>::const_iterator
  668. it = vect_const_begin(col), ite = vect_const_end(col);
  669. for (size_type k = 0; it != ite; ++it, ++k)
  670. aux[index_of_it(it, k, store_type())] += gmm::abs(*it);
  671. }
  672. return vect_norminf(aux);
  673. }
  674. template <typename M>
  675. typename number_traits<typename linalg_traits<M>::value_type>
  676. ::magnitude_type
  677. mat_norminf(const M &m, col_and_row)
  678. { return mat_norminf(m, row_major()); }
  679. template <typename M>
  680. typename number_traits<typename linalg_traits<M>::value_type>
  681. ::magnitude_type
  682. mat_norminf(const M &m, row_and_col)
  683. { return mat_norminf(m, row_major()); }
  684. ///@endcond
  685. /** infinity-norm of a matrix.*/
  686. template <typename M>
  687. typename number_traits<typename linalg_traits<M>::value_type>
  688. ::magnitude_type
  689. mat_norminf(const M &m) {
  690. return mat_norminf(m, typename linalg_traits<M>::sub_orientation());
  691. }
  692. /* ******************************************************************** */
  693. /* Max norm for matrices */
  694. /* ******************************************************************** */
  695. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  696. template <typename M>
  697. typename number_traits<typename linalg_traits<M>::value_type>
  698. ::magnitude_type
  699. mat_maxnorm(const M &m, row_major) {
  700. typename number_traits<typename linalg_traits<M>::value_type>
  701. ::magnitude_type res(0);
  702. for (size_type i = 0; i < mat_nrows(m); ++i)
  703. res = std::max(res, vect_norminf(mat_const_row(m,i)));
  704. return res;
  705. }
  706. template <typename M>
  707. typename number_traits<typename linalg_traits<M>::value_type>
  708. ::magnitude_type
  709. mat_maxnorm(const M &m, col_major) {
  710. typename number_traits<typename linalg_traits<M>::value_type>
  711. ::magnitude_type res(0);
  712. for (size_type i = 0; i < mat_ncols(m); ++i)
  713. res = std::max(res, vect_norminf(mat_const_col(m,i)));
  714. return res;
  715. }
  716. ///@endcond
  717. /** max-norm of a matrix. */
  718. template <typename M>
  719. typename number_traits<typename linalg_traits<M>::value_type>
  720. ::magnitude_type
  721. mat_maxnorm(const M &m) {
  722. return mat_maxnorm(m,
  723. typename principal_orientation_type<typename
  724. linalg_traits<M>::sub_orientation>::potype());
  725. }
  726. /* ******************************************************************** */
  727. /* Clean */
  728. /* ******************************************************************** */
  729. /** Clean a vector or matrix (replace near-zero entries with zeroes). */
  730. template <typename L> inline void clean(L &l, double threshold);
  731. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  732. template <typename L, typename T>
  733. void clean(L &l, double threshold, abstract_dense, T) {
  734. typedef typename number_traits<T>::magnitude_type R;
  735. typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
  736. for (; it != ite; ++it)
  737. if (gmm::abs(*it) < R(threshold)) *it = T(0);
  738. }
  739. template <typename L, typename T>
  740. void clean(L &l, double threshold, abstract_skyline, T)
  741. { gmm::clean(l, threshold, abstract_dense(), T()); }
  742. template <typename L, typename T>
  743. void clean(L &l, double threshold, abstract_sparse, T) {
  744. typedef typename number_traits<T>::magnitude_type R;
  745. typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
  746. std::vector<size_type> ind;
  747. for (; it != ite; ++it)
  748. if (gmm::abs(*it) < R(threshold)) ind.push_back(it.index());
  749. for (size_type i = 0; i < ind.size(); ++i) l[ind[i]] = T(0);
  750. }
  751. template <typename L, typename T>
  752. void clean(L &l, double threshold, abstract_dense, std::complex<T>) {
  753. typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
  754. for (; it != ite; ++it){
  755. if (gmm::abs((*it).real()) < T(threshold))
  756. *it = std::complex<T>(T(0), (*it).imag());
  757. if (gmm::abs((*it).imag()) < T(threshold))
  758. *it = std::complex<T>((*it).real(), T(0));
  759. }
  760. }
  761. template <typename L, typename T>
  762. void clean(L &l, double threshold, abstract_skyline, std::complex<T>)
  763. { gmm::clean(l, threshold, abstract_dense(), std::complex<T>()); }
  764. template <typename L, typename T>
  765. void clean(L &l, double threshold, abstract_sparse, std::complex<T>) {
  766. typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
  767. std::vector<size_type> ind;
  768. for (; it != ite; ++it) {
  769. bool r = (gmm::abs((*it).real()) < T(threshold));
  770. bool i = (gmm::abs((*it).imag()) < T(threshold));
  771. if (r && i) ind.push_back(it.index());
  772. else if (r) *it = std::complex<T>(T(0), (*it).imag());
  773. else if (i) *it = std::complex<T>((*it).real(), T(0));
  774. }
  775. for (size_type i = 0; i < ind.size(); ++i)
  776. l[ind[i]] = std::complex<T>(T(0),T(0));
  777. }
  778. template <typename L> inline void clean(L &l, double threshold,
  779. abstract_vector) {
  780. gmm::clean(l, threshold, typename linalg_traits<L>::storage_type(),
  781. typename linalg_traits<L>::value_type());
  782. }
  783. template <typename L> inline void clean(const L &l, double threshold);
  784. template <typename L> void clean(L &l, double threshold, row_major) {
  785. for (size_type i = 0; i < mat_nrows(l); ++i)
  786. gmm::clean(mat_row(l, i), threshold);
  787. }
  788. template <typename L> void clean(L &l, double threshold, col_major) {
  789. for (size_type i = 0; i < mat_ncols(l); ++i)
  790. gmm::clean(mat_col(l, i), threshold);
  791. }
  792. template <typename L> inline void clean(L &l, double threshold,
  793. abstract_matrix) {
  794. gmm::clean(l, threshold,
  795. typename principal_orientation_type<typename
  796. linalg_traits<L>::sub_orientation>::potype());
  797. }
  798. template <typename L> inline void clean(L &l, double threshold)
  799. { clean(l, threshold, typename linalg_traits<L>::linalg_type()); }
  800. template <typename L> inline void clean(const L &l, double threshold)
  801. { gmm::clean(linalg_const_cast(l), threshold); }
  802. /* ******************************************************************** */
  803. /* Copy */
  804. /* ******************************************************************** */
  805. ///@endcond
  806. /** Copy vectors or matrices.
  807. @param l1 source vector or matrix.
  808. @param l2 destination.
  809. */
  810. template <typename L1, typename L2> inline
  811. void copy(const L1& l1, L2& l2) {
  812. if ((const void *)(&l1) != (const void *)(&l2)) {
  813. if (same_origin(l1,l2))
  814. GMM_WARNING2("Warning : a conflict is possible in copy\n");
  815. copy(l1, l2, typename linalg_traits<L1>::linalg_type(),
  816. typename linalg_traits<L2>::linalg_type());
  817. }
  818. }
  819. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  820. template <typename L1, typename L2> inline
  821. void copy(const L1& l1, const L2& l2) { copy(l1, linalg_const_cast(l2)); }
  822. template <typename L1, typename L2> inline
  823. void copy(const L1& l1, L2& l2, abstract_vector, abstract_vector) {
  824. GMM_ASSERT2(vect_size(l1) == vect_size(l2), "dimensions mismatch, "
  825. << vect_size(l1) << " !=" << vect_size(l2));
  826. copy_vect(l1, l2, typename linalg_traits<L1>::storage_type(),
  827. typename linalg_traits<L2>::storage_type());
  828. }
  829. template <typename L1, typename L2> inline
  830. void copy(const L1& l1, L2& l2, abstract_matrix, abstract_matrix) {
  831. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  832. if (!m || !n) return;
  833. GMM_ASSERT2(n==mat_ncols(l2) && m==mat_nrows(l2), "dimensions mismatch");
  834. copy_mat(l1, l2, typename linalg_traits<L1>::sub_orientation(),
  835. typename linalg_traits<L2>::sub_orientation());
  836. }
  837. template <typename V1, typename V2, typename C1, typename C2> inline
  838. void copy_vect(const V1 &v1, const V2 &v2, C1, C2)
  839. { copy_vect(v1, const_cast<V2 &>(v2), C1(), C2()); }
  840. template <typename L1, typename L2>
  841. void copy_mat_by_row(const L1& l1, L2& l2) {
  842. size_type nbr = mat_nrows(l1);
  843. for (size_type i = 0; i < nbr; ++i)
  844. copy_vect(mat_const_row(l1, i), mat_row(l2, i),
  845. typename linalg_traits<L1>::storage_type(),
  846. typename linalg_traits<L2>::storage_type());
  847. }
  848. template <typename L1, typename L2>
  849. void copy_mat_by_col(const L1 &l1, L2 &l2) {
  850. size_type nbc = mat_ncols(l1);
  851. for (size_type i = 0; i < nbc; ++i) {
  852. copy_vect(mat_const_col(l1, i), mat_col(l2, i),
  853. typename linalg_traits<L1>::storage_type(),
  854. typename linalg_traits<L2>::storage_type());
  855. }
  856. }
  857. template <typename L1, typename L2> inline
  858. void copy_mat(const L1& l1, L2& l2, row_major, row_major)
  859. { copy_mat_by_row(l1, l2); }
  860. template <typename L1, typename L2> inline
  861. void copy_mat(const L1& l1, L2& l2, row_major, row_and_col)
  862. { copy_mat_by_row(l1, l2); }
  863. template <typename L1, typename L2> inline
  864. void copy_mat(const L1& l1, L2& l2, row_and_col, row_and_col)
  865. { copy_mat_by_row(l1, l2); }
  866. template <typename L1, typename L2> inline
  867. void copy_mat(const L1& l1, L2& l2, row_and_col, row_major)
  868. { copy_mat_by_row(l1, l2); }
  869. template <typename L1, typename L2> inline
  870. void copy_mat(const L1& l1, L2& l2, col_and_row, row_major)
  871. { copy_mat_by_row(l1, l2); }
  872. template <typename L1, typename L2> inline
  873. void copy_mat(const L1& l1, L2& l2, row_major, col_and_row)
  874. { copy_mat_by_row(l1, l2); }
  875. template <typename L1, typename L2> inline
  876. void copy_mat(const L1& l1, L2& l2, col_and_row, row_and_col)
  877. { copy_mat_by_row(l1, l2); }
  878. template <typename L1, typename L2> inline
  879. void copy_mat(const L1& l1, L2& l2, row_and_col, col_and_row)
  880. { copy_mat_by_row(l1, l2); }
  881. template <typename L1, typename L2> inline
  882. void copy_mat(const L1& l1, L2& l2, col_major, col_major)
  883. { copy_mat_by_col(l1, l2); }
  884. template <typename L1, typename L2> inline
  885. void copy_mat(const L1& l1, L2& l2, col_major, col_and_row)
  886. { copy_mat_by_col(l1, l2); }
  887. template <typename L1, typename L2> inline
  888. void copy_mat(const L1& l1, L2& l2, col_major, row_and_col)
  889. { copy_mat_by_col(l1, l2); }
  890. template <typename L1, typename L2> inline
  891. void copy_mat(const L1& l1, L2& l2, row_and_col, col_major)
  892. { copy_mat_by_col(l1, l2); }
  893. template <typename L1, typename L2> inline
  894. void copy_mat(const L1& l1, L2& l2, col_and_row, col_major)
  895. { copy_mat_by_col(l1, l2); }
  896. template <typename L1, typename L2> inline
  897. void copy_mat(const L1& l1, L2& l2, col_and_row, col_and_row)
  898. { copy_mat_by_col(l1, l2); }
  899. template <typename L1, typename L2> inline
  900. void copy_mat_mixed_rc(const L1& l1, L2& l2, size_type i) {
  901. copy_mat_mixed_rc(l1, l2, i, typename linalg_traits<L1>::storage_type());
  902. }
  903. template <typename L1, typename L2>
  904. void copy_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_sparse) {
  905. typename linalg_traits<L1>::const_iterator
  906. it = vect_const_begin(l1), ite = vect_const_end(l1);
  907. for (; it != ite; ++it)
  908. l2(i, it.index()) = *it;
  909. }
  910. template <typename L1, typename L2>
  911. void copy_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_skyline) {
  912. typename linalg_traits<L1>::const_iterator
  913. it = vect_const_begin(l1), ite = vect_const_end(l1);
  914. for (; it != ite; ++it)
  915. l2(i, it.index()) = *it;
  916. }
  917. template <typename L1, typename L2>
  918. void copy_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_dense) {
  919. typename linalg_traits<L1>::const_iterator
  920. it = vect_const_begin(l1), ite = vect_const_end(l1);
  921. for (size_type j = 0; it != ite; ++it, ++j) l2(i, j) = *it;
  922. }
  923. template <typename L1, typename L2> inline
  924. void copy_mat_mixed_cr(const L1& l1, L2& l2, size_type i) {
  925. copy_mat_mixed_cr(l1, l2, i, typename linalg_traits<L1>::storage_type());
  926. }
  927. template <typename L1, typename L2>
  928. void copy_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_sparse) {
  929. typename linalg_traits<L1>::const_iterator
  930. it = vect_const_begin(l1), ite = vect_const_end(l1);
  931. for (; it != ite; ++it) l2(it.index(), i) = *it;
  932. }
  933. template <typename L1, typename L2>
  934. void copy_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_skyline) {
  935. typename linalg_traits<L1>::const_iterator
  936. it = vect_const_begin(l1), ite = vect_const_end(l1);
  937. for (; it != ite; ++it) l2(it.index(), i) = *it;
  938. }
  939. template <typename L1, typename L2>
  940. void copy_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_dense) {
  941. typename linalg_traits<L1>::const_iterator
  942. it = vect_const_begin(l1), ite = vect_const_end(l1);
  943. for (size_type j = 0; it != ite; ++it, ++j) l2(j, i) = *it;
  944. }
  945. template <typename L1, typename L2>
  946. void copy_mat(const L1& l1, L2& l2, row_major, col_major) {
  947. clear(l2);
  948. size_type nbr = mat_nrows(l1);
  949. for (size_type i = 0; i < nbr; ++i)
  950. copy_mat_mixed_rc(mat_const_row(l1, i), l2, i);
  951. }
  952. template <typename L1, typename L2>
  953. void copy_mat(const L1& l1, L2& l2, col_major, row_major) {
  954. clear(l2);
  955. size_type nbc = mat_ncols(l1);
  956. for (size_type i = 0; i < nbc; ++i)
  957. copy_mat_mixed_cr(mat_const_col(l1, i), l2, i);
  958. }
  959. template <typename L1, typename L2> inline
  960. void copy_vect(const L1 &l1, L2 &l2, abstract_dense, abstract_dense) {
  961. std::copy(vect_const_begin(l1), vect_const_end(l1), vect_begin(l2));
  962. }
  963. template <typename L1, typename L2> inline // to be optimised ?
  964. void copy_vect(const L1 &l1, L2 &l2, abstract_skyline, abstract_skyline) {
  965. typename linalg_traits<L1>::const_iterator it1 = vect_const_begin(l1),
  966. ite1 = vect_const_end(l1);
  967. while (it1 != ite1 && *it1 == typename linalg_traits<L1>::value_type(0))
  968. ++it1;
  969. if (ite1 - it1 > 0) {
  970. clear(l2);
  971. typename linalg_traits<L2>::iterator it2 = vect_begin(l2),
  972. ite2 = vect_end(l2);
  973. while (*(ite1-1) == typename linalg_traits<L1>::value_type(0)) ite1--;
  974. if (it2 == ite2) {
  975. l2[it1.index()] = *it1; ++it1;
  976. l2[ite1.index()-1] = *(ite1-1); --ite1;
  977. if (it1 < ite1)
  978. { it2 = vect_begin(l2); ++it2; std::copy(it1, ite1, it2); }
  979. }
  980. else {
  981. ptrdiff_t m = it1.index() - it2.index();
  982. if (m >= 0 && ite1.index() <= ite2.index())
  983. std::copy(it1, ite1, it2 + m);
  984. else {
  985. if (m < 0) l2[it1.index()] = *it1;
  986. if (ite1.index() > ite2.index()) l2[ite1.index()-1] = *(ite1-1);
  987. it2 = vect_begin(l2); ite2 = vect_end(l2);
  988. m = it1.index() - it2.index();
  989. std::copy(it1, ite1, it2 + m);
  990. }
  991. }
  992. }
  993. }
  994. template <typename L1, typename L2>
  995. void copy_vect(const L1& l1, L2& l2, abstract_sparse, abstract_dense) {
  996. clear(l2);
  997. typename linalg_traits<L1>::const_iterator
  998. it = vect_const_begin(l1), ite = vect_const_end(l1);
  999. for (; it != ite; ++it) { l2[it.index()] = *it; }
  1000. }
  1001. template <typename L1, typename L2>
  1002. void copy_vect(const L1& l1, L2& l2, abstract_sparse, abstract_skyline) {
  1003. clear(l2);
  1004. typename linalg_traits<L1>::const_iterator
  1005. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1006. for (; it != ite; ++it) l2[it.index()] = *it;
  1007. }
  1008. template <typename L1, typename L2>
  1009. void copy_vect(const L1& l1, L2& l2, abstract_skyline, abstract_dense) {
  1010. typedef typename linalg_traits<L1>::value_type T;
  1011. typedef typename linalg_traits<L1>::const_iterator l1_const_iterator;
  1012. typedef typename linalg_traits<L2>::iterator l2_iterator;
  1013. l1_const_iterator it = vect_const_begin(l1), ite = vect_const_end(l1);
  1014. if (it == ite)
  1015. gmm::clear(l2);
  1016. else {
  1017. l2_iterator it2 = vect_begin(l2), ite2 = vect_end(l2);
  1018. size_type i = it.index(), j;
  1019. for (j = 0; j < i; ++j, ++it2) *it2 = T(0);
  1020. for (; it != ite; ++it, ++it2) *it2 = *it;
  1021. for (; it2 != ite2; ++it2) *it2 = T(0);
  1022. }
  1023. }
  1024. template <typename L1, typename L2>
  1025. void copy_vect(const L1& l1, L2& l2, abstract_sparse, abstract_sparse) {
  1026. typename linalg_traits<L1>::const_iterator
  1027. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1028. clear(l2);
  1029. for (; it != ite; ++it)
  1030. if (*it != (typename linalg_traits<L1>::value_type)(0))
  1031. l2[it.index()] = *it;
  1032. }
  1033. template <typename L1, typename L2>
  1034. void copy_vect(const L1& l1, L2& l2, abstract_dense, abstract_sparse) {
  1035. clear(l2);
  1036. typename linalg_traits<L1>::const_iterator
  1037. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1038. for (size_type i = 0; it != ite; ++it, ++i)
  1039. if (*it != (typename linalg_traits<L1>::value_type)(0))
  1040. l2[i] = *it;
  1041. }
  1042. template <typename L1, typename L2> // to be optimised ...
  1043. void copy_vect(const L1& l1, L2& l2, abstract_dense, abstract_skyline) {
  1044. clear(l2);
  1045. typename linalg_traits<L1>::const_iterator
  1046. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1047. for (size_type i = 0; it != ite; ++it, ++i)
  1048. if (*it != (typename linalg_traits<L1>::value_type)(0))
  1049. l2[i] = *it;
  1050. }
  1051. template <typename L1, typename L2>
  1052. void copy_vect(const L1& l1, L2& l2, abstract_skyline, abstract_sparse) {
  1053. clear(l2);
  1054. typename linalg_traits<L1>::const_iterator
  1055. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1056. for (; it != ite; ++it)
  1057. if (*it != (typename linalg_traits<L1>::value_type)(0))
  1058. l2[it.index()] = *it;
  1059. }
  1060. /* ******************************************************************** */
  1061. /* Matrix and vector addition */
  1062. /* algorithms are built in order to avoid some conflicts with */
  1063. /* repeated arguments or with overlapping part of a same object. */
  1064. /* In the latter case, conflicts are still possible. */
  1065. /* ******************************************************************** */
  1066. ///@endcond
  1067. /** Add two vectors or matrices
  1068. @param l1
  1069. @param l2 contains on output, l2+l1.
  1070. */
  1071. template <typename L1, typename L2> inline
  1072. void add(const L1& l1, L2& l2) {
  1073. add_spec(l1, l2, typename linalg_traits<L2>::linalg_type());
  1074. }
  1075. ///@cond
  1076. template <typename L1, typename L2> inline
  1077. void add(const L1& l1, const L2& l2) { add(l1, linalg_const_cast(l2)); }
  1078. template <typename L1, typename L2> inline
  1079. void add_spec(const L1& l1, L2& l2, abstract_vector) {
  1080. GMM_ASSERT2(vect_size(l1) == vect_size(l2), "dimensions mismatch, "
  1081. << vect_size(l1) << " !=" << vect_size(l2));
  1082. add(l1, l2, typename linalg_traits<L1>::storage_type(),
  1083. typename linalg_traits<L2>::storage_type());
  1084. }
  1085. template <typename L1, typename L2> inline
  1086. void add_spec(const L1& l1, L2& l2, abstract_matrix) {
  1087. GMM_ASSERT2(mat_nrows(l1)==mat_nrows(l2) && mat_ncols(l1)==mat_ncols(l2),
  1088. "dimensions mismatch");
  1089. add(l1, l2, typename linalg_traits<L1>::sub_orientation(),
  1090. typename linalg_traits<L2>::sub_orientation());
  1091. }
  1092. template <typename L1, typename L2>
  1093. void add(const L1& l1, L2& l2, row_major, row_major) {
  1094. typename linalg_traits<L1>::const_row_iterator it1 = mat_row_begin(l1),
  1095. ite = mat_row_end(l1);
  1096. typename linalg_traits<L2>::row_iterator it2 = mat_row_begin(l2);
  1097. for ( ; it1 != ite; ++it1, ++it2)
  1098. add(linalg_traits<L1>::row(it1), linalg_traits<L2>::row(it2));
  1099. }
  1100. template <typename L1, typename L2>
  1101. void add(const L1& l1, L2& l2, col_major, col_major) {
  1102. typename linalg_traits<L1>::const_col_iterator
  1103. it1 = mat_col_const_begin(l1),
  1104. ite = mat_col_const_end(l1);
  1105. typename linalg_traits<L2>::col_iterator it2 = mat_col_begin(l2);
  1106. for ( ; it1 != ite; ++it1, ++it2)
  1107. add(linalg_traits<L1>::col(it1), linalg_traits<L2>::col(it2));
  1108. }
  1109. template <typename L1, typename L2> inline
  1110. void add_mat_mixed_rc(const L1& l1, L2& l2, size_type i) {
  1111. add_mat_mixed_rc(l1, l2, i, typename linalg_traits<L1>::storage_type());
  1112. }
  1113. template <typename L1, typename L2>
  1114. void add_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_sparse) {
  1115. typename linalg_traits<L1>::const_iterator
  1116. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1117. for (; it != ite; ++it) l2(i, it.index()) += *it;
  1118. }
  1119. template <typename L1, typename L2>
  1120. void add_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_skyline) {
  1121. typename linalg_traits<L1>::const_iterator
  1122. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1123. for (; it != ite; ++it) l2(i, it.index()) += *it;
  1124. }
  1125. template <typename L1, typename L2>
  1126. void add_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_dense) {
  1127. typename linalg_traits<L1>::const_iterator
  1128. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1129. for (size_type j = 0; it != ite; ++it, ++j) l2(i, j) += *it;
  1130. }
  1131. template <typename L1, typename L2> inline
  1132. void add_mat_mixed_cr(const L1& l1, L2& l2, size_type i) {
  1133. add_mat_mixed_cr(l1, l2, i, typename linalg_traits<L1>::storage_type());
  1134. }
  1135. template <typename L1, typename L2>
  1136. void add_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_sparse) {
  1137. typename linalg_traits<L1>::const_iterator
  1138. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1139. for (; it != ite; ++it) l2(it.index(), i) += *it;
  1140. }
  1141. template <typename L1, typename L2>
  1142. void add_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_skyline) {
  1143. typename linalg_traits<L1>::const_iterator
  1144. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1145. for (; it != ite; ++it) l2(it.index(), i) += *it;
  1146. }
  1147. template <typename L1, typename L2>
  1148. void add_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_dense) {
  1149. typename linalg_traits<L1>::const_iterator
  1150. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1151. for (size_type j = 0; it != ite; ++it, ++j) l2(j, i) += *it;
  1152. }
  1153. template <typename L1, typename L2>
  1154. void add(const L1& l1, L2& l2, row_major, col_major) {
  1155. size_type nbr = mat_nrows(l1);
  1156. for (size_type i = 0; i < nbr; ++i)
  1157. add_mat_mixed_rc(mat_const_row(l1, i), l2, i);
  1158. }
  1159. template <typename L1, typename L2>
  1160. void add(const L1& l1, L2& l2, col_major, row_major) {
  1161. size_type nbc = mat_ncols(l1);
  1162. for (size_type i = 0; i < nbc; ++i)
  1163. add_mat_mixed_cr(mat_const_col(l1, i), l2, i);
  1164. }
  1165. template <typename L1, typename L2> inline
  1166. void add(const L1& l1, L2& l2, row_and_col, row_major)
  1167. { add(l1, l2, row_major(), row_major()); }
  1168. template <typename L1, typename L2> inline
  1169. void add(const L1& l1, L2& l2, row_and_col, row_and_col)
  1170. { add(l1, l2, row_major(), row_major()); }
  1171. template <typename L1, typename L2> inline
  1172. void add(const L1& l1, L2& l2, row_and_col, col_and_row)
  1173. { add(l1, l2, row_major(), row_major()); }
  1174. template <typename L1, typename L2> inline
  1175. void add(const L1& l1, L2& l2, col_and_row, row_and_col)
  1176. { add(l1, l2, row_major(), row_major()); }
  1177. template <typename L1, typename L2> inline
  1178. void add(const L1& l1, L2& l2, row_major, row_and_col)
  1179. { add(l1, l2, row_major(), row_major()); }
  1180. template <typename L1, typename L2> inline
  1181. void add(const L1& l1, L2& l2, col_and_row, row_major)
  1182. { add(l1, l2, row_major(), row_major()); }
  1183. template <typename L1, typename L2> inline
  1184. void add(const L1& l1, L2& l2, row_major, col_and_row)
  1185. { add(l1, l2, row_major(), row_major()); }
  1186. template <typename L1, typename L2> inline
  1187. void add(const L1& l1, L2& l2, row_and_col, col_major)
  1188. { add(l1, l2, col_major(), col_major()); }
  1189. template <typename L1, typename L2> inline
  1190. void add(const L1& l1, L2& l2, col_major, row_and_col)
  1191. { add(l1, l2, col_major(), col_major()); }
  1192. template <typename L1, typename L2> inline
  1193. void add(const L1& l1, L2& l2, col_and_row, col_major)
  1194. { add(l1, l2, col_major(), col_major()); }
  1195. template <typename L1, typename L2> inline
  1196. void add(const L1& l1, L2& l2, col_and_row, col_and_row)
  1197. { add(l1, l2, col_major(), col_major()); }
  1198. template <typename L1, typename L2> inline
  1199. void add(const L1& l1, L2& l2, col_major, col_and_row)
  1200. { add(l1, l2, col_major(), col_major()); }
  1201. ///@endcond
  1202. /** Addition of two vectors/matrices
  1203. @param l1
  1204. @param l2
  1205. @param l3 contains l1+l2 on output
  1206. */
  1207. template <typename L1, typename L2, typename L3> inline
  1208. void add(const L1& l1, const L2& l2, L3& l3) {
  1209. add_spec(l1, l2, l3, typename linalg_traits<L2>::linalg_type());
  1210. }
  1211. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  1212. template <typename L1, typename L2, typename L3> inline
  1213. void add(const L1& l1, const L2& l2, const L3& l3)
  1214. { add(l1, l2, linalg_const_cast(l3)); }
  1215. template <typename L1, typename L2, typename L3> inline
  1216. void add_spec(const L1& l1, const L2& l2, L3& l3, abstract_matrix)
  1217. { copy(l2, l3); add(l1, l3); }
  1218. template <typename L1, typename L2, typename L3> inline
  1219. void add_spec(const L1& l1, const L2& l2, L3& l3, abstract_vector) {
  1220. GMM_ASSERT2(vect_size(l1) == vect_size(l2) &&
  1221. vect_size(l1) == vect_size(l3), "dimensions mismatch");
  1222. if ((const void *)(&l1) == (const void *)(&l3))
  1223. add(l2, l3);
  1224. else if ((const void *)(&l2) == (const void *)(&l3))
  1225. add(l1, l3);
  1226. else
  1227. add(l1, l2, l3, typename linalg_traits<L1>::storage_type(),
  1228. typename linalg_traits<L2>::storage_type(),
  1229. typename linalg_traits<L3>::storage_type());
  1230. }
  1231. template <typename IT1, typename IT2, typename IT3>
  1232. void add_full_(IT1 it1, IT2 it2, IT3 it3, IT3 ite) {
  1233. for (; it3 != ite; ++it3, ++it2, ++it1) *it3 = *it1 + *it2;
  1234. }
  1235. template <typename IT1, typename IT2, typename IT3>
  1236. void add_almost_full_(IT1 it1, IT1 ite1, IT2 it2, IT3 it3, IT3 ite3) {
  1237. IT3 it = it3;
  1238. for (; it != ite3; ++it, ++it2) *it = *it2;
  1239. for (; it1 != ite1; ++it1)
  1240. *(it3 + it1.index()) += *it1;
  1241. }
  1242. template <typename IT1, typename IT2, typename IT3>
  1243. void add_to_full_(IT1 it1, IT1 ite1, IT2 it2, IT2 ite2,
  1244. IT3 it3, IT3 ite3) {
  1245. typedef typename std::iterator_traits<IT3>::value_type T;
  1246. IT3 it = it3;
  1247. for (; it != ite3; ++it) *it = T(0);
  1248. for (; it1 != ite1; ++it1) *(it3 + it1.index()) = *it1;
  1249. for (; it2 != ite2; ++it2) *(it3 + it2.index()) += *it2;
  1250. }
  1251. template <typename L1, typename L2, typename L3> inline
  1252. void add(const L1& l1, const L2& l2, L3& l3,
  1253. abstract_dense, abstract_dense, abstract_dense) {
  1254. add_full_(vect_const_begin(l1), vect_const_begin(l2),
  1255. vect_begin(l3), vect_end(l3));
  1256. }
  1257. // generic function for add(v1, v2, v3).
  1258. // Need to be specialized to optimize particular additions.
  1259. template <typename L1, typename L2, typename L3,
  1260. typename ST1, typename ST2, typename ST3>
  1261. inline void add(const L1& l1, const L2& l2, L3& l3, ST1, ST2, ST3)
  1262. { copy(l2, l3); add(l1, l3, ST1(), ST3()); }
  1263. template <typename L1, typename L2, typename L3> inline
  1264. void add(const L1& l1, const L2& l2, L3& l3,
  1265. abstract_sparse, abstract_dense, abstract_dense) {
  1266. add_almost_full_(vect_const_begin(l1), vect_const_end(l1),
  1267. vect_const_begin(l2), vect_begin(l3), vect_end(l3));
  1268. }
  1269. template <typename L1, typename L2, typename L3> inline
  1270. void add(const L1& l1, const L2& l2, L3& l3,
  1271. abstract_dense, abstract_sparse, abstract_dense)
  1272. { add(l2, l1, l3, abstract_sparse(), abstract_dense(), abstract_dense()); }
  1273. template <typename L1, typename L2, typename L3> inline
  1274. void add(const L1& l1, const L2& l2, L3& l3,
  1275. abstract_sparse, abstract_sparse, abstract_dense) {
  1276. add_to_full_(vect_const_begin(l1), vect_const_end(l1),
  1277. vect_const_begin(l2), vect_const_end(l2),
  1278. vect_begin(l3), vect_end(l3));
  1279. }
  1280. template <typename L1, typename L2, typename L3>
  1281. void add_spspsp(const L1& l1, const L2& l2, L3& l3, linalg_true) {
  1282. typename linalg_traits<L1>::const_iterator
  1283. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1284. typename linalg_traits<L2>::const_iterator
  1285. it2 = vect_const_begin(l2), ite2 = vect_const_end(l2);
  1286. clear(l3);
  1287. while (it1 != ite1 && it2 != ite2) {
  1288. ptrdiff_t d = it1.index() - it2.index();
  1289. if (d < 0)
  1290. { l3[it1.index()] += *it1; ++it1; }
  1291. else if (d > 0)
  1292. { l3[it2.index()] += *it2; ++it2; }
  1293. else
  1294. { l3[it1.index()] = *it1 + *it2; ++it1; ++it2; }
  1295. }
  1296. for (; it1 != ite1; ++it1) l3[it1.index()] += *it1;
  1297. for (; it2 != ite2; ++it2) l3[it2.index()] += *it2;
  1298. }
  1299. template <typename L1, typename L2, typename L3>
  1300. void add_spspsp(const L1& l1, const L2& l2, L3& l3, linalg_false)
  1301. { copy(l2, l3); add(l2, l3); }
  1302. template <typename L1, typename L2, typename L3>
  1303. void add(const L1& l1, const L2& l2, L3& l3,
  1304. abstract_sparse, abstract_sparse, abstract_sparse) {
  1305. add_spspsp(l1, l2, l3, typename linalg_and<typename
  1306. linalg_traits<L1>::index_sorted,
  1307. typename linalg_traits<L2>::index_sorted>::bool_type());
  1308. }
  1309. template <typename L1, typename L2>
  1310. void add(const L1& l1, L2& l2, abstract_dense, abstract_dense) {
  1311. typename linalg_traits<L1>::const_iterator it1 = vect_const_begin(l1);
  1312. typename linalg_traits<L2>::iterator
  1313. it2 = vect_begin(l2), ite = vect_end(l2);
  1314. for (; it2 != ite; ++it2, ++it1) *it2 += *it1;
  1315. }
  1316. template <typename L1, typename L2>
  1317. void add(const L1& l1, L2& l2, abstract_dense, abstract_skyline) {
  1318. typedef typename linalg_traits<L1>::const_iterator const_l1_iterator;
  1319. typedef typename linalg_traits<L2>::iterator l2_iterator;
  1320. typedef typename linalg_traits<L1>::value_type T;
  1321. const_l1_iterator it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1322. size_type i1 = 0, ie1 = vect_size(l1);
  1323. while (it1 != ite1 && *it1 == T(0)) { ++it1; ++i1; }
  1324. if (it1 != ite1) {
  1325. l2_iterator it2 = vect_begin(l2), ite2 = vect_end(l2);
  1326. while (ie1 && *(ite1-1) == T(0)) { ite1--; --ie1; }
  1327. if (it2 == ite2 || i1 < it2.index()) {
  1328. l2[i1] = *it1; ++i1; ++it1;
  1329. if (it1 == ite1) return;
  1330. it2 = vect_begin(l2); ite2 = vect_end(l2);
  1331. }
  1332. if (ie1 > ite2.index()) {
  1333. --ite1; l2[ie1 - 1] = *ite1;
  1334. it2 = vect_begin(l2);
  1335. }
  1336. it2 += i1 - it2.index();
  1337. for (; it1 != ite1; ++it1, ++it2) { *it2 += *it1; }
  1338. }
  1339. }
  1340. template <typename L1, typename L2>
  1341. void add(const L1& l1, L2& l2, abstract_skyline, abstract_dense) {
  1342. typename linalg_traits<L1>::const_iterator it1 = vect_const_begin(l1),
  1343. ite1 = vect_const_end(l1);
  1344. if (it1 != ite1) {
  1345. typename linalg_traits<L2>::iterator it2 = vect_begin(l2);
  1346. it2 += it1.index();
  1347. for (; it1 != ite1; ++it2, ++it1) *it2 += *it1;
  1348. }
  1349. }
  1350. template <typename L1, typename L2>
  1351. void add(const L1& l1, L2& l2, abstract_sparse, abstract_dense) {
  1352. typename linalg_traits<L1>::const_iterator
  1353. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1354. for (; it1 != ite1; ++it1) l2[it1.index()] += *it1;
  1355. }
  1356. template <typename L1, typename L2>
  1357. void add(const L1& l1, L2& l2, abstract_sparse, abstract_sparse) {
  1358. typename linalg_traits<L1>::const_iterator
  1359. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1360. for (; it1 != ite1; ++it1) l2[it1.index()] += *it1;
  1361. }
  1362. template <typename L1, typename L2>
  1363. void add(const L1& l1, L2& l2, abstract_sparse, abstract_skyline) {
  1364. typename linalg_traits<L1>::const_iterator
  1365. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1366. for (; it1 != ite1; ++it1) l2[it1.index()] += *it1;
  1367. }
  1368. template <typename L1, typename L2>
  1369. void add(const L1& l1, L2& l2, abstract_skyline, abstract_sparse) {
  1370. typename linalg_traits<L1>::const_iterator
  1371. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1372. for (; it1 != ite1; ++it1)
  1373. if (*it1 != typename linalg_traits<L1>::value_type(0))
  1374. l2[it1.index()] += *it1;
  1375. }
  1376. template <typename L1, typename L2>
  1377. void add(const L1& l1, L2& l2, abstract_skyline, abstract_skyline) {
  1378. typedef typename linalg_traits<L1>::const_iterator const_l1_iterator;
  1379. typedef typename linalg_traits<L2>::iterator l2_iterator;
  1380. typedef typename linalg_traits<L1>::value_type T1;
  1381. typedef typename linalg_traits<L2>::value_type T2;
  1382. const_l1_iterator it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1383. while (it1 != ite1 && *it1 == T1(0)) ++it1;
  1384. if (ite1 != it1) {
  1385. l2_iterator it2 = vect_begin(l2), ite2 = vect_end(l2);
  1386. while (*(ite1-1) == T1(0)) ite1--;
  1387. if (it2 == ite2 || it1.index() < it2.index()) {
  1388. l2[it1.index()] = T2(0);
  1389. it2 = vect_begin(l2); ite2 = vect_end(l2);
  1390. }
  1391. if (ite1.index() > ite2.index()) {
  1392. l2[ite1.index() - 1] = T2(0);
  1393. it2 = vect_begin(l2);
  1394. }
  1395. it2 += it1.index() - it2.index();
  1396. for (; it1 != ite1; ++it1, ++it2) *it2 += *it1;
  1397. }
  1398. }
  1399. template <typename L1, typename L2>
  1400. void add(const L1& l1, L2& l2, abstract_dense, abstract_sparse) {
  1401. typename linalg_traits<L1>::const_iterator
  1402. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1403. for (size_type i = 0; it1 != ite1; ++it1, ++i)
  1404. if (*it1 != typename linalg_traits<L1>::value_type(0)) l2[i] += *it1;
  1405. }
  1406. /* ******************************************************************** */
  1407. /* Matrix-vector mult */
  1408. /* ******************************************************************** */
  1409. ///@endcond
  1410. /** matrix-vector or matrix-matrix product.
  1411. @param l1 a matrix.
  1412. @param l2 a vector or matrix.
  1413. @param l3 the product l1*l2.
  1414. */
  1415. template <typename L1, typename L2, typename L3> inline
  1416. void mult(const L1& l1, const L2& l2, L3& l3) {
  1417. mult_dispatch(l1, l2, l3, typename linalg_traits<L2>::linalg_type());
  1418. }
  1419. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  1420. template <typename L1, typename L2, typename L3> inline
  1421. void mult(const L1& l1, const L2& l2, const L3& l3)
  1422. { mult(l1, l2, linalg_const_cast(l3)); }
  1423. template <typename L1, typename L2, typename L3> inline
  1424. void mult_dispatch(const L1& l1, const L2& l2, L3& l3, abstract_vector) {
  1425. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  1426. if (!m || !n) { gmm::clear(l3); return; }
  1427. GMM_ASSERT2(n==vect_size(l2) && m==vect_size(l3), "dimensions mismatch");
  1428. if (!same_origin(l2, l3))
  1429. mult_spec(l1, l2, l3, typename principal_orientation_type<typename
  1430. linalg_traits<L1>::sub_orientation>::potype());
  1431. else {
  1432. GMM_WARNING2("Warning, A temporary is used for mult\n");
  1433. typename temporary_vector<L3>::vector_type temp(vect_size(l3));
  1434. mult_spec(l1, l2, temp, typename principal_orientation_type<typename
  1435. linalg_traits<L1>::sub_orientation>::potype());
  1436. copy(temp, l3);
  1437. }
  1438. }
  1439. template <typename L1, typename L2, typename L3>
  1440. void mult_by_row(const L1& l1, const L2& l2, L3& l3, abstract_sparse) {
  1441. typedef typename linalg_traits<L3>::value_type T;
  1442. clear(l3);
  1443. size_type nr = mat_nrows(l1);
  1444. for (size_type i = 0; i < nr; ++i) {
  1445. T aux = vect_sp(mat_const_row(l1, i), l2);
  1446. if (aux != T(0)) l3[i] = aux;
  1447. }
  1448. }
  1449. template <typename L1, typename L2, typename L3>
  1450. void mult_by_row(const L1& l1, const L2& l2, L3& l3, abstract_skyline) {
  1451. typedef typename linalg_traits<L3>::value_type T;
  1452. clear(l3);
  1453. size_type nr = mat_nrows(l1);
  1454. for (size_type i = 0; i < nr; ++i) {
  1455. T aux = vect_sp(mat_const_row(l1, i), l2);
  1456. if (aux != T(0)) l3[i] = aux;
  1457. }
  1458. }
  1459. #ifdef STORM_HAVE_INTELTBB
  1460. /* Official Intel Hint on blocked_range vs. linear iterators: http://software.intel.com/en-us/forums/topic/289505
  1461. */
  1462. template <typename IT1, typename IT2>
  1463. class forward_range_mult {
  1464. IT1 my_begin;
  1465. IT1 my_end;
  1466. IT2 my_begin_row;
  1467. size_t my_size;
  1468. public:
  1469. IT1 begin() const {return my_begin;}
  1470. IT2 begin_row() const {return my_begin_row;}
  1471. IT1 end() const {return my_end;}
  1472. bool empty() const {return my_begin==my_end;}
  1473. bool is_divisible() const {return my_size>1;}
  1474. forward_range_mult( IT1 first, IT1 last, IT2 row_first, size_t size ) : my_begin(first), my_end(last), my_begin_row(row_first), my_size(size) {
  1475. assert( size==size_t(std::distance( first,last )));
  1476. }
  1477. forward_range_mult( IT1 first, IT1 last, IT2 row_first) : my_begin(first), my_end(last), my_begin_row(row_first) {
  1478. my_size = std::distance( first,last );
  1479. }
  1480. forward_range_mult( forward_range_mult& r, tbb::split ) {
  1481. size_t h = r.my_size/2;
  1482. my_end = r.my_end;
  1483. my_begin = r.my_begin;
  1484. my_begin_row = r.my_begin_row;
  1485. std::advance( my_begin, h ); // Might be scaling issue
  1486. std::advance( my_begin_row, h );
  1487. my_size = r.my_size-h;
  1488. r.my_end = my_begin;
  1489. r.my_size = h;
  1490. }
  1491. };
  1492. template <typename L1, typename L2, typename L3>
  1493. class tbbHelper_mult_by_row {
  1494. L2 const* my_l2;
  1495. // Typedefs for Iterator Types
  1496. typedef typename linalg_traits<L3>::iterator frm_IT1;
  1497. typedef typename linalg_traits<L1>::const_row_iterator frm_IT2;
  1498. public:
  1499. void operator()( const forward_range_mult<frm_IT1, frm_IT2>& r ) const {
  1500. L2 const& l2 = *my_l2;
  1501. frm_IT1 it = r.begin();
  1502. frm_IT1 ite = r.end();
  1503. frm_IT2 itr = r.begin_row();
  1504. for (; it != ite; ++it, ++itr) {
  1505. *it = vect_sp(linalg_traits<L1>::row(itr), l2,
  1506. typename linalg_traits<L1>::storage_type(),
  1507. typename linalg_traits<L2>::storage_type());
  1508. }
  1509. }
  1510. tbbHelper_mult_by_row(L2 const* l2) :
  1511. my_l2(l2)
  1512. {}
  1513. };
  1514. #endif
  1515. template <typename L1, typename L2, typename L3>
  1516. void mult_by_row(const L1& l1, const L2& l2, L3& l3, abstract_dense) {
  1517. typename linalg_traits<L3>::iterator it=vect_begin(l3), ite=vect_end(l3);
  1518. typename linalg_traits<L1>::const_row_iterator
  1519. itr = mat_row_const_begin(l1);
  1520. #ifdef STORM_HAVE_INTELTBB
  1521. tbb::parallel_for(forward_range_mult<typename linalg_traits<L3>::iterator, typename linalg_traits<L1>::const_row_iterator>(it, ite, itr), tbbHelper_mult_by_row<L1, L2, L3>(&l2));
  1522. #else
  1523. for (; it != ite; ++it, ++itr)
  1524. *it = vect_sp(linalg_traits<L1>::row(itr), l2,
  1525. typename linalg_traits<L1>::storage_type(),
  1526. typename linalg_traits<L2>::storage_type());
  1527. #endif
  1528. }
  1529. template <typename L1, typename L2, typename L3>
  1530. void mult_by_col(const L1& l1, const L2& l2, L3& l3, abstract_dense) {
  1531. clear(l3);
  1532. size_type nc = mat_ncols(l1);
  1533. for (size_type i = 0; i < nc; ++i)
  1534. add(scaled(mat_const_col(l1, i), l2[i]), l3);
  1535. }
  1536. template <typename L1, typename L2, typename L3>
  1537. void mult_by_col(const L1& l1, const L2& l2, L3& l3, abstract_sparse) {
  1538. typedef typename linalg_traits<L2>::value_type T;
  1539. clear(l3);
  1540. typename linalg_traits<L2>::const_iterator it = vect_const_begin(l2),
  1541. ite = vect_const_end(l2);
  1542. for (; it != ite; ++it)
  1543. if (*it != T(0)) add(scaled(mat_const_col(l1, it.index()), *it), l3);
  1544. }
  1545. template <typename L1, typename L2, typename L3>
  1546. void mult_by_col(const L1& l1, const L2& l2, L3& l3, abstract_skyline) {
  1547. typedef typename linalg_traits<L2>::value_type T;
  1548. clear(l3);
  1549. typename linalg_traits<L2>::const_iterator it = vect_const_begin(l2),
  1550. ite = vect_const_end(l2);
  1551. for (; it != ite; ++it)
  1552. if (*it != T(0)) add(scaled(mat_const_col(l1, it.index()), *it), l3);
  1553. }
  1554. template <typename L1, typename L2, typename L3> inline
  1555. void mult_spec(const L1& l1, const L2& l2, L3& l3, row_major)
  1556. { mult_by_row(l1, l2, l3, typename linalg_traits<L3>::storage_type()); }
  1557. template <typename L1, typename L2, typename L3> inline
  1558. void mult_spec(const L1& l1, const L2& l2, L3& l3, col_major)
  1559. { mult_by_col(l1, l2, l3, typename linalg_traits<L2>::storage_type()); }
  1560. template <typename L1, typename L2, typename L3> inline
  1561. void mult_spec(const L1& l1, const L2& l2, L3& l3, abstract_null_type)
  1562. { mult_ind(l1, l2, l3, typename linalg_traits<L1>::storage_type()); }
  1563. template <typename L1, typename L2, typename L3>
  1564. void mult_ind(const L1& l1, const L2& l2, L3& l3, abstract_indirect) {
  1565. GMM_ASSERT1(false, "gmm::mult(m, ., .) undefined for this kind of matrix");
  1566. }
  1567. template <typename L1, typename L2, typename L3, typename L4> inline
  1568. void mult(const L1& l1, const L2& l2, const L3& l3, L4& l4) {
  1569. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  1570. copy(l3, l4);
  1571. if (!m || !n) { gmm::copy(l3, l4); return; }
  1572. GMM_ASSERT2(n==vect_size(l2) && m==vect_size(l4), "dimensions mismatch");
  1573. if (!same_origin(l2, l4)) {
  1574. mult_add_spec(l1, l2, l4, typename principal_orientation_type<typename
  1575. linalg_traits<L1>::sub_orientation>::potype());
  1576. }
  1577. else {
  1578. GMM_WARNING2("Warning, A temporary is used for mult\n");
  1579. typename temporary_vector<L2>::vector_type temp(vect_size(l2));
  1580. copy(l2, temp);
  1581. mult_add_spec(l1,temp, l4, typename principal_orientation_type<typename
  1582. linalg_traits<L1>::sub_orientation>::potype());
  1583. }
  1584. }
  1585. template <typename L1, typename L2, typename L3, typename L4> inline
  1586. void mult(const L1& l1, const L2& l2, const L3& l3, const L4& l4)
  1587. { mult(l1, l2, l3, linalg_const_cast(l4)); }
  1588. ///@endcond
  1589. /** Multiply-accumulate. l3 += l1*l2; */
  1590. template <typename L1, typename L2, typename L3> inline
  1591. void mult_add(const L1& l1, const L2& l2, L3& l3) {
  1592. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  1593. if (!m || !n) return;
  1594. GMM_ASSERT2(n==vect_size(l2) && m==vect_size(l3), "dimensions mismatch");
  1595. if (!same_origin(l2, l3)) {
  1596. mult_add_spec(l1, l2, l3, typename principal_orientation_type<typename
  1597. linalg_traits<L1>::sub_orientation>::potype());
  1598. }
  1599. else {
  1600. GMM_WARNING2("Warning, A temporary is used for mult\n");
  1601. typename temporary_vector<L3>::vector_type temp(vect_size(l2));
  1602. copy(l2, temp);
  1603. mult_add_spec(l1,temp, l3, typename principal_orientation_type<typename
  1604. linalg_traits<L1>::sub_orientation>::potype());
  1605. }
  1606. }
  1607. /** Multiply-accumulate. l4 = l1*l2 + l3; */
  1608. template <typename L1, typename L2, typename L3, typename L4> inline
  1609. void mult_add(const L1& l1, const L2& l2, const L3& l3, L4& l4) {
  1610. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  1611. if (!m || !n) return;
  1612. GMM_ASSERT2(n==vect_size(l2) && m==vect_size(l3) && vect_size(l3) == vect_size(l4), "dimensions mismatch");
  1613. if (!same_origin(l2, l3)) {
  1614. mult_add_spec(l1, l2, l3, l4, typename principal_orientation_type<typename
  1615. linalg_traits<L1>::sub_orientation>::potype());
  1616. }
  1617. else {
  1618. GMM_WARNING2("Warning, A temporary is used for mult\n");
  1619. typename temporary_vector<L3>::vector_type temp(vect_size(l2));
  1620. copy(l2, temp);
  1621. mult_add_spec(l1, temp, l3, l4, typename principal_orientation_type<typename
  1622. linalg_traits<L1>::sub_orientation>::potype());
  1623. }
  1624. }
  1625. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  1626. template <typename L1, typename L2, typename L3> inline
  1627. void mult_add(const L1& l1, const L2& l2, const L3& l3)
  1628. { mult_add(l1, l2, linalg_const_cast(l3)); }
  1629. template <typename L1, typename L2, typename L3>
  1630. void mult_add_by_row(const L1& l1, const L2& l2, L3& l3, abstract_sparse) {
  1631. typedef typename linalg_traits<L3>::value_type T;
  1632. size_type nr = mat_nrows(l1);
  1633. for (size_type i = 0; i < nr; ++i) {
  1634. T aux = vect_sp(mat_const_row(l1, i), l2);
  1635. if (aux != T(0)) l3[i] += aux;
  1636. }
  1637. }
  1638. template <typename L1, typename L2, typename L3>
  1639. void mult_add_by_row(const L1& l1, const L2& l2, L3& l3, abstract_skyline) {
  1640. typedef typename linalg_traits<L3>::value_type T;
  1641. size_type nr = mat_nrows(l1);
  1642. for (size_type i = 0; i < nr; ++i) {
  1643. T aux = vect_sp(mat_const_row(l1, i), l2);
  1644. if (aux != T(0)) l3[i] += aux;
  1645. }
  1646. }
  1647. template <typename L1, typename L2, typename L3>
  1648. void mult_add_by_row(const L1& l1, const L2& l2, L3& l3, abstract_dense) {
  1649. typename linalg_traits<L3>::iterator it=vect_begin(l3), ite=vect_end(l3);
  1650. typename linalg_traits<L1>::const_row_iterator
  1651. itr = mat_row_const_begin(l1);
  1652. for (; it != ite; ++it, ++itr)
  1653. *it += vect_sp(linalg_traits<L1>::row(itr), l2);
  1654. }
  1655. template <typename L1, typename L2, typename L3, typename L4>
  1656. void mult_add_by_row(const L1& l1, const L2& l2, const L3& l3, L4& l4, abstract_dense) {
  1657. typename linalg_traits<L3>::const_iterator add_it=vect_begin(l3), add_ite=vect_end(l3);
  1658. typename linalg_traits<L4>::iterator target_it=vect_begin(l4), target_ite=vect_end(l4);
  1659. typename linalg_traits<L1>::const_row_iterator
  1660. itr = mat_row_const_begin(l1);
  1661. for (; add_it != add_ite; ++add_it, ++target_it, ++itr)
  1662. *target_it = vect_sp(linalg_traits<L1>::row(itr), l2) + *add_it;
  1663. }
  1664. template <typename L1, typename L2, typename L3>
  1665. void mult_add_by_col(const L1& l1, const L2& l2, L3& l3, abstract_dense) {
  1666. size_type nc = mat_ncols(l1);
  1667. for (size_type i = 0; i < nc; ++i)
  1668. add(scaled(mat_const_col(l1, i), l2[i]), l3);
  1669. }
  1670. template <typename L1, typename L2, typename L3>
  1671. void mult_add_by_col(const L1& l1, const L2& l2, L3& l3, abstract_sparse) {
  1672. typename linalg_traits<L2>::const_iterator it = vect_const_begin(l2),
  1673. ite = vect_const_end(l2);
  1674. for (; it != ite; ++it)
  1675. if (*it != typename linalg_traits<L2>::value_type(0))
  1676. add(scaled(mat_const_col(l1, it.index()), *it), l3);
  1677. }
  1678. template <typename L1, typename L2, typename L3>
  1679. void mult_add_by_col(const L1& l1, const L2& l2, L3& l3, abstract_skyline) {
  1680. typename linalg_traits<L2>::const_iterator it = vect_const_begin(l2),
  1681. ite = vect_const_end(l2);
  1682. for (; it != ite; ++it)
  1683. if (*it != typename linalg_traits<L2>::value_type(0))
  1684. add(scaled(mat_const_col(l1, it.index()), *it), l3);
  1685. }
  1686. template <typename L1, typename L2, typename L3> inline
  1687. void mult_add_spec(const L1& l1, const L2& l2, L3& l3, row_major)
  1688. { mult_add_by_row(l1, l2, l3, typename linalg_traits<L3>::storage_type()); }
  1689. template <typename L1, typename L2, typename L3, typename L4> inline
  1690. void mult_add_spec(const L1& l1, const L2& l2, const L3& l3, L4& l4, row_major)
  1691. { mult_add_by_row(l1, l2, l3, l4, typename linalg_traits<L3>::storage_type()); }
  1692. template <typename L1, typename L2, typename L3> inline
  1693. void mult_add_spec(const L1& l1, const L2& l2, L3& l3, col_major)
  1694. { mult_add_by_col(l1, l2, l3, typename linalg_traits<L2>::storage_type()); }
  1695. template <typename L1, typename L2, typename L3> inline
  1696. void mult_add_spec(const L1& l1, const L2& l2, L3& l3, abstract_null_type)
  1697. { mult_ind(l1, l2, l3, typename linalg_traits<L1>::storage_type()); }
  1698. template <typename L1, typename L2, typename L3>
  1699. void transposed_mult(const L1& l1, const L2& l2, const L3& l3)
  1700. { mult(gmm::transposed(l1), l2, l3); }
  1701. /* ******************************************************************** */
  1702. /* Matrix-matrix mult */
  1703. /* ******************************************************************** */
  1704. struct g_mult {}; // generic mult, less optimized
  1705. struct c_mult {}; // col x col -> col mult
  1706. struct r_mult {}; // row x row -> row mult
  1707. struct rcmult {}; // row x col -> col mult
  1708. struct crmult {}; // col x row -> row mult
  1709. template<typename SO1, typename SO2, typename SO3> struct mult_t;
  1710. #define DEFMU__ template<> struct mult_t
  1711. DEFMU__<row_major , row_major , row_major > { typedef r_mult t; };
  1712. DEFMU__<row_major , row_major , col_major > { typedef g_mult t; };
  1713. DEFMU__<row_major , row_major , col_and_row> { typedef r_mult t; };
  1714. DEFMU__<row_major , row_major , row_and_col> { typedef r_mult t; };
  1715. DEFMU__<row_major , col_major , row_major > { typedef rcmult t; };
  1716. DEFMU__<row_major , col_major , col_major > { typedef rcmult t; };
  1717. DEFMU__<row_major , col_major , col_and_row> { typedef rcmult t; };
  1718. DEFMU__<row_major , col_major , row_and_col> { typedef rcmult t; };
  1719. DEFMU__<row_major , col_and_row, row_major > { typedef r_mult t; };
  1720. DEFMU__<row_major , col_and_row, col_major > { typedef rcmult t; };
  1721. DEFMU__<row_major , col_and_row, col_and_row> { typedef rcmult t; };
  1722. DEFMU__<row_major , col_and_row, row_and_col> { typedef rcmult t; };
  1723. DEFMU__<row_major , row_and_col, row_major > { typedef r_mult t; };
  1724. DEFMU__<row_major , row_and_col, col_major > { typedef rcmult t; };
  1725. DEFMU__<row_major , row_and_col, col_and_row> { typedef r_mult t; };
  1726. DEFMU__<row_major , row_and_col, row_and_col> { typedef r_mult t; };
  1727. DEFMU__<col_major , row_major , row_major > { typedef crmult t; };
  1728. DEFMU__<col_major , row_major , col_major > { typedef g_mult t; };
  1729. DEFMU__<col_major , row_major , col_and_row> { typedef crmult t; };
  1730. DEFMU__<col_major , row_major , row_and_col> { typedef crmult t; };
  1731. DEFMU__<col_major , col_major , row_major > { typedef g_mult t; };
  1732. DEFMU__<col_major , col_major , col_major > { typedef c_mult t; };
  1733. DEFMU__<col_major , col_major , col_and_row> { typedef c_mult t; };
  1734. DEFMU__<col_major , col_major , row_and_col> { typedef c_mult t; };
  1735. DEFMU__<col_major , col_and_row, row_major > { typedef crmult t; };
  1736. DEFMU__<col_major , col_and_row, col_major > { typedef c_mult t; };
  1737. DEFMU__<col_major , col_and_row, col_and_row> { typedef c_mult t; };
  1738. DEFMU__<col_major , col_and_row, row_and_col> { typedef c_mult t; };
  1739. DEFMU__<col_major , row_and_col, row_major > { typedef crmult t; };
  1740. DEFMU__<col_major , row_and_col, col_major > { typedef c_mult t; };
  1741. DEFMU__<col_major , row_and_col, col_and_row> { typedef c_mult t; };
  1742. DEFMU__<col_major , row_and_col, row_and_col> { typedef c_mult t; };
  1743. DEFMU__<col_and_row, row_major , row_major > { typedef r_mult t; };
  1744. DEFMU__<col_and_row, row_major , col_major > { typedef c_mult t; };
  1745. DEFMU__<col_and_row, row_major , col_and_row> { typedef r_mult t; };
  1746. DEFMU__<col_and_row, row_major , row_and_col> { typedef r_mult t; };
  1747. DEFMU__<col_and_row, col_major , row_major > { typedef rcmult t; };
  1748. DEFMU__<col_and_row, col_major , col_major > { typedef c_mult t; };
  1749. DEFMU__<col_and_row, col_major , col_and_row> { typedef c_mult t; };
  1750. DEFMU__<col_and_row, col_major , row_and_col> { typedef c_mult t; };
  1751. DEFMU__<col_and_row, col_and_row, row_major > { typedef r_mult t; };
  1752. DEFMU__<col_and_row, col_and_row, col_major > { typedef c_mult t; };
  1753. DEFMU__<col_and_row, col_and_row, col_and_row> { typedef c_mult t; };
  1754. DEFMU__<col_and_row, col_and_row, row_and_col> { typedef c_mult t; };
  1755. DEFMU__<col_and_row, row_and_col, row_major > { typedef r_mult t; };
  1756. DEFMU__<col_and_row, row_and_col, col_major > { typedef c_mult t; };
  1757. DEFMU__<col_and_row, row_and_col, col_and_row> { typedef c_mult t; };
  1758. DEFMU__<col_and_row, row_and_col, row_and_col> { typedef r_mult t; };
  1759. DEFMU__<row_and_col, row_major , row_major > { typedef r_mult t; };
  1760. DEFMU__<row_and_col, row_major , col_major > { typedef c_mult t; };
  1761. DEFMU__<row_and_col, row_major , col_and_row> { typedef r_mult t; };
  1762. DEFMU__<row_and_col, row_major , row_and_col> { typedef r_mult t; };
  1763. DEFMU__<row_and_col, col_major , row_major > { typedef rcmult t; };
  1764. DEFMU__<row_and_col, col_major , col_major > { typedef c_mult t; };
  1765. DEFMU__<row_and_col, col_major , col_and_row> { typedef c_mult t; };
  1766. DEFMU__<row_and_col, col_major , row_and_col> { typedef c_mult t; };
  1767. DEFMU__<row_and_col, col_and_row, row_major > { typedef rcmult t; };
  1768. DEFMU__<row_and_col, col_and_row, col_major > { typedef rcmult t; };
  1769. DEFMU__<row_and_col, col_and_row, col_and_row> { typedef rcmult t; };
  1770. DEFMU__<row_and_col, col_and_row, row_and_col> { typedef rcmult t; };
  1771. DEFMU__<row_and_col, row_and_col, row_major > { typedef r_mult t; };
  1772. DEFMU__<row_and_col, row_and_col, col_major > { typedef c_mult t; };
  1773. DEFMU__<row_and_col, row_and_col, col_and_row> { typedef r_mult t; };
  1774. DEFMU__<row_and_col, row_and_col, row_and_col> { typedef r_mult t; };
  1775. template <typename L1, typename L2, typename L3>
  1776. void mult_dispatch(const L1& l1, const L2& l2, L3& l3, abstract_matrix) {
  1777. typedef typename temporary_matrix<L3>::matrix_type temp_mat_type;
  1778. size_type n = mat_ncols(l1);
  1779. if (n == 0) { gmm::clear(l3); return; }
  1780. GMM_ASSERT2(n == mat_nrows(l2) && mat_nrows(l1) == mat_nrows(l3) &&
  1781. mat_ncols(l2) == mat_ncols(l3), "dimensions mismatch");
  1782. if (same_origin(l2, l3) || same_origin(l1, l3)) {
  1783. GMM_WARNING2("A temporary is used for mult");
  1784. temp_mat_type temp(mat_nrows(l3), mat_ncols(l3));
  1785. mult_spec(l1, l2, temp, typename mult_t<
  1786. typename linalg_traits<L1>::sub_orientation,
  1787. typename linalg_traits<L2>::sub_orientation,
  1788. typename linalg_traits<temp_mat_type>::sub_orientation>::t());
  1789. copy(temp, l3);
  1790. }
  1791. else
  1792. mult_spec(l1, l2, l3, typename mult_t<
  1793. typename linalg_traits<L1>::sub_orientation,
  1794. typename linalg_traits<L2>::sub_orientation,
  1795. typename linalg_traits<L3>::sub_orientation>::t());
  1796. }
  1797. // Completely generic but inefficient
  1798. template <typename L1, typename L2, typename L3>
  1799. void mult_spec(const L1& l1, const L2& l2, L3& l3, g_mult) {
  1800. typedef typename linalg_traits<L3>::value_type T;
  1801. GMM_WARNING2("Inefficient generic matrix-matrix mult is used");
  1802. for (size_type i = 0; i < mat_nrows(l3) ; ++i)
  1803. for (size_type j = 0; j < mat_ncols(l3) ; ++j) {
  1804. T a(0);
  1805. for (size_type k = 0; k < mat_nrows(l2) ; ++k) a += l1(i, k)*l2(k, j);
  1806. l3(i, j) = a;
  1807. }
  1808. }
  1809. // row x col matrix-matrix mult
  1810. template <typename L1, typename L2, typename L3>
  1811. void mult_row_col_with_temp(const L1& l1, const L2& l2, L3& l3, col_major) {
  1812. typedef typename temporary_col_matrix<L1>::matrix_type temp_col_mat;
  1813. temp_col_mat temp(mat_nrows(l1), mat_ncols(l1));
  1814. copy(l1, temp);
  1815. mult(temp, l2, l3);
  1816. }
  1817. template <typename L1, typename L2, typename L3>
  1818. void mult_row_col_with_temp(const L1& l1, const L2& l2, L3& l3, row_major) {
  1819. typedef typename temporary_row_matrix<L2>::matrix_type temp_row_mat;
  1820. temp_row_mat temp(mat_nrows(l2), mat_ncols(l2));
  1821. copy(l2, temp);
  1822. mult(l1, temp, l3);
  1823. }
  1824. template <typename L1, typename L2, typename L3>
  1825. void mult_spec(const L1& l1, const L2& l2, L3& l3, rcmult) {
  1826. if (is_sparse(l1) && is_sparse(l2)) {
  1827. GMM_WARNING3("Inefficient row matrix - col matrix mult for "
  1828. "sparse matrices, using temporary");
  1829. mult_row_col_with_temp(l1, l2, l3,
  1830. typename principal_orientation_type<typename
  1831. linalg_traits<L3>::sub_orientation>::potype());
  1832. }
  1833. else {
  1834. typename linalg_traits<L2>::const_col_iterator
  1835. it2b = linalg_traits<L2>::col_begin(l2), it2,
  1836. ite = linalg_traits<L2>::col_end(l2);
  1837. size_type i,j, k = mat_nrows(l1);
  1838. for (i = 0; i < k; ++i) {
  1839. typename linalg_traits<L1>::const_sub_row_type r1=mat_const_row(l1, i);
  1840. for (it2 = it2b, j = 0; it2 != ite; ++it2, ++j)
  1841. l3(i,j) = vect_sp(r1, linalg_traits<L2>::col(it2));
  1842. }
  1843. }
  1844. }
  1845. // row - row matrix-matrix mult
  1846. template <typename L1, typename L2, typename L3> inline
  1847. void mult_spec(const L1& l1, const L2& l2, L3& l3, r_mult) {
  1848. mult_spec(l1, l2, l3,r_mult(),typename linalg_traits<L1>::storage_type());
  1849. }
  1850. template <typename L1, typename L2, typename L3>
  1851. void mult_spec(const L1& l1, const L2& l2, L3& l3, r_mult, abstract_dense) {
  1852. // optimizable
  1853. clear(l3);
  1854. size_type nn = mat_nrows(l3), mm = mat_nrows(l2);
  1855. for (size_type i = 0; i < nn; ++i) {
  1856. for (size_type j = 0; j < mm; ++j) {
  1857. add(scaled(mat_const_row(l2, j), l1(i, j)), mat_row(l3, i));
  1858. }
  1859. }
  1860. }
  1861. template <typename L1, typename L2, typename L3>
  1862. void mult_spec(const L1& l1, const L2& l2, L3& l3, r_mult, abstract_sparse) {
  1863. // optimizable
  1864. clear(l3);
  1865. size_type nn = mat_nrows(l3);
  1866. for (size_type i = 0; i < nn; ++i) {
  1867. typename linalg_traits<L1>::const_sub_row_type rl1=mat_const_row(l1, i);
  1868. typename linalg_traits<typename linalg_traits<L1>::const_sub_row_type>::
  1869. const_iterator it = vect_const_begin(rl1), ite = vect_const_end(rl1);
  1870. for (; it != ite; ++it)
  1871. add(scaled(mat_const_row(l2, it.index()), *it), mat_row(l3, i));
  1872. }
  1873. }
  1874. template <typename L1, typename L2, typename L3>
  1875. void mult_spec(const L1& l1, const L2& l2, L3& l3, r_mult, abstract_skyline)
  1876. { mult_spec(l1, l2, l3, r_mult(), abstract_sparse()); }
  1877. // col - col matrix-matrix mult
  1878. template <typename L1, typename L2, typename L3> inline
  1879. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult) {
  1880. mult_spec(l1, l2,l3,c_mult(),typename linalg_traits<L2>::storage_type(),
  1881. typename linalg_traits<L2>::sub_orientation());
  1882. }
  1883. template <typename L1, typename L2, typename L3, typename ORIEN>
  1884. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult,
  1885. abstract_dense, ORIEN) {
  1886. typedef typename linalg_traits<L2>::value_type T;
  1887. size_type nn = mat_ncols(l3), mm = mat_ncols(l1);
  1888. for (size_type i = 0; i < nn; ++i) {
  1889. clear(mat_col(l3, i));
  1890. for (size_type j = 0; j < mm; ++j) {
  1891. T b = l2(j, i);
  1892. if (b != T(0)) add(scaled(mat_const_col(l1, j), b), mat_col(l3, i));
  1893. }
  1894. }
  1895. }
  1896. template <typename L1, typename L2, typename L3, typename ORIEN>
  1897. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult,
  1898. abstract_sparse, ORIEN) {
  1899. // optimizable
  1900. clear(l3);
  1901. size_type nn = mat_ncols(l3);
  1902. for (size_type i = 0; i < nn; ++i) {
  1903. typename linalg_traits<L2>::const_sub_col_type rc2=mat_const_col(l2, i);
  1904. typename linalg_traits<typename linalg_traits<L2>::const_sub_col_type>::
  1905. const_iterator it = vect_const_begin(rc2), ite = vect_const_end(rc2);
  1906. for (; it != ite; ++it)
  1907. add(scaled(mat_const_col(l1, it.index()), *it), mat_col(l3, i));
  1908. }
  1909. }
  1910. template <typename L1, typename L2, typename L3>
  1911. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult,
  1912. abstract_sparse, row_major) {
  1913. typedef typename linalg_traits<L2>::value_type T;
  1914. GMM_WARNING3("Inefficient matrix-matrix mult for sparse matrices");
  1915. clear(l3);
  1916. size_type mm = mat_nrows(l2), nn = mat_ncols(l3);
  1917. for (size_type i = 0; i < nn; ++i)
  1918. for (size_type j = 0; j < mm; ++j) {
  1919. T a = l2(i,j);
  1920. if (a != T(0)) add(scaled(mat_const_col(l1, j), a), mat_col(l3, i));
  1921. }
  1922. }
  1923. template <typename L1, typename L2, typename L3, typename ORIEN> inline
  1924. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult,
  1925. abstract_skyline, ORIEN)
  1926. { mult_spec(l1, l2, l3, c_mult(), abstract_sparse(), ORIEN()); }
  1927. // col - row matrix-matrix mult
  1928. template <typename L1, typename L2, typename L3> inline
  1929. void mult_spec(const L1& l1, const L2& l2, L3& l3, crmult)
  1930. { mult_spec(l1,l2,l3,crmult(), typename linalg_traits<L1>::storage_type()); }
  1931. template <typename L1, typename L2, typename L3>
  1932. void mult_spec(const L1& l1, const L2& l2, L3& l3, crmult, abstract_dense) {
  1933. // optimizable
  1934. clear(l3);
  1935. size_type nn = mat_ncols(l1), mm = mat_nrows(l1);
  1936. for (size_type i = 0; i < nn; ++i) {
  1937. for (size_type j = 0; j < mm; ++j)
  1938. add(scaled(mat_const_row(l2, i), l1(j, i)), mat_row(l3, j));
  1939. }
  1940. }
  1941. template <typename L1, typename L2, typename L3>
  1942. void mult_spec(const L1& l1, const L2& l2, L3& l3, crmult, abstract_sparse) {
  1943. // optimizable
  1944. clear(l3);
  1945. size_type nn = mat_ncols(l1);
  1946. for (size_type i = 0; i < nn; ++i) {
  1947. typename linalg_traits<L1>::const_sub_col_type rc1=mat_const_col(l1, i);
  1948. typename linalg_traits<typename linalg_traits<L1>::const_sub_col_type>::
  1949. const_iterator it = vect_const_begin(rc1), ite = vect_const_end(rc1);
  1950. for (; it != ite; ++it)
  1951. add(scaled(mat_const_row(l2, i), *it), mat_row(l3, it.index()));
  1952. }
  1953. }
  1954. template <typename L1, typename L2, typename L3> inline
  1955. void mult_spec(const L1& l1, const L2& l2, L3& l3, crmult, abstract_skyline)
  1956. { mult_spec(l1, l2, l3, crmult(), abstract_sparse()); }
  1957. /* ******************************************************************** */
  1958. /* Symmetry test. */
  1959. /* ******************************************************************** */
  1960. ///@endcond
  1961. /** test if A is symmetric.
  1962. @param A a matrix.
  1963. @param tol a threshold.
  1964. */
  1965. template <typename MAT> inline
  1966. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol
  1967. = magnitude_of_linalg(MAT)(-1)) {
  1968. typedef magnitude_of_linalg(MAT) R;
  1969. if (tol < R(0)) tol = default_tol(R()) * mat_maxnorm(A);
  1970. if (mat_nrows(A) != mat_ncols(A)) return false;
  1971. return is_symmetric(A, tol, typename linalg_traits<MAT>::storage_type());
  1972. }
  1973. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  1974. template <typename MAT>
  1975. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  1976. abstract_dense) {
  1977. size_type m = mat_nrows(A);
  1978. for (size_type i = 1; i < m; ++i)
  1979. for (size_type j = 0; j < i; ++j)
  1980. if (gmm::abs(A(i, j)-A(j, i)) > tol) return false;
  1981. return true;
  1982. }
  1983. template <typename MAT>
  1984. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  1985. abstract_sparse) {
  1986. return is_symmetric(A, tol, typename principal_orientation_type<typename
  1987. linalg_traits<MAT>::sub_orientation>::potype());
  1988. }
  1989. template <typename MAT>
  1990. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  1991. row_major) {
  1992. for (size_type i = 0; i < mat_nrows(A); ++i) {
  1993. typedef typename linalg_traits<MAT>::const_sub_row_type row_type;
  1994. row_type row = mat_const_row(A, i);
  1995. typename linalg_traits<row_type>::const_iterator
  1996. it = vect_const_begin(row), ite = vect_const_end(row);
  1997. for (; it != ite; ++it)
  1998. if (gmm::abs(*it - A(it.index(), i)) > tol) return false;
  1999. }
  2000. return true;
  2001. }
  2002. template <typename MAT>
  2003. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  2004. col_major) {
  2005. for (size_type i = 0; i < mat_ncols(A); ++i) {
  2006. typedef typename linalg_traits<MAT>::const_sub_col_type col_type;
  2007. col_type col = mat_const_col(A, i);
  2008. typename linalg_traits<col_type>::const_iterator
  2009. it = vect_const_begin(col), ite = vect_const_end(col);
  2010. for (; it != ite; ++it)
  2011. if (gmm::abs(*it - A(i, it.index())) > tol) return false;
  2012. }
  2013. return true;
  2014. }
  2015. template <typename MAT>
  2016. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  2017. abstract_skyline)
  2018. { return is_symmetric(A, tol, abstract_sparse()); }
  2019. ///@endcond
  2020. /** test if A is Hermitian.
  2021. @param A a matrix.
  2022. @param tol a threshold.
  2023. */
  2024. template <typename MAT> inline
  2025. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol
  2026. = magnitude_of_linalg(MAT)(-1)) {
  2027. typedef magnitude_of_linalg(MAT) R;
  2028. if (tol < R(0)) tol = default_tol(R()) * mat_maxnorm(A);
  2029. if (mat_nrows(A) != mat_ncols(A)) return false;
  2030. return is_hermitian(A, tol, typename linalg_traits<MAT>::storage_type());
  2031. }
  2032. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  2033. template <typename MAT>
  2034. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2035. abstract_dense) {
  2036. size_type m = mat_nrows(A);
  2037. for (size_type i = 1; i < m; ++i)
  2038. for (size_type j = 0; j < i; ++j)
  2039. if (gmm::abs(A(i, j)-gmm::conj(A(j, i))) > tol) return false;
  2040. return true;
  2041. }
  2042. template <typename MAT>
  2043. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2044. abstract_sparse) {
  2045. return is_hermitian(A, tol, typename principal_orientation_type<typename
  2046. linalg_traits<MAT>::sub_orientation>::potype());
  2047. }
  2048. template <typename MAT>
  2049. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2050. row_major) {
  2051. for (size_type i = 0; i < mat_nrows(A); ++i) {
  2052. typedef typename linalg_traits<MAT>::const_sub_row_type row_type;
  2053. row_type row = mat_const_row(A, i);
  2054. typename linalg_traits<row_type>::const_iterator
  2055. it = vect_const_begin(row), ite = vect_const_end(row);
  2056. for (; it != ite; ++it)
  2057. if (gmm::abs(gmm::conj(*it) - A(it.index(), i)) > tol) return false;
  2058. }
  2059. return true;
  2060. }
  2061. template <typename MAT>
  2062. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2063. col_major) {
  2064. for (size_type i = 0; i < mat_ncols(A); ++i) {
  2065. typedef typename linalg_traits<MAT>::const_sub_col_type col_type;
  2066. col_type col = mat_const_col(A, i);
  2067. typename linalg_traits<col_type>::const_iterator
  2068. it = vect_const_begin(col), ite = vect_const_end(col);
  2069. for (; it != ite; ++it)
  2070. if (gmm::abs(gmm::conj(*it) - A(i, it.index())) > tol) return false;
  2071. }
  2072. return true;
  2073. }
  2074. template <typename MAT>
  2075. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2076. abstract_skyline)
  2077. { return is_hermitian(A, tol, abstract_sparse()); }
  2078. ///@endcond
  2079. }
  2080. #endif // GMM_BLAS_H__