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.

2365 lines
90 KiB

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