YARP
Yet Another Robot Platform
yarp::sig::Matrix Class Reference

A class for a Matrix. More...

#include <yarp/sig/Matrix.h>

+ Inheritance diagram for yarp::sig::Matrix:

Detailed Description

A class for a Matrix.

A Matrix can be sent/read to/from a port. Use the [] operator to access the r row of the matrix. The function returns a pointer so [][] access the r,c element in the matrix.

Definition at line 45 of file Matrix.h.

Public Member Functions

 Matrix ()
 
 Matrix (size_t r, size_t c)
 
 Matrix (const Matrix &m)
 Copy constructor. More...
 
 ~Matrix ()
 Destructor. More...
 
const Matrixoperator= (const Matrix &r)
 Copy operator. More...
 
const Matrixoperator= (double v)
 Set all elements of the matrix to a given value. More...
 
size_t rows () const
 Return number of rows. More...
 
size_t cols () const
 Return number of columns. More...
 
void resize (size_t r, size_t c)
 Resize the matrix, if matrix is not empty preserve old content. More...
 
double * operator[] (size_t r)
 Single element access, no range check. More...
 
const double * operator[] (size_t r) const
 Single element access, no range check (const version). More...
 
const double & operator() (size_t r, size_t c) const
 Single element access, no range check. More...
 
double & operator() (size_t r, size_t c)
 Single element access, no range check. More...
 
void zero ()
 Zero the matrix. More...
 
bool setRow (size_t row, const Vector &r)
 Set a row of the matrix copying the values from a vector: the vector length must be equal to the number of columns of the matrix. More...
 
bool setCol (size_t col, const Vector &c)
 Set a column of the matrix copying the values from a vector: the vector length must be equal to the number of rows of the matrix. More...
 
Matrix transposed () const
 Return the transposed of the matrix. More...
 
const Matrixeye ()
 Build an identity matrix, don't resize. More...
 
const Matrixdiagonal (const Vector &d)
 Build a diagonal matrix, don't resize. More...
 
Matrix submatrix (size_t r1, size_t r2, size_t c1, size_t c2) const
 Extract a submatrix from (r1,c1) to (r2,c2) (extremes included), as in Matlab B=A(r1:r2, c1:c2). More...
 
bool setSubmatrix (const Matrix &m, size_t r, size_t c)
 Set a portion of this matrix with the values of the specified matrix m. More...
 
bool setSubrow (const Vector &v, size_t r, size_t c)
 Set a portion of a row of this matrix with the values of the specified vector v. More...
 
bool setSubcol (const Vector &v, size_t r, size_t c)
 Set a portion of a column of this matrix with the values of the specified vector v. More...
 
Vector getRow (size_t r) const
 Get a row of the matrix as a vector. More...
 
Vector getCol (size_t c) const
 Get a columns of the matrix as a vector. More...
 
Matrix removeCols (size_t first_col, size_t how_many)
 Modifies the matrix, removing one or more columns from it. More...
 
Matrix removeRows (size_t first_row, size_t how_many)
 Modifies the matrix, removing one or more rows from it. More...
 
Vector subrow (size_t r, size_t c, size_t size) const
 Get a subrow of the matrix as a vector. More...
 
Vector subcol (size_t r, size_t c, size_t size) const
 Get a subcolumn of the matrix as a vector. More...
 
std::string toString (int precision=-1, int width=-1, const char *endRowStr="\) const
 Print matrix to a string. More...
 
double * data ()
 Return a pointer to the first element. More...
 
const double * data () const
 Return a pointer to the first element (const version). More...
 
bool operator== (const yarp::sig::Matrix &r) const
 True iff all elements of a match all element of b. More...
 
virtual bool read (yarp::os::ConnectionReader &connection) override
 Read this object from a network connection. More...
 
virtual bool write (yarp::os::ConnectionWriter &connection) const override
 Write vector to a connection. More...
 
- Public Member Functions inherited from yarp::os::Portable
virtual Type getType () const
 
- Public Member Functions inherited from yarp::os::PortReader
virtual ~PortReader ()
 Destructor. More...
 
virtual Type getReadType () const
 
- Public Member Functions inherited from yarp::os::PortWriter
virtual ~PortWriter ()
 Destructor. More...
 
virtual void onCompletion () const
 This is called when the port has finished all writing operations. More...
 
virtual void onCommencement () const
 This is called when the port is about to begin writing operations. More...
 
virtual yarp::os::Type getWriteType () const
 

Private Member Functions

void updatePointers ()
 Update pointer to data, call this every time you change the size of the object. More...
 

Private Attributes

double * storage
 
double ** matrix
 
size_t nrows
 
size_t ncols
 

Additional Inherited Members

- Static Public Member Functions inherited from yarp::os::Portable
static bool copyPortable (PortWriter &writer, PortReader &reader)
 Copy one portable to another, via writing and reading. More...
 

Constructor & Destructor Documentation

◆ Matrix() [1/3]

yarp::sig::Matrix::Matrix ( )
inline

Definition at line 61 of file Matrix.h.

◆ Matrix() [2/3]

Matrix::Matrix ( size_t  r,
size_t  c 
)

Definition at line 551 of file Matrix.cpp.

◆ Matrix() [3/3]

Matrix::Matrix ( const Matrix m)

Copy constructor.

Definition at line 562 of file Matrix.cpp.

◆ ~Matrix()

Matrix::~Matrix ( )

Destructor.

Definition at line 259 of file Matrix.cpp.

Member Function Documentation

◆ cols()

size_t yarp::sig::Matrix::cols ( ) const
inline

Return number of columns.

Definition at line 101 of file Matrix.h.

◆ data() [1/2]

double* yarp::sig::Matrix::data ( )
inline

Return a pointer to the first element.

Returns
the pointer to the first element (or NULL if either dimension of the matrix is 0)

Definition at line 304 of file Matrix.h.

◆ data() [2/2]

const double* yarp::sig::Matrix::data ( ) const
inline

Return a pointer to the first element (const version).

Returns
the (const) pointer to the first element (or NULL if either dimension of the matrix is 0)

Definition at line 311 of file Matrix.h.

◆ diagonal()

const Matrix & Matrix::diagonal ( const Vector d)

Build a diagonal matrix, don't resize.

Returns
a reference to the object.

Definition at line 460 of file Matrix.cpp.

◆ eye()

const Matrix & Matrix::eye ( )

Build an identity matrix, don't resize.

Returns
a reference to the object.

Definition at line 446 of file Matrix.cpp.

◆ getCol()

Vector Matrix::getCol ( size_t  c) const

Get a columns of the matrix as a vector.

Parameters
cthe column number
Returns
a vector which contains the requested row

Definition at line 409 of file Matrix.cpp.

◆ getRow()

Vector Matrix::getRow ( size_t  r) const

Get a row of the matrix as a vector.

Parameters
rthe row number
Returns
a vector which contains the requested row

Definition at line 398 of file Matrix.cpp.

◆ operator()() [1/2]

const double& yarp::sig::Matrix::operator() ( size_t  r,
size_t  c 
) const
inline

Single element access, no range check.

Parameters
rrow number.
ccol number.
Returns
a reference to the element.

Definition at line 135 of file Matrix.h.

◆ operator()() [2/2]

double& yarp::sig::Matrix::operator() ( size_t  r,
size_t  c 
)
inline

Single element access, no range check.

Parameters
rrow number.
ccol number.
Returns
a (const) reference to the element.

Definition at line 144 of file Matrix.h.

◆ operator=() [1/2]

const Matrix & Matrix::operator= ( const Matrix r)

Copy operator.

Definition at line 224 of file Matrix.cpp.

◆ operator=() [2/2]

const Matrix & Matrix::operator= ( double  v)

Set all elements of the matrix to a given value.

Parameters
va scalar
Returns
a reference to the object

Definition at line 250 of file Matrix.cpp.

◆ operator==()

bool Matrix::operator== ( const yarp::sig::Matrix r) const

True iff all elements of a match all element of b.

Definition at line 474 of file Matrix.cpp.

◆ operator[]() [1/2]

double* yarp::sig::Matrix::operator[] ( size_t  r)
inline

Single element access, no range check.

Returns a pointer to the r-th row.

Parameters
rrow number.
Returns
a pointer to the first element of the row.

Definition at line 117 of file Matrix.h.

◆ operator[]() [2/2]

const double* yarp::sig::Matrix::operator[] ( size_t  r) const
inline

Single element access, no range check (const version).

Returns a pointer to the r-th row.

Parameters
rrow number.
Returns
a (const) pointer to the first element of the row.

Definition at line 126 of file Matrix.h.

◆ read()

bool Matrix::read ( yarp::os::ConnectionReader reader)
overridevirtual

Read this object from a network connection.

Override this for your particular class.

Parameters
readeran interface to the network connection for reading
Returns
true iff the object is successfully read

Implements yarp::os::Portable.

Definition at line 116 of file Matrix.cpp.

◆ removeCols()

Matrix Matrix::removeCols ( size_t  first_col,
size_t  how_many 
)

Modifies the matrix, removing one or more columns from it.

Parameters
first_colthe number of the first column to remove
how_manythe number of columns to remove
Returns
the matrix with the specified columns removed

Definition at line 330 of file Matrix.cpp.

◆ removeRows()

Matrix Matrix::removeRows ( size_t  first_row,
size_t  how_many 
)

Modifies the matrix, removing one or more rows from it.

Parameters
first_rowthe number of the first row to remove
how_manythe number of rows to remove
Returns
the matrix with the specified rows removed

Definition at line 358 of file Matrix.cpp.

◆ resize()

void Matrix::resize ( size_t  r,
size_t  c 
)

Resize the matrix, if matrix is not empty preserve old content.

Parameters
rnumber of rows
cnumber of columns

Definition at line 268 of file Matrix.cpp.

◆ rows()

size_t yarp::sig::Matrix::rows ( ) const
inline

Return number of rows.

Definition at line 95 of file Matrix.h.

◆ setCol()

bool Matrix::setCol ( size_t  col,
const Vector c 
)

Set a column of the matrix copying the values from a vector: the vector length must be equal to the number of rows of the matrix.

Parameters
colthe column number
ca vector which contains the desired values for the column
Returns
true if operation succeeds, false if not

Definition at line 507 of file Matrix.cpp.

◆ setRow()

bool Matrix::setRow ( size_t  row,
const Vector r 
)

Set a row of the matrix copying the values from a vector: the vector length must be equal to the number of columns of the matrix.

Parameters
rowthe row number
rvector which contains the desired values for the row
Returns
true if operation succeeds, false if not

Definition at line 496 of file Matrix.cpp.

◆ setSubcol()

bool Matrix::setSubcol ( const Vector v,
size_t  r,
size_t  c 
)

Set a portion of a column of this matrix with the values of the specified vector v.

The portion to be set is from row r to row r+v.size()-1.

Parameters
vvector containing the values to set
rstart row index
cindex of the column to set
Returns
true if the operation succeeded, false otherwise

Definition at line 540 of file Matrix.cpp.

◆ setSubmatrix()

bool Matrix::setSubmatrix ( const Matrix m,
size_t  r,
size_t  c 
)

Set a portion of this matrix with the values of the specified matrix m.

The portion to be set is from row r to row r+m.rows()-1 and from column c to column c+m.cols()-1.

Parameters
mmatrix containing the values to set
rstart row
cstart column
Returns
true if the operation succeeded, false otherwise

Definition at line 518 of file Matrix.cpp.

◆ setSubrow()

bool Matrix::setSubrow ( const Vector v,
size_t  r,
size_t  c 
)

Set a portion of a row of this matrix with the values of the specified vector v.

The portion to be set is from column c to column c+v.size()-1.

Parameters
vvector containing the values to set
rindex of the row to set
cstart column
Returns
true if the operation succeeded, false otherwise

Definition at line 529 of file Matrix.cpp.

◆ subcol()

Vector Matrix::subcol ( size_t  r,
size_t  c,
size_t  size 
) const

Get a subcolumn of the matrix as a vector.

Parameters
rthe first row number
cthe column number
sizethe size of the subcolumn
Returns
a vector which contains the requested subcolumn

Definition at line 433 of file Matrix.cpp.

◆ submatrix()

Matrix yarp::sig::Matrix::submatrix ( size_t  r1,
size_t  r2,
size_t  c1,
size_t  c2 
) const
inline

Extract a submatrix from (r1,c1) to (r2,c2) (extremes included), as in Matlab B=A(r1:r2, c1:c2).

Parameters
r1start row
c1start column
r2end row
c2end column
Returns
the sumbatrix

Definition at line 196 of file Matrix.h.

◆ subrow()

Vector Matrix::subrow ( size_t  r,
size_t  c,
size_t  size 
) const

Get a subrow of the matrix as a vector.

Parameters
rthe row number
cthe first column number
sizethe size of the subrow
Returns
a vector which contains the requested subrow

Definition at line 420 of file Matrix.cpp.

◆ toString()

std::string Matrix::toString ( int  precision = -1,
int  width = -1,
const char *  endRowStr = "\n" 
) const

Print matrix to a string.

Quick implementation, space for improvement.

Useful for display, writing to text. To get a nice format the optional parameters precision and width may be used (same meaning as in printf and cout).

Parameters
precisionthe number of digits to be printed after the decimal point.
widthminimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is never truncated.
endRowStrstring appended at the end of every row, except for the last one. If width is specified the inter-value separator is a blank space, otherwise it is a tab. Warning: the string format might change in the future. This method is here to ease debugging.
Returns
a const string which contain the matrix in text form.

Definition at line 173 of file Matrix.cpp.

◆ transposed()

Matrix Matrix::transposed ( ) const

Return the transposed of the matrix.

Returns
the transposed copy of the matrix.

Definition at line 386 of file Matrix.cpp.

◆ updatePointers()

void Matrix::updatePointers ( )
private

Update pointer to data, call this every time you change the size of the object.

Definition at line 210 of file Matrix.cpp.

◆ write()

bool Matrix::write ( yarp::os::ConnectionWriter connection) const
overridevirtual

Write vector to a connection.

return true iff a vector was written correctly

Implements yarp::os::Portable.

Definition at line 143 of file Matrix.cpp.

◆ zero()

void Matrix::zero ( )

Zero the matrix.

Set all elements of the matrix to zero.

Definition at line 325 of file Matrix.cpp.

Member Data Documentation

◆ matrix

double** yarp::sig::Matrix::matrix
private

Definition at line 49 of file Matrix.h.

◆ ncols

size_t yarp::sig::Matrix::ncols
private

Definition at line 52 of file Matrix.h.

◆ nrows

size_t yarp::sig::Matrix::nrows
private

Definition at line 51 of file Matrix.h.

◆ storage

double* yarp::sig::Matrix::storage
private

Definition at line 48 of file Matrix.h.


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