Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
vpRowVector.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Operation on row vectors.
32  */
33 
34 #ifndef vpRowVector_H
35 #define vpRowVector_H
36 
37 #include <vector>
38 
39 #include <visp3/core/vpArray2D.h>
40 #include <visp3/core/vpColVector.h>
41 #include <visp3/core/vpMath.h>
42 #include <visp3/core/vpMatrix.h>
43 
44 class vpMatrix;
45 class vpColVector;
46 
106 class VISP_EXPORT vpRowVector : public vpArray2D<double>
107 {
108 public:
110  vpRowVector() : vpArray2D<double>() { }
113  explicit vpRowVector(unsigned int n) : vpArray2D<double>(1, n) { }
115  vpRowVector(unsigned int n, double val) : vpArray2D<double>(1, n, val) { }
118  vpRowVector(const vpRowVector &v) : vpArray2D<double>(v) { }
119  vpRowVector(const vpRowVector &v, unsigned int c, unsigned int ncols);
120  vpRowVector(const vpMatrix &M);
121  vpRowVector(const vpMatrix &M, unsigned int i);
122  vpRowVector(const std::vector<double> &v);
123  vpRowVector(const std::vector<float> &v);
124 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
126  vpRowVector(const std::initializer_list<double> &list) : vpArray2D<double>(list) { }
127 #endif
128 
133  void clear()
134  {
135  if (data != nullptr) {
136  free(data);
137  data = nullptr;
138  }
139 
140  if (rowPtrs != nullptr) {
141  free(rowPtrs);
142  rowPtrs = nullptr;
143  }
144  rowNum = colNum = dsize = 0;
145  }
146 
147  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
148  std::ostream &csvPrint(std::ostream &os) const;
149 
154  inline void deg2rad()
155  {
156  double d2r = M_PI / 180.0;
157 
158  (*this) *= d2r;
159  }
160 
161 
179  vpRowVector extract(unsigned int c, unsigned int rowsize) const
180  {
181  if (c >= colNum || c + rowsize > colNum) {
183  "Cannot extract a (1x%d) row vector from a (1x%d) "
184  "row vector starting at index %d",
185  rowsize, colNum, c));
186  }
187 
188  return vpRowVector(*this, c, rowsize);
189  }
190 
191  double frobeniusNorm() const;
192  void init(const vpRowVector &v, unsigned int c, unsigned int ncols);
193  void insert(unsigned int i, const vpRowVector &v);
194 
195  std::ostream &maplePrint(std::ostream &os) const;
196  std::ostream &matlabPrint(std::ostream &os) const;
197 
198  vpRowVector &normalize();
199  vpRowVector &normalize(vpRowVector &x) const;
200 
202  inline double &operator[](unsigned int n) { return *(data + n); }
204  inline const double &operator[](unsigned int n) const { return *(data + n); }
205 
207  vpRowVector &operator=(const vpRowVector &v);
208  vpRowVector &operator=(const vpMatrix &M);
209  vpRowVector &operator=(const std::vector<double> &v);
210  vpRowVector &operator=(const std::vector<float> &v);
211  vpRowVector &operator=(double x);
212 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
214  vpRowVector &operator=(const std::initializer_list<double> &list);
215 #endif
217  bool operator==(const vpRowVector &v) const;
218  bool operator!=(const vpRowVector &v) const;
219 
220  double operator*(const vpColVector &x) const;
221  vpRowVector operator*(const vpMatrix &M) const;
222  vpRowVector operator*(double x) const;
223  vpRowVector &operator*=(double x);
224 
225  vpRowVector operator/(double x) const;
226  vpRowVector &operator/=(double x);
227 
228  vpRowVector operator+(const vpRowVector &v) const;
229  vpRowVector &operator+=(vpRowVector v);
230 
231  vpRowVector operator-(const vpRowVector &v) const;
232  vpRowVector &operator-=(vpRowVector v);
233  vpRowVector operator-() const;
234 
236  vpRowVector &operator<<(double val);
237  vpRowVector &operator,(double val);
238 
239  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
244  inline void rad2deg()
245  {
246  double r2d = 180.0 / M_PI;
247 
248  (*this) *= r2d;
249  }
250 
251  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
252  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
253 
259  inline void resize(unsigned int i, bool flagNullify = true) { vpArray2D<double>::resize(1, i, flagNullify); }
260 
271  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
272  {
273  if (nrows != 1)
275  "Cannot resize a row vector to a (%dx%d) dimension "
276  "vector that has more than one row",
277  nrows, ncols));
278  vpArray2D<double>::resize(nrows, ncols, flagNullify);
279  }
280 
281  void stack(double d);
282  void stack(const vpRowVector &v);
283 
284  double sum() const;
285  double sumSquare() const;
286  vpColVector t() const;
287  std::vector<double> toStdVector() const;
288  vpColVector transpose() const;
289  void transpose(vpColVector &v) const;
290 
291  static double mean(const vpRowVector &v);
292  static double median(const vpRowVector &v);
293  static vpRowVector stack(const vpRowVector &A, const vpRowVector &B);
294  static void stack(const vpRowVector &A, const vpRowVector &B, vpRowVector &C);
295  static double stdev(const vpRowVector &v, bool useBesselCorrection = false);
296 
297 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
306  vp_deprecated void init() { }
310  vp_deprecated void stackMatrices(const vpRowVector &r) { stack(r); }
315  vp_deprecated static vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B) { return stack(A, B); }
320  vp_deprecated static void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
321  {
322  stack(A, B, C);
323  }
324 
328  vp_deprecated void setIdentity(const double &val = 1.0);
329  vp_deprecated double euclideanNorm() const;
331 #endif
332 };
333 
334 VISP_EXPORT vpRowVector operator*(const double &x, const vpRowVector &v);
335 
336 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:125
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:411
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:299
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:523
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1055
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1223
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:439
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:377
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1169
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector operator*(const double &x, const vpColVector &v)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:107
vp_deprecated void setIdentity(const double &val=1.0)
vpRowVector(const vpRowVector &v)
Definition: vpRowVector.h:118
vpRowVector(unsigned int n, double val)
Construct a row vector of size n. Each element is set to val.
Definition: vpRowVector.h:115
double & operator[](unsigned int n)
Operator that allows to set a value of an element : v[i] = x.
Definition: vpRowVector.h:202
vpRowVector(unsigned int n)
Definition: vpRowVector.h:113
static vp_deprecated vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B)
Definition: vpRowVector.h:315
vp_deprecated void stackMatrices(const vpRowVector &r)
Definition: vpRowVector.h:310
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:259
void deg2rad()
Definition: vpRowVector.h:154
void rad2deg()
Definition: vpRowVector.h:244
vpRowVector()
Basic constructor that creates an empty 0-size row vector.
Definition: vpRowVector.h:110
vpRowVector(const std::initializer_list< double > &list)
Definition: vpRowVector.h:126
vp_deprecated void init()
Definition: vpRowVector.h:306
static vp_deprecated void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
Definition: vpRowVector.h:320
void clear()
Definition: vpRowVector.h:133
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpRowVector.h:271
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpRowVector.h:204
vpRowVector extract(unsigned int c, unsigned int rowsize) const
Definition: vpRowVector.h:179
vpMatrix operator*(const double &x, const vpMatrix &A)