GetFEM  5.6
gmm_matrix.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-2026 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /** @file gmm_matrix.h
32  @author Yves Renard <Yves.Renard@insa-lyon.fr>
33  @date October 13, 2002.
34  @brief Declaration of some matrix types (gmm::dense_matrix,
35  gmm::row_matrix, gmm::col_matrix, gmm::csc_matrix, etc.)
36 */
37 
38 #ifndef GMM_MATRIX_H__
39 #define GMM_MATRIX_H__
40 
41 #include "gmm_vector.h"
42 #include "gmm_sub_vector.h"
43 #include "gmm_sub_matrix.h"
44 #include "gmm_transposed.h"
45 
46 namespace gmm
47 {
48 
49  /* ******************************************************************** */
50  /* */
51  /* Identity matrix */
52  /* */
53  /* ******************************************************************** */
54 
55  struct identity_matrix {
56  template <class MAT> void build_with(const MAT &) {}
57  };
58 
59  template <typename M> inline
60  void add(const identity_matrix&, M &v1) {
61  size_type n = std::min(gmm::mat_nrows(v1), gmm::mat_ncols(v1));
62  for (size_type i = 0; i < n; ++i)
63  v1(i,i) += typename linalg_traits<M>::value_type(1);
64  }
65  template <typename M> inline
66  void add(const identity_matrix &II, const M &v1)
67  { add(II, linalg_const_cast(v1)); }
68 
69  template <typename V1, typename V2> inline
70  void mult(const identity_matrix&, const V1 &v1, V2 &v2)
71  { copy(v1, v2); }
72  template <typename V1, typename V2> inline
73  void mult(const identity_matrix&, const V1 &v1, const V2 &v2)
74  { copy(v1, v2); }
75  template <typename V1, typename V2, typename V3> inline
76  void mult(const identity_matrix&, const V1 &v1, const V2 &v2, V3 &v3)
77  { add(v1, v2, v3); }
78  template <typename V1, typename V2, typename V3> inline
79  void mult(const identity_matrix&, const V1 &v1, const V2 &v2, const V3 &v3)
80  { add(v1, v2, v3); }
81  template <typename V1, typename V2> inline
82  void left_mult(const identity_matrix&, const V1 &v1, V2 &v2)
83  { copy(v1, v2); }
84  template <typename V1, typename V2> inline
85  void left_mult(const identity_matrix&, const V1 &v1, const V2 &v2)
86  { copy(v1, v2); }
87  template <typename V1, typename V2> inline
88  void right_mult(const identity_matrix&, const V1 &v1, V2 &v2)
89  { copy(v1, v2); }
90  template <typename V1, typename V2> inline
91  void right_mult(const identity_matrix&, const V1 &v1, const V2 &v2)
92  { copy(v1, v2); }
93  template <typename V1, typename V2> inline
94  void transposed_left_mult(const identity_matrix&, const V1 &v1, V2 &v2)
95  { copy(v1, v2); }
96  template <typename V1, typename V2> inline
97  void transposed_left_mult(const identity_matrix&, const V1 &v1,const V2 &v2)
98  { copy(v1, v2); }
99  template <typename V1, typename V2> inline
100  void transposed_right_mult(const identity_matrix&, const V1 &v1, V2 &v2)
101  { copy(v1, v2); }
102  template <typename V1, typename V2> inline
103  void transposed_right_mult(const identity_matrix&,const V1 &v1,const V2 &v2)
104  { copy(v1, v2); }
105  template <typename M> void copy_ident(const identity_matrix&, M &m) {
106  size_type i = 0, n = std::min(mat_nrows(m), mat_ncols(m));
107  clear(m);
108  for (; i < n; ++i) m(i,i) = typename linalg_traits<M>::value_type(1);
109  }
110  template <typename M> inline void copy(const identity_matrix&, M &m)
111  { copy_ident(identity_matrix(), m); }
112  template <typename M> inline void copy(const identity_matrix &, const M &m)
113  { copy_ident(identity_matrix(), linalg_const_cast(m)); }
114  template <typename V1, typename V2> inline
115  typename linalg_traits<V1>::value_type
116  vect_sp(const identity_matrix &, const V1 &v1, const V2 &v2)
117  { return vect_sp(v1, v2); }
118  template <typename V1, typename V2> inline
119  typename linalg_traits<V1>::value_type
120  vect_hp(const identity_matrix &, const V1 &v1, const V2 &v2)
121  { return vect_hp(v1, v2); }
122  template<typename M> inline bool is_identity(const M&) { return false; }
123  inline bool is_identity(const identity_matrix&) { return true; }
124 
125  /* ******************************************************************** */
126  /* */
127  /* Row matrix */
128  /* */
129  /* ******************************************************************** */
130 
131  template<typename V> class row_matrix {
132  protected :
133  std::vector<V> li; /* array of rows. */
134  size_type nc;
135 
136  public :
137 
138  typedef typename linalg_traits<V>::reference reference;
139  typedef typename linalg_traits<V>::value_type value_type;
140 
141  row_matrix(size_type r, size_type c) : li(r, V(c)), nc(c) {}
142  row_matrix() : nc(0) {}
143  reference operator ()(size_type l, size_type c)
144  { return li[l][c]; }
145  value_type operator ()(size_type l, size_type c) const
146  { return li[l][c]; }
147 
148  void clear_mat();
149  void resize(size_type m, size_type n);
150 
151  typename std::vector<V>::iterator begin()
152  { return li.begin(); }
153  typename std::vector<V>::iterator end()
154  { return li.end(); }
155  typename std::vector<V>::const_iterator begin() const
156  { return li.begin(); }
157  typename std::vector<V>::const_iterator end() const
158  { return li.end(); }
159 
160 
161  V& row(size_type i) { return li[i]; }
162  const V& row(size_type i) const { return li[i]; }
163  V& operator[](size_type i) { return li[i]; }
164  const V& operator[](size_type i) const { return li[i]; }
165 
166  inline size_type nrows() const { return li.size(); }
167  inline size_type ncols() const { return nc; }
168 
169  void swap(row_matrix<V> &m) { std::swap(li, m.li); std::swap(nc, m.nc); }
170  void swap_row(size_type i, size_type j) { std::swap(li[i], li[j]); }
171  };
172 
173  template<typename V>
174  void row_matrix<V>::resize(size_type m, size_type n) {
175  size_type nr = std::min(nrows(), m);
176  li.resize(m);
177  for (size_type i=nr; i < m; ++i) gmm::resize(li[i], n);
178  if (n != nc) {
179  for (size_type i=0; i < nr; ++i) gmm::resize(li[i], n);
180  nc = n;
181  }
182  }
183 
184 
185  template<typename V>
186  void row_matrix<V>::clear_mat()
187  { for (size_type i=0; i < nrows(); ++i) clear(li[i]); }
188 
189  template <typename V>
190  struct linalg_traits<row_matrix<V> > {
191  typedef row_matrix<V> this_type;
192  typedef this_type origin_type;
193  typedef linalg_false is_reference;
194  typedef abstract_matrix linalg_type;
195  typedef typename linalg_traits<V>::value_type value_type;
196  typedef typename linalg_traits<V>::reference reference;
197  typedef typename linalg_traits<V>::storage_type storage_type;
198  typedef V & sub_row_type;
199  typedef const V & const_sub_row_type;
200  typedef typename std::vector<V>::iterator row_iterator;
201  typedef typename std::vector<V>::const_iterator const_row_iterator;
202  typedef abstract_null_type sub_col_type;
203  typedef abstract_null_type const_sub_col_type;
204  typedef abstract_null_type col_iterator;
205  typedef abstract_null_type const_col_iterator;
206  typedef row_major sub_orientation;
207  typedef linalg_true index_sorted;
208  static size_type nrows(const this_type &m) { return m.nrows(); }
209  static size_type ncols(const this_type &m) { return m.ncols(); }
210  static row_iterator row_begin(this_type &m) { return m.begin(); }
211  static row_iterator row_end(this_type &m) { return m.end(); }
212  static const_row_iterator row_begin(const this_type &m)
213  { return m.begin(); }
214  static const_row_iterator row_end(const this_type &m)
215  { return m.end(); }
216  static const_sub_row_type row(const const_row_iterator &it)
217  { return const_sub_row_type(*it); }
218  static sub_row_type row(const row_iterator &it)
219  { return sub_row_type(*it); }
220  static origin_type* origin(this_type &m) { return &m; }
221  static const origin_type* origin(const this_type &m) { return &m; }
222  static void do_clear(this_type &m) { m.clear_mat(); }
223  static value_type access(const const_row_iterator &itrow, size_type j)
224  { return (*itrow)[j]; }
225  static reference access(const row_iterator &itrow, size_type j)
226  { return (*itrow)[j]; }
227  static void resize(this_type &v, size_type m, size_type n)
228  { v.resize(m, n); }
229  static void reshape(this_type &, size_type, size_type)
230  { GMM_ASSERT1(false, "Sorry, to be done"); }
231  };
232 
233  template<typename V> std::ostream &operator <<
234  (std::ostream &o, const row_matrix<V>& m) { gmm::write(o,m); return o; }
235 
236  /* ******************************************************************** */
237  /* */
238  /* Column matrix */
239  /* */
240  /* ******************************************************************** */
241 
242  template<typename V> class col_matrix {
243  protected :
244  std::vector<V> li; /* array of columns. */
245  size_type nr;
246 
247  public :
248 
249  typedef typename linalg_traits<V>::reference reference;
250  typedef typename linalg_traits<V>::value_type value_type;
251 
252  col_matrix(size_type r, size_type c) : li(c, V(r)), nr(r) { }
253  col_matrix() : nr(0) {}
254  reference operator ()(size_type l, size_type c)
255  { return li[c][l]; }
256  value_type operator ()(size_type l, size_type c) const
257  { return li[c][l]; }
258 
259  void clear_mat();
260  void resize(size_type, size_type);
261 
262  V& col(size_type i) { return li[i]; }
263  const V& col(size_type i) const { return li[i]; }
264  V& operator[](size_type i) { return li[i]; }
265  const V& operator[](size_type i) const { return li[i]; }
266 
267  typename std::vector<V>::iterator begin()
268  { return li.begin(); }
269  typename std::vector<V>::iterator end()
270  { return li.end(); }
271  typename std::vector<V>::const_iterator begin() const
272  { return li.begin(); }
273  typename std::vector<V>::const_iterator end() const
274  { return li.end(); }
275 
276  inline size_type ncols() const { return li.size(); }
277  inline size_type nrows() const { return nr; }
278 
279  void swap(col_matrix<V> &m) { std::swap(li, m.li); std::swap(nr, m.nr); }
280  void swap_col(size_type i, size_type j) { std::swap(li[i], li[j]); }
281  };
282 
283  template<typename V> void col_matrix<V>::resize(size_type m, size_type n) {
284  size_type nc = std::min(ncols(), n);
285  li.resize(n);
286  for (size_type i=nc; i < n; ++i) gmm::resize(li[i], m);
287  if (m != nr) {
288  for (size_type i=0; i < nc; ++i) gmm::resize(li[i], m);
289  nr = m;
290  }
291  }
292 
293  template<typename V> void col_matrix<V>::clear_mat()
294  { for (size_type i=0; i < ncols(); ++i) clear(li[i]); }
295 
296  template <typename V> struct linalg_traits<col_matrix<V> > {
297  typedef col_matrix<V> this_type;
298  typedef this_type origin_type;
299  typedef linalg_false is_reference;
300  typedef abstract_matrix linalg_type;
301  typedef typename linalg_traits<V>::value_type value_type;
302  typedef typename linalg_traits<V>::reference reference;
303  typedef typename linalg_traits<V>::storage_type storage_type;
304  typedef V &sub_col_type;
305  typedef const V &const_sub_col_type;
306  typedef typename std::vector<V>::iterator col_iterator;
307  typedef typename std::vector<V>::const_iterator const_col_iterator;
308  typedef abstract_null_type sub_row_type;
309  typedef abstract_null_type const_sub_row_type;
310  typedef abstract_null_type row_iterator;
311  typedef abstract_null_type const_row_iterator;
312  typedef col_major sub_orientation;
313  typedef linalg_true index_sorted;
314  static size_type nrows(const this_type &m) { return m.nrows(); }
315  static size_type ncols(const this_type &m) { return m.ncols(); }
316  static col_iterator col_begin(this_type &m) { return m.begin(); }
317  static col_iterator col_end(this_type &m) { return m.end(); }
318  static const_col_iterator col_begin(const this_type &m)
319  { return m.begin(); }
320  static const_col_iterator col_end(const this_type &m)
321  { return m.end(); }
322  static const_sub_col_type col(const const_col_iterator &it)
323  { return *it; }
324  static sub_col_type col(const col_iterator &it)
325  { return *it; }
326  static origin_type* origin(this_type &m) { return &m; }
327  static const origin_type* origin(const this_type &m) { return &m; }
328  static void do_clear(this_type &m) { m.clear_mat(); }
329  static value_type access(const const_col_iterator &itcol, size_type j)
330  { return (*itcol)[j]; }
331  static reference access(const col_iterator &itcol, size_type j)
332  { return (*itcol)[j]; }
333  static void resize(this_type &v, size_type m, size_type n)
334  { v.resize(m,n); }
335  static void reshape(this_type &, size_type, size_type)
336  { GMM_ASSERT1(false, "Sorry, to be done"); }
337  };
338 
339  template<typename V> std::ostream &operator <<
340  (std::ostream &o, const col_matrix<V>& m) { gmm::write(o,m); return o; }
341 
342  /* ******************************************************************** */
343  /* */
344  /* Dense matrix */
345  /* */
346  /* ******************************************************************** */
347 
348  template<typename T> class dense_matrix : public std::vector<T> {
349  public:
350  typedef typename std::vector<T>::size_type size_type;
351  typedef typename std::vector<T>::iterator iterator;
352  typedef typename std::vector<T>::const_iterator const_iterator;
353  typedef typename std::vector<T>::reference reference;
354  typedef typename std::vector<T>::const_reference const_reference;
355 
356  protected:
357  size_type nbc, nbl;
358 
359  public:
360 
361  inline const_reference operator ()(size_type l, size_type c) const {
362  GMM_ASSERT2(l < nbl && c < nbc, "out of range");
363  return *(this->begin() + c*nbl+l);
364  }
365  inline reference operator ()(size_type l, size_type c) {
366  GMM_ASSERT2(l < nbl && c < nbc, "out of range");
367  return *(this->begin() + c*nbl+l);
368  }
369 
370  std::vector<T> &as_vector() { return *this; }
371  const std::vector<T> &as_vector() const { return *this; }
372 
373  void resize(size_type, size_type);
374  void base_resize(size_type, size_type);
375  void reshape(size_type, size_type);
376 
377  void fill(T a, T b = T(0));
378  inline size_type nrows() const { return nbl; }
379  inline size_type ncols() const { return nbc; }
380  void swap(dense_matrix<T> &m)
381  { std::vector<T>::swap(m); std::swap(nbc, m.nbc); std::swap(nbl, m.nbl); }
382 
383  dense_matrix(size_type l, size_type c)
384  : std::vector<T>(c*l), nbc(c), nbl(l) {}
385  dense_matrix() { nbl = nbc = 0; }
386  };
387 
388  template<typename T>
389  void dense_matrix<T>::reshape(size_type m,size_type n) {
390  GMM_ASSERT2(n*m == nbl*nbc, "dimensions mismatch");
391  nbl = m; nbc = n;
392  }
393 
394  template<typename T>
395  void dense_matrix<T>::base_resize(size_type m, size_type n)
396  { std::vector<T>::resize(n*m); nbl = m; nbc = n; }
397 
398  template<typename T>
399  void dense_matrix<T>::resize(size_type m, size_type n) {
400  if (n*m > nbc*nbl) std::vector<T>::resize(n*m);
401  if (m < nbl) {
402  for (size_type i = 1; i < std::min(nbc, n); ++i)
403  std::copy(this->begin()+i*nbl, this->begin()+(i*nbl+m),
404  this->begin()+i*m);
405  for (size_type i = std::min(nbc, n); i < n; ++i)
406  std::fill(this->begin()+(i*m), this->begin()+(i+1)*m, T(0));
407  }
408  else if (m > nbl) { /* do nothing when the nb of rows does not change */
409  for (size_type i = std::min(nbc, n); i > 1; --i)
410  std::copy(this->begin()+(i-1)*nbl, this->begin()+i*nbl,
411  this->begin()+(i-1)*m);
412  for (size_type i = 0; i < std::min(nbc, n); ++i)
413  std::fill(this->begin()+(i*m+nbl), this->begin()+(i+1)*m, T(0));
414  }
415  if (n*m < nbc*nbl) std::vector<T>::resize(n*m);
416  nbl = m; nbc = n;
417  }
418 
419  template<typename T>
420  void dense_matrix<T>::fill(T a, T b) {
421  std::fill(this->begin(), this->end(), b);
422  size_type n = std::min(nbl, nbc);
423  if (a != b) for (size_type i = 0; i < n; ++i) (*this)(i,i) = a;
424  }
425 
426  template <typename T>
427  struct linalg_traits<dense_matrix<T> > {
428  typedef dense_matrix<T> this_type;
429  typedef this_type origin_type;
430  typedef linalg_false is_reference;
431  typedef abstract_matrix linalg_type;
432  typedef T value_type;
433  typedef T& reference;
434  typedef abstract_dense storage_type;
435  typedef tab_ref_reg_spaced_with_origin<typename this_type::iterator,
436  this_type> sub_row_type;
437  typedef tab_ref_reg_spaced_with_origin<typename this_type::const_iterator,
438  this_type> const_sub_row_type;
439  typedef dense_compressed_iterator<typename this_type::iterator,
440  typename this_type::iterator,
441  this_type *> row_iterator;
442  typedef dense_compressed_iterator<typename this_type::const_iterator,
443  typename this_type::iterator,
444  const this_type *> const_row_iterator;
445  typedef tab_ref_with_origin<typename this_type::iterator,
446  this_type> sub_col_type;
447  typedef tab_ref_with_origin<typename this_type::const_iterator,
448  this_type> const_sub_col_type;
449  typedef dense_compressed_iterator<typename this_type::iterator,
450  typename this_type::iterator,
451  this_type *> col_iterator;
452  typedef dense_compressed_iterator<typename this_type::const_iterator,
453  typename this_type::iterator,
454  const this_type *> const_col_iterator;
455  typedef col_and_row sub_orientation;
456  typedef linalg_true index_sorted;
457  static size_type nrows(const this_type &m) { return m.nrows(); }
458  static size_type ncols(const this_type &m) { return m.ncols(); }
459  static const_sub_row_type row(const const_row_iterator &it)
460  { return const_sub_row_type(*it, it.nrows, it.ncols, it.origin); }
461  static const_sub_col_type col(const const_col_iterator &it)
462  { return const_sub_col_type(*it, *it + it.nrows, it.origin); }
463  static sub_row_type row(const row_iterator &it)
464  { return sub_row_type(*it, it.nrows, it.ncols, it.origin); }
465  static sub_col_type col(const col_iterator &it)
466  { return sub_col_type(*it, *it + it.nrows, it.origin); }
467  static row_iterator row_begin(this_type &m)
468  { return row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), 0, &m); }
469  static row_iterator row_end(this_type &m)
470  { return row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), m.nrows(), &m); }
471  static const_row_iterator row_begin(const this_type &m)
472  { return const_row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), 0, &m); }
473  static const_row_iterator row_end(const this_type &m)
474  { return const_row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), m.nrows(), &m); }
475  static col_iterator col_begin(this_type &m)
476  { return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), 0, &m); }
477  static col_iterator col_end(this_type &m)
478  { return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), m.ncols(), &m); }
479  static const_col_iterator col_begin(const this_type &m)
480  { return const_col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), 0, &m); }
481  static const_col_iterator col_end(const this_type &m)
482  { return const_col_iterator(m.begin(),m.nrows(),m.nrows(),m.ncols(),m.ncols(), &m); }
483  static origin_type* origin(this_type &m) { return &m; }
484  static const origin_type* origin(const this_type &m) { return &m; }
485  static void do_clear(this_type &m) { m.fill(value_type(0)); }
486  static value_type access(const const_col_iterator &itcol, size_type j)
487  { return (*itcol)[j]; }
488  static reference access(const col_iterator &itcol, size_type j)
489  { return (*itcol)[j]; }
490  static void resize(this_type &v, size_type m, size_type n)
491  { v.resize(m,n); }
492  static void reshape(this_type &v, size_type m, size_type n)
493  { v.reshape(m, n); }
494  };
495 
496  template<typename T> std::ostream &operator <<
497  (std::ostream &o, const dense_matrix<T>& m) { gmm::write(o,m); return o; }
498 
499 
500  /* ******************************************************************** */
501  /* */
502  /* Read only compressed sparse column matrix */
503  /* */
504  /* ******************************************************************** */
505 
506  template <typename T, typename IND_TYPE = unsigned int, int shift = 0>
507  struct csc_matrix {
508 
509  std::vector<T> pr;
510  std::vector<IND_TYPE> ir;
511  std::vector<IND_TYPE> jc;
512  size_type nc, nr;
513 
514  typedef T value_type;
515  typedef T& access_type;
516 
517  template <typename Matrix> void init_with_good_format(const Matrix &B);
518  template <typename Matrix> void init_with(const Matrix &A);
519  void init_with(const col_matrix<gmm::rsvector<T> > &B)
520  { init_with_good_format(B); }
521  void init_with(const col_matrix<wsvector<T> > &B)
522  { init_with_good_format(B); }
523  template <typename PT1, typename PT2, typename PT3, int cshift>
524  void init_with(const csc_matrix_ref<PT1,PT2,PT3,cshift>& B)
525  { init_with_good_format(B); }
526  template <typename U, int cshift>
527  void init_with(const csc_matrix<U, IND_TYPE, cshift>& B)
528  { init_with_good_format(B); }
529 
530  void init_with_identity(size_type n);
531 
532  csc_matrix() : nc(0), nr(0) {}
533  csc_matrix(size_type nnr, size_type nnc);
534 
535  size_type nrows() const { return nr; }
536  size_type ncols() const { return nc; }
537  void swap(csc_matrix<T, IND_TYPE, shift> &m) {
538  std::swap(pr, m.pr);
539  std::swap(ir, m.ir); std::swap(jc, m.jc);
540  std::swap(nc, m.nc); std::swap(nr, m.nr);
541  }
542  value_type operator()(size_type i, size_type j) const
543  { return mat_col(*this, j)[i]; }
544  };
545 
546  template <typename T, typename IND_TYPE, int shift> template<typename Matrix>
547  void csc_matrix<T, IND_TYPE, shift>::init_with_good_format(const Matrix &B) {
548  typedef typename linalg_traits<Matrix>::const_sub_col_type col_type;
549  nc = mat_ncols(B); nr = mat_nrows(B);
550  jc.resize(nc+1);
551  jc[0] = shift;
552  for (size_type j = 0; j < nc; ++j) {
553  jc[j+1] = IND_TYPE(jc[j] + nnz(mat_const_col(B, j)));
554  }
555  pr.resize(jc[nc]);
556  ir.resize(jc[nc]);
557  for (size_type j = 0; j < nc; ++j) {
558  col_type col = mat_const_col(B, j);
559  typename linalg_traits<typename org_type<col_type>::t>::const_iterator
560  it = vect_const_begin(col), ite = vect_const_end(col);
561  for (size_type k = 0; it != ite; ++it, ++k) {
562  pr[jc[j]-shift+k] = *it;
563  ir[jc[j]-shift+k] = IND_TYPE(it.index() + shift);
564  }
565  }
566  }
567 
568  template <typename T, typename IND_TYPE, int shift>
569  template <typename Matrix>
570  void csc_matrix<T, IND_TYPE, shift>::init_with(const Matrix &A) {
571  col_matrix<wsvector<T> > B(mat_nrows(A), mat_ncols(A));
572  copy(A, B);
573  init_with_good_format(B);
574  }
575 
576  template <typename T, typename IND_TYPE, int shift>
577  void csc_matrix<T, IND_TYPE, shift>::init_with_identity(size_type n) {
578  nc = nr = n;
579  pr.resize(nc); ir.resize(nc); jc.resize(nc+1);
580  for (size_type j = 0; j < nc; ++j)
581  { ir[j] = jc[j] = shift + j; pr[j] = T(1); }
582  jc[nc] = shift + nc;
583  }
584 
585  template <typename T, typename IND_TYPE, int shift>
586  csc_matrix<T, IND_TYPE, shift>::csc_matrix(size_type nnr, size_type nnc)
587  : nc(nnc), nr(nnr) {
588  pr.resize(1); ir.resize(1); jc.resize(nc+1);
589  for (size_type j = 0; j <= nc; ++j) jc[j] = shift;
590  }
591 
592  template <typename T, typename IND_TYPE, int shift>
593  struct linalg_traits<csc_matrix<T, IND_TYPE, shift> > {
594  typedef csc_matrix<T, IND_TYPE, shift> this_type;
595  typedef linalg_const is_reference;
596  typedef abstract_matrix linalg_type;
597  typedef T value_type;
598  typedef T origin_type;
599  typedef T reference;
600  typedef abstract_sparse storage_type;
601  typedef abstract_null_type sub_row_type;
602  typedef abstract_null_type const_sub_row_type;
603  typedef abstract_null_type row_iterator;
604  typedef abstract_null_type const_row_iterator;
605  typedef abstract_null_type sub_col_type;
606  typedef cs_vector_ref<const T *, const IND_TYPE *, shift>
607  const_sub_col_type;
608  typedef sparse_compressed_iterator<const T *, const IND_TYPE *,
609  const IND_TYPE *, shift>
610  const_col_iterator;
611  typedef abstract_null_type col_iterator;
612  typedef col_major sub_orientation;
613  typedef linalg_true index_sorted;
614  static size_type nrows(const this_type &m) { return m.nrows(); }
615  static size_type ncols(const this_type &m) { return m.ncols(); }
616  static const_col_iterator col_begin(const this_type &m) {
617  return const_col_iterator(m.pr.data(), m.ir.data(), m.jc.data(), m.nr,
618  m.pr.data());
619  }
620  static const_col_iterator col_end(const this_type &m) {
621  return const_col_iterator(m.pr.data(), m.ir.data(), m.jc.data()+m.nc, m.nr,
622  m.pr.data());
623  }
624  static const_sub_col_type col(const const_col_iterator &it) {
625  return const_sub_col_type(it.pr + *(it.jc) - shift,
626  it.ir + *(it.jc) - shift,
627  *(it.jc + 1) - *(it.jc), it.n);
628  }
629  static const origin_type* origin(const this_type &m) { return m.pr.data(); }
630  static void do_clear(this_type &m) { m.do_clear(); }
631  static value_type access(const const_col_iterator &itcol, size_type j)
632  { return col(itcol)[j]; }
633  };
634 
635  template <typename T, typename IND_TYPE, int shift>
636  std::ostream &operator <<
637  (std::ostream &o, const csc_matrix<T, IND_TYPE, shift>& m)
638  { gmm::write(o,m); return o; }
639 
640  template <typename T, typename IND_TYPE, int shift>
641  inline void copy(const identity_matrix &, csc_matrix<T, IND_TYPE, shift>& M)
642  { M.init_with_identity(mat_nrows(M)); }
643 
644  template <typename Matrix, typename T, typename IND_TYPE, int shift>
645  inline void copy(const Matrix &A, csc_matrix<T, IND_TYPE, shift>& M)
646  { M.init_with(A); }
647 
648  /* ******************************************************************** */
649  /* */
650  /* Read only compressed sparse row matrix */
651  /* */
652  /* ******************************************************************** */
653 
654  template <typename T, typename IND_TYPE = unsigned int, int shift = 0>
655  struct csr_matrix {
656 
657  std::vector<T> pr; // values.
658  std::vector<IND_TYPE> ir; // col indices.
659  std::vector<IND_TYPE> jc; // row repartition on pr and ir.
660  size_type nc, nr;
661 
662  typedef T value_type;
663  typedef T& access_type;
664 
665 
666  template <typename Matrix> void init_with_good_format(const Matrix &B);
667  void init_with(const row_matrix<wsvector<T> > &B)
668  { init_with_good_format(B); }
669  void init_with(const row_matrix<rsvector<T> > &B)
670  { init_with_good_format(B); }
671  template <typename PT1, typename PT2, typename PT3, int cshift>
672  void init_with(const csr_matrix_ref<PT1,PT2,PT3,cshift>& B)
673  { init_with_good_format(B); }
674  template <typename U, int cshift>
675  void init_with(const csr_matrix<U, IND_TYPE, cshift>& B)
676  { init_with_good_format(B); }
677 
678  template <typename Matrix> void init_with(const Matrix &A);
679  void init_with_identity(size_type n);
680 
681  csr_matrix() : nc(0), nr(0) {}
682  csr_matrix(size_type nnr, size_type nnc);
683 
684  size_type nrows() const { return nr; }
685  size_type ncols() const { return nc; }
686  void swap(csr_matrix<T, IND_TYPE, shift> &m) {
687  std::swap(pr, m.pr);
688  std::swap(ir,m.ir); std::swap(jc, m.jc);
689  std::swap(nc, m.nc); std::swap(nr,m.nr);
690  }
691 
692  value_type operator()(size_type i, size_type j) const
693  { return mat_row(*this, i)[j]; }
694  };
695 
696  template <typename T, typename IND_TYPE, int shift> template <typename Matrix>
697  void csr_matrix<T, IND_TYPE, shift>::init_with_good_format(const Matrix &B) {
698  typedef typename linalg_traits<Matrix>::const_sub_row_type row_type;
699  nc = mat_ncols(B); nr = mat_nrows(B);
700  jc.resize(nr+1);
701  jc[0] = shift;
702  for (size_type j = 0; j < nr; ++j) {
703  jc[j+1] = IND_TYPE(jc[j] + nnz(mat_const_row(B, j)));
704  }
705  pr.resize(jc[nr]);
706  ir.resize(jc[nr]);
707  for (size_type j = 0; j < nr; ++j) {
708  row_type row = mat_const_row(B, j);
709  typename linalg_traits<typename org_type<row_type>::t>::const_iterator
710  it = vect_const_begin(row), ite = vect_const_end(row);
711  for (size_type k = 0; it != ite; ++it, ++k) {
712  pr[jc[j]-shift+k] = *it;
713  ir[jc[j]-shift+k] = IND_TYPE(it.index()+shift);
714  }
715  }
716  }
717 
718  template <typename T, typename IND_TYPE, int shift> template <typename Matrix>
719  void csr_matrix<T, IND_TYPE, shift>::init_with(const Matrix &A) {
720  row_matrix<wsvector<T> > B(mat_nrows(A), mat_ncols(A));
721  copy(A, B);
722  init_with_good_format(B);
723  }
724 
725  template <typename T, typename IND_TYPE, int shift>
726  void csr_matrix<T, IND_TYPE, shift>::init_with_identity(size_type n) {
727  nc = nr = n;
728  pr.resize(nr); ir.resize(nr); jc.resize(nr+1);
729  for (size_type j = 0; j < nr; ++j)
730  { ir[j] = jc[j] = shift + j; pr[j] = T(1); }
731  jc[nr] = shift + nr;
732  }
733 
734  template <typename T, typename IND_TYPE, int shift>
735  csr_matrix<T, IND_TYPE, shift>::csr_matrix(size_type nnr, size_type nnc)
736  : nc(nnc), nr(nnr) {
737  pr.resize(1); ir.resize(1); jc.resize(nr+1);
738  for (size_type j = 0; j < nr; ++j) jc[j] = shift;
739  jc[nr] = shift;
740  }
741 
742 
743  template <typename T, typename IND_TYPE, int shift>
744  struct linalg_traits<csr_matrix<T, IND_TYPE, shift> > {
745  typedef csr_matrix<T, IND_TYPE, shift> this_type;
746  typedef linalg_const is_reference;
747  typedef abstract_matrix linalg_type;
748  typedef T value_type;
749  typedef T origin_type;
750  typedef T reference;
751  typedef abstract_sparse storage_type;
752  typedef abstract_null_type sub_col_type;
753  typedef abstract_null_type const_sub_col_type;
754  typedef abstract_null_type col_iterator;
755  typedef abstract_null_type const_col_iterator;
756  typedef abstract_null_type sub_row_type;
757  typedef cs_vector_ref<const T *, const IND_TYPE *, shift>
758  const_sub_row_type;
759  typedef sparse_compressed_iterator<const T *, const IND_TYPE *,
760  const IND_TYPE *, shift>
761  const_row_iterator;
762  typedef abstract_null_type row_iterator;
763  typedef row_major sub_orientation;
764  typedef linalg_true index_sorted;
765  static size_type nrows(const this_type &m) { return m.nrows(); }
766  static size_type ncols(const this_type &m) { return m.ncols(); }
767  static const_row_iterator row_begin(const this_type &m)
768  { return const_row_iterator(m.pr.data(), m.ir.data(), m.jc.data(), m.nc,
769  m.pr.data()); }
770  static const_row_iterator row_end(const this_type &m)
771  { return const_row_iterator(m.pr.data(), m.ir.data(), m.jc.data() + m.nr,
772  m.nc, m.pr.data()); }
773  static const_sub_row_type row(const const_row_iterator &it) {
774  return const_sub_row_type(it.pr + *(it.jc) - shift,
775  it.ir + *(it.jc) - shift,
776  *(it.jc + 1) - *(it.jc), it.n);
777  }
778  static const origin_type* origin(const this_type &m) { return m.pr.data(); }
779  static void do_clear(this_type &m) { m.do_clear(); }
780  static value_type access(const const_row_iterator &itrow, size_type j)
781  { return row(itrow)[j]; }
782  };
783 
784  template <typename T, typename IND_TYPE, int shift>
785  std::ostream &operator <<
786  (std::ostream &o, const csr_matrix<T, IND_TYPE, shift>& m)
787  { gmm::write(o,m); return o; }
788 
789  template <typename T, typename IND_TYPE, int shift>
790  inline void copy(const identity_matrix &, csr_matrix<T, IND_TYPE, shift>& M)
791  { M.init_with_identity(mat_nrows(M)); }
792 
793  template <typename Matrix, typename T, typename IND_TYPE, int shift>
794  inline void copy(const Matrix &A, csr_matrix<T, IND_TYPE, shift>& M)
795  { M.init_with(A); }
796 
797  /* ******************************************************************** */
798  /* */
799  /* Block matrix */
800  /* */
801  /* ******************************************************************** */
802 
803  template <typename MAT> class block_matrix {
804  protected :
805  std::vector<MAT> blocks;
806  size_type nrowblocks_;
807  size_type ncolblocks_;
808  std::vector<sub_interval> introw, intcol;
809 
810  public :
811  typedef typename linalg_traits<MAT>::value_type value_type;
812  typedef typename linalg_traits<MAT>::reference reference;
813 
814  size_type nrows() const { return introw[nrowblocks_-1].max; }
815  size_type ncols() const { return intcol[ncolblocks_-1].max; }
816  size_type nrowblocks() const { return nrowblocks_; }
817  size_type ncolblocks() const { return ncolblocks_; }
818  const sub_interval &subrowinterval(size_type i) const { return introw[i]; }
819  const sub_interval &subcolinterval(size_type i) const { return intcol[i]; }
820  const MAT &block(size_type i, size_type j) const
821  { return blocks[j*ncolblocks_+i]; }
822  MAT &block(size_type i, size_type j)
823  { return blocks[j*ncolblocks_+i]; }
824  void do_clear();
825  // to be done : read and write access to a component
826  value_type operator() (size_type i, size_type j) const {
827  size_type k, l;
828  for (k = 0; k < nrowblocks_; ++k)
829  if (i >= introw[k].min && i < introw[k].max) break;
830  for (l = 0; l < nrowblocks_; ++l)
831  if (j >= introw[l].min && j < introw[l].max) break;
832  return (block(k, l))(i - introw[k].min, j - introw[l].min);
833  }
834  reference operator() (size_type i, size_type j) {
835  size_type k, l;
836  for (k = 0; k < nrowblocks_; ++k)
837  if (i >= introw[k].min && i < introw[k].max) break;
838  for (l = 0; l < nrowblocks_; ++l)
839  if (j >= introw[l].min && j < introw[l].max) break;
840  return (block(k, l))(i - introw[k].min, j - introw[l].min);
841  }
842 
843  template <typename CONT> void resize(const CONT &c1, const CONT &c2);
844  template <typename CONT> block_matrix(const CONT &c1, const CONT &c2)
845  { resize(c1, c2); }
846  block_matrix() {}
847 
848  };
849 
850  template <typename MAT> struct linalg_traits<block_matrix<MAT> > {
851  typedef block_matrix<MAT> this_type;
852  typedef linalg_false is_reference;
853  typedef abstract_matrix linalg_type;
854  typedef this_type origin_type;
855  typedef typename linalg_traits<MAT>::value_type value_type;
856  typedef typename linalg_traits<MAT>::reference reference;
857  typedef typename linalg_traits<MAT>::storage_type storage_type;
858  typedef abstract_null_type sub_row_type; // to be done ...
859  typedef abstract_null_type const_sub_row_type; // to be done ...
860  typedef abstract_null_type row_iterator; // to be done ...
861  typedef abstract_null_type const_row_iterator; // to be done ...
862  typedef abstract_null_type sub_col_type; // to be done ...
863  typedef abstract_null_type const_sub_col_type; // to be done ...
864  typedef abstract_null_type col_iterator; // to be done ...
865  typedef abstract_null_type const_col_iterator; // to be done ...
866  typedef abstract_null_type sub_orientation; // to be done ...
867  typedef linalg_true index_sorted;
868  static size_type nrows(const this_type &m) { return m.nrows(); }
869  static size_type ncols(const this_type &m) { return m.ncols(); }
870  static origin_type* origin(this_type &m) { return &m; }
871  static const origin_type* origin(const this_type &m) { return &m; }
872  static void do_clear(this_type &m) { m.do_clear(); }
873  // access to be done ...
874  static void resize(this_type &, size_type , size_type)
875  { GMM_ASSERT1(false, "Sorry, to be done"); }
876  static void reshape(this_type &, size_type , size_type)
877  { GMM_ASSERT1(false, "Sorry, to be done"); }
878  };
879 
880  template <typename MAT>
881  void block_matrix<MAT>::do_clear() {
882  for (size_type j = 0; j < ncolblocks_; ++j)
883  for (size_type i = 0; i < nrowblocks_; ++i)
884  clear(block(i,j));
885  }
886 
887  template <typename MAT> template <typename CONT>
888  void block_matrix<MAT>::resize(const CONT &c1, const CONT &c2) {
889  nrowblocks_ = c1.size(); ncolblocks_ = c2.size();
890  blocks.resize(nrowblocks_ * ncolblocks_);
891  intcol.resize(ncolblocks_);
892  introw.resize(nrowblocks_);
893  for (size_type j = 0, l = 0; j < ncolblocks_; ++j) {
894  intcol[j] = sub_interval(l, c2[j]); l += c2[j];
895  for (size_type i = 0, k = 0; i < nrowblocks_; ++i) {
896  if (j == 0) { introw[i] = sub_interval(k, c1[i]); k += c1[i]; }
897  block(i, j) = MAT(c1[i], c2[j]);
898  }
899  }
900  }
901 
902  template <typename M1, typename M2>
903  void copy(const block_matrix<M1> &m1, M2 &m2) {
904  for (size_type j = 0; j < m1.ncolblocks(); ++j)
905  for (size_type i = 0; i < m1.nrowblocks(); ++i)
906  copy(m1.block(i,j), sub_matrix(m2, m1.subrowinterval(i),
907  m1.subcolinterval(j)));
908  }
909 
910  template <typename M1, typename M2>
911  void copy(const block_matrix<M1> &m1, const M2 &m2)
912  { copy(m1, linalg_const_cast(m2)); }
913 
914 
915  template <typename MAT, typename V1, typename V2>
916  void mult(const block_matrix<MAT> &m, const V1 &v1, V2 &v2) {
917  clear(v2);
918  typename sub_vector_type<V2 *, sub_interval>::vector_type sv;
919  for (size_type i = 0; i < m.nrowblocks() ; ++i)
920  for (size_type j = 0; j < m.ncolblocks() ; ++j) {
921  sv = sub_vector(v2, m.subrowinterval(i));
922  mult(m.block(i,j),
923  sub_vector(v1, m.subcolinterval(j)), sv, sv);
924  }
925  }
926 
927  template <typename MAT, typename V1, typename V2, typename V3>
928  void mult(const block_matrix<MAT> &m, const V1 &v1, const V2 &v2, V3 &v3) {
929  typename sub_vector_type<V3 *, sub_interval>::vector_type sv;
930  for (size_type i = 0; i < m.nrowblocks() ; ++i)
931  for (size_type j = 0; j < m.ncolblocks() ; ++j) {
932  sv = sub_vector(v3, m.subrowinterval(i));
933  if (j == 0)
934  mult(m.block(i,j),
935  sub_vector(v1, m.subcolinterval(j)),
936  sub_vector(v2, m.subrowinterval(i)), sv);
937  else
938  mult(m.block(i,j),
939  sub_vector(v1, m.subcolinterval(j)), sv, sv);
940  }
941 
942  }
943 
944  template <typename MAT, typename V1, typename V2>
945  void mult(const block_matrix<MAT> &m, const V1 &v1, const V2 &v2)
946  { mult(m, v1, linalg_const_cast(v2)); }
947 
948  template <typename MAT, typename V1, typename V2, typename V3>
949  void mult(const block_matrix<MAT> &m, const V1 &v1, const V2 &v2,
950  const V3 &v3)
951  { mult_const(m, v1, v2, linalg_const_cast(v3)); }
952 
953 }
954  /* ******************************************************************** */
955  /* */
956  /* Distributed matrices */
957  /* */
958  /* ******************************************************************** */
959 
960 #ifdef GMM_USES_MPI
961 # include <mpi.h>
962 
963 namespace gmm {
964 
965 
966 
967  template <typename T> inline MPI_Datatype mpi_type(T)
968  { GMM_ASSERT1(false, "Sorry unsupported type"); return MPI_FLOAT; }
969  inline MPI_Datatype mpi_type(double) { return MPI_DOUBLE; }
970  inline MPI_Datatype mpi_type(float) { return MPI_FLOAT; }
971  inline MPI_Datatype mpi_type(long double) { return MPI_LONG_DOUBLE; }
972 #ifndef LAM_MPI
973  inline MPI_Datatype mpi_type(std::complex<float>) { return MPI_COMPLEX; }
974  inline MPI_Datatype mpi_type(std::complex<double>) { return MPI_DOUBLE_COMPLEX; }
975 #endif
976  inline MPI_Datatype mpi_type(int) { return MPI_INT; }
977  inline MPI_Datatype mpi_type(unsigned int) { return MPI_UNSIGNED; }
978  inline MPI_Datatype mpi_type(long) { return MPI_LONG; }
979  inline MPI_Datatype mpi_type(unsigned long) { return MPI_UNSIGNED_LONG; }
980 
981  template <typename MAT> struct mpi_distributed_matrix {
982  MAT M;
983 
984  mpi_distributed_matrix(size_type n, size_type m) : M(n, m) {}
985  mpi_distributed_matrix() {}
986 
987  const MAT &local_matrix() const { return M; }
988  MAT &local_matrix() { return M; }
989  };
990 
991  template <typename MAT> inline MAT &eff_matrix(MAT &m) { return m; }
992  template <typename MAT> inline
993  const MAT &eff_matrix(const MAT &m) { return m; }
994  template <typename MAT> inline
995  MAT &eff_matrix(mpi_distributed_matrix<MAT> &m) { return m.M; }
996  template <typename MAT> inline
997  const MAT &eff_matrix(const mpi_distributed_matrix<MAT> &m) { return m.M; }
998 
999 
1000  template <typename MAT1, typename MAT2>
1001  inline void copy(const mpi_distributed_matrix<MAT1> &m1,
1002  mpi_distributed_matrix<MAT2> &m2)
1003  { copy(eff_matrix(m1), eff_matrix(m2)); }
1004  template <typename MAT1, typename MAT2>
1005  inline void copy(const mpi_distributed_matrix<MAT1> &m1,
1006  const mpi_distributed_matrix<MAT2> &m2)
1007  { copy(m1.M, m2.M); }
1008 
1009  template <typename MAT1, typename MAT2>
1010  inline void copy(const mpi_distributed_matrix<MAT1> &m1, MAT2 &m2)
1011  { copy(m1.M, m2); }
1012  template <typename MAT1, typename MAT2>
1013  inline void copy(const mpi_distributed_matrix<MAT1> &m1, const MAT2 &m2)
1014  { copy(m1.M, m2); }
1015 
1016 
1017  template <typename MATSP, typename V1, typename V2> inline
1018  typename strongest_value_type3<V1,V2,MATSP>::value_type
1019  vect_sp(const mpi_distributed_matrix<MATSP> &ps, const V1 &v1,
1020  const V2 &v2) {
1021  typedef typename strongest_value_type3<V1,V2,MATSP>::value_type T;
1022  T res = vect_sp(ps.M, v1, v2), rest;
1023  MPI_Allreduce(&res, &rest, 1, mpi_type(T()), MPI_SUM,MPI_COMM_WORLD);
1024  return rest;
1025  }
1026 
1027  template <typename MAT, typename V1, typename V2>
1028  inline void mult_add(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
1029  V2 &v2) {
1030  typedef typename linalg_traits<V2>::value_type T;
1031  std::vector<T> v3(vect_size(v2)), v4(vect_size(v2));
1032  static double tmult_tot = 0.0;
1033  static double tmult_tot2 = 0.0;
1034  double t_ref = MPI_Wtime();
1035  gmm::mult(m.M, v1, v3);
1036  if (is_sparse(v2)) GMM_WARNING2("Using a plain temporary, here.");
1037  double t_ref2 = MPI_Wtime();
1038  MPI_Allreduce(&(v3[0]), &(v4[0]),gmm::vect_size(v2), mpi_type(T()),
1039  MPI_SUM,MPI_COMM_WORLD);
1040  tmult_tot2 = MPI_Wtime()-t_ref2;
1041  cout << "reduce mult mpi = " << tmult_tot2 << endl;
1042  gmm::add(v4, v2);
1043  tmult_tot = MPI_Wtime()-t_ref;
1044  cout << "tmult mpi = " << tmult_tot << endl;
1045  }
1046 
1047  template <typename MAT, typename V1, typename V2>
1048  void mult_add(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
1049  const V2 &v2_)
1050  { mult_add(m, v1, const_cast<V2 &>(v2_)); }
1051 
1052  template <typename MAT, typename V1, typename V2>
1053  inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
1054  const V2 &v2_)
1055  { V2 &v2 = const_cast<V2 &>(v2_); clear(v2); mult_add(m, v1, v2); }
1056 
1057  template <typename MAT, typename V1, typename V2>
1058  inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
1059  V2 &v2)
1060  { clear(v2); mult_add(m, v1, v2); }
1061 
1062  template <typename MAT, typename V1, typename V2, typename V3>
1063  inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
1064  const V2 &v2, const V3 &v3_)
1065  { V3 &v3 = const_cast<V3 &>(v3_); gmm::copy(v2, v3); mult_add(m, v1, v3); }
1066 
1067  template <typename MAT, typename V1, typename V2, typename V3>
1068  inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
1069  const V2 &v2, V3 &v3)
1070  { gmm::copy(v2, v3); mult_add(m, v1, v3); }
1071 
1072 
1073  template <typename MAT> inline
1074  size_type mat_nrows(const mpi_distributed_matrix<MAT> &M)
1075  { return mat_nrows(M.M); }
1076  template <typename MAT> inline
1077  size_type mat_ncols(const mpi_distributed_matrix<MAT> &M)
1078  { return mat_nrows(M.M); }
1079  template <typename MAT> inline
1080  void resize(mpi_distributed_matrix<MAT> &M, size_type m, size_type n)
1081  { resize(M.M, m, n); }
1082  template <typename MAT> inline void clear(mpi_distributed_matrix<MAT> &M)
1083  { clear(M.M); }
1084 
1085 
1086  // For compute reduced system
1087  template <typename MAT1, typename MAT2> inline
1088  void mult(const MAT1 &M1, const mpi_distributed_matrix<MAT2> &M2,
1089  mpi_distributed_matrix<MAT2> &M3)
1090  { mult(M1, M2.M, M3.M); }
1091  template <typename MAT1, typename MAT2> inline
1092  void mult(const mpi_distributed_matrix<MAT2> &M2,
1093  const MAT1 &M1, mpi_distributed_matrix<MAT2> &M3)
1094  { mult(M2.M, M1, M3.M); }
1095  template <typename MAT1, typename MAT2, typename MAT3> inline
1096  void mult(const MAT1 &M1, const mpi_distributed_matrix<MAT2> &M2,
1097  MAT3 &M3)
1098  { mult(M1, M2.M, M3); }
1099  template <typename MAT1, typename MAT2, typename MAT3> inline
1100  void mult(const MAT1 &M1, const mpi_distributed_matrix<MAT2> &M2,
1101  const MAT3 &M3)
1102  { mult(M1, M2.M, M3); }
1103 
1104  template <typename M, typename SUBI1, typename SUBI2>
1105  struct sub_matrix_type<const mpi_distributed_matrix<M> *, SUBI1, SUBI2>
1106  { typedef abstract_null_type matrix_type; };
1107 
1108  template <typename M, typename SUBI1, typename SUBI2>
1109  struct sub_matrix_type<mpi_distributed_matrix<M> *, SUBI1, SUBI2>
1110  { typedef abstract_null_type matrix_type; };
1111 
1112  template <typename M, typename SUBI1, typename SUBI2> inline
1113  typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI2>
1114  ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI2>::matrix_type,
1115  M *>::return_type
1116  sub_matrix(mpi_distributed_matrix<M> &m, const SUBI1 &si1, const SUBI2 &si2)
1117  { return sub_matrix(m.M, si1, si2); }
1118 
1119  template <typename MAT, typename SUBI1, typename SUBI2> inline
1120  typename select_return<typename sub_matrix_type<const MAT *, SUBI1, SUBI2>
1121  ::matrix_type, typename sub_matrix_type<MAT *, SUBI1, SUBI2>::matrix_type,
1122  const MAT *>::return_type
1123  sub_matrix(const mpi_distributed_matrix<MAT> &m, const SUBI1 &si1,
1124  const SUBI2 &si2)
1125  { return sub_matrix(m.M, si1, si2); }
1126 
1127  template <typename M, typename SUBI1> inline
1128  typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI1>
1129  ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>::matrix_type,
1130  M *>::return_type
1131  sub_matrix(mpi_distributed_matrix<M> &m, const SUBI1 &si1)
1132  { return sub_matrix(m.M, si1, si1); }
1133 
1134  template <typename M, typename SUBI1> inline
1135  typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI1>
1136  ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>::matrix_type,
1137  const M *>::return_type
1138  sub_matrix(const mpi_distributed_matrix<M> &m, const SUBI1 &si1)
1139  { return sub_matrix(m.M, si1, si1); }
1140 
1141 
1142  template <typename L> struct transposed_return<const mpi_distributed_matrix<L> *>
1143  { typedef abstract_null_type return_type; };
1144  template <typename L> struct transposed_return<mpi_distributed_matrix<L> *>
1145  { typedef abstract_null_type return_type; };
1146 
1147  template <typename L> inline typename transposed_return<const L *>::return_type
1148  transposed(const mpi_distributed_matrix<L> &l)
1149  { return transposed(l.M); }
1150 
1151  template <typename L> inline typename transposed_return<L *>::return_type
1152  transposed(mpi_distributed_matrix<L> &l)
1153  { return transposed(l.M); }
1154 
1155 
1156  template <typename MAT>
1157  struct linalg_traits<mpi_distributed_matrix<MAT> > {
1158  typedef mpi_distributed_matrix<MAT> this_type;
1159  typedef MAT origin_type;
1160  typedef linalg_false is_reference;
1161  typedef abstract_matrix linalg_type;
1162  typedef typename linalg_traits<MAT>::value_type value_type;
1163  typedef typename linalg_traits<MAT>::reference reference;
1164  typedef typename linalg_traits<MAT>::storage_type storage_type;
1165  typedef abstract_null_type sub_row_type;
1166  typedef abstract_null_type const_sub_row_type;
1167  typedef abstract_null_type row_iterator;
1168  typedef abstract_null_type const_row_iterator;
1169  typedef abstract_null_type sub_col_type;
1170  typedef abstract_null_type const_sub_col_type;
1171  typedef abstract_null_type col_iterator;
1172  typedef abstract_null_type const_col_iterator;
1173  typedef abstract_null_type sub_orientation;
1174  typedef abstract_null_type index_sorted;
1175  static size_type nrows(const this_type &m) { return nrows(m.M); }
1176  static size_type ncols(const this_type &m) { return ncols(m.M); }
1177  static void do_clear(this_type &m) { clear(m.M); }
1178  };
1179 
1180 }
1181 
1182 
1183 #endif // GMM_USES_MPI
1184 
1185 namespace std {
1186  template <typename V>
1187  void swap(gmm::row_matrix<V> &m1, gmm::row_matrix<V> &m2)
1188  { m1.swap(m2); }
1189  template <typename V>
1190  void swap(gmm::col_matrix<V> &m1, gmm::col_matrix<V> &m2)
1191  { m1.swap(m2); }
1192  template <typename T>
1193  void swap(gmm::dense_matrix<T> &m1, gmm::dense_matrix<T> &m2)
1194  { m1.swap(m2); }
1195  template <typename T, typename IND_TYPE, int shift> void
1196  swap(gmm::csc_matrix<T, IND_TYPE, shift> &m1, gmm::csc_matrix<T, IND_TYPE, shift> &m2)
1197  { m1.swap(m2); }
1198  template <typename T, typename IND_TYPE, int shift> void
1199  swap(gmm::csr_matrix<T, IND_TYPE, shift> &m1, gmm::csr_matrix<T, IND_TYPE, shift> &m2)
1200  { m1.swap(m2); }
1201 }
1202 
1203 
1204 
1205 
1206 #endif /* GMM_MATRIX_H__ */
sparse vector built upon std::vector.
Definition: gmm_vector.h:995
size_type nnz(const L &l)
count the number of non-zero entries of a vector or matrix.
Definition: gmm_blas.h:68
void mult_add(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1790
void reshape(M &v, size_type m, size_type n)
*‍/
Definition: gmm_blas.h:250
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
void fill(L &l, typename gmm::linalg_traits< L >::value_type x)
*‍/
Definition: gmm_blas.h:103
strongest_value_type< V1, V2 >::value_type vect_hp(const V1 &v1, const V2 &v2)
*‍/
Definition: gmm_blas.h:510
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:58
void resize(V &v, size_type n)
*‍/
Definition: gmm_blas.h:209
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
strongest_value_type< V1, V2 >::value_type vect_sp(const V1 &v1, const V2 &v2)
*‍/
Definition: gmm_blas.h:263
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
Generic sub-matrices.
Generic sub-vectors.
Generic transposed matrices.
Declaration of the vector types (gmm::rsvector, gmm::wsvector, gmm::slvector ,..)
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48