YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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>
15
20namespace yarp::sig {
21class Matrix;
22}
23
24namespace yarp::sig {
25YARP_sig_API bool submatrix(const Matrix& in, Matrix& out, size_t r1, size_t r2, size_t c1, size_t c2);
26YARP_sig_API bool removeCols(const Matrix& in, Matrix& out, size_t first_col, size_t how_many);
27YARP_sig_API bool removeRows(const Matrix& in, Matrix& out, size_t first_row, size_t how_many);
28}
29
39{
40private:
41 double *storage;
42 double **matrix; //double pointer access to elements
43
44 size_t nrows;
45 size_t ncols;
46
51 void updatePointers();
52
53public:
55 storage(0),
56 matrix(0),
57 nrows(0),
58 ncols(0)
59 {}
60
61 Matrix(size_t r, size_t c);
62
66 Matrix(const Matrix &m);
67
71 ~Matrix();
72
76 const Matrix &operator=(const Matrix &r);
77
83 const Matrix &operator=(double v);
84
88 size_t rows() const
89 { return nrows; }
90
94 size_t cols() const
95 { return ncols; }
96
102 void resize(size_t r, size_t c);
103
110 inline double *operator[](size_t r)
111 { return matrix[r]; }
112
119 inline const double *operator[](size_t r) const
120 { return matrix[r]; }
121
128 inline const double &operator()(size_t r, size_t c) const
129 { return matrix[r][c]; }
130
137 inline double &operator()(size_t r, size_t c)
138 { return matrix[r][c]; }
139
143 void zero();
144
151 bool setRow(size_t row, const Vector &r);
152
159 bool setCol(size_t col, const Vector &c);
160
165 Matrix transposed() const;
166
171 const Matrix &eye();
172
177 const Matrix &diagonal(const Vector &d);
178
189 Matrix submatrix(size_t r1, size_t r2, size_t c1, size_t c2) const
190 {
191 Matrix ret;
192 ret.resize(r2-r1+1, c2-c1+1);
193
194 yarp::sig::submatrix((*this), ret, r1, r2, c1, c2);
195 return ret;
196 }
197
208 bool setSubmatrix(const Matrix &m, size_t r, size_t c);
209
219 bool setSubrow(const Vector &v, size_t r, size_t c);
220
230 bool setSubcol(const Vector &v, size_t r, size_t c);
231
237 Vector getRow(size_t r) const;
238
244 Vector getCol(size_t c) const;
245
252 Matrix removeCols(size_t first_col, size_t how_many);
253
260 Matrix removeRows(size_t first_row, size_t how_many);
261
269 Vector subrow(size_t r, size_t c, size_t size) const;
270
278 Vector subcol(size_t r, size_t c, size_t size) const;
279
291 std::string toString(int precision=-1, int width=-1, const char* endRowStr="\n") const;
292
297 inline double *data()
298 {return (nrows>0&&ncols>0)?storage:0/*NULL*/;}
299
304 inline const double *data() const
305 {return (nrows>0&&ncols>0)?storage:0/*NULL*/;}
306
310 bool operator==(const yarp::sig::Matrix &r) const;
311
313 /*
314 * Read vector from a connection.
315 * return true iff a vector was read correctly
316 */
317 bool read(yarp::os::ConnectionReader& connection) override;
318
323 bool write(yarp::os::ConnectionWriter& connection) const override;
324
325};
326
327#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:25
A class for a Matrix.
Definition Matrix.h:39
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 & operator()(size_t r, size_t c)
Single element access, no range check.
Definition Matrix.h:137
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
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:189
const double * operator[](size_t r) const
Single element access, no range check (const version).
Definition Matrix.h:119
void resize(size_t r, size_t c)
Resize the matrix, if matrix is not empty preserve old content.
Definition Matrix.cpp:265
double * operator[](size_t r)
Single element access, no range check.
Definition Matrix.h:110
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
const double * data() const
Return a pointer to the first element (const version).
Definition Matrix.h:304
const double & operator()(size_t r, size_t c) const
Single element access, no range check.
Definition Matrix.h:128
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:94
~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
double * data()
Return a pointer to the first element.
Definition Matrix.h:297
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
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
size_t rows() const
Return number of rows.
Definition Matrix.h:88
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
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