Visual Servoing Platform  version 3.6.1 under development (2024-06-22)
vpRowVector.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 VP_ROW_VECTOR_H
35 #define VP_ROW_VECTOR_H
36 
37 #include <vector>
38 
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpArray2D.h>
41 #include <visp3/core/vpColVector.h>
42 #include <visp3/core/vpMath.h>
43 #include <visp3/core/vpMatrix.h>
44 
46 
47 class vpMatrix;
48 class vpColVector;
49 
109 class VISP_EXPORT vpRowVector : public vpArray2D<double>
110 {
111 public:
113  vpRowVector() : vpArray2D<double>() { }
116  explicit vpRowVector(unsigned int n) : vpArray2D<double>(1, n) { }
118  vpRowVector(unsigned int n, double val) : vpArray2D<double>(1, n, val) { }
121  vpRowVector(const vpRowVector &v) : vpArray2D<double>(v) { }
122  vpRowVector(const vpRowVector &v, unsigned int c, unsigned int ncols);
123  explicit vpRowVector(const vpMatrix &M);
124  vpRowVector(const vpMatrix &M, unsigned int i);
125  explicit vpRowVector(const std::vector<double> &v);
126  explicit vpRowVector(const std::vector<float> &v);
127 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
129  explicit vpRowVector(const std::initializer_list<double> &list) : vpArray2D<double>(list) { }
130 #endif
131 
136  void clear()
137  {
138  if (data != nullptr) {
139  free(data);
140  data = nullptr;
141  }
142 
143  if (rowPtrs != nullptr) {
144  free(rowPtrs);
145  rowPtrs = nullptr;
146  }
147  rowNum = 0;
148  colNum = 0;
149  dsize = 0;
150  }
151 
152  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
153  std::ostream &csvPrint(std::ostream &os) const;
154 
159  inline void deg2rad()
160  {
161  double d2r = M_PI / 180.0;
162 
163  (*this) *= d2r;
164  }
165 
166 
184  vpRowVector extract(unsigned int c, unsigned int rowsize) const
185  {
186  if ((c >= colNum) || ((c + rowsize) > colNum)) {
188  "Cannot extract a (1x%d) row vector from a (1x%d) "
189  "row vector starting at index %d",
190  rowsize, colNum, c));
191  }
192 
193  return vpRowVector(*this, c, rowsize);
194  }
195 
196  double frobeniusNorm() const;
197  void init(const vpRowVector &v, unsigned int c, unsigned int ncols);
198  void insert(unsigned int i, const vpRowVector &v);
199 
200  std::ostream &maplePrint(std::ostream &os) const;
201  std::ostream &matlabPrint(std::ostream &os) const;
202 
203  vpRowVector &normalize();
204  vpRowVector &normalize(vpRowVector &x) const;
205 
207  inline double &operator[](unsigned int n) { return *(data + n); }
209  inline const double &operator[](unsigned int n) const { return *(data + n); }
210 
212  vpRowVector &operator=(const vpRowVector &v);
213  vpRowVector &operator=(const vpMatrix &M);
214  vpRowVector &operator=(const std::vector<double> &v);
215  vpRowVector &operator=(const std::vector<float> &v);
216  vpRowVector &operator=(double x);
217 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
219  vpRowVector &operator=(const std::initializer_list<double> &list);
220 #endif
222  bool operator==(const vpRowVector &v) const;
223  bool operator!=(const vpRowVector &v) const;
224 
225  double operator*(const vpColVector &x) const;
226  vpRowVector operator*(const vpMatrix &M) const;
227  vpRowVector operator*(double x) const;
228  vpRowVector &operator*=(double x);
229 
230  vpRowVector operator/(double x) const;
231  vpRowVector &operator/=(double x);
232 
233  vpRowVector operator+(const vpRowVector &v) const;
234  vpRowVector &operator+=(vpRowVector v);
235 
236  vpRowVector operator-(const vpRowVector &v) const;
237  vpRowVector &operator-=(vpRowVector v);
238  vpRowVector operator-() const;
239 
241  vpRowVector &operator<<(double val);
242  vpRowVector &operator,(double val);
243 
244  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
249  inline void rad2deg()
250  {
251  double r2d = 180.0 / M_PI;
252 
253  (*this) *= r2d;
254  }
255 
256  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
257  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
258 
264  inline void resize(unsigned int i, bool flagNullify = true) { vpArray2D<double>::resize(1, i, flagNullify); }
265 
276  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
277  {
278  if (nrows != 1) {
280  "Cannot resize a row vector to a (%dx%d) dimension "
281  "vector that has more than one row",
282  nrows, ncols));
283  }
284  vpArray2D<double>::resize(nrows, ncols, flagNullify);
285  }
286 
287  void stack(double d);
288  void stack(const vpRowVector &v);
289 
290  double sum() const;
291  double sumSquare() const;
292  vpColVector t() const;
293  std::vector<double> toStdVector() const;
294  vpColVector transpose() const;
295  void transpose(vpColVector &v) const;
296 
297  static double mean(const vpRowVector &v);
298  static double median(const vpRowVector &v);
299  static vpRowVector stack(const vpRowVector &A, const vpRowVector &B);
300  static void stack(const vpRowVector &A, const vpRowVector &B, vpRowVector &C);
301  static double stdev(const vpRowVector &v, bool useBesselCorrection = false);
302 
303 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
312  vp_deprecated void init() { }
316  vp_deprecated void stackMatrices(const vpRowVector &r) { stack(r); }
321  vp_deprecated static vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B) { return stack(A, B); }
326  vp_deprecated static void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
327  {
328  stack(A, B, C);
329  }
330 
334  vp_deprecated void setIdentity(const double &val = 1.0);
335  vp_deprecated double euclideanNorm() const;
337 #endif
338 };
339 
340 VISP_EXPORT vpRowVector operator*(const double &x, const vpRowVector &v);
341 
343 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:127
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:476
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:344
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:593
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1145
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1330
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:505
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:435
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1273
Implementation of column vector and the associated operations.
Definition: vpColVector.h:171
vpColVector operator*(const double &x, const vpColVector &v)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ fatalError
Fatal error.
Definition: vpException.h:72
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:151
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:110
vp_deprecated void setIdentity(const double &val=1.0)
vpRowVector(const vpRowVector &v)
Definition: vpRowVector.h:121
vpRowVector(unsigned int n, double val)
Construct a row vector of size n. Each element is set to val.
Definition: vpRowVector.h:118
double & operator[](unsigned int n)
Operator that allows to set a value of an element : v[i] = x.
Definition: vpRowVector.h:207
vpRowVector(unsigned int n)
Definition: vpRowVector.h:116
static vp_deprecated vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B)
Definition: vpRowVector.h:321
vp_deprecated void stackMatrices(const vpRowVector &r)
Definition: vpRowVector.h:316
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:264
void deg2rad()
Definition: vpRowVector.h:159
void rad2deg()
Definition: vpRowVector.h:249
vpRowVector()
Basic constructor that creates an empty 0-size row vector.
Definition: vpRowVector.h:113
vpRowVector(const std::initializer_list< double > &list)
Definition: vpRowVector.h:129
vp_deprecated void init()
Definition: vpRowVector.h:312
static vp_deprecated void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
Definition: vpRowVector.h:326
void clear()
Definition: vpRowVector.h:136
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpRowVector.h:276
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpRowVector.h:209
vpRowVector extract(unsigned int c, unsigned int rowsize) const
Definition: vpRowVector.h:184
vpMatrix operator*(const double &x, const vpMatrix &A)