ARTS  2.4.0(git:4fb77825)
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...
 
void split (Index offset, Index nrows)
 Reduce matrix to the row range [offset, offset + nrows]. More...
 
void insert_row (Index r, Vector v)
 Insert row function. More...
 
void insert_elements (Index nnz, const ArrayOfIndex &rowind, const ArrayOfIndex &colind, ConstVectorView data)
 Insert vector of elements with given row and column indices. More...
 
void resize (Index r, Index c)
 Resize function. More...
 
bool empty () const
 Returns true if variable size is zero. 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...
 
Vector diagonal () const
 Diagonal elements as vector. More...
 
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 &x)
 Add sparse matrix. More...
 
Sparseoperator-= (const Sparse &x)
 Subtract sparse matrix. More...
 
Sparseoperator*= (Numeric x)
 Scale matrix. More...
 
Sparseoperator/= (Numeric x)
 Scale matrix by reciprocal. More...
 
 operator Matrix () const
 Convert to dense matrix. More...
 
void list_elements (Vector &values, ArrayOfIndex &row_indices, ArrayOfIndex &column_indices) const
 List elements in matrix. More...
 
Numericget_element_pointer ()
 
int * get_column_index_pointer ()
 
int * get_row_start_pointer ()
 

Static Public Member Functions

static Sparse diagonal (ConstVectorView v)
 Create a sparse matrix from a vector. More...
 

Private Attributes

Eigen::SparseMatrix< Numeric, Eigen::RowMajor > matrix
 The actual matrix. More...
 

Friends

std::ostream & operator<< (std::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 transpose_mult (VectorView y, const Sparse &M, ConstVectorView x)
 Sparse matrix - Vector multiplication. More...
 
void mult (MatrixView A, const Sparse &B, const ConstMatrixView &C)
 SparseMatrix - Matrix multiplication. More...
 
void mult (MatrixView A, const ConstMatrixView &B, const Sparse &C)
 Matrix - SparseMatrix 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...
 
void id_mat (Sparse &A)
 Sparse identity matrix. More...
 

Detailed Description

The Sparse class.

Wrapper class for Eigen sparse matrices. The storage format used is compressed row storage. Thus inserting of elements in a single row ordered by column index is performed in constant time, if the rows are themselves inserted in increasing order.

Definition at line 60 of file matpackII.h.

Constructor & Destructor Documentation

◆ Sparse() [1/2]

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

◆ Sparse() [2/2]

Sparse::Sparse ( Index  r,
Index  c 
)

Constructor setting size.

Parameters
rRow dimension of new sparse matrix.
cColumn dimension of new sparse matrix.

Definition at line 157 of file matpackII.cc.

Member Function Documentation

◆ diagonal() [1/2]

Vector Sparse::diagonal ( ) const

Diagonal elements as vector.

Extracts the diagonal elements from the sparse matrix. matrix.

Returns
A vector containing the diagonal elements.

Definition at line 172 of file matpackII.cc.

References matrix, min, ncols(), and nrows().

◆ diagonal() [2/2]

Sparse Sparse::diagonal ( ConstVectorView  v)
static

Create a sparse matrix from a vector.

Parameters
vvector containing the diagonal elements.
Returns
Sparse matrix with the elements of the given vector on the diagonal.

Definition at line 161 of file matpackII.cc.

References insert_elements(), and ConstVectorView::nelem().

◆ empty()

bool Sparse::empty ( ) const

Returns true if variable size is zero.

Definition at line 61 of file matpackII.cc.

References matrix.

◆ get_column_index_pointer()

int* Sparse::get_column_index_pointer ( )
inline

Definition at line 123 of file matpackII.h.

References matrix.

Referenced by get_covariance_matrix_block(), and get_variable_value().

◆ get_element_pointer()

Numeric* Sparse::get_element_pointer ( )
inline

Definition at line 122 of file matpackII.h.

References matrix.

Referenced by get_covariance_matrix_block(), and get_variable_value().

◆ get_row_start_pointer()

int* Sparse::get_row_start_pointer ( )
inline

Definition at line 124 of file matpackII.h.

References matrix.

Referenced by get_covariance_matrix_block(), and get_variable_value().

◆ insert_elements()

void Sparse::insert_elements ( Index  nelems,
const ArrayOfIndex rowind,
const ArrayOfIndex colind,
ConstVectorView  data 
)

Insert vector of elements with given row and column indices.

Efficient inserting of a vector of elements into the sparse matrix. Overwrites elements currently in the matrix. The complexity is linear in the number of elements and should therefore be the preferred way of inserting elements into the sparse matrix.

Parameters
nelemsThe number of elements to insert.
rowindA vector containing the row indices.
colindA vector containing the column indices.
dataThe vector containing the elements.

Definition at line 337 of file matpackII.cc.

References data, and matrix.

Referenced by diagonal(), DiagonalMatrix(), InteractiveWorkspace::set_sparse_variable(), and xml_read_from_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.

Definition at line 314 of file matpackII.cc.

References matrix, ncols(), ConstVectorView::nelem(), and nrows().

Referenced by antenna1d_matrix(), antenna2d_interp_response(), met_mm_polarisation_hmatrix(), sensor_responseFrequencySwitching(), sensor_responseMixerBackendPrecalcWeights(), sensor_responsePolarisation(), sensor_responseStokesRotation(), spectrometer_matrix(), and test_insert_row().

◆ list_elements()

void Sparse::list_elements ( Vector values,
ArrayOfIndex row_indices,
ArrayOfIndex column_indices 
) const

List elements in matrix.

Writes the values in the matrix into the given Vector values and the row and column indices into the given ArrayOfIndex objects.

Parameters
valuesThe values contained in the matrix.
row_indicesThe row indices corresponding to the values.
column_indicesThe column indices corresponding to the values.

Definition at line 270 of file matpackII.cc.

References matrix, ncols(), nnz(), nrows(), and Vector::resize().

Referenced by xml_write_to_stream().

◆ ncols()

◆ nnz()

Index Sparse::nnz ( ) const

Returns the number of nonzero elements.

Definition at line 72 of file matpackII.cc.

References matrix.

Referenced by get_covariance_matrix_block(), get_variable_value(), list_elements(), Select(), and xml_write_to_stream().

◆ nrows()

◆ operator Matrix()

Sparse::operator Matrix ( ) const
explicit

Convert to dense matrix.

Converts a given sparse matrix to a dense matrix. Intended mainly for testing purposes.

Returns
The dense representation of the given sparse matrix.

Definition at line 189 of file matpackII.cc.

References ARTS::Var::ncols(), and ARTS::Var::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.

Definition at line 111 of file matpackII.cc.

◆ operator*=()

Sparse & Sparse::operator*= ( Numeric  x)

Scale matrix.

Parameters
xScalar factor to scale the matrix with.
Returns
Reference to the scaled sparse matrix object.

Definition at line 243 of file matpackII.cc.

References matrix, and ARTS::Var::x().

◆ operator+=()

Sparse & Sparse::operator+= ( const Sparse A)

Add sparse matrix.

Element-wise, in-situ sum of two sparse matrices. Matrix must have the same dimensions.

Parameters
AThe sparse matrix to add to the Sparse object.
Returns
Reference to the resulting sum.

Definition at line 215 of file matpackII.cc.

References matrix.

◆ operator-=()

Sparse & Sparse::operator-= ( const Sparse A)

Subtract sparse matrix.

Element-wise, in-situ difference of two sparse matrices. Matrix must have the same dimensions.

Parameters
AThe sparse matrix to add to the Sparse object.
Returns
Reference to the resulting sum.

Definition at line 231 of file matpackII.cc.

References matrix.

◆ operator/=()

Sparse & Sparse::operator/= ( Numeric  x)

Scale matrix by reciprocal.

Parameters
xReciprocal of the factor to scale the matrix with.
Returns
Reference to the scaled sparse matrix object.

Definition at line 255 of file matpackII.cc.

References matrix, and ARTS::Var::x().

◆ 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.

Definition at line 361 of file matpackII.cc.

Referenced by add(), antenna1d_matrix(), antenna2d_interp_response(), DiagonalMatrix(), SparseMatrixIdentity(), spectrometer_matrix(), sub(), test47(), test_identity(), test_insert_row(), test_workspace_methods(), 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 130 of file matpackII.cc.

Referenced by sensor_responseStokesRotation().

◆ 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.

Definition at line 91 of file matpackII.cc.

Referenced by mueller_rotation(), random_fill_matrix(), Select(), test40(), and test49().

◆ split()

void Sparse::split ( Index  offset,
Index  nrows 
)

Reduce matrix to the row range [offset, offset + nrows].

Definition at line 296 of file matpackII.cc.

References matrix, and ncols().

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 394 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 630 of file matpackII.cc.

◆ id_mat

void id_mat ( Sparse A)
friend

Sparse identity matrix.

Set the given Sparse matrix object to the identity matrix. The matrix must be square.

Parameters
AThe matrix to be set to the identity matrix.

Definition at line 647 of file matpackII.cc.

◆ mult [1/4]

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

Matrix - SparseMatrix multiplication.

Calculates the matrix product:

A = B*C, where C 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 539 of file matpackII.cc.

◆ mult [2/4]

void mult ( MatrixView  A,
const Sparse B,
const 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 490 of file matpackII.cc.

◆ mult [3/4]

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

◆ mult [4/4]

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.

Definition at line 417 of file matpackII.cc.

◆ operator<<

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

Output operator for Sparse.

Parameters
osOutput stream.
vSparse matrix to print.
Returns
Output stream.

Definition at line 375 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 667 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 582 of file matpackII.cc.

◆ transpose_mult

void transpose_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.

Definition at line 452 of file matpackII.cc.

Member Data Documentation

◆ matrix


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