12 #ifndef MFEM_SPARSEMAT_HPP
13 #define MFEM_SPARSEMAT_HPP
17 #include "../general/mem_alloc.hpp"
18 #include "../general/table.hpp"
19 #include "../general/globals.hpp"
26 #if defined(__alignas_is_defined)
67 #ifdef MFEM_USE_MEMALLOC
96 SparseMatrix(
int *i,
int *j,
double *data,
int m,
int n);
100 SparseMatrix(
int *i,
int *j,
double *data,
int m,
int n,
bool ownij,
101 bool owna,
bool issorted);
134 inline int *
GetI()
const {
return I; }
136 inline int *
GetJ()
const {
return J; }
140 int RowSize(
const int i)
const;
172 virtual double &
Elem(
int i,
int j);
175 virtual const double &
Elem(
int i,
int j)
const;
197 const double a = 1.0)
const;
201 const double a=1.0)
const;
294 double sc = 1.0)
const;
298 double sc = 1.0)
const;
308 void Finalize(
int skip_zeros,
bool fix_empty_rows);
321 inline void SetColPtr(
const int row)
const;
324 inline void _Add_(
const int col,
const double a)
326 inline void _Set_(
const int col,
const double a)
328 inline double _Get_(
const int col)
const;
330 inline double &
SearchRow(
const int row,
const int col);
331 inline void _Add_(
const int row,
const int col,
const double a)
333 inline void _Set_(
const int row,
const int col,
const double a)
336 void Set(
const int i,
const int j,
const double a);
337 void Add(
const int i,
const int j,
const double a);
363 void ScaleRow(
const int row,
const double scale);
400 int Walk(
int &i,
int &j,
double &a);
443 SparseMatrix *
Transpose(
const SparseMatrix &A);
454 SparseMatrix *
Mult(
const SparseMatrix &A,
const SparseMatrix &B,
455 SparseMatrix *OAB = NULL);
459 const AbstractSparseMatrix &B);
462 DenseMatrix *
Mult(
const SparseMatrix &A, DenseMatrix &B);
465 DenseMatrix *
RAP(
const SparseMatrix &A, DenseMatrix &P);
469 SparseMatrix *
RAP(
const SparseMatrix &A,
const SparseMatrix &R,
470 SparseMatrix *ORAP = NULL);
473 SparseMatrix *
RAP(
const SparseMatrix &Rt,
const SparseMatrix &A,
474 const SparseMatrix &P);
477 SparseMatrix *
Mult_AtDA(
const SparseMatrix &A,
const Vector &D,
478 SparseMatrix *OAtDA = NULL);
482 SparseMatrix *
Add(
const SparseMatrix & A,
const SparseMatrix & B);
484 SparseMatrix *
Add(
double a,
const SparseMatrix & A,
double b,
485 const SparseMatrix & B);
487 SparseMatrix *
Add(Array<SparseMatrix *> & Ai);
499 for (
int i = 0; i <
width; i++)
504 for (RowNode *node_p =
Rows[row]; node_p != NULL; node_p = node_p->Prev)
514 for (
int i = 0; i <
width; i++)
519 for (
int j =
I[row], end =
I[row+1]; j < end; j++)
531 node_p = node_p->Prev)
536 for (
int j =
I[
current_row], end =
I[current_row+1]; j < end; j++)
549 #ifdef MFEM_USE_MEMALLOC
552 node_p =
new RowNode;
555 node_p->Column = col;
559 return node_p->Value;
564 MFEM_VERIFY(j != -1,
"Entry for column " << col <<
" is not allocated.");
574 return (node_p == NULL) ? 0.0 : node_p->Value;
579 return (j == -1) ? 0.0 :
A[j];
589 for (node_p =
Rows[row]; 1; node_p = node_p->Prev)
593 #ifdef MFEM_USE_MEMALLOC
596 node_p =
new RowNode;
598 node_p->Prev =
Rows[row];
599 node_p->Column = col;
604 else if (node_p->Column == col)
609 return node_p->Value;
613 int *Ip =
I+row, *Jp =
J;
614 for (
int k = Ip[0], end = Ip[1]; k < end; k++)
621 MFEM_ABORT(
"Could not find entry for row = " << row <<
", col = " << col);
void _Add_(const int row, const int col, const double a)
double GetJacobiScaling() const
Determine appropriate scaling for Jacobi iteration.
int CheckFinite() const
Count the number of entries that are NOT finite, i.e. Inf or Nan.
SparseMatrix * TransposeAbstractSparseMatrix(const AbstractSparseMatrix &A, int useActualWidth)
Transpose of a sparse matrix. A does not need to be a CSR matrix.
int RowSize(const int i) const
Returns the number of elements in row i.
virtual int NumNonZeroElems() const
Returns the number of the nonzero elements in the matrix.
void Add(const int i, const int j, const double a)
void _Add_(const int col, const double a)
void Clear()
Clear the contents of the SparseMatrix.
void EliminateCol(int col, DiagonalPolicy dpolicy=DIAG_ZERO)
Eliminates the column col from the matrix.
void _Set_(const int row, const int col, const double a)
HypreParMatrix * RAP(const HypreParMatrix *A, const HypreParMatrix *P)
Returns the matrix P^t * A * P.
void DiagScale(const Vector &b, Vector &x, double sc=1.0) const
virtual void Finalize(int skip_zeros=1)
Finalize the matrix initialization, switching the storage format from LIL to CSR. ...
void MakeRef(const SparseMatrix &master)
Clear the contents of the SparseMatrix and make it a reference to master.
double & SearchRow(const int col)
bool Empty() const
Check if the SparseMatrix is empty.
int * GetRowColumns(const int row)
Return a pointer to the column indices in a row.
int * I
Array with size (height+1) containing the row offsets.
Set the diagonal value to zero.
void BooleanMult(const Array< int > &x, Array< int > &y) const
y = A * x, but treat all elements as booleans (zero=false, nonzero=true).
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Abstract data type for sparse matrices.
SparseMatrix & operator+=(const SparseMatrix &B)
Data type dense matrix using column-major storage.
void AddMult(const Vector &x, Vector &y, const double a=1.0) const
y += A * x (default) or y += a * A * x
virtual ~SparseMatrix()
Destroys sparse matrix.
void AddMultTranspose(const Vector &x, Vector &y, const double a=1.0) const
y += At * x (default) or y += a * At * x
Abstract data type for matrix inverse.
void EliminateRowColMultipleRHS(int rc, const Vector &sol, DenseMatrix &rhs, DiagonalPolicy dpolicy=DIAG_ONE)
Similar to EliminateRowCol(int, const double, Vector &, DiagonalPolicy), but multiple values for elim...
void PrintInfo(std::ostream &out) const
Print various sparse matrix staticstics.
virtual double & Elem(int i, int j)
Returns reference to a_{ij}.
void LoseData()
Lose the ownership of the graph (I, J) and data (A) arrays.
void AddRow(const int row, const Array< int > &cols, const Vector &srow)
double * GetRowEntries(const int row)
Return a pointer to the entries in a row.
void SortColumnIndices()
Sort the column indices corresponding to each row.
void PrintMM(std::ostream &out=mfem::out) const
Prints matrix in Matrix Market sparse format.
void GetSubMatrix(const Array< int > &rows, const Array< int > &cols, DenseMatrix &subm) const
void _Set_(const int col, const double a)
void BooleanMultTranspose(const Array< int > &x, Array< int > &y) const
y = At * x, but treat all elements as booleans (zero=false, nonzero=true).
SparseMatrix * Mult_AtDA(const SparseMatrix &A, const Vector &D, SparseMatrix *OAtDA)
Matrix multiplication A^t D A. All matrices must be finalized.
void PartAddMult(const Array< int > &rows, const Vector &x, Vector &y, const double a=1.0) const
bool RowIsEmpty(const int row) const
void GetBlocks(Array2D< SparseMatrix * > &blocks) const
void ScaleRow(const int row, const double scale)
int ActualWidth()
Returns the actual Width of the matrix.
void Symmetrize()
(*this) = 1/2 ((*this) + (*this)^t)
void MoveDiagonalFirst()
Move the diagonal entry to the first position in each row, preserving the order of the rest of the co...
void SetGraphOwner(bool ownij)
Set the graph ownership flag (I and J arrays).
ID for class SparseMatrix.
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
bool OwnsData() const
Get the data ownership flag (A array).
void ScaleColumns(const Vector &sr)
void PrintCSR2(std::ostream &out) const
Prints a sparse matrix to stream out in CSR format.
virtual int GetRow(const int row, Array< int > &cols, Vector &srow) const
Extract all column indices and values from a given row.
bool isSorted
Are the columns sorted already.
SparseMatrix()
Create an empty SparseMatrix.
bool ownData
Say whether we own the pointers for A (should we free them?).
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
bool OwnsGraph() const
Get the graph ownership flag (I and J arrays).
void SetWidth(int width_=-1)
Change the width of a SparseMatrix.
bool areColumnsSorted() const
double IsSymmetric() const
Returns max_{i,j} |(i,j)-(j,i)| for a finalized matrix.
void SetDiagIdentity()
If a row contains only one diag entry of zero, set it to 1.
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
void ScaleRows(const Vector &sl)
void Swap< SparseMatrix >(SparseMatrix &a, SparseMatrix &b)
Specialization of the template function Swap<> for class SparseMatrix.
void PrintMatlab(std::ostream &out=mfem::out) const
Prints matrix in matlab format.
double * GetData() const
Return element data, i.e. array A.
void AddSubMatrix(const Array< int > &rows, const Array< int > &cols, const DenseMatrix &subm, int skip_zeros=1)
Set the diagonal value to one.
int * GetI() const
Return the array I.
Dynamic 2D array using row-major layout.
void Jacobi(const Vector &b, const Vector &x0, Vector &x1, double sc) const
void SetSubMatrix(const Array< int > &rows, const Array< int > &cols, const DenseMatrix &subm, int skip_zeros=1)
Type
Enumeration defining IDs for some classes derived from Operator.
void SparseMatrixFunction(SparseMatrix &S, double(*f)(double))
Applies f() to each element of the matrix (after it is finalized).
virtual MatrixInverse * Inverse() const
This virtual method is not supported: it always returns NULL.
void EliminateRowColDiag(int rc, double value)
Perform elimination and set the diagonal entry to the given value.
virtual void Mult(const Vector &x, Vector &y) const
Matrix vector multiplication.
void SetColPtr(const int row) const
void SetSubMatrixTranspose(const Array< int > &rows, const Array< int > &cols, const DenseMatrix &subm, int skip_zeros=1)
void Set(const int i, const int j, const double a)
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.
int * J
Array with size I[height], containing the column indices for all matrix entries, as indexed by the I ...
int Size() const
For backward compatibility, define Size() to be synonym of Height().
void Gauss_Seidel_back(const Vector &x, Vector &y) const
void Gauss_Seidel_forw(const Vector &x, Vector &y) const
Gauss-Seidel forward and backward iterations over a vector x.
RowNode ** Rows
Array of linked lists, one for every row. This array represents the linked list (LIL) storage format...
void EliminateRow(int row, const double sol, Vector &rhs)
Eliminates a column from the transpose matrix.
double * A
Array with size I[height], containing the actual entries of the sparse matrix, as indexed by the I ar...
void GetDiag(Vector &d) const
Returns the Diagonal of A.
MemAlloc< RowNode, 1024 > RowNodeAlloc
double GetRowNorml1(int irow) const
For i = irow compute .
virtual void EliminateZeroRows(const double threshold=1e-12)
If a row contains only zeros, set its diagonal to 1.
double & operator()(int i, int j)
Returns reference to A[i][j].
void MultTranspose(const Vector &x, Vector &y) const
Multiply a vector with the transposed matrix. y = At * x.
void EliminateCols(const Array< int > &cols, const Vector *x=NULL, Vector *b=NULL)
Eliminate all columns i for which cols[i] != 0.
SparseMatrix & operator=(const SparseMatrix &rhs)
Assignment operator: deep copy.
bool ownGraph
Say whether we own the pointers for I and J (should we free them?).
SparseMatrix & operator*=(double a)
void SetDataOwner(bool owna)
Set the data ownership flag (A array).
void SetRow(const int row, const Array< int > &cols, const Vector &srow)
int Walk(int &i, int &j, double &a)
Walks the sparse matrix.
void Print(std::ostream &out=mfem::out, int width_=4) const
Prints matrix to stream out.
void Jacobi2(const Vector &b, const Vector &x0, Vector &x1, double sc=1.0) const
int MaxRowSize() const
Returns the maximum number of elements among all rows.
void Jacobi3(const Vector &b, const Vector &x0, Vector &x1, double sc=1.0) const
SparseMatrix * MultAbstractSparseMatrix(const AbstractSparseMatrix &A, const AbstractSparseMatrix &B)
Matrix product of sparse matrices. A and B do not need to be CSR matrices.
double _Get_(const int col) const
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
void Swap(SparseMatrix &other)
int * GetJ() const
Return the array J.
void PrintCSR(std::ostream &out) const
Prints matrix to stream out in hypre_CSRMatrix format.
double InnerProduct(const Vector &x, const Vector &y) const
Compute y^t A x.
int width
Dimension of the input / number of columns in the matrix.
int CountSmallElems(double tol) const
Count the number of entries with |a_ij| <= tol.
void PartMult(const Array< int > &rows, const Vector &x, Vector &y) const
void GetRowSums(Vector &x) const
For all i compute .