H2Lib  3.0
Data Structures | Typedefs | Functions
sparsematrix

Representation of a sparse matrix in compressed row format. More...

Data Structures

struct  _sparsematrix
 Representation of a sparse matrix in compressed row format. More...
 

Typedefs

typedef struct _sparsematrix sparsematrix
 Representation of a sparse matrix in compressed row format.
 
typedef sparsematrixpsparsematrix
 Pointer to sparsematrix object.
 
typedef const sparsematrixpcsparsematrix
 Pointer to constant sparsematrix object.
 

Functions

psparsematrix new_raw_sparsematrix (uint rows, uint cols, uint nz)
 Create a sparsematrix object without initializing its arrays. More...
 
psparsematrix new_identity_sparsematrix (uint rows, uint cols)
 Creates a new sparsematrix object and initializes it to the idenity matrix $I \in \mathbb R^{n \times m}$. More...
 
psparsematrix new_zero_sparsematrix (psparsepattern sp)
 Create a sparsematrix based on a sparsepattern. More...
 
void del_sparsematrix (psparsematrix a)
 Delete a sparsematrix object. More...
 
field addentry_sparsematrix (psparsematrix a, uint row, uint col, field x)
 Add to a matrix entry, $a_{ij} \gets a_{ij} + x$. More...
 
void setentry_sparsematrix (psparsematrix a, uint row, uint col, field x)
 Set a matrix entry, $a_{ij} \gets x$. More...
 
size_t getsize_sparsematrix (pcsparsematrix a)
 Get size of a given sparsematrix object. More...
 
void sort_sparsematrix (psparsematrix a)
 Sort non-zero entries to ensure that diagonal entries come first. More...
 
void clear_sparsematrix (psparsematrix a)
 Set a matrix to zero. More...
 
void print_sparsematrix (pcsparsematrix a)
 Print a sparse matrix. More...
 
void print_eps_sparsematrix (pcsparsematrix a, const char *filename, uint offset)
 Print matrix to a Postscript file. More...
 
void addeval_sparsematrix_avector (field alpha, pcsparsematrix a, pcavector x, pavector y)
 Multiply a matrix $A$ by a vector $x$, $y \gets y + \alpha A x$. More...
 
void addevaltrans_sparsematrix_avector (field alpha, pcsparsematrix a, pcavector x, pavector y)
 Multiply the adjoint of a matrix $A$ by a vector $x$, $y \gets y + \alpha A^* x$. More...
 
void mvm_sparsematrix_avector (field alpha, bool trans, pcsparsematrix a, pcavector x, pavector y)
 Multiply a matrix $A$ or its adjoint $A^*$ by a vector, $y \gets y + \alpha A x$ or $y \gets y + \alpha A^* x$. More...
 
real norm2_sparsematrix (pcsparsematrix S)
 Approximate the spectral norm $\|S\|_2$ of a matrix $S$. More...
 
real norm2diff_sparsematrix (pcsparsematrix a, pcsparsematrix b)
 Approximate the spectral norm $\|A-B\|_2$ of the difference of two matrices $A$ and $B$. More...
 
void add_sparsematrix_amatrix (field alpha, bool atrans, pcsparsematrix a, pamatrix b)
 Add a sparsematrix to an amatrix, $B \gets B + \alpha A$ or $B \gets B + \alpha^* A$. More...
 

Detailed Description

Representation of a sparse matrix in compressed row format.

The sparsematrix class is used to store sparse matrices efficiently by avoiding to store zero entries. The internal representation is the compressed row format: the $i$-th row is described by the entries row[i] to row[i+1]-1 in the arrays col and coeff. For each $a_{ij}\neq 0$ there is exactly one $k$ such that row[i]<=k<row[i+1], col[k]=j and coeff[k] $=a_{ij}$.

Function Documentation

void add_sparsematrix_amatrix ( field  alpha,
bool  atrans,
pcsparsematrix  a,
pamatrix  b 
)

Add a sparsematrix to an amatrix, $B \gets B + \alpha A$ or $B \gets B + \alpha^* A$.

Parameters
alphaScaling factor $\alpha$.
atransSet if $A^*$ is to be added instead of $A$.
aSource matrix $A$.
bTarget matrix $B$.
field addentry_sparsematrix ( psparsematrix  a,
uint  row,
uint  col,
field  x 
)

Add to a matrix entry, $a_{ij} \gets a_{ij} + x$.

Only entries appearing in the sparsity pattern of the matrix are allowed.

Parameters
aTarget matrix $A$.
rowRow index $i$.
colColumn index $j$.
xSummand.
Returns
New value of $a_{ij}$.
void addeval_sparsematrix_avector ( field  alpha,
pcsparsematrix  a,
pcavector  x,
pavector  y 
)

Multiply a matrix $A$ by a vector $x$, $y \gets y + \alpha A x$.

The matrix $A$ is multiplied by the source vector $x$, the result is scaled by $\alpha$ and added to the target vector $y$.

Parameters
alphaScaling factor $\alpha$.
aMatrix $A$.
xSource vector $x$.
yTarget vector $y$.
void addevaltrans_sparsematrix_avector ( field  alpha,
pcsparsematrix  a,
pcavector  x,
pavector  y 
)

Multiply the adjoint of a matrix $A$ by a vector $x$, $y \gets y + \alpha A^* x$.

The adjoint $A^*$ is multiplied by the source vector $x$, the result is scaled by $\alpha$ and added to the target vector $y$.

Parameters
alphaScaling factor $\alpha$.
aMatrix $A$.
xSource vector $x$.
yTarget vector $y$.
void clear_sparsematrix ( psparsematrix  a)

Set a matrix to zero.

Parameters
aTarget matrix.
void del_sparsematrix ( psparsematrix  a)

Delete a sparsematrix object.

Releases the storage corresponding to the object.

Parameters
aObject to be deleted.
size_t getsize_sparsematrix ( pcsparsematrix  a)

Get size of a given sparsematrix object.

Computes the size of the sparsematrix object and the storage allocated for the coefficients.

Parameters
aTarget matrix.
Returns
Size of allocated storage in bytes.
void mvm_sparsematrix_avector ( field  alpha,
bool  trans,
pcsparsematrix  a,
pcavector  x,
pavector  y 
)

Multiply a matrix $A$ or its adjoint $A^*$ by a vector, $y \gets y + \alpha A x$ or $y \gets y + \alpha A^* x$.

The matrix or its adjoint is multiplied by the source vector $x$, the result is scaled by $\alpha$ and added to the target vector $y$.

Parameters
alphaScaling factor $\alpha$.
transSet if $A^*$ is to be used instead of $A$.
aMatrix $A$.
xSource vector $x$.
yTarget vector $y$.
psparsematrix new_identity_sparsematrix ( uint  rows,
uint  cols 
)

Creates a new sparsematrix object and initializes it to the idenity matrix $I \in \mathbb R^{n \times m}$.

Parameters
rowsNumber of rows is equal to $n$
colsNumber of columns is equal to $m$
Returns
New identity sparsematrix
psparsematrix new_raw_sparsematrix ( uint  rows,
uint  cols,
uint  nz 
)

Create a sparsematrix object without initializing its arrays.

Remarks
Should always be matched by a call to del_sparsematrix.
Parameters
rowsNumber of rows.
colsNumber of columns.
nzNumber of non-zero entries.
Returns
Allocated sparsematrix object, arrays row, col and coeff are uninitialized.
psparsematrix new_zero_sparsematrix ( psparsepattern  sp)

Create a sparsematrix based on a sparsepattern.

Remarks
Should always be matched by a call to del_sparsematrix.
Parameters
spDescription of matrix graph in a sparsepattern object.
Returns
Fully initialized sparsematrix object with given sparsity pattern and zero coefficients.
real norm2_sparsematrix ( pcsparsematrix  S)

Approximate the spectral norm $\|S\|_2$ of a matrix $S$.

The spectral norm is approximated by applying a few steps of the power iteration to the matrix $S^* S$ and computing the square root of the resulting eigenvalue approximation.

Parameters
SSparse matrix $S$.
Returns
Approximation of $\|S\|_2$.
real norm2diff_sparsematrix ( pcsparsematrix  a,
pcsparsematrix  b 
)

Approximate the spectral norm $\|A-B\|_2$ of the difference of two matrices $A$ and $B$.

The spectral norm is approximated by applying a few steps of the power iteration to the matrix $(A-B)^* (A-B)$ and computing the square root of the resulting eigenvalue approximation.

Parameters
aSparse matrix $A$.
bSparse matrix $B$.
Returns
Approximation of $\|A-B\|_2$.
void print_eps_sparsematrix ( pcsparsematrix  a,
const char *  filename,
uint  offset 
)

Print matrix to a Postscript file.

Parameters
aSource matrix.
filenameName of the target file.
offsetOffset added to all coordinates, e.g., to avoid boundary clipping with some printers.
void print_sparsematrix ( pcsparsematrix  a)

Print a sparse matrix.

Parameters
aSource matrix.
void setentry_sparsematrix ( psparsematrix  a,
uint  row,
uint  col,
field  x 
)

Set a matrix entry, $a_{ij} \gets x$.

Only entries appearing in the sparsity pattern of the matirx are allowed.

Parameters
aTarget matrix $A$.
rowRow index $i$.
colColumn index $j$.
xNew value of $a_{ij}$.
void sort_sparsematrix ( psparsematrix  a)

Sort non-zero entries to ensure that diagonal entries come first.

Reorder the entries of each row in col and coeff to place the diagonal entry first. Since many iterative solvers require us to handle this entry differently from all others, this optimization can improve the performance.

Parameters
aTarget matrix.