YARP
Yet Another Robot Platform
Matrix.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_SIG_MATRIX_H
11 #define YARP_SIG_MATRIX_H
12 
13 #include <cstdlib> //defines size_t
14 #include <cstring> //memset
15 #include <yarp/os/Portable.h>
16 #include <yarp/sig/Vector.h>
17 #include <yarp/os/ManagedBytes.h>
18 
23 namespace yarp {
24  namespace sig {
25  class Matrix;
26  }
27 }
28 
29 namespace yarp {
30  namespace sig {
31  YARP_sig_API bool submatrix(const Matrix &in, Matrix &out, size_t r1, size_t r2, size_t c1, size_t c2);
32  YARP_sig_API bool removeCols(const Matrix &in, Matrix &out, size_t first_col, size_t how_many);
33  YARP_sig_API bool removeRows(const Matrix &in, Matrix &out, size_t first_row, size_t how_many);
34  }
35 }
36 
46 {
47 private:
48  double *storage;
49  double **matrix; //double pointer access to elements
50 
51  size_t nrows;
52  size_t ncols;
53 
58  void updatePointers();
59 
60 public:
61  Matrix():
62  storage(0),
63  matrix(0),
64  nrows(0),
65  ncols(0)
66  {}
67 
68  Matrix(size_t r, size_t c);
69 
73  Matrix(const Matrix &m);
74 
78  ~Matrix();
79 
83  const Matrix &operator=(const Matrix &r);
84 
90  const Matrix &operator=(double v);
91 
95  size_t rows() const
96  { return nrows; }
97 
101  size_t cols() const
102  { return ncols; }
103 
109  void resize(size_t r, size_t c);
110 
117  inline double *operator[](size_t r)
118  { return matrix[r]; }
119 
126  inline const double *operator[](size_t r) const
127  { return matrix[r]; }
128 
135  inline const double &operator()(size_t r, size_t c) const
136  { return matrix[r][c]; }
137 
144  inline double &operator()(size_t r, size_t c)
145  { return matrix[r][c]; }
146 
150  void zero();
151 
158  bool setRow(size_t row, const Vector &r);
159 
166  bool setCol(size_t col, const Vector &c);
167 
172  Matrix transposed() const;
173 
178  const Matrix &eye();
179 
184  const Matrix &diagonal(const Vector &d);
185 
196  Matrix submatrix(size_t r1, size_t r2, size_t c1, size_t c2) const
197  {
198  Matrix ret;
199  ret.resize(r2-r1+1, c2-c1+1);
200 
201  yarp::sig::submatrix((*this), ret, r1, r2, c1, c2);
202  return ret;
203  }
204 
215  bool setSubmatrix(const Matrix &m, size_t r, size_t c);
216 
226  bool setSubrow(const Vector &v, size_t r, size_t c);
227 
237  bool setSubcol(const Vector &v, size_t r, size_t c);
238 
244  Vector getRow(size_t r) const;
245 
251  Vector getCol(size_t c) const;
252 
259  Matrix removeCols(size_t first_col, size_t how_many);
260 
267  Matrix removeRows(size_t first_row, size_t how_many);
268 
276  Vector subrow(size_t r, size_t c, size_t size) const;
277 
285  Vector subcol(size_t r, size_t c, size_t size) const;
286 
298  std::string toString(int precision=-1, int width=-1, const char* endRowStr="\n") const;
299 
304  inline double *data()
305  {return (nrows>0&&ncols>0)?storage:0/*NULL*/;}
306 
311  inline const double *data() const
312  {return (nrows>0&&ncols>0)?storage:0/*NULL*/;}
313 
317  bool operator==(const yarp::sig::Matrix &r) const;
318 
320  /*
321  * Read vector from a connection.
322  * return true iff a vector was read correctly
323  */
324  bool read(yarp::os::ConnectionReader& connection) override;
325 
330  bool write(yarp::os::ConnectionWriter& connection) const override;
331 
332 };
333 
334 #endif // YARP_SIG_MATRIX_H
yarp::sig::removeRows
bool removeRows(const Matrix &in, Matrix &out, size_t first_row, size_t how_many)
Definition: Matrix.cpp:70
yarp::sig::Matrix::toString
std::string toString(int precision=-1, int width=-1, const char *endRowStr="\n") const
Print matrix to a string.
Definition: Matrix.cpp:167
yarp::os::Portable
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
yarp::sig::Matrix::subrow
Vector subrow(size_t r, size_t c, size_t size) const
Get a subrow of the matrix as a vector.
Definition: Matrix.cpp:403
Vector.h
contains the definition of a Vector type
yarp::sig::Matrix::setSubrow
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.
Definition: Matrix.cpp:512
yarp::sig::removeCols
bool removeCols(const Matrix &in, Matrix &out, size_t first_col, size_t how_many)
Definition: Matrix.cpp:50
yarp::sig::Matrix::transposed
Matrix transposed() const
Return the transposed of the matrix.
Definition: Matrix.cpp:369
yarp::sig::Matrix::diagonal
const Matrix & diagonal(const Vector &d)
Build a diagonal matrix, don't resize.
Definition: Matrix.cpp:443
yarp::sig::Matrix::removeCols
Matrix removeCols(size_t first_col, size_t how_many)
Modifies the matrix, removing one or more columns from it.
Definition: Matrix.cpp:313
Portable.h
yarp::sig::Matrix::rows
size_t rows() const
Return number of rows.
Definition: Matrix.h:95
yarp::sig::Matrix::submatrix
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,...
Definition: Matrix.h:196
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::sig::Matrix::setSubcol
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.
Definition: Matrix.cpp:523
yarp::sig::Matrix::~Matrix
~Matrix()
Destructor.
Definition: Matrix.cpp:242
yarp::sig::VectorOf< double >
yarp::sig::Matrix::read
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: Matrix.cpp:112
ManagedBytes.h
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::sig::Matrix::eye
const Matrix & eye()
Build an identity matrix, don't resize.
Definition: Matrix.cpp:429
yarp::sig::Matrix::setCol
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 n...
Definition: Matrix.cpp:490
YARP_sig_API
#define YARP_sig_API
Definition: api.h:19
yarp::sig::Matrix::Matrix
Matrix()
Definition: Matrix.h:61
yarp::sig::Matrix::cols
size_t cols() const
Return number of columns.
Definition: Matrix.h:101
yarp::sig::Matrix::resize
void resize(size_t r, size_t c)
Resize the matrix, if matrix is not empty preserve old content.
Definition: Matrix.cpp:251
yarp::sig::Matrix::operator[]
double * operator[](size_t r)
Single element access, no range check.
Definition: Matrix.h:117
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::sig::Matrix::data
double * data()
Return a pointer to the first element.
Definition: Matrix.h:304
yarp::sig::Matrix::subcol
Vector subcol(size_t r, size_t c, size_t size) const
Get a subcolumn of the matrix as a vector.
Definition: Matrix.cpp:416
yarp::sig::Matrix::write
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
Definition: Matrix.cpp:139
yarp::sig::Matrix::getCol
Vector getCol(size_t c) const
Get a columns of the matrix as a vector.
Definition: Matrix.cpp:392
yarp::sig::Matrix::operator()
double & operator()(size_t r, size_t c)
Single element access, no range check.
Definition: Matrix.h:144
yarp::sig::Matrix::operator()
const double & operator()(size_t r, size_t c) const
Single element access, no range check.
Definition: Matrix.h:135
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::sig::submatrix
bool submatrix(const Matrix &in, Matrix &out, size_t r1, size_t r2, size_t c1, size_t c2)
Definition: Matrix.cpp:90
yarp::sig::Matrix::operator==
bool operator==(const yarp::sig::Matrix &r) const
True iff all elements of a match all element of b.
Definition: Matrix.cpp:457
yarp::sig::Matrix::removeRows
Matrix removeRows(size_t first_row, size_t how_many)
Modifies the matrix, removing one or more rows from it.
Definition: Matrix.cpp:341
yarp::sig::Matrix::zero
void zero()
Zero the matrix.
Definition: Matrix.cpp:308
yarp::sig::Matrix::operator[]
const double * operator[](size_t r) const
Single element access, no range check (const version).
Definition: Matrix.h:126
yarp::sig::Matrix::getRow
Vector getRow(size_t r) const
Get a row of the matrix as a vector.
Definition: Matrix.cpp:381
yarp::sig::Matrix::data
const double * data() const
Return a pointer to the first element (const version).
Definition: Matrix.h:311
yarp::sig::Matrix::operator=
const Matrix & operator=(const Matrix &r)
Copy operator.
Definition: Matrix.cpp:207
yarp::sig::Matrix::setRow
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 numb...
Definition: Matrix.cpp:479
yarp::sig::Matrix
A class for a Matrix.
Definition: Matrix.h:46
yarp::sig::Matrix::setSubmatrix
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.
Definition: Matrix.cpp:501