MFEM  v4.1.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
sparsemat.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef MFEM_SPARSEMAT_HPP
13 #define MFEM_SPARSEMAT_HPP
14 
15 // Data types for sparse matrix
16 
17 #include "../general/mem_alloc.hpp"
18 #include "../general/mem_manager.hpp"
19 #include "../general/device.hpp"
20 #include "../general/table.hpp"
21 #include "../general/globals.hpp"
22 #include "densemat.hpp"
23 
24 namespace mfem
25 {
26 
27 class
28 #if defined(__alignas_is_defined)
29  alignas(double)
30 #endif
31  RowNode
32 {
33 public:
34  double Value;
35  RowNode *Prev;
36  int Column;
37 };
38 
39 /// Data type sparse matrix
41 {
42 protected:
43  /// @name Arrays used by the CSR storage format.
44  /** */
45  ///@{
46  /// @brief %Array with size (#height+1) containing the row offsets.
47  /** The data for row r, 0 <= r < height, is at offsets j, I[r] <= j < I[r+1].
48  The offsets, j, are indices in the #J and #A arrays. The first entry in
49  this array is always zero, I[0] = 0, and the last entry, I[height], gives
50  the total number of entries stored (at a minimum, all nonzeros must be
51  represented) in the sparse matrix. */
53  /** @brief %Array with size #I[#height], containing the column indices for
54  all matrix entries, as indexed by the #I array. */
56  /** @brief %Array with size #I[#height], containing the actual entries of the
57  sparse matrix, as indexed by the #I array. */
59  ///@}
60 
61  /** @brief %Array of linked lists, one for every row. This array represents
62  the linked list (LIL) storage format. */
63  RowNode **Rows;
64 
65  mutable int current_row;
66  mutable int* ColPtrJ;
67  mutable RowNode ** ColPtrNode;
68 
69  /// Transpose of A. Owned. Used to perform MultTranspose() on devices.
70  mutable SparseMatrix *At;
71 
72 #ifdef MFEM_USE_MEMALLOC
75 #endif
76 
77  /// Are the columns sorted already.
78  bool isSorted;
79 
80  void Destroy(); // Delete all owned data
81  void SetEmpty(); // Init all entries with empty values
82 
83 public:
84  /// Create an empty SparseMatrix.
86 
87  /** @brief Create a sparse matrix with flexible sparsity structure using a
88  row-wise linked list (LIL) format. */
89  /** New entries are added as needed by methods like AddSubMatrix(),
90  SetSubMatrix(), etc. Calling Finalize() will convert the SparseMatrix to
91  the more compact compressed sparse row (CSR) format. */
92  explicit SparseMatrix(int nrows, int ncols = -1);
93 
94  /** @brief Create a sparse matrix in CSR format. Ownership of @a i, @a j, and
95  @a data is transferred to the SparseMatrix. */
96  SparseMatrix(int *i, int *j, double *data, int m, int n);
97 
98  /** @brief Create a sparse matrix in CSR format. Ownership of @a i, @a j, and
99  @a data is optionally transferred to the SparseMatrix. */
100  /** If the parameter @a data is NULL, then the internal #A array is allocated
101  by this constructor (initializing it with zeros and taking ownership,
102  regardless of the parameter @a owna). */
103  SparseMatrix(int *i, int *j, double *data, int m, int n, bool ownij,
104  bool owna, bool issorted);
105 
106  /** @brief Create a sparse matrix in CSR format where each row has space
107  allocated for exactly @a rowsize entries. */
108  /** SetRow() can then be called or the #I, #J, #A arrays can be used
109  directly. */
110  SparseMatrix(int nrows, int ncols, int rowsize);
111 
112  /// Copy constructor (deep copy).
113  /** If @a mat is finalized and @a copy_graph is false, the #I and #J arrays
114  will use a shallow copy (copy the pointers only) without transferring
115  ownership. */
116  SparseMatrix(const SparseMatrix &mat, bool copy_graph = true);
117 
118  /// Create a SparseMatrix with diagonal @a v, i.e. A = Diag(v)
119  SparseMatrix(const Vector & v);
120 
121 
122  /// Assignment operator: deep copy
123  SparseMatrix& operator=(const SparseMatrix &rhs);
124 
125  /** @brief Clear the contents of the SparseMatrix and make it a reference to
126  @a master */
127  /** After this call, the matrix will point to the same data as @a master but
128  it will not own its data. The @a master must be finalized. */
129  void MakeRef(const SparseMatrix &master);
130 
131  /// For backward compatibility, define Size() to be synonym of Height().
132  int Size() const { return Height(); }
133 
134  /// Clear the contents of the SparseMatrix.
135  void Clear() { Destroy(); SetEmpty(); }
136 
137  /// Check if the SparseMatrix is empty.
138  bool Empty() const { return (A == NULL) && (Rows == NULL); }
139 
140  /// Return the array #I.
141  inline int *GetI() { return I; }
142  /// Return the array #I, const version.
143  inline const int *GetI() const { return I; }
144 
145  /// Return the array #J.
146  inline int *GetJ() { return J; }
147  /// Return the array #J, const version.
148  inline const int *GetJ() const { return J; }
149 
150  /// Return the element data, i.e. the array #A.
151  inline double *GetData() { return A; }
152  /// Return the element data, i.e. the array #A, const version.
153  inline const double *GetData() const { return A; }
154 
155  // Memory access methods for the #I array.
156  Memory<int> &GetMemoryI() { return I; }
157  const Memory<int> &GetMemoryI() const { return I; }
158  const int *ReadI(bool on_dev = true) const
159  { return mfem::Read(I, Height()+1, on_dev); }
160  int *WriteI(bool on_dev = true)
161  { return mfem::Write(I, Height()+1, on_dev); }
162  int *ReadWriteI(bool on_dev = true)
163  { return mfem::ReadWrite(I, Height()+1, on_dev); }
164  const int *HostReadI() const
165  { return mfem::Read(I, Height()+1, false); }
166  int *HostWriteI()
167  { return mfem::Write(I, Height()+1, false); }
169  { return mfem::ReadWrite(I, Height()+1, false); }
170 
171  // Memory access methods for the #J array.
172  Memory<int> &GetMemoryJ() { return J; }
173  const Memory<int> &GetMemoryJ() const { return J; }
174  const int *ReadJ(bool on_dev = true) const
175  { return mfem::Read(J, J.Capacity(), on_dev); }
176  int *WriteJ(bool on_dev = true)
177  { return mfem::Write(J, J.Capacity(), on_dev); }
178  int *ReadWriteJ(bool on_dev = true)
179  { return mfem::ReadWrite(J, J.Capacity(), on_dev); }
180  const int *HostReadJ() const
181  { return mfem::Read(J, J.Capacity(), false); }
182  int *HostWriteJ()
183  { return mfem::Write(J, J.Capacity(), false); }
185  { return mfem::ReadWrite(J, J.Capacity(), false); }
186 
187  // Memory access methods for the #A array.
189  const Memory<double> &GetMemoryData() const { return A; }
190  const double *ReadData(bool on_dev = true) const
191  { return mfem::Read(A, A.Capacity(), on_dev); }
192  double *WriteData(bool on_dev = true)
193  { return mfem::Write(A, A.Capacity(), on_dev); }
194  double *ReadWriteData(bool on_dev = true)
195  { return mfem::ReadWrite(A, A.Capacity(), on_dev); }
196  const double *HostReadData() const
197  { return mfem::Read(A, A.Capacity(), false); }
198  double *HostWriteData()
199  { return mfem::Write(A, A.Capacity(), false); }
201  { return mfem::ReadWrite(A, A.Capacity(), false); }
202 
203  /// Returns the number of elements in row @a i.
204  int RowSize(const int i) const;
205 
206  /// Returns the maximum number of elements among all rows.
207  int MaxRowSize() const;
208 
209  /// Return a pointer to the column indices in a row.
210  int *GetRowColumns(const int row);
211  /// Return a pointer to the column indices in a row, const version.
212  const int *GetRowColumns(const int row) const;
213 
214  /// Return a pointer to the entries in a row.
215  double *GetRowEntries(const int row);
216  /// Return a pointer to the entries in a row, const version.
217  const double *GetRowEntries(const int row) const;
218 
219  /// Change the width of a SparseMatrix.
220  /*!
221  * If width_ = -1 (DEFAULT), this routine will set the new width
222  * to the actual Width of the matrix awidth = max(J) + 1.
223  * Values 0 <= width_ < awidth are not allowed (error check in Debug Mode only)
224  *
225  * This method can be called for matrices finalized or not.
226  */
227  void SetWidth(int width_ = -1);
228 
229  /// Returns the actual Width of the matrix.
230  /*! This method can be called for matrices finalized or not. */
231  int ActualWidth() const;
232 
233  /// Sort the column indices corresponding to each row.
234  void SortColumnIndices();
235 
236  /** @brief Move the diagonal entry to the first position in each row,
237  preserving the order of the rest of the columns. */
238  void MoveDiagonalFirst();
239 
240  /// Returns reference to a_{ij}.
241  virtual double &Elem(int i, int j);
242 
243  /// Returns constant reference to a_{ij}.
244  virtual const double &Elem(int i, int j) const;
245 
246  /// Returns reference to A[i][j].
247  double &operator()(int i, int j);
248 
249  /// Returns reference to A[i][j].
250  const double &operator()(int i, int j) const;
251 
252  /// Returns the Diagonal of A
253  void GetDiag(Vector & d) const;
254 
255  /// Produces a DenseMatrix from a SparseMatrix
256  DenseMatrix *ToDenseMatrix() const;
257 
258  /// Produces a DenseMatrix from a SparseMatrix
259  void ToDenseMatrix(DenseMatrix & B) const;
260 
261  virtual MemoryClass GetMemoryClass() const
262  {
263  return Finalized() ?
265  }
266 
267  /// Matrix vector multiplication.
268  virtual void Mult(const Vector &x, Vector &y) const;
269 
270  /// y += A * x (default) or y += a * A * x
271  void AddMult(const Vector &x, Vector &y, const double a = 1.0) const;
272 
273  /// Multiply a vector with the transposed matrix. y = At * x
274  void MultTranspose(const Vector &x, Vector &y) const;
275 
276  /// y += At * x (default) or y += a * At * x
277  void AddMultTranspose(const Vector &x, Vector &y,
278  const double a = 1.0) const;
279 
280  /** @brief Build and store internally the transpose of this matrix which will
281  be used in the methods AddMultTranspose() and MultTranspose(). */
282  /** If this method has been called, the internal transpose matrix will be
283  used to perform the action of the transpose matrix in AddMultTranspose(),
284  and MultTranspose().
285 
286  Warning: any changes in this matrix will invalidate the internal
287  transpose. To rebuild the transpose, call ResetTranspose() followed by a
288  call to this method. If the internal transpose is already built, this
289  method has no effect.
290 
291  When any non-default backend is enabled, i.e. Device::IsEnabled() is
292  true, the methods AddMultTranspose(), and MultTranspose(), require the
293  internal transpose to be built. If that is not the case (i.e. the
294  internal transpose is not built), these methods will raise an error with
295  an appropriate message pointing to this method. When using the default
296  backend, calling this method is optional.
297 
298  This method can only be used when the sparse matrix is finalized. */
299  void BuildTranspose() const;
300 
301  /** Reset (destroy) the internal transpose matrix. See BuildTranspose() for
302  more details. */
303  void ResetTranspose() const;
304 
305  void PartMult(const Array<int> &rows, const Vector &x, Vector &y) const;
306  void PartAddMult(const Array<int> &rows, const Vector &x, Vector &y,
307  const double a=1.0) const;
308 
309  /// y = A * x, treating all entries as booleans (zero=false, nonzero=true).
310  /** The actual values stored in the data array, #A, are not used - this means
311  and that all entries in the sparsity pattern are considered to be true by
312  this method. */
313  void BooleanMult(const Array<int> &x, Array<int> &y) const;
314 
315  /// y = At * x, treating all entries as booleans (zero=false, nonzero=true).
316  /** The actual values stored in the data array, #A, are not used - this means
317  and that all entries in the sparsity pattern are considered to be true by
318  this method. */
319  void BooleanMultTranspose(const Array<int> &x, Array<int> &y) const;
320 
321  /// Compute y^t A x
322  double InnerProduct(const Vector &x, const Vector &y) const;
323 
324  /// For all i compute \f$ x_i = \sum_j A_{ij} \f$
325  void GetRowSums(Vector &x) const;
326  /// For i = irow compute \f$ x_i = \sum_j | A_{i, j} | \f$
327  double GetRowNorml1(int irow) const;
328 
329  /// This virtual method is not supported: it always returns NULL.
330  virtual MatrixInverse *Inverse() const;
331 
332  /// Eliminates a column from the transpose matrix.
333  void EliminateRow(int row, const double sol, Vector &rhs);
334 
335  /// Eliminates a row from the matrix.
336  /*!
337  * - If @a dpolicy = #DIAG_ZERO, all the entries in the row will be set to 0.
338  * - If @a dpolicy = #DIAG_ONE (matrix must be square), the diagonal entry
339  * will be set equal to 1 and all other entries in the row to 0.
340  * - The policy #DIAG_KEEP is not supported.
341  */
342  void EliminateRow(int row, DiagonalPolicy dpolicy = DIAG_ZERO);
343 
344  /// Eliminates the column @a col from the matrix.
345  /** - If @a dpolicy = #DIAG_ZERO, all entries in the column will be set to 0.
346  - If @a dpolicy = #DIAG_ONE (matrix must be square), the diagonal entry
347  will be set equal to 1 and all other entries in the column to 0.
348  - The policy #DIAG_KEEP is not supported. */
349  void EliminateCol(int col, DiagonalPolicy dpolicy = DIAG_ZERO);
350 
351  /// Eliminate all columns i for which @a cols[i] != 0.
352  /** Elimination of a column means that all entries in the column are set to
353  zero. In addition, if the pointers @a x and @a b are not NULL, the
354  eliminated matrix entries are multiplied by the corresponding solution
355  value in @a *x and subtracted from the r.h.s. vector, @a *b. */
356  void EliminateCols(const Array<int> &cols, const Vector *x = NULL,
357  Vector *b = NULL);
358 
359  /** @brief Similar to EliminateCols + save the eliminated entries into
360  @a Ae so that (*this) + Ae is equal to the original matrix. */
361  void EliminateCols(const Array<int> &col_marker, SparseMatrix &Ae);
362 
363  /// Eliminate row @a rc and column @a rc and modify the @a rhs using @a sol.
364  /** Eliminates the column @a rc to the @a rhs, deletes the row @a rc and
365  replaces the element (rc,rc) with 1.0; assumes that element (i,rc)
366  is assembled if and only if the element (rc,i) is assembled.
367  By default, elements (rc,rc) are set to 1.0, although this behavior
368  can be adjusted by changing the @a dpolicy parameter. */
369  void EliminateRowCol(int rc, const double sol, Vector &rhs,
370  DiagonalPolicy dpolicy = DIAG_ONE);
371 
372  /** @brief Similar to
373  EliminateRowCol(int, const double, Vector &, DiagonalPolicy), but
374  multiple values for eliminated unknowns are accepted, and accordingly
375  multiple right-hand-sides are used. */
376  void EliminateRowColMultipleRHS(int rc, const Vector &sol,
377  DenseMatrix &rhs,
378  DiagonalPolicy dpolicy = DIAG_ONE);
379 
380  /// Perform elimination and set the diagonal entry to the given value
381  void EliminateRowColDiag(int rc, double value);
382 
383  /// Eliminate row @a rc and column @a rc.
384  void EliminateRowCol(int rc, DiagonalPolicy dpolicy = DIAG_ONE);
385 
386  /** @brief Similar to EliminateRowCol(int, DiagonalPolicy) + save the
387  eliminated entries into @a Ae so that (*this) + Ae is equal to the
388  original matrix */
389  void EliminateRowCol(int rc, SparseMatrix &Ae,
390  DiagonalPolicy dpolicy = DIAG_ONE);
391 
392  /// If a row contains only one diag entry of zero, set it to 1.
393  void SetDiagIdentity();
394  /// If a row contains only zeros, set its diagonal to 1.
395  virtual void EliminateZeroRows(const double threshold = 1e-12);
396 
397  /// Gauss-Seidel forward and backward iterations over a vector x.
398  void Gauss_Seidel_forw(const Vector &x, Vector &y) const;
399  void Gauss_Seidel_back(const Vector &x, Vector &y) const;
400 
401  /// Determine appropriate scaling for Jacobi iteration
402  double GetJacobiScaling() const;
403  /** One scaled Jacobi iteration for the system A x = b.
404  x1 = x0 + sc D^{-1} (b - A x0) where D is the diag of A. */
405  void Jacobi(const Vector &b, const Vector &x0, Vector &x1, double sc) const;
406 
407  void DiagScale(const Vector &b, Vector &x, double sc = 1.0) const;
408 
409  /** x1 = x0 + sc D^{-1} (b - A x0) where \f$ D_{ii} = \sum_j |A_{ij}| \f$. */
410  void Jacobi2(const Vector &b, const Vector &x0, Vector &x1,
411  double sc = 1.0) const;
412 
413  /** x1 = x0 + sc D^{-1} (b - A x0) where \f$ D_{ii} = \sum_j A_{ij} \f$. */
414  void Jacobi3(const Vector &b, const Vector &x0, Vector &x1,
415  double sc = 1.0) const;
416 
417  /** @brief Finalize the matrix initialization, switching the storage format
418  from LIL to CSR. */
419  /** This method should be called once, after the matrix has been initialized.
420  Internally, this method converts the matrix from row-wise linked list
421  (LIL) format into CSR (compressed sparse row) format. */
422  virtual void Finalize(int skip_zeros = 1) { Finalize(skip_zeros, false); }
423 
424  /// A slightly more general version of the Finalize(int) method.
425  void Finalize(int skip_zeros, bool fix_empty_rows);
426 
427  /// Returns whether or not CSR format has been finalized.
428  bool Finalized() const { return !A.Empty(); }
429  /// Returns whether or not the columns are sorted.
430  bool ColumnsAreSorted() const { return isSorted; }
431 
432  /** @brief Remove entries smaller in absolute value than a given tolerance
433  @a tol. If @a fix_empty_rows is true, a zero value is inserted in the
434  diagonal entry (for square matrices only) */
435  void Threshold(double tol, bool fix_empty_rows = false);
436 
437  /** Split the matrix into M x N blocks of sparse matrices in CSR format.
438  The 'blocks' array is M x N (i.e. M and N are determined by its
439  dimensions) and its entries are overwritten by the new blocks. */
440  void GetBlocks(Array2D<SparseMatrix *> &blocks) const;
441 
442  void GetSubMatrix(const Array<int> &rows, const Array<int> &cols,
443  DenseMatrix &subm) const;
444 
445  /** @brief Initialize the SparseMatrix for fast access to the entries of the
446  given @a row which becomes the "current row". */
447  /** Fast access to the entries of the "current row" can be performed using
448  the methods: SearchRow(const int), _Add_(const int, const double),
449  _Set_(const int, const double), and _Get_(const int). */
450  inline void SetColPtr(const int row) const;
451  /** @brief Reset the "current row" set by calling SetColPtr(). This method
452  must be called between any two calls to SetColPtr(). */
453  inline void ClearColPtr() const;
454  /// Perform a fast search for an entry in the "current row". See SetColPtr().
455  /** If the matrix is not finalized and the entry is not found in the
456  SparseMatrix, it will be added to the sparsity pattern initialized with
457  zero. If the matrix is finalized and the entry is not found, an error
458  will be generated. */
459  inline double &SearchRow(const int col);
460  /// Add a value to an entry in the "current row". See SetColPtr().
461  inline void _Add_(const int col, const double a)
462  { SearchRow(col) += a; }
463  /// Set an entry in the "current row". See SetColPtr().
464  inline void _Set_(const int col, const double a)
465  { SearchRow(col) = a; }
466  /// Read the value of an entry in the "current row". See SetColPtr().
467  inline double _Get_(const int col) const;
468 
469  inline double &SearchRow(const int row, const int col);
470  inline void _Add_(const int row, const int col, const double a)
471  { SearchRow(row, col) += a; }
472  inline void _Set_(const int row, const int col, const double a)
473  { SearchRow(row, col) = a; }
474 
475  void Set(const int i, const int j, const double a);
476  void Add(const int i, const int j, const double a);
477 
478  void SetSubMatrix(const Array<int> &rows, const Array<int> &cols,
479  const DenseMatrix &subm, int skip_zeros = 1);
480 
481  void SetSubMatrixTranspose(const Array<int> &rows, const Array<int> &cols,
482  const DenseMatrix &subm, int skip_zeros = 1);
483 
484  void AddSubMatrix(const Array<int> &rows, const Array<int> &cols,
485  const DenseMatrix &subm, int skip_zeros = 1);
486 
487  bool RowIsEmpty(const int row) const;
488 
489  /// Extract all column indices and values from a given row.
490  /** If the matrix is finalized (i.e. in CSR format), @a cols and @a srow will
491  simply be references to the specific portion of the #J and #A arrays.
492  As required by the AbstractSparseMatrix interface this method returns:
493  - 0, if @a cols and @a srow are copies of the values in the matrix, i.e.
494  when the matrix is open.
495  - 1, if @a cols and @a srow are views of the values in the matrix, i.e.
496  when the matrix is finalized.
497  @warning This method breaks the const-ness when the matrix is finalized
498  because it gives write access to the #J and #A arrays. */
499  virtual int GetRow(const int row, Array<int> &cols, Vector &srow) const;
500 
501  void SetRow(const int row, const Array<int> &cols, const Vector &srow);
502  void AddRow(const int row, const Array<int> &cols, const Vector &srow);
503 
504  void ScaleRow(const int row, const double scale);
505  /// this = diag(sl) * this;
506  void ScaleRows(const Vector & sl);
507  /// this = this * diag(sr);
508  void ScaleColumns(const Vector & sr);
509 
510  /** @brief Add the sparse matrix 'B' to '*this'. This operation will cause an
511  error if '*this' is finalized and 'B' has larger sparsity pattern. */
513 
514  /** @brief Add the sparse matrix 'B' scaled by the scalar 'a' into '*this'.
515  Only entries in the sparsity pattern of '*this' are added. */
516  void Add(const double a, const SparseMatrix &B);
517 
518  SparseMatrix &operator=(double a);
519 
520  SparseMatrix &operator*=(double a);
521 
522  /// Prints matrix to stream out.
523  void Print(std::ostream &out = mfem::out, int width_ = 4) const;
524 
525  /// Prints matrix in matlab format.
526  void PrintMatlab(std::ostream &out = mfem::out) const;
527 
528  /// Prints matrix in Matrix Market sparse format.
529  void PrintMM(std::ostream &out = mfem::out) const;
530 
531  /// Prints matrix to stream out in hypre_CSRMatrix format.
532  void PrintCSR(std::ostream &out) const;
533 
534  /// Prints a sparse matrix to stream out in CSR format.
535  void PrintCSR2(std::ostream &out) const;
536 
537  /// Print various sparse matrix statistics.
538  void PrintInfo(std::ostream &out) const;
539 
540  /// Returns max_{i,j} |(i,j)-(j,i)| for a finalized matrix
541  double IsSymmetric() const;
542 
543  /// (*this) = 1/2 ((*this) + (*this)^t)
544  void Symmetrize();
545 
546  /// Returns the number of the nonzero elements in the matrix
547  virtual int NumNonZeroElems() const;
548 
549  double MaxNorm() const;
550 
551  /// Count the number of entries with |a_ij| <= tol.
552  int CountSmallElems(double tol) const;
553 
554  /// Count the number of entries that are NOT finite, i.e. Inf or Nan.
555  int CheckFinite() const;
556 
557  /// Set the graph ownership flag (I and J arrays).
558  void SetGraphOwner(bool ownij)
559  { I.SetHostPtrOwner(ownij); J.SetHostPtrOwner(ownij); }
560 
561  /// Set the data ownership flag (A array).
562  void SetDataOwner(bool owna) { A.SetHostPtrOwner(owna); }
563 
564  /// Get the graph ownership flag (I and J arrays).
565  bool OwnsGraph() const { return I.OwnsHostPtr() && J.OwnsHostPtr(); }
566 
567  /// Get the data ownership flag (A array).
568  bool OwnsData() const { return A.OwnsHostPtr(); }
569 
570  /// Lose the ownership of the graph (I, J) and data (A) arrays.
571  void LoseData() { SetGraphOwner(false); SetDataOwner(false); }
572 
573  void Swap(SparseMatrix &other);
574 
575  /// Destroys sparse matrix.
576  virtual ~SparseMatrix() { Destroy(); }
577 
578  Type GetType() const { return MFEM_SPARSEMAT; }
579 };
580 
581 /// Applies f() to each element of the matrix (after it is finalized).
582 void SparseMatrixFunction(SparseMatrix &S, double (*f)(double));
583 
584 
585 /// Transpose of a sparse matrix. A must be finalized.
586 SparseMatrix *Transpose(const SparseMatrix &A);
587 /// Transpose of a sparse matrix. A does not need to be a CSR matrix.
588 SparseMatrix *TransposeAbstractSparseMatrix (const AbstractSparseMatrix &A,
589  int useActualWidth);
590 
591 /// Matrix product A.B.
592 /** If @a OAB is not NULL, we assume it has the structure of A.B and store the
593  result in @a OAB. If @a OAB is NULL, we create a new SparseMatrix to store
594  the result and return a pointer to it.
595 
596  All matrices must be finalized. */
597 SparseMatrix *Mult(const SparseMatrix &A, const SparseMatrix &B,
598  SparseMatrix *OAB = NULL);
599 
600 /// C = A^T B
601 SparseMatrix *TransposeMult(const SparseMatrix &A, const SparseMatrix &B);
602 
603 /// Matrix product of sparse matrices. A and B do not need to be CSR matrices
604 SparseMatrix *MultAbstractSparseMatrix (const AbstractSparseMatrix &A,
605  const AbstractSparseMatrix &B);
606 
607 /// Matrix product A.B
608 DenseMatrix *Mult(const SparseMatrix &A, DenseMatrix &B);
609 
610 /// RAP matrix product (with R=P^T)
611 DenseMatrix *RAP(const SparseMatrix &A, DenseMatrix &P);
612 
613 /// RAP matrix product (with R=P^T)
614 DenseMatrix *RAP(DenseMatrix &A, const SparseMatrix &P);
615 
616 /** RAP matrix product (with P=R^T). ORAP is like OAB above.
617  All matrices must be finalized. */
618 SparseMatrix *RAP(const SparseMatrix &A, const SparseMatrix &R,
619  SparseMatrix *ORAP = NULL);
620 
621 /// General RAP with given R^T, A and P
622 SparseMatrix *RAP(const SparseMatrix &Rt, const SparseMatrix &A,
623  const SparseMatrix &P);
624 
625 /// Matrix multiplication A^t D A. All matrices must be finalized.
626 SparseMatrix *Mult_AtDA(const SparseMatrix &A, const Vector &D,
627  SparseMatrix *OAtDA = NULL);
628 
629 
630 /// Matrix addition result = A + B.
631 SparseMatrix * Add(const SparseMatrix & A, const SparseMatrix & B);
632 /// Matrix addition result = a*A + b*B
633 SparseMatrix * Add(double a, const SparseMatrix & A, double b,
634  const SparseMatrix & B);
635 /// Matrix addition result = sum_i A_i
636 SparseMatrix * Add(Array<SparseMatrix *> & Ai);
637 
638 /// B += alpha * A
639 void Add(const SparseMatrix &A, double alpha, DenseMatrix &B);
640 
641 /// Produces a block matrix with blocks A_{ij}*B
642 DenseMatrix *OuterProduct(const DenseMatrix &A, const DenseMatrix &B);
643 
644 /// Produces a block matrix with blocks A_{ij}*B
645 SparseMatrix *OuterProduct(const DenseMatrix &A, const SparseMatrix &B);
646 
647 /// Produces a block matrix with blocks A_{ij}*B
648 SparseMatrix *OuterProduct(const SparseMatrix &A, const DenseMatrix &B);
649 
650 /// Produces a block matrix with blocks A_{ij}*B
651 SparseMatrix *OuterProduct(const SparseMatrix &A, const SparseMatrix &B);
652 
653 
654 // Inline methods
655 
656 inline void SparseMatrix::SetColPtr(const int row) const
657 {
658  if (Rows)
659  {
660  if (ColPtrNode == NULL)
661  {
662  ColPtrNode = new RowNode *[width];
663  for (int i = 0; i < width; i++)
664  {
665  ColPtrNode[i] = NULL;
666  }
667  }
668  for (RowNode *node_p = Rows[row]; node_p != NULL; node_p = node_p->Prev)
669  {
670  ColPtrNode[node_p->Column] = node_p;
671  }
672  }
673  else
674  {
675  if (ColPtrJ == NULL)
676  {
677  ColPtrJ = new int[width];
678  for (int i = 0; i < width; i++)
679  {
680  ColPtrJ[i] = -1;
681  }
682  }
683  for (int j = I[row], end = I[row+1]; j < end; j++)
684  {
685  ColPtrJ[J[j]] = j;
686  }
687  }
688  current_row = row;
689 }
690 
691 inline void SparseMatrix::ClearColPtr() const
692 {
693  if (Rows)
694  {
695  for (RowNode *node_p = Rows[current_row]; node_p != NULL;
696  node_p = node_p->Prev)
697  {
698  ColPtrNode[node_p->Column] = NULL;
699  }
700  }
701  else
702  {
703  for (int j = I[current_row], end = I[current_row+1]; j < end; j++)
704  {
705  ColPtrJ[J[j]] = -1;
706  }
707  }
708 }
709 
710 inline double &SparseMatrix::SearchRow(const int col)
711 {
712  if (Rows)
713  {
714  RowNode *node_p = ColPtrNode[col];
715  if (node_p == NULL)
716  {
717 #ifdef MFEM_USE_MEMALLOC
718  node_p = NodesMem->Alloc();
719 #else
720  node_p = new RowNode;
721 #endif
722  node_p->Prev = Rows[current_row];
723  node_p->Column = col;
724  node_p->Value = 0.0;
725  Rows[current_row] = ColPtrNode[col] = node_p;
726  }
727  return node_p->Value;
728  }
729  else
730  {
731  const int j = ColPtrJ[col];
732  MFEM_VERIFY(j != -1, "Entry for column " << col << " is not allocated.");
733  return A[j];
734  }
735 }
736 
737 inline double SparseMatrix::_Get_(const int col) const
738 {
739  if (Rows)
740  {
741  RowNode *node_p = ColPtrNode[col];
742  return (node_p == NULL) ? 0.0 : node_p->Value;
743  }
744  else
745  {
746  const int j = ColPtrJ[col];
747  return (j == -1) ? 0.0 : A[j];
748  }
749 }
750 
751 inline double &SparseMatrix::SearchRow(const int row, const int col)
752 {
753  if (Rows)
754  {
755  RowNode *node_p;
756 
757  for (node_p = Rows[row]; 1; node_p = node_p->Prev)
758  {
759  if (node_p == NULL)
760  {
761 #ifdef MFEM_USE_MEMALLOC
762  node_p = NodesMem->Alloc();
763 #else
764  node_p = new RowNode;
765 #endif
766  node_p->Prev = Rows[row];
767  node_p->Column = col;
768  node_p->Value = 0.0;
769  Rows[row] = node_p;
770  break;
771  }
772  else if (node_p->Column == col)
773  {
774  break;
775  }
776  }
777  return node_p->Value;
778  }
779  else
780  {
781  int *Ip = I+row, *Jp = J;
782  for (int k = Ip[0], end = Ip[1]; k < end; k++)
783  {
784  if (Jp[k] == col)
785  {
786  return A[k];
787  }
788  }
789  MFEM_ABORT("Could not find entry for row = " << row << ", col = " << col);
790  }
791  return A[0];
792 }
793 
794 /// Specialization of the template function Swap<> for class SparseMatrix
795 template<> inline void Swap<SparseMatrix>(SparseMatrix &a, SparseMatrix &b)
796 {
797  a.Swap(b);
798 }
799 
800 } // namespace mfem
801 
802 #endif
RowNode ** ColPtrNode
Definition: sparsemat.hpp:67
Memory< int > I
Array with size (height+1) containing the row offsets.
Definition: sparsemat.hpp:52
Elem * Alloc()
Definition: mem_alloc.hpp:157
void _Add_(const int row, const int col, const double a)
Definition: sparsemat.hpp:470
double GetJacobiScaling() const
Determine appropriate scaling for Jacobi iteration.
Definition: sparsemat.cpp:2080
int CheckFinite() const
Count the number of entries that are NOT finite, i.e. Inf or Nan.
Definition: sparsemat.cpp:1237
SparseMatrix * TransposeAbstractSparseMatrix(const AbstractSparseMatrix &A, int useActualWidth)
Transpose of a sparse matrix. A does not need to be a CSR matrix.
Definition: sparsemat.cpp:3036
int RowSize(const int i) const
Returns the number of elements in row i.
Definition: sparsemat.cpp:255
virtual int NumNonZeroElems() const
Returns the number of the nonzero elements in the matrix.
Definition: sparsemat.cpp:1162
void Add(const int i, const int j, const double a)
Definition: sparsemat.cpp:2281
void _Add_(const int col, const double a)
Add a value to an entry in the &quot;current row&quot;. See SetColPtr().
Definition: sparsemat.hpp:461
void BuildTranspose() const
Build and store internally the transpose of this matrix which will be used in the methods AddMultTran...
Definition: sparsemat.cpp:671
void Clear()
Clear the contents of the SparseMatrix.
Definition: sparsemat.hpp:135
void EliminateCol(int col, DiagonalPolicy dpolicy=DIAG_ZERO)
Eliminates the column col from the matrix.
Definition: sparsemat.cpp:1314
SparseMatrix * At
Transpose of A. Owned. Used to perform MultTranspose() on devices.
Definition: sparsemat.hpp:70
Type GetType() const
Definition: sparsemat.hpp:578
static MemoryClass GetHostMemoryClass()
Get the current Host MemoryClass. This is the MemoryClass used by most MFEM host Memory objects...
Definition: device.hpp:244
bool ColumnsAreSorted() const
Returns whether or not the columns are sorted.
Definition: sparsemat.hpp:430
void _Set_(const int row, const int col, const double a)
Definition: sparsemat.hpp:472
HypreParMatrix * RAP(const HypreParMatrix *A, const HypreParMatrix *P)
Returns the matrix P^t * A * P.
Definition: hypre.cpp:1636
void DiagScale(const Vector &b, Vector &x, double sc=1.0) const
Definition: sparsemat.cpp:2144
virtual void Finalize(int skip_zeros=1)
Finalize the matrix initialization, switching the storage format from LIL to CSR. ...
Definition: sparsemat.hpp:422
void MakeRef(const SparseMatrix &master)
Clear the contents of the SparseMatrix and make it a reference to master.
Definition: sparsemat.cpp:226
double & SearchRow(const int col)
Perform a fast search for an entry in the &quot;current row&quot;. See SetColPtr().
Definition: sparsemat.hpp:710
DenseMatrix * ToDenseMatrix() const
Produces a DenseMatrix from a SparseMatrix.
Definition: sparsemat.cpp:518
bool Empty() const
Check if the SparseMatrix is empty.
Definition: sparsemat.hpp:138
int * GetRowColumns(const int row)
Return a pointer to the column indices in a row.
Definition: sparsemat.cpp:302
Set the diagonal value to zero.
Definition: matrix.hpp:34
void BooleanMult(const Array< int > &x, Array< int > &y) const
y = A * x, treating all entries as booleans (zero=false, nonzero=true).
Definition: sparsemat.cpp:729
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:476
const double * ReadData(bool on_dev=true) const
Definition: sparsemat.hpp:190
int * GetJ()
Return the array J.
Definition: sparsemat.hpp:146
Abstract data type for sparse matrices.
Definition: matrix.hpp:80
SparseMatrix & operator+=(const SparseMatrix &B)
Add the sparse matrix &#39;B&#39; to &#39;*this&#39;. This operation will cause an error if &#39;*this&#39; is finalized and ...
Definition: sparsemat.cpp:2623
T * Write(Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for write access to mem with the mfem::Device&#39;s DeviceMemoryClass, if on_dev = true...
Definition: device.hpp:312
Data type dense matrix using column-major storage.
Definition: densemat.hpp:23
void AddMult(const Vector &x, Vector &y, const double a=1.0) const
y += A * x (default) or y += a * A * x
Definition: sparsemat.cpp:555
double MaxNorm() const
Definition: sparsemat.cpp:1184
virtual ~SparseMatrix()
Destroys sparse matrix.
Definition: sparsemat.hpp:576
int * GetI()
Return the array I.
Definition: sparsemat.hpp:141
void AddMultTranspose(const Vector &x, Vector &y, const double a=1.0) const
y += At * x (default) or y += a * At * x
Definition: sparsemat.cpp:626
Abstract data type for matrix inverse.
Definition: matrix.hpp:69
void EliminateRowColMultipleRHS(int rc, const Vector &sol, DenseMatrix &rhs, DiagonalPolicy dpolicy=DIAG_ONE)
Similar to EliminateRowCol(int, const double, Vector &amp;, DiagonalPolicy), but multiple values for elim...
Definition: sparsemat.cpp:1523
void PrintInfo(std::ostream &out) const
Print various sparse matrix statistics.
Definition: sparsemat.cpp:2861
virtual double & Elem(int i, int j)
Returns reference to a_{ij}.
Definition: sparsemat.cpp:421
void LoseData()
Lose the ownership of the graph (I, J) and data (A) arrays.
Definition: sparsemat.hpp:571
void AddRow(const int row, const Array< int > &cols, const Vector &srow)
Definition: sparsemat.cpp:2504
double * GetRowEntries(const int row)
Return a pointer to the entries in a row.
Definition: sparsemat.cpp:316
void SortColumnIndices()
Sort the column indices corresponding to each row.
Definition: sparsemat.cpp:368
Memory< double > & GetMemoryData()
Definition: sparsemat.hpp:188
void PrintMM(std::ostream &out=mfem::out) const
Prints matrix in Matrix Market sparse format.
Definition: sparsemat.cpp:2790
int Capacity() const
Return the size of the allocated memory.
Memory< int > & GetMemoryI()
Definition: sparsemat.hpp:156
void GetSubMatrix(const Array< int > &rows, const Array< int > &cols, DenseMatrix &subm) const
Definition: sparsemat.cpp:2368
double * HostReadWriteData()
Definition: sparsemat.hpp:200
void _Set_(const int col, const double a)
Set an entry in the &quot;current row&quot;. See SetColPtr().
Definition: sparsemat.hpp:464
void BooleanMultTranspose(const Array< int > &x, Array< int > &y) const
y = At * x, treating all entries as booleans (zero=false, nonzero=true).
Definition: sparsemat.cpp:759
SparseMatrix * Mult_AtDA(const SparseMatrix &A, const Vector &D, SparseMatrix *OAtDA)
Matrix multiplication A^t D A. All matrices must be finalized.
Definition: sparsemat.cpp:3404
void PartAddMult(const Array< int > &rows, const Vector &x, Vector &y, const double a=1.0) const
Definition: sparsemat.cpp:711
bool RowIsEmpty(const int row) const
Definition: sparsemat.cpp:2396
void GetBlocks(Array2D< SparseMatrix * > &blocks) const
Definition: sparsemat.cpp:1011
const int * ReadJ(bool on_dev=true) const
Definition: sparsemat.hpp:174
void ScaleRow(const int row, const double scale)
Definition: sparsemat.cpp:2536
RowNodeAlloc * NodesMem
Definition: sparsemat.hpp:74
double * GetData()
Return the element data, i.e. the array A.
Definition: sparsemat.hpp:151
Memory< int > & GetMemoryJ()
Definition: sparsemat.hpp:172
void Symmetrize()
(*this) = 1/2 ((*this) + (*this)^t)
Definition: sparsemat.cpp:1143
void MoveDiagonalFirst()
Move the diagonal entry to the first position in each row, preserving the order of the rest of the co...
Definition: sparsemat.cpp:397
void SetGraphOwner(bool ownij)
Set the graph ownership flag (I and J arrays).
Definition: sparsemat.hpp:558
ID for class SparseMatrix.
Definition: operator.hpp:232
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
Definition: densemat.cpp:1926
bool OwnsData() const
Get the data ownership flag (A array).
Definition: sparsemat.hpp:568
void ClearColPtr() const
Reset the &quot;current row&quot; set by calling SetColPtr(). This method must be called between any two calls ...
Definition: sparsemat.hpp:691
void ScaleColumns(const Vector &sr)
this = this * diag(sr);
Definition: sparsemat.cpp:2595
void PrintCSR2(std::ostream &out) const
Prints a sparse matrix to stream out in CSR format.
Definition: sparsemat.cpp:2836
virtual int GetRow(const int row, Array< int > &cols, Vector &srow) const
Extract all column indices and values from a given row.
Definition: sparsemat.cpp:2417
bool isSorted
Are the columns sorted already.
Definition: sparsemat.hpp:78
Memory< double > A
Array with size I[height], containing the actual entries of the sparse matrix, as indexed by the I ar...
Definition: sparsemat.hpp:58
SparseMatrix()
Create an empty SparseMatrix.
Definition: sparsemat.hpp:85
Data type sparse matrix.
Definition: sparsemat.hpp:40
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
Definition: operator.hpp:57
static MemoryClass GetDeviceMemoryClass()
Get the current Device MemoryClass. This is the MemoryClass used by most MFEM device kernels to acces...
Definition: device.hpp:257
bool OwnsGraph() const
Get the graph ownership flag (I and J arrays).
Definition: sparsemat.hpp:565
void SetWidth(int width_=-1)
Change the width of a SparseMatrix.
Definition: sparsemat.cpp:330
double b
Definition: lissajous.cpp:42
const double * HostReadData() const
Definition: sparsemat.hpp:196
int * ReadWriteI(bool on_dev=true)
Definition: sparsemat.hpp:162
double IsSymmetric() const
Returns max_{i,j} |(i,j)-(j,i)| for a finalized matrix.
Definition: sparsemat.cpp:1101
const int * HostReadJ() const
Definition: sparsemat.hpp:180
void SetDiagIdentity()
If a row contains only one diag entry of zero, set it to 1.
Definition: sparsemat.cpp:1883
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
Definition: table.cpp:414
bool OwnsHostPtr() const
Return true if the host pointer is owned. Ownership indicates whether the pointer will be deleted by ...
void ScaleRows(const Vector &sl)
this = diag(sl) * this;
Definition: sparsemat.cpp:2564
void Swap< SparseMatrix >(SparseMatrix &a, SparseMatrix &b)
Specialization of the template function Swap&lt;&gt; for class SparseMatrix.
Definition: sparsemat.hpp:795
DenseMatrix * OuterProduct(const DenseMatrix &A, const DenseMatrix &B)
Produces a block matrix with blocks A_{ij}*B.
Definition: sparsemat.cpp:3548
int * WriteI(bool on_dev=true)
Definition: sparsemat.hpp:160
void PrintMatlab(std::ostream &out=mfem::out) const
Prints matrix in matlab format.
Definition: sparsemat.cpp:2770
const T * Read(const Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for read access to mem with the mfem::Device&#39;s DeviceMemoryClass, if on_dev = true...
Definition: device.hpp:295
void AddSubMatrix(const Array< int > &rows, const Array< int > &cols, const DenseMatrix &subm, int skip_zeros=1)
Definition: sparsemat.cpp:2224
Set the diagonal value to one.
Definition: matrix.hpp:35
Dynamic 2D array using row-major layout.
Definition: array.hpp:316
void Jacobi(const Vector &b, const Vector &x0, Vector &x1, double sc) const
Definition: sparsemat.cpp:2113
void SetSubMatrix(const Array< int > &rows, const Array< int > &cols, const DenseMatrix &subm, int skip_zeros=1)
Definition: sparsemat.cpp:2300
virtual MemoryClass GetMemoryClass() const
Return the MemoryClass preferred by the Operator.
Definition: sparsemat.hpp:261
bool Finalized() const
Returns whether or not CSR format has been finalized.
Definition: sparsemat.hpp:428
const double * GetData() const
Return the element data, i.e. the array A, const version.
Definition: sparsemat.hpp:153
Type
Enumeration defining IDs for some classes derived from Operator.
Definition: operator.hpp:229
void SparseMatrixFunction(SparseMatrix &S, double(*f)(double))
Applies f() to each element of the matrix (after it is finalized).
Definition: sparsemat.cpp:2969
virtual MatrixInverse * Inverse() const
This virtual method is not supported: it always returns NULL.
Definition: sparsemat.cpp:1261
const Memory< double > & GetMemoryData() const
Definition: sparsemat.hpp:189
void EliminateRowColDiag(int rc, double value)
Perform elimination and set the diagonal entry to the given value.
Definition: sparsemat.cpp:1726
const Memory< int > & GetMemoryI() const
Definition: sparsemat.hpp:157
virtual void Mult(const Vector &x, Vector &y) const
Matrix vector multiplication.
Definition: sparsemat.cpp:548
void SetColPtr(const int row) const
Initialize the SparseMatrix for fast access to the entries of the given row which becomes the &quot;curren...
Definition: sparsemat.hpp:656
const int * GetJ() const
Return the array J, const version.
Definition: sparsemat.hpp:148
void SetHostPtrOwner(bool own) const
Set/clear the ownership flag for the host pointer. Ownership indicates whether the pointer will be de...
void SetSubMatrixTranspose(const Array< int > &rows, const Array< int > &cols, const DenseMatrix &subm, int skip_zeros=1)
Definition: sparsemat.cpp:2333
void Set(const int i, const int j, const double a)
Definition: sparsemat.cpp:2262
void EliminateRowCol(int rc, const double sol, Vector &rhs, DiagonalPolicy dpolicy=DIAG_ONE)
Eliminate row rc and column rc and modify the rhs using sol.
Definition: sparsemat.cpp:1427
void Threshold(double tol, bool fix_empty_rows=false)
Remove entries smaller in absolute value than a given tolerance tol. If fix_empty_rows is true...
Definition: sparsemat.cpp:870
double * HostWriteData()
Definition: sparsemat.hpp:198
int Size() const
For backward compatibility, define Size() to be synonym of Height().
Definition: sparsemat.hpp:132
void Gauss_Seidel_back(const Vector &x, Vector &y) const
Definition: sparsemat.cpp:1997
const int * ReadI(bool on_dev=true) const
Definition: sparsemat.hpp:158
void Gauss_Seidel_forw(const Vector &x, Vector &y) const
Gauss-Seidel forward and backward iterations over a vector x.
Definition: sparsemat.cpp:1913
int * HostReadWriteJ()
Definition: sparsemat.hpp:184
RowNode ** Rows
Array of linked lists, one for every row. This array represents the linked list (LIL) storage format...
Definition: sparsemat.hpp:63
const Memory< int > & GetMemoryJ() const
Definition: sparsemat.hpp:173
int * WriteJ(bool on_dev=true)
Definition: sparsemat.hpp:176
void EliminateRow(int row, const double sol, Vector &rhs)
Eliminates a column from the transpose matrix.
Definition: sparsemat.cpp:1266
double a
Definition: lissajous.cpp:41
int ActualWidth() const
Returns the actual Width of the matrix.
Definition: sparsemat.cpp:2943
void GetDiag(Vector &d) const
Returns the Diagonal of A.
Definition: sparsemat.cpp:489
MemAlloc< RowNode, 1024 > RowNodeAlloc
Definition: sparsemat.hpp:73
T * ReadWrite(Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for read+write access to mem with the mfem::Device&#39;s DeviceMemoryClass, if on_dev = true, or the mfem::Device&#39;s HostMemoryClass, otherwise.
Definition: device.hpp:329
double GetRowNorml1(int irow) const
For i = irow compute .
Definition: sparsemat.cpp:846
void ResetTranspose() const
Definition: sparsemat.cpp:679
SparseMatrix * TransposeMult(const SparseMatrix &A, const SparseMatrix &B)
C = A^T B.
Definition: sparsemat.cpp:3241
virtual void EliminateZeroRows(const double threshold=1e-12)
If a row contains only zeros, set its diagonal to 1.
Definition: sparsemat.cpp:1894
bool Empty() const
Return true if the Memory object is empty, see Reset().
double & operator()(int i, int j)
Returns reference to A[i][j].
Definition: sparsemat.cpp:431
void MultTranspose(const Vector &x, Vector &y) const
Multiply a vector with the transposed matrix. y = At * x.
Definition: sparsemat.cpp:619
void EliminateCols(const Array< int > &cols, const Vector *x=NULL, Vector *b=NULL)
Eliminate all columns i for which cols[i] != 0.
Definition: sparsemat.cpp:1354
SparseMatrix & operator=(const SparseMatrix &rhs)
Assignment operator: deep copy.
Definition: sparsemat.cpp:216
SparseMatrix & operator*=(double a)
Definition: sparsemat.cpp:2702
void SetDataOwner(bool owna)
Set the data ownership flag (A array).
Definition: sparsemat.hpp:562
void SetRow(const int row, const Array< int > &cols, const Vector &srow)
Definition: sparsemat.cpp:2457
const double alpha
Definition: ex15.cpp:336
double * ReadWriteData(bool on_dev=true)
Definition: sparsemat.hpp:194
int * ReadWriteJ(bool on_dev=true)
Definition: sparsemat.hpp:178
Vector data type.
Definition: vector.hpp:48
void Print(std::ostream &out=mfem::out, int width_=4) const
Prints matrix to stream out.
Definition: sparsemat.cpp:2726
const int * HostReadI() const
Definition: sparsemat.hpp:164
const int * GetI() const
Return the array I, const version.
Definition: sparsemat.hpp:143
void Jacobi2(const Vector &b, const Vector &x0, Vector &x1, double sc=1.0) const
Definition: sparsemat.cpp:2176
int MaxRowSize() const
Returns the maximum number of elements among all rows.
Definition: sparsemat.cpp:278
int * HostReadWriteI()
Definition: sparsemat.hpp:168
void Jacobi3(const Vector &b, const Vector &x0, Vector &x1, double sc=1.0) const
Definition: sparsemat.cpp:2200
SparseMatrix * MultAbstractSparseMatrix(const AbstractSparseMatrix &A, const AbstractSparseMatrix &B)
Matrix product of sparse matrices. A and B do not need to be CSR matrices.
Definition: sparsemat.cpp:3249
double _Get_(const int col) const
Read the value of an entry in the &quot;current row&quot;. See SetColPtr().
Definition: sparsemat.hpp:737
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:66
Memory< int > J
Array with size I[height], containing the column indices for all matrix entries, as indexed by the I ...
Definition: sparsemat.hpp:55
void Swap(SparseMatrix &other)
Definition: sparsemat.cpp:3656
void PrintCSR(std::ostream &out) const
Prints matrix to stream out in hypre_CSRMatrix format.
Definition: sparsemat.cpp:2812
double InnerProduct(const Vector &x, const Vector &y) const
Compute y^t A x.
Definition: sparsemat.cpp:782
MemoryClass
Memory classes identify sets of memory types.
Definition: mem_manager.hpp:57
int width
Dimension of the input / number of columns in the matrix.
Definition: operator.hpp:28
int CountSmallElems(double tol) const
Count the number of entries with |a_ij| &lt;= tol.
Definition: sparsemat.cpp:1209
void PartMult(const Array< int > &rows, const Vector &x, Vector &y) const
Definition: sparsemat.cpp:685
void GetRowSums(Vector &x) const
For all i compute .
Definition: sparsemat.cpp:823
double * WriteData(bool on_dev=true)
Definition: sparsemat.hpp:192