Public Types | Public Member Functions | Protected Attributes | Related Functions
vnl_sparse_matrix< T > Class Template Reference

Simple sparse matrix. More...

#include <vnl_sparse_matrix.h>

Inheritance diagram for vnl_sparse_matrix< T >:
Inheritance graph
[legend]

List of all members.

Public Types

typedef vnl_sparse_matrix_pair< T > pair_t
typedef vcl_vector< pair_trow
typedef vcl_vector< rowvnl_sparse_matrix_elements

Public Member Functions

 vnl_sparse_matrix ()
 Construct an empty matrix.
 vnl_sparse_matrix (unsigned int m, unsigned int n)
 Construct an empty m*n matrix.
 vnl_sparse_matrix (vnl_sparse_matrix< T > const &rhs)
 Construct an m*n Matrix and copy rhs into it.
vnl_sparse_matrix< T > & operator= (vnl_sparse_matrix< T > const &rhs)
 Copy another vnl_sparse_matrix<T> into this.
void mult (vnl_vector< T > const &rhs, vnl_vector< T > &result) const
 Multiply this*rhs, where rhs is a vector.
void mult (unsigned int n, unsigned int m, T const *p, T *q) const
 Multiply this*p, a fortran order matrix.
void pre_mult (const vnl_vector< T > &lhs, vnl_vector< T > &result) const
 Multiplies lhs*this, where lhs is a vector.
T & operator() (unsigned int row, unsigned int column)
 Get a reference to an entry in the matrix.
operator() (unsigned int row, unsigned int column) const
 Get the value of an entry in the matrix.
get (unsigned int row, unsigned int column) const
 Get an entry in the matrix.
void put (unsigned int row, unsigned int column, T value)
 Put (i.e., add or overwrite) an entry into the matrix.
void diag_AtA (vnl_vector< T > &result) const
 Get diag(A_transpose * A).
vnl_sparse_matrixset_row (unsigned int r, vcl_vector< int > const &cols, vcl_vector< T > const &vals)
 Set a whole row at once. Much faster. Returns *this.
rowget_row (unsigned int r)
 Return row as vector of pairs.
vnl_sparse_matrix< T > & vcat (vnl_sparse_matrix< T > const &A)
 Laminate matrix A onto the bottom of this one.
unsigned int rows () const
 Get the number of rows in the matrix.
unsigned int columns () const
 Get the number of columns in the matrix.
unsigned int cols () const
 Get the number of columns in the matrix.
bool empty_row (unsigned int r) const
 Return whether a given row is empty.
sum_row (unsigned int r)
 This is occasionally useful.
vnl_sparse_matrixscale_row (unsigned int r, T scale)
 Useful for normalizing row sums in convolution operators.
void clear ()
 Set all elements to null.
void set_size (int r, int c)
 Resizes the array to have r rows and c cols -- sets elements to null.
void resize (int r, int c)
 Resizes the array to have r rows and c cols.
void reset () const
 Resets the internal iterator.
bool next () const
 Moves the internal iterator to next non-zero entry in matrix.
int getrow () const
 Returns the row of the entry pointed to by internal iterator.
int getcolumn () const
 Returns the column of the entry pointed to by internal iterator.
value () const
 Returns the value pointed to by the internal iterator.
bool operator== (vnl_sparse_matrix< T > const &rhs) const
 Comparison.
bool operator!= (vnl_sparse_matrix< T > const &rhs) const
 Inequality.
vnl_sparse_matrix< T > operator- () const
 Unary minus.
vnl_sparse_matrix< T > operator+ (vnl_sparse_matrix< T > const &rhs) const
 addition.
vnl_sparse_matrix< T > operator- (vnl_sparse_matrix< T > const &rhs) const
 subtraction.
vnl_sparse_matrix< T > operator* (vnl_sparse_matrix< T > const &rhs) const
 multiplication.
vnl_sparse_matrix< T > & operator+= (vnl_sparse_matrix< T > const &rhs)
 in-place addition.
vnl_sparse_matrix< T > & operator-= (vnl_sparse_matrix< T > const &rhs)
 in-place subtraction.
vnl_sparse_matrix< T > & operator*= (vnl_sparse_matrix< T > const &rhs)
 in-place multiplication.
vnl_sparse_matrix< T > operator* (T const &rhs) const
 scalar multiplication.
vnl_sparse_matrix< T > & operator*= (T const &rhs)
 in-place scalar multiplication.
vnl_sparse_matrix< T > operator/ (T const &rhs) const
 scalar division.
vnl_sparse_matrix< T > & operator/= (T const &rhs)
 in-place scalar division.
vnl_sparse_matrix< T > transpose () const
 returns a new sparse matrix, viz. the transpose of this.
vnl_sparse_matrix< T > conjugate_transpose () const
 returns a new sparse matrix, viz. the conjugate (or Hermitian) transpose of this.
vnl_sparse_matrixset_identity ()
 Sets this matrix to an identity matrix, then returns "*this".
vnl_sparse_matrixnormalize_rows ()
 Normalizes each row so it is a unit vector, and returns "*this".
void add (const vnl_sparse_matrix< T > &rhs, vnl_sparse_matrix< T > &result) const
 Add rhs to this.
void subtract (const vnl_sparse_matrix< T > &rhs, vnl_sparse_matrix< T > &result) const
 Subtract rhs from this.
void mult (vnl_sparse_matrix< T > const &rhs, vnl_sparse_matrix< T > &result) const
 Multiply this*rhs, another sparse matrix.

Protected Attributes

vnl_sparse_matrix_elements elements
unsigned int rs_
unsigned int cs_
unsigned int itr_row
row::const_iterator itr_cur
bool itr_isreset

Related Functions

(Note that these are not member functions.)

template<class T >
void vsl_b_write (vsl_b_ostream &os, const vnl_sparse_matrix< T > &v)
 Binary save vnl_sparse_matrix to stream.
template<class T >
void vsl_b_read (vsl_b_istream &is, vnl_sparse_matrix< T > &v)
 Binary load vnl_sparse_matrix from stream.
template<class T >
void vsl_print_summary (vcl_ostream &os, const vnl_sparse_matrix< T > &b)
 Print human readable summary of object to a stream.

Detailed Description

template<class T>
class vnl_sparse_matrix< T >

Simple sparse matrix.

Stores non-zero elements as a sparse_matrix_pair

Definition at line 107 of file vnl_sparse_matrix.h.


Member Typedef Documentation

template<class T>
typedef vnl_sparse_matrix_pair<T> vnl_sparse_matrix< T >::pair_t

Definition at line 110 of file vnl_sparse_matrix.h.

template<class T>
typedef vcl_vector< pair_t > vnl_sparse_matrix< T >::row

Definition at line 116 of file vnl_sparse_matrix.h.

template<class T>
typedef vcl_vector< row > vnl_sparse_matrix< T >::vnl_sparse_matrix_elements

Definition at line 117 of file vnl_sparse_matrix.h.


Constructor & Destructor Documentation

template<class T >
vnl_sparse_matrix< T >::vnl_sparse_matrix ( )

Construct an empty matrix.

Definition at line 25 of file vnl_sparse_matrix.txx.

template<class T >
vnl_sparse_matrix< T >::vnl_sparse_matrix ( unsigned int  m,
unsigned int  n 
)

Construct an empty m*n matrix.

Construct an empty m*n matrix. There are m rows and n columns.

Definition at line 33 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T >::vnl_sparse_matrix ( vnl_sparse_matrix< T > const &  rhs)

Construct an m*n Matrix and copy rhs into it.

Definition at line 41 of file vnl_sparse_matrix.txx.


Member Function Documentation

template<class T>
void vnl_sparse_matrix< T >::add ( const vnl_sparse_matrix< T > &  rhs,
vnl_sparse_matrix< T > &  result 
) const

Add rhs to this.

Deprecated for direct use: please use operator "+" instead.

Definition at line 303 of file vnl_sparse_matrix.txx.

template<class T>
void vnl_sparse_matrix< T >::clear ( ) [inline]

Set all elements to null.

Definition at line 189 of file vnl_sparse_matrix.h.

template<class T>
unsigned int vnl_sparse_matrix< T >::cols ( ) const [inline]

Get the number of columns in the matrix.

Definition at line 177 of file vnl_sparse_matrix.h.

template<class T>
unsigned int vnl_sparse_matrix< T >::columns ( ) const [inline]

Get the number of columns in the matrix.

Definition at line 174 of file vnl_sparse_matrix.h.

template<class T >
vnl_sparse_matrix< T > vnl_sparse_matrix< T >::conjugate_transpose ( ) const

returns a new sparse matrix, viz. the conjugate (or Hermitian) transpose of this.

Definition at line 954 of file vnl_sparse_matrix.txx.

template<class T>
void vnl_sparse_matrix< T >::diag_AtA ( vnl_vector< T > &  result) const

Get diag(A_transpose * A).

Useful for forming Jacobi preconditioners for linear solvers.

Definition at line 478 of file vnl_sparse_matrix.txx.

template<class T>
bool vnl_sparse_matrix< T >::empty_row ( unsigned int  r) const [inline]

Return whether a given row is empty.

Definition at line 180 of file vnl_sparse_matrix.h.

template<class T >
T vnl_sparse_matrix< T >::get ( unsigned int  r,
unsigned int  c 
) const

Get an entry in the matrix.

This is the "const" version of operator().

Definition at line 445 of file vnl_sparse_matrix.txx.

template<class T>
row& vnl_sparse_matrix< T >::get_row ( unsigned int  r) [inline]

Return row as vector of pairs.

Added to aid binary I/O

Definition at line 165 of file vnl_sparse_matrix.h.

template<class T >
int vnl_sparse_matrix< T >::getcolumn ( ) const

Returns the column of the entry pointed to by internal iterator.

Definition at line 664 of file vnl_sparse_matrix.txx.

template<class T >
int vnl_sparse_matrix< T >::getrow ( ) const

Returns the row of the entry pointed to by internal iterator.

Definition at line 655 of file vnl_sparse_matrix.txx.

template<class T>
void vnl_sparse_matrix< T >::mult ( vnl_vector< T > const &  rhs,
vnl_vector< T > &  result 
) const

Multiply this*rhs, where rhs is a vector.

Multiply this*rhs, a vector.

Definition at line 240 of file vnl_sparse_matrix.txx.

template<class T>
void vnl_sparse_matrix< T >::mult ( unsigned int  prows,
unsigned int  pcols,
T const *  p,
T *  q 
) const

Multiply this*p, a fortran order matrix.

The matrix p has n rows and m columns, and is in fortran order, ie. columns first.

Definition at line 154 of file vnl_sparse_matrix.txx.

template<class T>
void vnl_sparse_matrix< T >::mult ( vnl_sparse_matrix< T > const &  rhs,
vnl_sparse_matrix< T > &  result 
) const

Multiply this*rhs, another sparse matrix.

Deprecated for direct use: please use operator "*" instead.

Definition at line 64 of file vnl_sparse_matrix.txx.

template<class T >
bool vnl_sparse_matrix< T >::next ( ) const

Moves the internal iterator to next non-zero entry in matrix.

Returns true if there is another value, false otherwise. Use in combination with methods reset, getrow, getcolumn, and value.

Definition at line 619 of file vnl_sparse_matrix.txx.

template<class T >
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::normalize_rows ( )

Normalizes each row so it is a unit vector, and returns "*this".

Make each row of the matrix have unit norm.

Zero rows are not modified Returning "*this" allows "chaining" two or more operations:

     M.normalize_rows() += M2;

Note that there is no method normalize_columns() since its implementation would be much more inefficient than normalize_rows()!

All-zero rows are ignored.

Definition at line 870 of file vnl_sparse_matrix.txx.

template<class T>
bool vnl_sparse_matrix< T >::operator!= ( vnl_sparse_matrix< T > const &  rhs) const [inline]

Inequality.

Definition at line 218 of file vnl_sparse_matrix.h.

template<class T >
T & vnl_sparse_matrix< T >::operator() ( unsigned int  row,
unsigned int  column 
)

Get a reference to an entry in the matrix.

Definition at line 409 of file vnl_sparse_matrix.txx.

template<class T >
T vnl_sparse_matrix< T >::operator() ( unsigned int  row,
unsigned int  column 
) const

Get the value of an entry in the matrix.

Definition at line 428 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > vnl_sparse_matrix< T >::operator* ( vnl_sparse_matrix< T > const &  rhs) const

multiplication.

Definition at line 775 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > vnl_sparse_matrix< T >::operator* ( T const &  rhs) const

scalar multiplication.

Definition at line 832 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::operator*= ( vnl_sparse_matrix< T > const &  rhs)

in-place multiplication.

Definition at line 862 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::operator*= ( T const &  rhs)

in-place scalar multiplication.

Definition at line 784 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > vnl_sparse_matrix< T >::operator+ ( vnl_sparse_matrix< T > const &  rhs) const

addition.

Definition at line 757 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::operator+= ( vnl_sparse_matrix< T > const &  rhs)

in-place addition.

Definition at line 848 of file vnl_sparse_matrix.txx.

template<class T >
vnl_sparse_matrix< T > vnl_sparse_matrix< T >::operator- ( ) const

Unary minus.

Definition at line 728 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > vnl_sparse_matrix< T >::operator- ( vnl_sparse_matrix< T > const &  rhs) const

subtraction.

Definition at line 766 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::operator-= ( vnl_sparse_matrix< T > const &  rhs)

in-place subtraction.

Definition at line 855 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > vnl_sparse_matrix< T >::operator/ ( T const &  rhs) const

scalar division.

Definition at line 840 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::operator/= ( T const &  rhs)

in-place scalar division.

Definition at line 808 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::operator= ( vnl_sparse_matrix< T > const &  rhs)

Copy another vnl_sparse_matrix<T> into this.

Definition at line 49 of file vnl_sparse_matrix.txx.

template<class T>
bool vnl_sparse_matrix< T >::operator== ( vnl_sparse_matrix< T > const &  rhs) const

Comparison.

Definition at line 682 of file vnl_sparse_matrix.txx.

template<class T>
void vnl_sparse_matrix< T >::pre_mult ( const vnl_vector< T > &  lhs,
vnl_vector< T > &  result 
) const

Multiplies lhs*this, where lhs is a vector.

Multiply lhs*this, where lhs is a vector.

Definition at line 266 of file vnl_sparse_matrix.txx.

template<class T>
void vnl_sparse_matrix< T >::put ( unsigned int  row,
unsigned int  column,
value 
)

Put (i.e., add or overwrite) an entry into the matrix.

Definition at line 461 of file vnl_sparse_matrix.txx.

template<class T >
void vnl_sparse_matrix< T >::reset ( ) const

Resets the internal iterator.

Definition at line 607 of file vnl_sparse_matrix.txx.

template<class T >
void vnl_sparse_matrix< T >::resize ( int  r,
int  c 
)

Resizes the array to have r rows and c cols.

Resizes the matrix so that it has r rows and c columns, leaving the current contents.

This is more wasteful of resources than set_size, but it preserves the contents.

Definition at line 582 of file vnl_sparse_matrix.txx.

template<class T>
unsigned int vnl_sparse_matrix< T >::rows ( ) const [inline]

Get the number of rows in the matrix.

Definition at line 171 of file vnl_sparse_matrix.h.

template<class T>
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::scale_row ( unsigned int  r,
scale 
)

Useful for normalizing row sums in convolution operators.

Definition at line 550 of file vnl_sparse_matrix.txx.

template<class T >
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::set_identity ( )

Sets this matrix to an identity matrix, then returns "*this".

Fill this matrix with 1s on the main diagonal and 0s elsewhere.

Returning "*this" allows e.g. passing an identity matrix as argument to a function f, without having to name the constructed matrix:

Returning "*this" also allows "chaining" two or more operations: e.g., to set a matrix to identity, then add an other matrix to it:

     M.set_identity() += M2;

If the matrix is not square, anyhow set main diagonal to 1, the rest to 0.

Definition at line 911 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::set_row ( unsigned int  r,
vcl_vector< int > const &  cols,
vcl_vector< T > const &  vals 
)

Set a whole row at once. Much faster. Returns *this.

Set row in the matrix.

Definition at line 500 of file vnl_sparse_matrix.txx.

template<class T >
void vnl_sparse_matrix< T >::set_size ( int  r,
int  c 
)

Resizes the array to have r rows and c cols -- sets elements to null.

Resizes the matrix so that it has r rows and c columns, clearing the current contents.

Definition at line 563 of file vnl_sparse_matrix.txx.

template<class T>
void vnl_sparse_matrix< T >::subtract ( const vnl_sparse_matrix< T > &  rhs,
vnl_sparse_matrix< T > &  result 
) const

Subtract rhs from this.

Deprecated for direct use: please use operator "-" instead.

Definition at line 356 of file vnl_sparse_matrix.txx.

template<class T >
T vnl_sparse_matrix< T >::sum_row ( unsigned int  r)

This is occasionally useful.

This is occasionally useful. Sums a row of the matrix efficiently.

Definition at line 537 of file vnl_sparse_matrix.txx.

template<class T >
vnl_sparse_matrix< T > vnl_sparse_matrix< T >::transpose ( ) const

returns a new sparse matrix, viz. the transpose of this.

Definition at line 928 of file vnl_sparse_matrix.txx.

template<class T >
T vnl_sparse_matrix< T >::value ( ) const

Returns the value pointed to by the internal iterator.

Definition at line 673 of file vnl_sparse_matrix.txx.

template<class T>
vnl_sparse_matrix< T > & vnl_sparse_matrix< T >::vcat ( vnl_sparse_matrix< T > const &  A)

Laminate matrix A onto the bottom of this one.

Definition at line 518 of file vnl_sparse_matrix.txx.


Friends And Related Function Documentation

template<class T >
void vsl_b_read ( vsl_b_istream &  is,
vnl_sparse_matrix< T > &  v 
) [related]

Binary load vnl_sparse_matrix from stream.

template<class T >
void vsl_b_write ( vsl_b_ostream &  os,
const vnl_sparse_matrix< T > &  v 
) [related]

Binary save vnl_sparse_matrix to stream.

template<class T >
void vsl_print_summary ( vcl_ostream &  os,
const vnl_sparse_matrix< T > &  b 
) [related]

Print human readable summary of object to a stream.


Member Data Documentation

template<class T>
unsigned int vnl_sparse_matrix< T >::cs_ [protected]

Definition at line 302 of file vnl_sparse_matrix.h.

template<class T>
vnl_sparse_matrix_elements vnl_sparse_matrix< T >::elements [protected]

Definition at line 301 of file vnl_sparse_matrix.h.

template<class T>
row::const_iterator vnl_sparse_matrix< T >::itr_cur [mutable, protected]

Definition at line 306 of file vnl_sparse_matrix.h.

template<class T>
bool vnl_sparse_matrix< T >::itr_isreset [mutable, protected]

Definition at line 307 of file vnl_sparse_matrix.h.

template<class T>
unsigned int vnl_sparse_matrix< T >::itr_row [mutable, protected]

Definition at line 305 of file vnl_sparse_matrix.h.

template<class T>
unsigned int vnl_sparse_matrix< T >::rs_ [protected]

Definition at line 302 of file vnl_sparse_matrix.h.


The documentation for this class was generated from the following files: