Sparse Class Reference

The Sparse class. More...

#include <matpackII.h>

List of all members.

Public Member Functions

 Sparse ()
 Default constructor.
 Sparse (Index r, Index c)
 Constructor setting size.
 Sparse (const Sparse &m)
 Copy constructor from another Sparse.
void insert_row (Index r, Vector v)
 Insert row function.
void make_I (Index r, Index c)
 Make Identity matrix.
void resize (Index r, Index c)
 Resize function.
 ~Sparse ()
 Destructor for Sparse.
Index nrows () const
 Returns the number of rows.
Index ncols () const
 Returns the number of columns.
Index nnz () const
 Returns the number of nonzero elements.
const vector< Numeric > * data () const
const vector< Index > * rowind () const
const vector< Index > * colptr () const
Numericrw (Index r, Index c)
 Read and write index operator.
Numeric ro (Index r, Index c) const
 Read only index operator.
Numeric operator() (Index r, Index c) const
 Plain index operator.
Sparseoperator= (const Sparse &m)
 Assignment from another Sparse.

Private Attributes

vector< Numeric > * mdata
 The actual data values.
vector< Index > * mrowind
 Row indices.
vector< Index > * mcolptr
 Pointers to first data element for each column.
Index mrr
 Number of rows in the sparse matrix.
Index mcr
 Number of rows in the sparse matrix.

Friends

ostream & operator<< (ostream &os, const Sparse &v)
 Output operator for Sparse.
void abs (Sparse &A, const Sparse &B)
 Absolute value of sparse matrix elements.
void mult (VectorView y, const Sparse &M, ConstVectorView x)
 Sparse matrix - Vector multiplication.
void mult (MatrixView A, const Sparse &B, ConstMatrixView C)
 SparseMatrix - Matrix multiplication.
void mult (Sparse &A, const Sparse &B, const Sparse &C)
 Sparse - Sparse multiplication.
void transpose (Sparse &A, const Sparse &B)
 Transpose of sparse matrix.


Detailed Description

The Sparse class.

The chosen storage format is the `compressed column' format. This is the same format used by Matlab. See Matlab User Guide for a description.

Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 55 of file matpackII.h.


Constructor & Destructor Documentation

Sparse::Sparse (  ) 

Default constructor.

Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 205 of file matpackII.cc.

Sparse::Sparse ( Index  r,
Index  c 
)

Constructor setting size.

Elements *mdata and *mrowind have to grow later on, when we add data elements. But *mcolptr always has the dimension of the number of columns of the matrix plus one, so it is allocated directly. (And properly initialized to zero.)

Why is there an extra element in *mcolptr? We store also the index behind* the last element of the last column. Or in other words the starting index that the next column *would* have. This just safes a little time when computing indices. Also, this corresponds to the number of nonzero elements.

Parameters:
r Row dimension of new sparse matrix.
c Column dimension of new sparse matrix.
Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 234 of file matpackII.cc.

Sparse::Sparse ( const Sparse m  ) 

Copy constructor from another Sparse.

This automatically sets the size and copies the data.

Parameters:
m The other Sparse to copy from.
Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 254 of file matpackII.cc.

Sparse::~Sparse (  ) 

Destructor for Sparse.

This is important, since Sparse uses new to allocate storage.

Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 274 of file matpackII.cc.

References mcolptr, mdata, and mrowind.


Member Function Documentation

void Sparse::insert_row ( Index  r,
Vector  v 
)

Insert row function.

Inserts a Vector as row of elements at the given position.

The row index must agree with the size of the matrix. This function can not be used to expand the matrix. Only non-zero values will be stored. If the destination row already exist it will be overwritten.

Parameters:
r Where to insert the row
v Vector to be inserted.
Author:
Mattias Ekström <ekstrom@rss.chalmers.se>
Date:
2003-08-11

Definition at line 296 of file matpackII.cc.

References copy(), mcolptr, mcr, mdata, mrowind, mrr, and ConstVectorView::nelem().

Referenced by antenna1d_matrix(), mixer_matrix(), sensor_responseBeamSwitching(), sensor_responseMultiMixerBackend(), spectrometer_matrix(), and test44().

void Sparse::make_I ( Index  r,
Index  c 
)

Make Identity matrix.

This functions sets the Sparse matrix to be an identity matrix.

The matrix will be remade to fit the given number of rows and columns.

Parameters:
r New row dimension.
c New column dimension.
Author:
Mattias Ekström
Date:
2003-12-05

Definition at line 467 of file matpackII.cc.

References mcolptr, mcr, mdata, min, mrowind, and mrr.

Referenced by sensor_responseInit(), and test47().

void Sparse::resize ( Index  r,
Index  c 
)

Resize function.

If the size is already correct this function does nothing.

All data is lost after resizing! The new Sparse is not initialised so it will be empty.

Parameters:
r New row dimension.
c New column dimension.
Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 509 of file matpackII.cc.

References mcolptr, mcr, mdata, mrowind, and mrr.

Referenced by antenna1d_matrix(), mixer_matrix(), sensor_responseAntenna(), sensor_responseBackend(), sensor_responseBeamSwitching(), sensor_responseMixer(), sensor_responseMultiMixerBackend(), spectrometer_matrix(), and xml_read_from_stream().

Index Sparse::nrows (  )  const

Index Sparse::ncols (  )  const

Index Sparse::nnz (  )  const

Returns the number of nonzero elements.

Definition at line 64 of file matpackII.cc.

References mcolptr.

Referenced by xml_write_to_stream().

const vector<Numeric>* Sparse::data (  )  const [inline]

Definition at line 79 of file matpackII.h.

References mdata.

Referenced by xml_write_to_stream().

const vector<Index>* Sparse::rowind (  )  const [inline]

Definition at line 80 of file matpackII.h.

References mrowind.

Referenced by xml_write_to_stream().

const vector<Index>* Sparse::colptr (  )  const [inline]

Definition at line 81 of file matpackII.h.

References mcolptr.

Referenced by xml_write_to_stream().

Numeric & Sparse::rw ( Index  r,
Index  c 
)

Read and write index operator.

This has to correctly handle two cases:

1. The data element exists. In this case the operator acts similar to the const index operator in that the element is returned.

2. The element does not exist. In this case it is created.

Parameters:
r Row index.
c Column index.
Returns:
The data element with these indices.
Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 89 of file matpackII.cc.

References mcolptr, mcr, mdata, mrowind, and mrr.

Referenced by mult(), test3(), test40(), test41(), test42(), test43(), test44(), test48(), and xml_read_from_stream().

Numeric Sparse::ro ( Index  r,
Index  c 
) const

Read only index operator.

This has to correctly handle two cases:

1. The data element exists. In this case the element is returned.

2. The element does not exist. In this case the value 0 is returned.

Parameters:
r Row index.
c Column index.
Returns:
The data element with these indices, or zero.
Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 166 of file matpackII.cc.

References mcr, mdata, and mrr.

Referenced by operator()().

Numeric Sparse::operator() ( Index  r,
Index  c 
) const

Plain index operator.

This is the same as the .ro index operator.

Parameters:
r Row index.
c Column index.
Returns:
The data element with these indices.
Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 148 of file matpackII.cc.

References ro().

Sparse & Sparse::operator= ( const Sparse m  ) 

Assignment from another Sparse.

It is crucial that we implement this, otherwise the compiler creates a default assignment operator which does not do the right thing. (It will lead to segmentation faults).

The dimensions of the target sparse matrix are adjusted automatically. This is important, so that structures containing Sparse are copied correctly.

Parameters:
m The other Sparse to copy to this one.
Returns:
This Sparse, by tradition, to allow assignment chains.
Author:
Stefan Buehler
Date:
2002-12-19

Definition at line 544 of file matpackII.cc.

References mcolptr, mcr, mdata, mrowind, and mrr.


Friends And Related Function Documentation

ostream& operator<< ( ostream &  os,
const Sparse v 
) [friend]

Output operator for Sparse.

Parameters:
os Output stream.
v Sparse matrix to print.
Returns:
Output stream.
Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 592 of file matpackII.cc.

void abs ( Sparse A,
const Sparse B 
) [friend]

Absolute value of sparse matrix elements.

Computes the absolute values of the elements in sparse matrix B.

The output matrix A must have been initialized with the correct size.

Parameters:
A Output: Absolute value matrix.
B Original matrix.
Author:
Mattias Ekstrom
Date:
2005-03-21

Definition at line 634 of file matpackII.cc.

void mult ( VectorView  y,
const Sparse M,
ConstVectorView  x 
) [friend]

Sparse matrix - Vector multiplication.

This calculates the product

y = M*x, where M is sparse.

Output comes first!

Dimensions of y, M, and x must match. No memory reallocation takes place, only the data is copied.

Parameters:
y Output: The multiplication result.
M Matrix for multiplication (sparse).
x Vector for multiplication.
Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 676 of file matpackII.cc.

void mult ( MatrixView  A,
const Sparse B,
ConstMatrixView  C 
) [friend]

SparseMatrix - Matrix multiplication.

Calculates the matrix product:

A = B*C, where B is sparse.

Output comes first!

Dimensions of A, B, and C must match. No memory reallocation takes place, only the data is copied.

Parameters:
A Output: Result matrix (full).
B First matrix to multiply (sparse).
C Second matrix to multiply (full).
Author:
Stefan Buehler <sbuehler@ltu.se>
Date:
Tue Jul 15 15:05:40 2003

Definition at line 729 of file matpackII.cc.

void mult ( Sparse A,
const Sparse B,
const Sparse C 
) [friend]

Sparse - Sparse multiplication.

Calculates A = B*C, where result A is sparse, and B and C are also sparse.

Output comes first!

Dimensions of A, B, and C must match. No memory reallocation takes place, only the data is copied.

Parameters:
A Output: Result matrix.
B First product matrix.
C Second product matrix.
Author:
Mattias Ekstroem
Date:
2003-08-06

Definition at line 875 of file matpackII.cc.

void transpose ( Sparse A,
const Sparse B 
) [friend]

Transpose of sparse matrix.

Computes the transpose of the sparse matrix B.

The output matrix A must have been initialized with the correct size.

Parameters:
A Output: Transposed matrix.
B Original matrix.
Author:
Mattias Ekstroem
Date:
2003-08-05

Definition at line 785 of file matpackII.cc.

Referenced by mult().


Member Data Documentation

The actual data values.

Definition at line 101 of file matpackII.h.

Referenced by abs(), data(), insert_row(), make_I(), mult(), operator<<(), operator=(), resize(), ro(), rw(), transpose(), and ~Sparse().

Row indices.

Definition at line 103 of file matpackII.h.

Referenced by abs(), insert_row(), make_I(), mult(), operator<<(), operator=(), resize(), rowind(), rw(), transpose(), and ~Sparse().

Pointers to first data element for each column.

Definition at line 105 of file matpackII.h.

Referenced by abs(), colptr(), insert_row(), make_I(), mult(), nnz(), operator<<(), operator=(), resize(), rw(), transpose(), and ~Sparse().

Index Sparse::mrr [private]

Number of rows in the sparse matrix.

Definition at line 107 of file matpackII.h.

Referenced by insert_row(), make_I(), nrows(), operator=(), resize(), ro(), and rw().

Index Sparse::mcr [private]

Number of rows in the sparse matrix.

Definition at line 109 of file matpackII.h.

Referenced by insert_row(), make_I(), mult(), ncols(), operator=(), resize(), ro(), rw(), and transpose().


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

Generated on Mon Mar 23 14:07:02 2009 for ARTS by  doxygen 1.5.6