YARP
Yet Another Robot Platform
Matrix.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_SIG_MATRIX_H
8 #define YARP_SIG_MATRIX_H
9 
10 #include <cstdlib> //defines size_t
11 #include <cstring> //memset
12 #include <yarp/os/Portable.h>
13 #include <yarp/sig/Vector.h>
14 #include <yarp/os/ManagedBytes.h>
15 
20 namespace yarp {
21  namespace sig {
22  class Matrix;
23  }
24 }
25 
26 namespace yarp {
27  namespace sig {
28  YARP_sig_API bool submatrix(const Matrix &in, Matrix &out, size_t r1, size_t r2, size_t c1, size_t c2);
29  YARP_sig_API bool removeCols(const Matrix &in, Matrix &out, size_t first_col, size_t how_many);
30  YARP_sig_API bool removeRows(const Matrix &in, Matrix &out, size_t first_row, size_t how_many);
31  }
32 }
33 
43 {
44 private:
45  double *storage;
46  double **matrix; //double pointer access to elements
47 
48  size_t nrows;
49  size_t ncols;
50 
55  void updatePointers();
56 
57 public:
58  Matrix():
59  storage(0),
60  matrix(0),
61  nrows(0),
62  ncols(0)
63  {}
64 
65  Matrix(size_t r, size_t c);
66 
70  Matrix(const Matrix &m);
71 
75  ~Matrix();
76 
80  const Matrix &operator=(const Matrix &r);
81 
87  const Matrix &operator=(double v);
88 
92  size_t rows() const
93  { return nrows; }
94 
98  size_t cols() const
99  { return ncols; }
100 
106  void resize(size_t r, size_t c);
107 
114  inline double *operator[](size_t r)
115  { return matrix[r]; }
116 
123  inline const double *operator[](size_t r) const
124  { return matrix[r]; }
125 
132  inline const double &operator()(size_t r, size_t c) const
133  { return matrix[r][c]; }
134 
141  inline double &operator()(size_t r, size_t c)
142  { return matrix[r][c]; }
143 
147  void zero();
148 
155  bool setRow(size_t row, const Vector &r);
156 
163  bool setCol(size_t col, const Vector &c);
164 
169  Matrix transposed() const;
170 
175  const Matrix &eye();
176 
181  const Matrix &diagonal(const Vector &d);
182 
193  Matrix submatrix(size_t r1, size_t r2, size_t c1, size_t c2) const
194  {
195  Matrix ret;
196  ret.resize(r2-r1+1, c2-c1+1);
197 
198  yarp::sig::submatrix((*this), ret, r1, r2, c1, c2);
199  return ret;
200  }
201 
212  bool setSubmatrix(const Matrix &m, size_t r, size_t c);
213 
223  bool setSubrow(const Vector &v, size_t r, size_t c);
224 
234  bool setSubcol(const Vector &v, size_t r, size_t c);
235 
241  Vector getRow(size_t r) const;
242 
248  Vector getCol(size_t c) const;
249 
256  Matrix removeCols(size_t first_col, size_t how_many);
257 
264  Matrix removeRows(size_t first_row, size_t how_many);
265 
273  Vector subrow(size_t r, size_t c, size_t size) const;
274 
282  Vector subcol(size_t r, size_t c, size_t size) const;
283 
295  std::string toString(int precision=-1, int width=-1, const char* endRowStr="\n") const;
296 
301  inline double *data()
302  {return (nrows>0&&ncols>0)?storage:0/*NULL*/;}
303 
308  inline const double *data() const
309  {return (nrows>0&&ncols>0)?storage:0/*NULL*/;}
310 
314  bool operator==(const yarp::sig::Matrix &r) const;
315 
317  /*
318  * Read vector from a connection.
319  * return true iff a vector was read correctly
320  */
321  bool read(yarp::os::ConnectionReader& connection) override;
322 
327  bool write(yarp::os::ConnectionWriter& connection) const override;
328 
329 };
330 
331 #endif // YARP_SIG_MATRIX_H
bool ret
contains the definition of a Vector type
An interface for reading from a network connection.
An interface for writing to a network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:26
A class for a Matrix.
Definition: Matrix.h:43
Matrix removeRows(size_t first_row, size_t how_many)
Modifies the matrix, removing one or more rows from it.
Definition: Matrix.cpp:358
void zero()
Zero the matrix.
Definition: Matrix.cpp:323
Vector getRow(size_t r) const
Get a row of the matrix as a vector.
Definition: Matrix.cpp:402
double * data()
Return a pointer to the first element.
Definition: Matrix.h:301
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: Matrix.cpp:112
Vector subcol(size_t r, size_t c, size_t size) const
Get a subcolumn of the matrix as a vector.
Definition: Matrix.cpp:441
double * operator[](size_t r)
Single element access, no range check.
Definition: Matrix.h:114
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:193
void resize(size_t r, size_t c)
Resize the matrix, if matrix is not empty preserve old content.
Definition: Matrix.cpp:265
bool operator==(const yarp::sig::Matrix &r) const
True iff all elements of a match all element of b.
Definition: Matrix.cpp:488
Matrix transposed() const
Return the transposed of the matrix.
Definition: Matrix.cpp:388
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:526
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:553
const Matrix & diagonal(const Vector &d)
Build a diagonal matrix, don't resize.
Definition: Matrix.cpp:472
double & operator()(size_t r, size_t c)
Single element access, no range check.
Definition: Matrix.h:141
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:513
size_t cols() const
Return number of columns.
Definition: Matrix.h:98
~Matrix()
Destructor.
Definition: Matrix.cpp:254
Vector getCol(size_t c) const
Get a columns of the matrix as a vector.
Definition: Matrix.cpp:414
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:539
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:566
const double * operator[](size_t r) const
Single element access, no range check (const version).
Definition: Matrix.h:123
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
Definition: Matrix.cpp:142
const Matrix & operator=(const Matrix &r)
Copy operator.
Definition: Matrix.cpp:214
const double * data() const
Return a pointer to the first element (const version).
Definition: Matrix.h:308
size_t rows() const
Return number of rows.
Definition: Matrix.h:92
Vector subrow(size_t r, size_t c, size_t size) const
Get a subrow of the matrix as a vector.
Definition: Matrix.cpp:426
Matrix removeCols(size_t first_col, size_t how_many)
Modifies the matrix, removing one or more columns from it.
Definition: Matrix.cpp:328
std::string toString(int precision=-1, int width=-1, const char *endRowStr="\n") const
Print matrix to a string.
Definition: Matrix.cpp:171
const Matrix & eye()
Build an identity matrix, don't resize.
Definition: Matrix.cpp:456
const double & operator()(size_t r, size_t c) const
Single element access, no range check.
Definition: Matrix.h:132
bool removeRows(const Matrix &in, Matrix &out, size_t first_row, size_t how_many)
Definition: Matrix.cpp:68
bool submatrix(const Matrix &in, Matrix &out, size_t r1, size_t r2, size_t c1, size_t c2)
Definition: Matrix.cpp:89
bool removeCols(const Matrix &in, Matrix &out, size_t first_col, size_t how_many)
Definition: Matrix.cpp:47
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_sig_API
Definition: api.h:18