Public Types | Public Member Functions | Protected Attributes | Related Functions | List of all members
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]

Public Types

typedef vnl_sparse_matrix_pair< T > pair_t
 
typedef std::vector< pair_trow
 
typedef std::vector< rowvnl_sparse_matrix_elements
 

Public Member Functions

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

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. More...
 
template<class T >
void vsl_b_read (vsl_b_istream &is, vnl_sparse_matrix< T > &v)
 Binary load vnl_sparse_matrix from stream. More...
 
template<class T >
void vsl_print_summary (std::ostream &os, const vnl_sparse_matrix< T > &b)
 Print human readable summary of object to a stream. More...
 

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 108 of file vnl_sparse_matrix.h.

Member Typedef Documentation

◆ pair_t

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

Definition at line 111 of file vnl_sparse_matrix.h.

◆ row

template<class T>
typedef std::vector< pair_t > vnl_sparse_matrix< T >::row

Definition at line 112 of file vnl_sparse_matrix.h.

◆ vnl_sparse_matrix_elements

template<class T>
typedef std::vector< row > vnl_sparse_matrix< T >::vnl_sparse_matrix_elements

Definition at line 113 of file vnl_sparse_matrix.h.

Constructor & Destructor Documentation

◆ vnl_sparse_matrix() [1/3]

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

Construct an empty matrix.

Definition at line 28 of file vnl_sparse_matrix.hxx.

◆ vnl_sparse_matrix() [2/3]

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 36 of file vnl_sparse_matrix.hxx.

◆ vnl_sparse_matrix() [3/3]

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 44 of file vnl_sparse_matrix.hxx.

Member Function Documentation

◆ add()

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 306 of file vnl_sparse_matrix.hxx.

◆ clear()

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

Set all elements to null.

Definition at line 184 of file vnl_sparse_matrix.h.

◆ cols()

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

Get the number of columns in the matrix.

Definition at line 172 of file vnl_sparse_matrix.h.

◆ columns()

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

Get the number of columns in the matrix.

Definition at line 169 of file vnl_sparse_matrix.h.

◆ conjugate_transpose()

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 957 of file vnl_sparse_matrix.hxx.

◆ diag_AtA()

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 481 of file vnl_sparse_matrix.hxx.

◆ empty_row()

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 175 of file vnl_sparse_matrix.h.

◆ get()

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 448 of file vnl_sparse_matrix.hxx.

◆ get_row()

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 160 of file vnl_sparse_matrix.h.

◆ getcolumn()

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

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

Definition at line 667 of file vnl_sparse_matrix.hxx.

◆ getrow()

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

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

Definition at line 658 of file vnl_sparse_matrix.hxx.

◆ mult() [1/3]

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 243 of file vnl_sparse_matrix.hxx.

◆ mult() [2/3]

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 157 of file vnl_sparse_matrix.hxx.

◆ mult() [3/3]

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 67 of file vnl_sparse_matrix.hxx.

◆ next()

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 622 of file vnl_sparse_matrix.hxx.

◆ normalize_rows()

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 873 of file vnl_sparse_matrix.hxx.

◆ operator *() [1/2]

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

multiplication.

Definition at line 778 of file vnl_sparse_matrix.hxx.

◆ operator *() [2/2]

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

scalar multiplication.

Definition at line 835 of file vnl_sparse_matrix.hxx.

◆ operator *=() [1/2]

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

in-place multiplication.

Definition at line 865 of file vnl_sparse_matrix.hxx.

◆ operator *=() [2/2]

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

in-place scalar multiplication.

Definition at line 787 of file vnl_sparse_matrix.hxx.

◆ operator!=()

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

Inequality.

Definition at line 213 of file vnl_sparse_matrix.h.

◆ operator()() [1/2]

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 412 of file vnl_sparse_matrix.hxx.

◆ operator()() [2/2]

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 431 of file vnl_sparse_matrix.hxx.

◆ operator+()

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

addition.

Definition at line 760 of file vnl_sparse_matrix.hxx.

◆ operator+=()

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

in-place addition.

Definition at line 851 of file vnl_sparse_matrix.hxx.

◆ operator-() [1/2]

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

Unary minus.

Definition at line 731 of file vnl_sparse_matrix.hxx.

◆ operator-() [2/2]

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

subtraction.

Definition at line 769 of file vnl_sparse_matrix.hxx.

◆ operator-=()

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

in-place subtraction.

Definition at line 858 of file vnl_sparse_matrix.hxx.

◆ operator/()

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

scalar division.

Definition at line 843 of file vnl_sparse_matrix.hxx.

◆ operator/=()

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

in-place scalar division.

Definition at line 811 of file vnl_sparse_matrix.hxx.

◆ operator=()

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 52 of file vnl_sparse_matrix.hxx.

◆ operator==()

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

Comparison.

Definition at line 685 of file vnl_sparse_matrix.hxx.

◆ pre_mult()

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 269 of file vnl_sparse_matrix.hxx.

◆ put()

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 464 of file vnl_sparse_matrix.hxx.

◆ reset()

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

Resets the internal iterator.

Definition at line 610 of file vnl_sparse_matrix.hxx.

◆ resize()

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 585 of file vnl_sparse_matrix.hxx.

◆ rows()

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

Get the number of rows in the matrix.

Definition at line 166 of file vnl_sparse_matrix.h.

◆ scale_row()

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 553 of file vnl_sparse_matrix.hxx.

◆ set_identity()

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 914 of file vnl_sparse_matrix.hxx.

◆ set_row()

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

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

Set row in the matrix.

Definition at line 503 of file vnl_sparse_matrix.hxx.

◆ set_size()

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 566 of file vnl_sparse_matrix.hxx.

◆ subtract()

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 359 of file vnl_sparse_matrix.hxx.

◆ sum_row()

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 540 of file vnl_sparse_matrix.hxx.

◆ transpose()

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 931 of file vnl_sparse_matrix.hxx.

◆ value()

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

Returns the value pointed to by the internal iterator.

Definition at line 676 of file vnl_sparse_matrix.hxx.

◆ vcat()

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 521 of file vnl_sparse_matrix.hxx.

Friends And Related Function Documentation

◆ vsl_b_read()

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

Binary load vnl_sparse_matrix from stream.

◆ vsl_b_write()

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.

◆ vsl_print_summary()

template<class T >
void vsl_print_summary ( std::ostream &  os,
const vnl_sparse_matrix< T > &  b 
)
related

Print human readable summary of object to a stream.

Member Data Documentation

◆ cs_

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

Definition at line 297 of file vnl_sparse_matrix.h.

◆ elements

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

Definition at line 296 of file vnl_sparse_matrix.h.

◆ itr_cur

template<class T>
row::const_iterator vnl_sparse_matrix< T >::itr_cur
mutableprotected

Definition at line 301 of file vnl_sparse_matrix.h.

◆ itr_isreset

template<class T>
bool vnl_sparse_matrix< T >::itr_isreset
mutableprotected

Definition at line 302 of file vnl_sparse_matrix.h.

◆ itr_row

template<class T>
unsigned int vnl_sparse_matrix< T >::itr_row
mutableprotected

Definition at line 300 of file vnl_sparse_matrix.h.

◆ rs_

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

Definition at line 297 of file vnl_sparse_matrix.h.


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