bpp-core3  3.0.0
bpp::LinearMatrix< Scalar > Class Template Reference

Matrix storage in one vector. More...

#include <Bpp/Numeric/Matrix/Matrix.h>

+ Inheritance diagram for bpp::LinearMatrix< Scalar >:
+ Collaboration diagram for bpp::LinearMatrix< Scalar >:

Public Member Functions

 LinearMatrix ()
 Build a 0 x 0 matrix. More...
 
 LinearMatrix (size_t nRow, size_t nCol)
 build a nRow x nCol matrix. More...
 
 LinearMatrix (const Matrix< Scalar > &m)
 
LinearMatrixoperator= (const Matrix< Scalar > &m)
 
virtual ~LinearMatrix ()
 Destructor. More...
 
LinearMatrixclone () const
 Create a copy of this object and send a pointer to it. More...
 
const Scalar & operator() (size_t i, size_t j) const
 
Scalar & operator() (size_t i, size_t j)
 
size_t getNumberOfRows () const
 
size_t getNumberOfColumns () const
 
std::vector< Scalar > row (size_t i) const
 
std::vector< Scalar > col (size_t j) const
 
void resize (size_t nRows, size_t nCols)
 Resize the matrix. More...
 
void resize (size_t nRows, size_t nCols, bool keepValues)
 Resize the matrix. More...
 
virtual bool equals (const Matrix &m, double threshold=NumConstants::TINY())
 

Private Member Functions

void resize_ (size_t nRows, size_t nCols)
 Internal basic resize fonctionnalities. More...
 

Private Attributes

std::vector< Scalar > m_
 
size_t rows_
 
size_t cols_
 

Detailed Description

template<class Scalar>
class bpp::LinearMatrix< Scalar >

Matrix storage in one vector.

This Matrix is a simple vector of Scalar of size n x m. Element access is in $O(1)$ but resizing the matrix while keeping the old values is in $O(nm)$.

Basic usage:

LinearMatrix<int> m(3, 2); // Create a 3x2 matrix of int
m(1, 2) = 5; // Set the value of element at row = 1, col = 2 to 5
int x = m(0, 1); // Get the value of element at row = 0, col = 1;
Author
Sylvain Gaillard

Definition at line 355 of file Matrix.h.

Constructor & Destructor Documentation

◆ LinearMatrix() [1/3]

template<class Scalar >
bpp::LinearMatrix< Scalar >::LinearMatrix ( )
inline

Build a 0 x 0 matrix.

Definition at line 367 of file Matrix.h.

References bpp::LinearMatrix< Scalar >::resize_().

Referenced by bpp::LinearMatrix< Scalar >::clone().

◆ LinearMatrix() [2/3]

template<class Scalar >
bpp::LinearMatrix< Scalar >::LinearMatrix ( size_t  nRow,
size_t  nCol 
)
inline

build a nRow x nCol matrix.

Definition at line 374 of file Matrix.h.

References bpp::LinearMatrix< Scalar >::resize_().

◆ LinearMatrix() [3/3]

template<class Scalar >
bpp::LinearMatrix< Scalar >::LinearMatrix ( const Matrix< Scalar > &  m)
inline

◆ ~LinearMatrix()

template<class Scalar >
virtual bpp::LinearMatrix< Scalar >::~LinearMatrix ( )
inlinevirtual

Destructor.

Definition at line 408 of file Matrix.h.

Member Function Documentation

◆ clone()

template<class Scalar >
LinearMatrix* bpp::LinearMatrix< Scalar >::clone ( ) const
inlinevirtual

Create a copy of this object and send a pointer to it.

Returns
A pointer toward the copy object.

Implements bpp::Clonable.

Definition at line 411 of file Matrix.h.

References bpp::LinearMatrix< Scalar >::LinearMatrix().

◆ col()

template<class Scalar >
std::vector<Scalar> bpp::LinearMatrix< Scalar >::col ( size_t  j) const
inlinevirtual
Returns
the column at position j as a vector.
Parameters
jThe index of the column.

Implements bpp::Matrix< Scalar >.

Definition at line 431 of file Matrix.h.

References bpp::LinearMatrix< Scalar >::getNumberOfRows(), and bpp::LinearMatrix< Scalar >::operator()().

◆ equals()

template<class Scalar >
virtual bool bpp::Matrix< Scalar >::equals ( const Matrix< Scalar > &  m,
double  threshold = NumConstants::TINY() 
)
inlinevirtualinherited

◆ getNumberOfColumns()

template<class Scalar >
size_t bpp::LinearMatrix< Scalar >::getNumberOfColumns ( ) const
inlinevirtual
Returns
The number of columns.

Implements bpp::Matrix< Scalar >.

Definition at line 419 of file Matrix.h.

References bpp::LinearMatrix< Scalar >::cols_.

Referenced by bpp::LinearMatrix< Scalar >::resize(), and bpp::LinearMatrix< Scalar >::row().

◆ getNumberOfRows()

template<class Scalar >
size_t bpp::LinearMatrix< Scalar >::getNumberOfRows ( ) const
inlinevirtual
Returns
The number of rows.

Implements bpp::Matrix< Scalar >.

Definition at line 417 of file Matrix.h.

References bpp::LinearMatrix< Scalar >::rows_.

Referenced by bpp::LinearMatrix< Scalar >::col(), and bpp::LinearMatrix< Scalar >::resize().

◆ operator()() [1/2]

template<class Scalar >
Scalar& bpp::LinearMatrix< Scalar >::operator() ( size_t  i,
size_t  j 
)
inlinevirtual
Returns
$m_{i,j}$.
Parameters
irow index.
jcolumn index.

Implements bpp::Matrix< Scalar >.

Definition at line 415 of file Matrix.h.

References bpp::LinearMatrix< Scalar >::cols_, and bpp::LinearMatrix< Scalar >::m_.

◆ operator()() [2/2]

template<class Scalar >
const Scalar& bpp::LinearMatrix< Scalar >::operator() ( size_t  i,
size_t  j 
) const
inlinevirtual

◆ operator=()

◆ resize() [1/2]

template<class Scalar >
void bpp::LinearMatrix< Scalar >::resize ( size_t  nRows,
size_t  nCols 
)
inlinevirtual

Resize the matrix.

Parameters
nRowsThe new number of rows.
nColsThe new number of columns.

This method resize the matrix keeping old data in place.

See also
LinearMatrix::resize(size_t nRow, size_t nCol, bool keepValues)

Implements bpp::Matrix< Scalar >.

Definition at line 447 of file Matrix.h.

◆ resize() [2/2]

template<class Scalar >
void bpp::LinearMatrix< Scalar >::resize ( size_t  nRows,
size_t  nCols,
bool  keepValues 
)
inline

Resize the matrix.

This task may be memory consumming if keepValues is true because it use a copy of the input matrix to keep trace of the values.

Parameters
nRowsthe new number of rows
nColsthe new number of columns
keepValuesif old values must be kept in the resized matrix. If keepValues = false, old values are still in the matrix but not at the same positions. For instance:
LinearMatrix<int> m(3, 2);
for (size_t i = 0 ; i < m.getNumberOfRows() ; i++) {
for (size_t j = 0 ; j < m.getNumberOfColumns() ; j++) {
m(i, j) = i * m.nCols() + j + 1;
}
}
// 3x2
// [
// [1, 2]
// [3, 4]
// [5, 6]
// ]
LinearMatrix<int> m2 = m;
m2.resize(2, 4, false); // resize the matrix with keepValues = false
// 2x4
// [
// [1, 2, 3, 4]
// [5, 6, 0, 0]
// ]
LinearMatrix<int> m3 = m;
m3.resize(2, 4, true); // resize the matrix with keepValues = true
// 2x4
// [
// [1, 2, 0, 0]
// [3, 4, 0, 0]
// ]
static void print(const Matrix &m, std::ostream &out=std::cout)
Print a matrix to a stream.
Definition: MatrixTools.h:730

Definition at line 495 of file Matrix.h.

References bpp::LinearMatrix< Scalar >::getNumberOfColumns(), bpp::LinearMatrix< Scalar >::getNumberOfRows(), bpp::LinearMatrix< Scalar >::operator()(), and bpp::LinearMatrix< Scalar >::resize_().

◆ resize_()

template<class Scalar >
void bpp::LinearMatrix< Scalar >::resize_ ( size_t  nRows,
size_t  nCols 
)
inlineprivate

◆ row()

template<class Scalar >
std::vector<Scalar> bpp::LinearMatrix< Scalar >::row ( size_t  i) const
inlinevirtual
Returns
the row at position i as a vector.
Parameters
iThe index of the row.

Implements bpp::Matrix< Scalar >.

Definition at line 421 of file Matrix.h.

References bpp::LinearMatrix< Scalar >::getNumberOfColumns(), and bpp::LinearMatrix< Scalar >::operator()().

Member Data Documentation

◆ cols_

◆ m_

◆ rows_


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