YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2026 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 <utility> //std::move
13#include <yarp/sig/MatrixData.h>
14#include <yarp/sig/Vector.h>
16
21namespace yarp::sig {
22class Matrix;
23}
24
25namespace yarp::sig {
26YARP_sig_API bool submatrix(const Matrix& in, Matrix& out, size_t r1, size_t r2, size_t c1, size_t c2);
27YARP_sig_API bool removeCols(const Matrix& in, Matrix& out, size_t first_col, size_t how_many);
28YARP_sig_API bool removeRows(const Matrix& in, Matrix& out, size_t first_row, size_t how_many);
29}
30
40{
41private:
42 using MatrixData::read;
43 using MatrixData::write;
44
45 double **matrix; //double pointer access to elements
46
51 void updatePointers();
52
53public:
55 matrix(0)
56 {}
57
58 Matrix(size_t r, size_t c);
59
63 Matrix(const Matrix &m);
64
68 ~Matrix();
69
73 const Matrix &operator=(const Matrix &r);
74
80 const Matrix &operator=(double v);
81
85 size_t rows() const
86 { return nrows; }
87
91 size_t cols() const
92 { return ncols; }
93
99 void resize(size_t r, size_t c);
100
107 inline double *operator[](size_t r)
108 { return matrix[r]; }
109
116 inline const double *operator[](size_t r) const
117 { return matrix[r]; }
118
125 inline const double &operator()(size_t r, size_t c) const
126 { return matrix[r][c]; }
127
134 inline double &operator()(size_t r, size_t c)
135 { return matrix[r][c]; }
136
140 void zero();
141
148 bool setRow(size_t row, const Vector &r);
149
156 bool setCol(size_t col, const Vector &c);
157
162 Matrix transposed() const;
163
168 const Matrix &eye();
169
174 const Matrix &diagonal(const Vector &d);
175
186 Matrix submatrix(size_t r1, size_t r2, size_t c1, size_t c2) const
187 {
188 Matrix ret;
189 ret.resize(r2-r1+1, c2-c1+1);
190
191 yarp::sig::submatrix((*this), ret, r1, r2, c1, c2);
192 return ret;
193 }
194
205 bool setSubmatrix(const Matrix &m, size_t r, size_t c);
206
216 bool setSubrow(const Vector &v, size_t r, size_t c);
217
227 bool setSubcol(const Vector &v, size_t r, size_t c);
228
234 Vector getRow(size_t r) const;
235
241 Vector getCol(size_t c) const;
242
249 Matrix removeCols(size_t first_col, size_t how_many);
250
257 Matrix removeRows(size_t first_row, size_t how_many);
258
266 Vector subrow(size_t r, size_t c, size_t size) const;
267
275 Vector subcol(size_t r, size_t c, size_t size) const;
276
288 std::string toString(int precision=-1, int width=-1, const char* endRowStr="\n") const;
289
294 inline double *data()
295 {return (nrows>0&&ncols>0)?storage.data():nullptr;}
296
301 inline const double *data() const
302 {return (nrows>0&&ncols>0)?storage.data():nullptr;}
303
307 bool operator==(const yarp::sig::Matrix &r) const;
308
309 bool read(yarp::os::ConnectionReader& connection) override
310 {
311 // Read into a temporary object, so that a failed or malformed read
312 // leaves this matrix (and its row pointers) untouched.
313 Matrix tmp;
314 if (!tmp.MatrixData::read(connection)) {
315 return false;
316 }
317 if (tmp.nrows < 0 ||
318 tmp.ncols < 0 ||
319 tmp.storage.size() != static_cast<size_t>(tmp.nrows) * static_cast<size_t>(tmp.ncols))
320 {
321 return false;
322 }
323 nrows = tmp.nrows;
324 ncols = tmp.ncols;
325 storage = std::move(tmp.storage);
326 updatePointers();
327 return true;
328 }
329
330 bool write(yarp::os::ConnectionWriter& connection) const override
331 {
332 return MatrixData::write(connection);
333 }
334
335};
336
337#endif // YARP_SIG_MATRIX_H
contains the definition of a Vector type
An interface for reading from a network connection.
An interface for writing to a network connection.
std::vector< double > storage
Definition MatrixData.h:28
bool write(const yarp::os::idl::WireWriter &writer) const override
std::int32_t nrows
Definition MatrixData.h:26
std::int32_t ncols
Definition MatrixData.h:27
std::string toString() const
A class for a Matrix.
Definition Matrix.h:40
Matrix removeRows(size_t first_row, size_t how_many)
Modifies the matrix, removing one or more rows from it.
Definition Matrix.cpp:328
void zero()
Zero the matrix.
Definition Matrix.cpp:297
Vector getRow(size_t r) const
Get a row of the matrix as a vector.
Definition Matrix.cpp:368
double & operator()(size_t r, size_t c)
Single element access, no range check.
Definition Matrix.h:134
Vector subcol(size_t r, size_t c, size_t size) const
Get a subcolumn of the matrix as a vector.
Definition Matrix.cpp:407
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:186
const double * operator[](size_t r) const
Single element access, no range check (const version).
Definition Matrix.h:116
void resize(size_t r, size_t c)
Resize the matrix, if matrix is not empty preserve old content.
Definition Matrix.cpp:245
double * operator[](size_t r)
Single element access, no range check.
Definition Matrix.h:107
bool operator==(const yarp::sig::Matrix &r) const
True iff all elements of a match all element of b.
Definition Matrix.cpp:454
Matrix transposed() const
Return the transposed of the matrix.
Definition Matrix.cpp:354
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:492
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:519
const Matrix & diagonal(const Vector &d)
Build a diagonal matrix, don't resize.
Definition Matrix.cpp:438
const double * data() const
Return a pointer to the first element (const version).
Definition Matrix.h:301
const double & operator()(size_t r, size_t c) const
Single element access, no range check.
Definition Matrix.h:125
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
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
Definition Matrix.h:330
size_t cols() const
Return number of columns.
Definition Matrix.h:91
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition Matrix.h:309
~Matrix()
Destructor.
Definition Matrix.cpp:238
Vector getCol(size_t c) const
Get a columns of the matrix as a vector.
Definition Matrix.cpp:380
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:505
double * data()
Return a pointer to the first element.
Definition Matrix.h:294
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:532
const Matrix & operator=(const Matrix &r)
Copy operator.
Definition Matrix.cpp:214
size_t rows() const
Return number of rows.
Definition Matrix.h:85
Vector subrow(size_t r, size_t c, size_t size) const
Get a subrow of the matrix as a vector.
Definition Matrix.cpp:392
Matrix removeCols(size_t first_col, size_t how_many)
Modifies the matrix, removing one or more columns from it.
Definition Matrix.cpp:302
const Matrix & eye()
Build an identity matrix, don't resize.
Definition Matrix.cpp:422
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