YARP
Yet Another Robot Platform
Matrix.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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
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:29
A class for a Matrix.
Definition: Matrix.h:46
Matrix removeRows(size_t first_row, size_t how_many)
Modifies the matrix, removing one or more rows from it.
Definition: Matrix.cpp:341
void zero()
Zero the matrix.
Definition: Matrix.cpp:308
Vector getRow(size_t r) const
Get a row of the matrix as a vector.
Definition: Matrix.cpp:381
double * data()
Return a pointer to the first element.
Definition: Matrix.h:304
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:416
double * operator[](size_t r)
Single element access, no range check.
Definition: Matrix.h:117
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
void resize(size_t r, size_t c)
Resize the matrix, if matrix is not empty preserve old content.
Definition: Matrix.cpp:251
bool operator==(const yarp::sig::Matrix &r) const
True iff all elements of a match all element of b.
Definition: Matrix.cpp:457
Matrix transposed() const
Return the transposed of the matrix.
Definition: Matrix.cpp:369
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
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
const Matrix & diagonal(const Vector &d)
Build a diagonal matrix, don't resize.
Definition: Matrix.cpp:443
double & operator()(size_t r, size_t c)
Single element access, no range check.
Definition: Matrix.h:144
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
size_t cols() const
Return number of columns.
Definition: Matrix.h:101
~Matrix()
Destructor.
Definition: Matrix.cpp:242
Vector getCol(size_t c) const
Get a columns of the matrix as a vector.
Definition: Matrix.cpp:392
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
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
const double * operator[](size_t r) const
Single element access, no range check (const version).
Definition: Matrix.h:126
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
Definition: Matrix.cpp:139
const Matrix & operator=(const Matrix &r)
Copy operator.
Definition: Matrix.cpp:207
const double * data() const
Return a pointer to the first element (const version).
Definition: Matrix.h:311
size_t rows() const
Return number of rows.
Definition: Matrix.h:95
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
Matrix removeCols(size_t first_col, size_t how_many)
Modifies the matrix, removing one or more columns from it.
Definition: Matrix.cpp:313
std::string toString(int precision=-1, int width=-1, const char *endRowStr="\n") const
Print matrix to a string.
Definition: Matrix.cpp:167
const Matrix & eye()
Build an identity matrix, don't resize.
Definition: Matrix.cpp:429
const double & operator()(size_t r, size_t c) const
Single element access, no range check.
Definition: Matrix.h:135
bool removeRows(const Matrix &in, Matrix &out, size_t first_row, size_t how_many)
Definition: Matrix.cpp:70
bool submatrix(const Matrix &in, Matrix &out, size_t r1, size_t r2, size_t c1, size_t c2)
Definition: Matrix.cpp:90
bool removeCols(const Matrix &in, Matrix &out, size_t first_col, size_t how_many)
Definition: Matrix.cpp:50
The main, catch-all namespace for YARP.
Definition: environment.h:25
#define YARP_sig_API
Definition: api.h:19