ug4
sparsematrix.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__CPU_ALGEBRA__SPARSEMATRIX__
34 #define __H__UG__CPU_ALGEBRA__SPARSEMATRIX__
35 
36 #include <math.h>
37 #include "common/common.h"
38 #include "../algebra_common/sparsematrix_util.h"
39 #include <iostream>
40 #include <algorithm>
42 
43 #include "../algebra_common/connection.h"
44 #include "../algebra_common/matrixrow.h"
45 #include "../common/operations_mat/operations_mat.h"
46 
47 #define PROFILE_SPMATRIX(name) PROFILE_BEGIN_GROUP(name, "SparseMatrix algebra")
48 
49 #ifndef NDEBUG
50 #define CHECK_ROW_ITERATORS
51 #endif
52 
53 namespace ug{
54 
57 
58 
59 // example for the variable CRS storage structure:
60 // say we have:
61 // rowStart = 0 3 8
62 // rowEnd = 3 6 11
63 // rowMax = 3 8 11
64 // cols ( | marking end of row): 2 5 6 | 2 6 7 x x| 8 9 10
65 
66 // now insert (0 3): row 0 is full (rowEnd[0]==rowMax[0]), copy it to the end, and insert index
67 // rowStart = 11 3 8
68 // rowEnd = 15 6 11
69 // rowMax = 17 8 11
70 // cols ( | marking end of row): x x x | 2 6 7 x x| 8 9 10 | 2 3 5 6 x x |
71 
72 // now insert (1 3): row 1 not full, we can add it
73 // rowStart 11 3 8
74 // rowEnd 15 7 11
75 // rowMax = 17 8 11
76 // cols : x x x | 2 3 6 7 x | 8 9 10 | 2 3 5 6 x x |
77 
78 // defragment:
79 // rowStart 0 4 8
80 // rowEnd 4 8 11
81 // rowMax = 4 8 11
82 // cols : 2 3 5 6 | 2 3 6 7 | 8 9 10
83 
84 
98 template<typename TValueType> class SparseMatrix
99 {
100 public:
101  typedef TValueType value_type;
102  enum {rows_sorted=true};
103 
105 
106 public:
110 
111 public:
112  // construction etc
113  //----------------------
114 
116  SparseMatrix();
118  virtual ~SparseMatrix () {}
119 
123  void clear_and_free();
124 
131  void resize_and_clear(size_t newRows, size_t newCols);
132  void resize_and_keep_values(size_t newRows, size_t newCols);
133  void clear_retain_structure();
134 
141  void set_as_transpose_of(const SparseMatrix<value_type> &B, double scale=1.0);
142  void set_as_transpose_of2(const SparseMatrix<value_type> &B, double scale=1.0);
143 
150  void set_as_copy_of(const SparseMatrix<value_type> &B, double scale=1.0);
152  {
153  set_as_copy_of(B);
154  return *this;
155  }
156 
157 
158 public:
160  template<typename vector_t>
161  void axpy(vector_t &dest,
162  const number &alpha1, const vector_t &v1,
163  const number &beta1, const vector_t &w1) const;
164 
166  template<typename vector_t>
167  void axpy_transposed(vector_t &dest,
168  const number &alpha1, const vector_t &v1,
169  const number &beta1, const vector_t &w1) const;
170 
172  template<typename vector_t>
173  void apply_ignore_zero_rows(vector_t &dest,
174  const number &beta1, const vector_t &w1) const;
175 
177  template<typename vector_t>
178  void apply_transposed_ignore_zero_rows(vector_t &dest,
179  const number &beta1, const vector_t &w1) const;
180 
181  // DEPRECATED!
183  // apply is deprecated because of axpy(res, 0.0, res, 1.0, beta, w1)
184  template<typename Vector_type>
185  bool apply(Vector_type &res, const Vector_type &x) const
186  {
187  axpy(res, 0.0, res, 1.0, x);
188  return true;
189  }
190 
192  // apply is deprecated because of axpy(res, 0.0, res, 1.0, beta, w1)
193  template<typename Vector_type>
194  bool apply_transposed(Vector_type &res, const Vector_type &x) const
195  {
196  axpy_transposed(res, 0.0, res, 1.0, x);
197  return true;
198  }
199 
200  // matmult_minus is deprecated because of axpy(res, 1.0, res, -1.0, x);
202  template<typename Vector_type>
203  bool matmul_minus(Vector_type &res, const Vector_type &x) const
204  {
205  axpy(res, 1.0, res, -1.0, x);
206  return true;
207  }
208 
209 
210 
216  inline bool is_isolated(size_t i) const;
217 
218  void scale(double d);
219  SparseMatrix<value_type> &operator *= (double d) { scale(d); return *this; }
220 
221  // submatrix set/get functions
222  //-------------------------------
223 
235  template<typename M>
236  void add(const M &mat);
237  template<typename M>
239  void set(const M &mat);
241  template<typename M>
242  void get(M &mat) const;
243 
244  // finalizing functions
245  //----------------------
246 
247 
248 
249  inline void check_rc(size_t r, size_t c) const
250  {
251  UG_ASSERT(r < num_rows() && c < num_cols(), "tried to access element (" << r << ", " << c << ") of " << num_rows() << " x " << num_cols() << " matrix.");
252  }
253 
254  inline void check_row_modifiable(size_t r) const
255  {
256 #ifdef CHECK_ROW_ITERATORS
257  UG_ASSERT(nrOfRowIterators[r] == 0, "row " << r << " is used by an iterator and should not be modified.")
258 #endif
259  }
260 
261 
263  void set(double a);
264 
272  const value_type &operator () (size_t r, size_t c) const
273  {
274  check_rc(r, c);
275  int j=get_index_const(r, c);
276  if(j == -1)
277  {
278  static value_type v(0.0);
279  return v;
280  }
281  UG_ASSERT(cols[j]==(int)c && j >= rowStart[r] && j < rowEnd[r], "");
282  return values[j];
283  }
284 
293  value_type &operator() (size_t r, size_t c)
294  {
295  check_rc(r, c);
296  int j=get_index(r, c);
297  UG_ASSERT(j != -1 && cols[j]==(int)c && j >= rowStart[r] && j < rowEnd[r], "");
298  return values[j];
299  }
300 
301 public:
302  // row functions
303 
311  void set_matrix_row(size_t row, connection *c, size_t nr);
312 
324  void add_matrix_row(size_t row, connection *c, size_t nr);
325 
326 
328  template<typename vector_t>
329  inline void mat_mult_add_row(size_t row, typename vector_t::value_type &dest, double alpha, const vector_t &v) const;
330 public:
331  // accessor functions
332  //----------------------
333 
335  inline size_t num_connections(size_t i) const
336  {
337  if(rowStart[i] == -1) return 0;
338  else return rowEnd[i]-rowStart[i];
339  }
340 
342  size_t num_rows() const { return rowEnd.size(); }
343 
345  size_t num_cols() const { return m_numCols; }
346 
348  size_t total_num_connections() const { return nnz; }
349 
350 public:
351 
352  // Iterators
353  //---------------------------
354 
355  // a row_iterator has to suppport
356  // operator ++, operator +=, index() const, const value_type &value() const, value_type &value()
357  // a const_row_iterator has to suppport
358  // operator ++, operator +=, index() const, const value_type &value() const
359 
360 
366  {
368 #ifdef CHECK_ROW_ITERATORS
369  size_t _row;
370 #endif
371  size_t i;
372  public:
373  inline void check() const {
374 #ifdef CHECK_ROW_ITERATORS
375  A.check_row(_row, i);
376 #endif
377  }
378  row_iterator(SparseMatrix &_A, size_t row, size_t _i) : A(_A)
379 #ifdef CHECK_ROW_ITERATORS
380  , _row(row)
381 #endif
382  , i(_i) { A.add_iterator(row); }
383  row_iterator(row_iterator &&other) : A(other.A),
384 #ifdef CHECK_ROW_ITERATORS
385  _row(other._row),
386 #endif
387  i(other.i) {
388 #ifdef CHECK_ROW_ITERATORS
389  A.add_iterator(other._row);
390 #else
391  A.add_iterator(-1);
392 #endif
393  }
394  row_iterator(const row_iterator &other) : A(other.A),
395 #ifdef CHECK_ROW_ITERATORS
396  _row(other._row),
397 #endif
398  i(other.i) {
399 #ifdef CHECK_ROW_ITERATORS
400  A.add_iterator(other._row);
401 #else
402  A.add_iterator(-1);
403 #endif
404  }
406 #ifdef CHECK_ROW_ITERATORS
408 #else
409  A.remove_iterator(-1);
410 #endif
411  }
412  row_iterator *operator ->() { return this; }
413  value_type &value() { check(); return A.values[i]; }
414  size_t index() const { check(); return A.cols[i]; }
415  bool operator != (const row_iterator &o) const { return i != o.i; }
416  void operator ++ () { ++i; }
417  void operator += (int nr) { i+=nr; }
418  bool operator == (const row_iterator &other) const { return other.i == i; check(); }
419  };
421  {
422  const SparseMatrix &A;
423 #ifdef CHECK_ROW_ITERATORS
424  size_t _row; // int?
425 #endif
426  size_t i;
427  public:
428  inline void check() const {
429 #ifdef CHECK_ROW_ITERATORS
430  A.check_row(_row, i);
431 #endif
432  }
433  const_row_iterator(const SparseMatrix &_A, size_t row, size_t _i) : A(_A),
434 #ifdef CHECK_ROW_ITERATORS
435  _row(row),
436 #endif
437  i(_i) {A.add_iterator(row);}
438  const_row_iterator(const const_row_iterator &other) : A(other.A),
439 #ifdef CHECK_ROW_ITERATORS
440  _row(other._row),
441 #endif
442  i(other.i) {
443 #ifdef CHECK_ROW_ITERATORS
445 #else
446  A.add_iterator(-1);
447 #endif
448  }
450 #ifdef CHECK_ROW_ITERATORS
451  _row(other._row),
452 #endif
453  i(other.i) {
454 #ifdef CHECK_ROW_ITERATORS
456 #else
457  A.add_iterator(-1);
458 #endif
459  }
463 #ifdef CHECK_ROW_ITERATORS
465 #else
466  A.remove_iterator(-1);
467 #endif
468  }
469  const const_row_iterator *operator ->() const { return this; }
470  const value_type &value() const { check(); return A.values[i]; }
471  size_t index() const { check(); return A.cols[i]; }
472  bool operator != (const const_row_iterator &o) const { return i != o.i; }
473  void operator ++ () { ++i; }
474  void operator += (int nr) { i+=nr; }
475  bool operator == (const const_row_iterator &other) const { return other.i == i; }
476  public: // BUG
477  // int row() const{return _row;}
478  size_t idx() const{return i;}
479  };
480 
481 
482 
483 
484  row_iterator begin_row(size_t r) { return row_iterator(*this, r, rowStart[r]); }
485  row_iterator end_row(size_t r) { return row_iterator(*this, r, rowEnd[r]); }
486  const_row_iterator begin_row(size_t r) const { return const_row_iterator(*this, r, rowStart[r]); }
487  const_row_iterator end_row(size_t r) const { return const_row_iterator(*this, r, rowEnd[r]); }
488 
489  row_type get_row(size_t r) { return row_type(*this, r); }
490  const_row_type get_row(size_t r) const { return const_row_type(*this, r); }
491 
492 public:
493  // connectivity functions
494  //-------------------------
495 
496  bool has_connection(size_t r, size_t c) const
497  {
498  check_rc(r, c);
499  bool bFound;
500  get_connection(r, c, bFound);
501  return bFound;
502  }
503 
509  row_iterator get_iterator_or_next(size_t r, size_t c)
510  {
511  check_rc(r, c);
512  if(rowStart[r] == -1 || rowStart[r] == rowEnd[r])
513  return end_row(r);
514  else
515  {
516  int j=get_index_internal(r, c);
517  if(j > maxValues) return end_row(r);
518  else return row_iterator(*this, r, j);
519  }
520  }
521 
527  const_row_iterator get_connection(size_t r, size_t c, bool &bFound) const
528  {
529  check_rc(r, c);
530  int j=get_index_const(r, c);
531  if(j != -1)
532  {
533  bFound = true;
534  return const_row_iterator(*this, r, j);
535  }
536  else
537  {
538  bFound = false;
539  return end_row(r);
540  }
541  }
547  row_iterator get_connection(size_t r, size_t c, bool &bFound)
548  {
549  check_rc(r, c);
550  int j=get_index_const(r, c);
551  if(j != -1)
552  {
553  bFound = true;
554  return row_iterator(*this, r, j);
555  }
556  else
557  {
558  bFound = false;
559  return end_row(r);
560  }
561  }
562 
568  const_row_iterator get_connection(size_t r, size_t c) const
569  {
570  bool b;
571  return get_connection(r, c, b);
572  }
579  row_iterator get_connection(size_t r, size_t c)
580  {
581  check_rc(r, c);
582  assert(bNeedsValues);
583  int j=get_index(r, c);
584  return row_iterator(*this, r, j);
585  }
586 
587 
588  void defragment()
589  {
590  if(num_rows() != 0 && num_cols() != 0)
592  }
593 
594  void defragment() const
595  {
596  (const_cast<this_type*>(this))->defragment();
597  }
598 
607  void copy_crs(size_t &numRows, size_t &numCols,
608  std::vector<value_type> &argValues, std::vector<int> &argRowStart,
609  std::vector<int> &argColInd) const
610  {
611  numRows = num_rows();
612  numCols = num_cols();
613  defragment();
614  argValues = values;
615  argRowStart = rowStart;
616  argColInd = cols;
617  }
618 
628  void get_crs(size_t &numRows, size_t &numCols,
629  value_type *&pValues, size_t *pRowStart, size_t *pColInd, size_t &nnz) const
630  {
631  defragment();
632  pValues = &values[0];
633  // FIXME:
634  //pRowStart = &rowStart[0]; // assigning int * to size_t *
635  //pColInd = &cols[0]; // assigning int * to size_t *
636  UG_THROW("SparseMatrix::get_crs() needs to be fixed.");
637  numRows = num_rows();
638  numCols = num_cols();
640  }
641 
647  void get_values(std::vector<value_type> &argValues) const
648  {
649  defragment();
650  argValues = values;
651  }
652 
653 
654 public:
655  // output functions
656  //----------------------
657 
658  void print(const char * const name = NULL) const;
659  void printtype() const;
660 
661  void print_to_file(const char *filename) const;
662  void printrow(size_t row) const;
663 
664  friend std::ostream &operator<<(std::ostream &out, const SparseMatrix &m)
665  {
666  out << "SparseMatrix " //<< m.name
667  << " [ " << m.num_rows() << " x " << m.num_cols() << " ]";
668  return out;
669  }
670 
671 
672  void p() const { print(); } // for use in gdb
673  void pr(size_t row) const {printrow(row); } // for use in gdb
674 
675 
676 
677 
678 
679 private:
680  // private functions
681 
682  void add_iterator(size_t row) const
683  {
684 #ifdef CHECK_ROW_ITERATORS
685  nrOfRowIterators[row]++;
686 #endif
687  iIterators++;
688  }
689  void remove_iterator(size_t row) const
690  {
691 #ifdef CHECK_ROW_ITERATORS
692  nrOfRowIterators[row]--;
693  UG_ASSERT(nrOfRowIterators[row] >= 0, row);
694 #endif
695  iIterators--;
696  UG_ASSERT(iIterators >= 0, row);
697 
698  }
699  inline void check_row(size_t row, int i) const
700  {
701  UG_ASSERT(i < rowEnd[row] && i >= rowStart[row], "row iterator row " << row << " pos " << i << " out of bounds [" << rowStart[row] << ", " << rowEnd[row] << "]");
702  }
703  void assureValuesSize(size_t s);
704  size_t get_nnz() const { return nnz; }
705 
706 private:
707  // disallowed operations (not defined):
708  //---------------------------------------
710 
711 
712 protected:
713  int get_index_internal(size_t row, int col) const;
714  int get_index_const(int r, int c) const;
715  int get_index(int r, int c);
716  void copyToNewSize(size_t newSize)
717  {
718  copyToNewSize(newSize, num_cols());
719  }
720  void copyToNewSize(size_t newSize, size_t maxCols);
721  void check_fragmentation() const;
722  int get_nnz_max_cols(size_t maxCols);
723 
724 public: // bug
725  int col(size_t i) const{
726  assert(i<cols.size());
727  return cols[i];
728  }
729 
730 #ifndef NDEBUG
731  int iters() const{
732  return iIterators;
733  }
734 #endif
735 
736 protected:
737  std::vector<int> rowStart;
738  std::vector<int> rowEnd;
739  std::vector<int> rowMax;
740  std::vector<int> cols;
741  size_t fragmented;
742  size_t nnz;
744 
745  std::vector<value_type> values;
748  mutable int iIterators;
749 
750 #ifdef CHECK_ROW_ITERATORS
751 public:
752  mutable std::vector<int> nrOfRowIterators;
753 #endif
754 };
755 
756 
757 template<typename T>
759 {
760  enum{
762  };
763 };
764 
766 template<typename vector_t, typename matrix_t>
767 inline void MatMultTransposedAdd(vector_t &dest,
768  const number &alpha1, const vector_t &v1,
769  const number &beta1, const SparseMatrix<matrix_t> &A1, const vector_t &w1)
770 {
771  A1.axpy_transposed(dest, alpha1, v1, beta1, w1);
772 }
773 
774 
775 
776 
777 
778 
779 
780 
781 // end group cpu_algebra
783 
784 } // namespace ug
785 
786 //#include "matrixrow.h"
787 #include "sparsematrix_impl.h"
788 #include "sparsematrix_print.h"
789 
790 #endif
location name
Definition: checkpoint_util.lua:128
Definition: connection.h:40
Definition: matrixrow.h:118
Definition: matrixrow.h:65
Definition: sparsematrix.h:421
size_t idx() const
Definition: sparsematrix.h:478
void operator++()
Definition: sparsematrix.h:473
const_row_iterator & operator=(const_row_iterator &&)=delete
void check() const
Definition: sparsematrix.h:428
const SparseMatrix & A
Definition: sparsematrix.h:422
size_t i
Definition: sparsematrix.h:426
void operator+=(int nr)
Definition: sparsematrix.h:474
bool operator==(const const_row_iterator &other) const
Definition: sparsematrix.h:475
size_t _row
Definition: sparsematrix.h:424
bool operator!=(const const_row_iterator &o) const
Definition: sparsematrix.h:472
~const_row_iterator()
Definition: sparsematrix.h:462
const_row_iterator(const const_row_iterator &other)
Definition: sparsematrix.h:438
const_row_iterator & operator=(const_row_iterator const &)=delete
const const_row_iterator * operator->() const
Definition: sparsematrix.h:469
size_t index() const
Definition: sparsematrix.h:471
const_row_iterator(const_row_iterator &&other)
Definition: sparsematrix.h:449
const value_type & value() const
Definition: sparsematrix.h:470
const_row_iterator(const SparseMatrix &_A, size_t row, size_t _i)
Definition: sparsematrix.h:433
Definition: sparsematrix.h:366
row_iterator * operator->()
Definition: sparsematrix.h:412
bool operator!=(const row_iterator &o) const
Definition: sparsematrix.h:415
~row_iterator()
Definition: sparsematrix.h:405
size_t i
Definition: sparsematrix.h:371
size_t index() const
Definition: sparsematrix.h:414
void operator+=(int nr)
Definition: sparsematrix.h:417
void operator++()
Definition: sparsematrix.h:416
row_iterator(const row_iterator &other)
Definition: sparsematrix.h:394
row_iterator(SparseMatrix &_A, size_t row, size_t _i)
Definition: sparsematrix.h:378
row_iterator(row_iterator &&other)
Definition: sparsematrix.h:383
size_t _row
Definition: sparsematrix.h:369
void check() const
Definition: sparsematrix.h:373
SparseMatrix & A
Definition: sparsematrix.h:367
bool operator==(const row_iterator &other) const
Definition: sparsematrix.h:418
value_type & value()
Definition: sparsematrix.h:413
sparse matrix for big, variable sparse matrices.
Definition: sparsematrix.h:99
bool has_connection(size_t r, size_t c) const
Definition: sparsematrix.h:496
bool apply_transposed(Vector_type &res, const Vector_type &x) const
calculate res = A.T x
Definition: sparsematrix.h:194
void apply_transposed_ignore_zero_rows(vector_t &dest, const number &beta1, const vector_t &w1) const
calculated dest = beta1*A*w1 . For empty cols of A (=empty rows of A^T), dest will not be changed
Definition: sparsematrix_impl.h:375
int get_index_internal(size_t row, int col) const
Definition: sparsematrix_impl.h:553
int get_index_const(int r, int c) const
Definition: sparsematrix_impl.h:584
void axpy(vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const vector_t &w1) const
calculate dest = alpha1*v1 + beta1*A*w1 (A = this matrix)
Definition: sparsematrix_impl.h:294
std::vector< int > cols
Definition: sparsematrix.h:740
void p() const
Definition: sparsematrix.h:672
size_t nnz
Definition: sparsematrix.h:742
SparseMatrix< value_type > & operator*=(double d)
Definition: sparsematrix.h:219
void print_to_file(const char *filename) const
void check_rc(size_t r, size_t c) const
Definition: sparsematrix.h:249
void resize_and_clear(size_t newRows, size_t newCols)
resizes the SparseMatrix
Definition: sparsematrix_impl.h:91
const_row_iterator get_connection(size_t r, size_t c, bool &bFound) const
Definition: sparsematrix.h:527
void copyToNewSize(size_t newSize)
Definition: sparsematrix.h:716
void set_as_transpose_of2(const SparseMatrix< value_type > &B, double scale=1.0)
Definition: sparsematrix_impl.h:186
AlgebraicConnection< TValueType > connection
Definition: sparsematrix.h:107
const_row_type get_row(size_t r) const
Definition: sparsematrix.h:490
int col(size_t i) const
Definition: sparsematrix.h:725
void get_values(std::vector< value_type > &argValues) const
Definition: sparsematrix.h:647
SparseMatrix< value_type > & operator=(const SparseMatrix< value_type > &B)
Definition: sparsematrix.h:151
row_iterator get_connection(size_t r, size_t c)
Definition: sparsematrix.h:579
void resize_and_keep_values(size_t newRows, size_t newCols)
Definition: sparsematrix_impl.h:112
void defragment() const
Definition: sparsematrix.h:594
SparseMatrix(SparseMatrix &)
disallow copy operator
TValueType value_type
Definition: sparsematrix.h:101
void assureValuesSize(size_t s)
Definition: sparsematrix_impl.h:760
size_t num_rows() const
returns number of rows
Definition: sparsematrix.h:342
void add(const M &mat)
Definition: sparsematrix_impl.h:507
std::vector< int > rowEnd
Definition: sparsematrix.h:738
void check_row_modifiable(size_t r) const
Definition: sparsematrix.h:254
virtual ~SparseMatrix()
destructor
Definition: sparsematrix.h:118
void pr(size_t row) const
Definition: sparsematrix.h:673
const_row_iterator end_row(size_t r) const
Definition: sparsematrix.h:487
int maxValues
Definition: sparsematrix.h:746
std::vector< int > nrOfRowIterators
Definition: sparsematrix.h:752
void check_fragmentation() const
Definition: sparsematrix_impl.h:753
SparseMatrix()
constructor for empty SparseMatrix
Definition: sparsematrix_impl.h:59
size_t num_connections(size_t i) const
returns number of connections of row row.
Definition: sparsematrix.h:335
int iters() const
Definition: sparsematrix.h:731
void defragment()
Definition: sparsematrix.h:588
void mat_mult_add_row(size_t row, typename vector_t::value_type &dest, double alpha, const vector_t &v) const
calculates dest += alpha * A[row, .] v;
Definition: sparsematrix_impl.h:259
std::vector< value_type > values
Definition: sparsematrix.h:745
bool is_isolated(size_t i) const
check for isolated condition of an index
Definition: sparsematrix_impl.h:416
MatrixRow< this_type > row_type
Definition: sparsematrix.h:108
row_iterator end_row(size_t r)
Definition: sparsematrix.h:485
SparseMatrix< value_type > this_type
Definition: sparsematrix.h:104
void set_as_copy_of(const SparseMatrix< value_type > &B, double scale=1.0)
create/recreate this as a copy of SparseMatrix B
Definition: sparsematrix_impl.h:472
size_t total_num_connections() const
returns the total number of connections
Definition: sparsematrix.h:348
row_iterator get_iterator_or_next(size_t r, size_t c)
Definition: sparsematrix.h:509
const value_type & operator()(size_t r, size_t c) const
Definition: sparsematrix.h:272
void set_as_transpose_of(const SparseMatrix< value_type > &B, double scale=1.0)
write in a empty SparseMatrix (this) the transpose SparseMatrix of B.
Definition: sparsematrix_impl.h:149
friend std::ostream & operator<<(std::ostream &out, const SparseMatrix &m)
Definition: sparsematrix.h:664
void set_matrix_row(size_t row, connection *c, size_t nr)
Definition: sparsematrix_impl.h:429
bool matmul_minus(Vector_type &res, const Vector_type &x) const
calculate res -= A x
Definition: sparsematrix.h:203
void add_iterator(size_t row) const
Definition: sparsematrix.h:682
void add_matrix_row(size_t row, connection *c, size_t nr)
Definition: sparsematrix_impl.h:463
void apply_ignore_zero_rows(vector_t &dest, const number &beta1, const vector_t &w1) const
calculated dest = beta1*A*w1 . For empty rows, dest will not be changed
Definition: sparsematrix_impl.h:273
int get_index(int r, int c)
Definition: sparsematrix_impl.h:596
void check_row(size_t row, int i) const
Definition: sparsematrix.h:699
void scale(double d)
Definition: sparsematrix_impl.h:487
row_iterator get_connection(size_t r, size_t c, bool &bFound)
Definition: sparsematrix.h:547
bool apply(Vector_type &res, const Vector_type &x) const
calculate res = A x
Definition: sparsematrix.h:185
int m_numCols
Definition: sparsematrix.h:747
int iIterators
Definition: sparsematrix.h:748
size_t num_cols() const
returns the number of cols
Definition: sparsematrix.h:345
void get(M &mat) const
get local matrix
Definition: sparsematrix_impl.h:538
void remove_iterator(size_t row) const
Definition: sparsematrix.h:689
void clear_and_free()
Clears the matrix vectors and frees their memory.
Definition: sparsematrix_impl.h:72
size_t get_nnz() const
Definition: sparsematrix.h:704
@ rows_sorted
Definition: sparsematrix.h:102
void get_crs(size_t &numRows, size_t &numCols, value_type *&pValues, size_t *pRowStart, size_t *pColInd, size_t &nnz) const
Definition: sparsematrix.h:628
size_t fragmented
Definition: sparsematrix.h:741
std::vector< int > rowMax
Definition: sparsematrix.h:739
row_iterator begin_row(size_t r)
Definition: sparsematrix.h:484
void clear_retain_structure()
Definition: sparsematrix_impl.h:142
const_row_iterator get_connection(size_t r, size_t c) const
Definition: sparsematrix.h:568
void axpy_transposed(vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const vector_t &w1) const
calculate dest = alpha1*v1 + beta1*A^T*w1 (A = this matrix)
Definition: sparsematrix_impl.h:344
int get_nnz_max_cols(size_t maxCols)
Definition: sparsematrix_impl.h:770
void copy_crs(size_t &numRows, size_t &numCols, std::vector< value_type > &argValues, std::vector< int > &argRowStart, std::vector< int > &argColInd) const
Definition: sparsematrix.h:607
bool bNeedsValues
Definition: sparsematrix.h:743
row_type get_row(size_t r)
Definition: sparsematrix.h:489
void set(const M &mat)
set local matrix
Definition: sparsematrix_impl.h:523
const_row_iterator begin_row(size_t r) const
Definition: sparsematrix.h:486
ConstMatrixRow< this_type > const_row_type
Definition: sparsematrix.h:109
std::vector< int > rowStart
Definition: sparsematrix.h:737
void print(const char *const name=NULL) const
Definition: sparsematrix_print.h:47
void printtype() const
Definition: sparsematrix_print.h:72
void printrow(size_t row) const
Definition: sparsematrix_print.h:58
number alpha
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
#define UG_THROW(msg)
Definition: error.h:57
double number
Definition: types.h:124
the ug namespace
bool MatMultTransposedAdd(vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const matrix_t &A1, const vector_t &w1)
calculates dest = alpha1*v1 + beta1 * A1 *w1;
Definition: operations_mat.h:121
@ MATRIX_USE_ROW_FUNCTIONS
Definition: matrix_algebra_types.h:68
#define CHECK_ROW_ITERATORS
Definition: sparsematrix.h:50
T value_type
Definition: sparsematrix_interface.h:2
Definition: matrix_algebra_types.h:79
static const int type
Definition: matrix_algebra_types.h:80