YARP  2.3.70.1
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:

Public Member Functions

 Matrix ()
 
 Matrix (int r, int 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...
 
int rows () const
 Return number of rows. More...
 
int cols () const
 Return number of columns. More...
 
void resize (int r, int c)
 Resize the matrix, if matrix is not empty preserve old content. More...
 
double * operator[] (int r)
 Single element access, no range check. More...
 
const double * operator[] (int r) const
 Single element access, no range check (const version). More...
 
const double & operator() (int r, int c) const
 Single element access, no range check. More...
 
double & operator() (int r, int c)
 Single element access, no range check. More...
 
void zero ()
 Zero the matrix. More...
 
bool setRow (int 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 (int 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 (int r1, int r2, int c1, int 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, int r, int c)
 Set a portion of this matrix with the values of the specified matrix m. More...
 
bool setSubrow (const Vector &v, int r, int c)
 Set a portion of a row of this matrix with the values of the specified vector v. More...
 
bool setSubcol (const Vector &v, int r, int c)
 Set a portion of a column of this matrix with the values of the specified vector v. More...
 
Vector getRow (int r) const
 Get a row of the matrix as a vector. More...
 
Vector getCol (int c) const
 Get a columns of the matrix as a vector. More...
 
Matrix removeCols (int first_col, int how_many)
 Modifies the matrix, removing one or more columns from it. More...
 
Matrix removeRows (int first_row, int how_many)
 Modifies the matrix, removing one or more rows from it. More...
 
Vector subrow (int r, int c, int size) const
 Get a subrow of the matrix as a vector. More...
 
Vector subcol (int r, int c, int size) const
 Get a subcolumn of the matrix as a vector. More...
 
yarp::os::ConstString 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) YARP_OVERRIDE
 Read this object from a network connection. More...
 
virtual bool write (yarp::os::ConnectionWriter &connection) YARP_OVERRIDE
 Write vector to a connection. More...
 
- Public Member Functions inherited from yarp::os::Portable
virtual Type getType ()
 
- Public Member Functions inherited from yarp::os::PortReader
virtual ~PortReader ()
 Destructor. More...
 
virtual Type getReadType ()
 
- Public Member Functions inherited from yarp::os::PortWriter
virtual ~PortWriter ()
 Destructor. More...
 
virtual void onCompletion ()
 This is called when the port has finished all writing operations. More...
 
virtual void onCommencement ()
 This is called when the port is about to begin writing operations. More...
 
virtual Type getWriteType ()
 

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
 
int nrows
 
int 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...
 

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 43 of file Matrix.h.

Constructor & Destructor Documentation

◆ Matrix() [1/3]

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

Definition at line 59 of file Matrix.h.

◆ Matrix() [2/3]

Matrix::Matrix ( int  r,
int  c 
)

Definition at line 536 of file Matrix.cpp.

◆ Matrix() [3/3]

Matrix::Matrix ( const Matrix m)

Copy constructor.

Definition at line 547 of file Matrix.cpp.

◆ ~Matrix()

Matrix::~Matrix ( )

Destructor.

Definition at line 247 of file Matrix.cpp.

Member Function Documentation

◆ cols()

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

Return number of columns.

Definition at line 99 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 302 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 309 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 448 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 434 of file Matrix.cpp.

◆ getCol()

Vector Matrix::getCol ( int  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 397 of file Matrix.cpp.

◆ getRow()

Vector Matrix::getRow ( int  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 386 of file Matrix.cpp.

◆ operator()() [1/2]

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

Single element access, no range check.

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

Definition at line 133 of file Matrix.h.

◆ operator()() [2/2]

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

Single element access, no range check.

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

Definition at line 142 of file Matrix.h.

◆ operator=() [1/2]

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

Copy operator.

Definition at line 212 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 238 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 462 of file Matrix.cpp.

◆ operator[]() [1/2]

double* yarp::sig::Matrix::operator[] ( int  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 115 of file Matrix.h.

◆ operator[]() [2/2]

const double* yarp::sig::Matrix::operator[] ( int  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 124 of file Matrix.h.

◆ read()

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

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 104 of file Matrix.cpp.

◆ removeCols()

Matrix Matrix::removeCols ( int  first_col,
int  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 318 of file Matrix.cpp.

◆ removeRows()

Matrix Matrix::removeRows ( int  first_row,
int  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 346 of file Matrix.cpp.

◆ resize()

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

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

Parameters
rnumber of rows
cnumber of columns

Definition at line 256 of file Matrix.cpp.

◆ rows()

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

Return number of rows.

Definition at line 93 of file Matrix.h.

◆ setCol()

bool Matrix::setCol ( int  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 492 of file Matrix.cpp.

◆ setRow()

bool Matrix::setRow ( int  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 481 of file Matrix.cpp.

◆ setSubcol()

bool Matrix::setSubcol ( const Vector v,
int  r,
int  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 525 of file Matrix.cpp.

◆ setSubmatrix()

bool Matrix::setSubmatrix ( const Matrix m,
int  r,
int  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 503 of file Matrix.cpp.

◆ setSubrow()

bool Matrix::setSubrow ( const Vector v,
int  r,
int  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 514 of file Matrix.cpp.

◆ subcol()

Vector Matrix::subcol ( int  r,
int  c,
int  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 421 of file Matrix.cpp.

◆ submatrix()

Matrix yarp::sig::Matrix::submatrix ( int  r1,
int  r2,
int  c1,
int  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 194 of file Matrix.h.

◆ subrow()

Vector Matrix::subrow ( int  r,
int  c,
int  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 408 of file Matrix.cpp.

◆ toString()

ConstString 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 161 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 374 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 198 of file Matrix.cpp.

◆ write()

bool Matrix::write ( yarp::os::ConnectionWriter connection)
virtual

Write vector to a connection.

return true iff a vector was written correctly

Implements yarp::os::Portable.

Definition at line 131 of file Matrix.cpp.

◆ zero()

void Matrix::zero ( )

Zero the matrix.

Set all elements of the matrix to zero.

Definition at line 313 of file Matrix.cpp.

Member Data Documentation

◆ matrix

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

Definition at line 47 of file Matrix.h.

◆ ncols

int yarp::sig::Matrix::ncols
private

Definition at line 50 of file Matrix.h.

◆ nrows

int yarp::sig::Matrix::nrows
private

Definition at line 49 of file Matrix.h.

◆ storage

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

Definition at line 46 of file Matrix.h.


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