ARTS  2.0.49
Sparse Class Reference

The Sparse class. More...

#include <matpackII.h>

Public Member Functions

 Sparse ()
 Default constructor. More...
 
 Sparse (Index r, Index c)
 Constructor setting size. More...
 
 Sparse (const Sparse &m)
 Copy constructor from another Sparse. More...
 
void insert_row (Index r, Vector v)
 Insert row function. More...
 
void make_I (Index r, Index c)
 Make Identity matrix. More...
 
void resize (Index r, Index c)
 Resize function. More...
 
 ~Sparse ()
 Destructor for Sparse. More...
 
Index nrows () const
 Returns the number of rows. More...
 
Index ncols () const
 Returns the number of columns. More...
 
Index nnz () const
 Returns the number of nonzero elements. More...
 
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. More...
 
Numeric ro (Index r, Index c) const
 Read only index operator. More...
 
Numeric operator() (Index r, Index c) const
 Plain index operator. More...
 
Sparseoperator= (const Sparse &m)
 Assignment from another Sparse. More...
 

Private Attributes

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

Friends

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

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 sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 55 of file matpackII.h.

Constructor & Destructor Documentation

◆ Sparse() [1/3]

Sparse::Sparse ( )

Default constructor.

Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 206 of file matpackII.cc.

◆ Sparse() [2/3]

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
rRow dimension of new sparse matrix.
cColumn dimension of new sparse matrix.
Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 235 of file matpackII.cc.

◆ Sparse() [3/3]

Sparse::Sparse ( const Sparse m)

Copy constructor from another Sparse.

This automatically sets the size and copies the data.

Parameters
mThe other Sparse to copy from.
Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 255 of file matpackII.cc.

References mcolptr, mdata, and mrowind.

◆ ~Sparse()

Sparse::~Sparse ( )

Destructor for Sparse.

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

Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 281 of file matpackII.cc.

References mcolptr, mdata, and mrowind.

Member Function Documentation

◆ colptr()

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

Definition at line 81 of file matpackII.h.

References mcolptr.

Referenced by xml_write_to_stream().

◆ data()

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

Definition at line 79 of file matpackII.h.

References mdata.

Referenced by add(), and xml_write_to_stream().

◆ insert_row()

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
rWhere to insert the row
vVector to be inserted.
Author
Mattias Ekstr?m ekstr.nosp@m.om@r.nosp@m.ss.ch.nosp@m.alme.nosp@m.rs.se
Date
2003-08-11

Definition at line 303 of file matpackII.cc.

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

Referenced by antenna1d_matrix(), antenna2d_simplified(), mixer_matrix(), sensor_responseBeamSwitching(), sensor_responseFillFgrid(), sensor_responseFrequencySwitching(), sensor_responseMultiMixerBackend(), sensor_responsePolarisation(), spectrometer_matrix(), and test44().

◆ make_I()

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
rNew row dimension.
cNew column dimension.
Author
Mattias Ekstr?m
Date
2003-12-05

Definition at line 474 of file matpackII.cc.

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

Referenced by sensor_responseInit(), and test47().

◆ ncols()

◆ nnz()

Index Sparse::nnz ( ) const

Returns the number of nonzero elements.

Definition at line 65 of file matpackII.cc.

References mcolptr.

Referenced by Select(), and xml_write_to_stream().

◆ nrows()

◆ operator()()

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

Plain index operator.

This is the same as the .ro index operator.

Parameters
rRow index.
cColumn index.
Returns
The data element with these indices.
Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 149 of file matpackII.cc.

References ro().

◆ operator=()

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
mThe 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 551 of file matpackII.cc.

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

◆ resize()

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
rNew row dimension.
cNew column dimension.
Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 516 of file matpackII.cc.

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

Referenced by antenna1d_matrix(), antenna2d_simplified(), mixer_matrix(), sensor_responseAntenna(), sensor_responseBackend(), sensor_responseBeamSwitching(), sensor_responseFillFgrid(), sensor_responseFrequencySwitching(), sensor_responseMixer(), sensor_responseMultiMixerBackend(), sensor_responsePolarisation(), sensor_responseWMRF(), spectrometer_matrix(), WMRFSelectChannels(), and xml_read_from_stream().

◆ ro()

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
rRow index.
cColumn index.
Returns
The data element with these indices, or zero.
Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 167 of file matpackII.cc.

References mcr, mdata, and mrr.

Referenced by operator()().

◆ rowind()

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

Definition at line 80 of file matpackII.h.

References mrowind.

Referenced by xml_write_to_stream().

◆ rw()

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
rRow index.
cColumn index.
Returns
The data element with these indices.
Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 90 of file matpackII.cc.

Referenced by add(), mult(), sub(), test40(), test41(), test42(), test43(), test44(), test48(), test49(), and xml_read_from_stream().

Friends And Related Function Documentation

◆ abs

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
AOutput: Absolute value matrix.
BOriginal matrix.
Author
Mattias Ekstrom
Date
2005-03-21

Definition at line 641 of file matpackII.cc.

◆ add

void add ( Sparse A,
const Sparse B,
const Sparse C 
)
friend

Sparse - Sparse addition.

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

Output comes first!

Dimensions of B, and C must match. A will be resized.

Parameters
AOutput: Result matrix.
BFirst summand matrix.
CSecond summand matrix.
Author
Oliver Lemke
Date
2009-09-03

Definition at line 974 of file matpackII.cc.

◆ mult [1/3]

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
AOutput: Result matrix (full).
BFirst matrix to multiply (sparse).
CSecond matrix to multiply (full).
Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 736 of file matpackII.cc.

◆ mult [2/3]

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
AOutput: Result matrix.
BFirst product matrix.
CSecond product matrix.
Author
Mattias Ekstroem
Date
2003-08-06

Definition at line 883 of file matpackII.cc.

◆ mult [3/3]

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
yOutput: The multiplication result.
MMatrix for multiplication (sparse).
xVector for multiplication.
Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 683 of file matpackII.cc.

◆ operator<<

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

Output operator for Sparse.

Parameters
osOutput stream.
vSparse matrix to print.
Returns
Output stream.
Author
Stefan Buehler sbueh.nosp@m.ler@.nosp@m.ltu.s.nosp@m.e
Date
Tue Jul 15 15:05:40 2003

Definition at line 599 of file matpackII.cc.

◆ sub

void sub ( Sparse A,
const Sparse B,
const Sparse C 
)
friend

Sparse - Sparse subtraction.

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

Output comes first!

Dimensions of B, and C must match. A will be resized.

Parameters
AOutput: Result matrix.
BFirst subtrahend matrix.
CSecond subtrahend matrix.
Author
Oliver Lemke
Date
2009-09-03

Definition at line 1023 of file matpackII.cc.

◆ transpose

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
AOutput: Transposed matrix.
BOriginal matrix.
Author
Mattias Ekstroem
Date
2003-08-05

Definition at line 792 of file matpackII.cc.

Member Data Documentation

◆ mcolptr

vector<Index>* Sparse::mcolptr
private

Pointers to first data element for each column.

Definition at line 107 of file matpackII.h.

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

◆ mcr

Index Sparse::mcr
private

Number of rows in the sparse matrix.

Definition at line 111 of file matpackII.h.

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

◆ mdata

vector<Numeric>* Sparse::mdata
private

The actual data values.

Definition at line 103 of file matpackII.h.

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

◆ mrowind

vector<Index>* Sparse::mrowind
private

Row indices.

Definition at line 105 of file matpackII.h.

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

◆ mrr

Index Sparse::mrr
private

Number of rows in the sparse matrix.

Definition at line 109 of file matpackII.h.

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


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