Visual Servoing Platform  version 3.6.0 under development (2023-09-29)
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 
110 class VISP_EXPORT vpRowVector : public vpArray2D<double>
111 {
112 public:
114  vpRowVector() : vpArray2D<double>() {}
117  explicit vpRowVector(unsigned int n) : vpArray2D<double>(1, n) {}
119  vpRowVector(unsigned int n, double val) : vpArray2D<double>(1, n, val) {}
122  vpRowVector(const vpRowVector &v) : vpArray2D<double>(v) {}
123  vpRowVector(const vpRowVector &v, unsigned int c, unsigned int ncols);
124  vpRowVector(const vpMatrix &M);
125  vpRowVector(const vpMatrix &M, unsigned int i);
126  vpRowVector(const std::vector<double> &v);
127  vpRowVector(const std::vector<float> &v);
128 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
130  vpRowVector(const std::initializer_list<double> &list) : vpArray2D<double>(list) {}
131 #endif
135  virtual ~vpRowVector() {}
136 
141  void clear()
142  {
143  if (data != NULL) {
144  free(data);
145  data = NULL;
146  }
147 
148  if (rowPtrs != NULL) {
149  free(rowPtrs);
150  rowPtrs = NULL;
151  }
152  rowNum = colNum = dsize = 0;
153  }
154 
155  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
156  std::ostream &csvPrint(std::ostream &os) const;
157 
162  inline void deg2rad()
163  {
164  double d2r = M_PI / 180.0;
165 
166  (*this) *= d2r;
167  }
168 
169 
186  vpRowVector extract(unsigned int c, unsigned int rowsize) const
187  {
188  if (c >= colNum || c + rowsize > colNum) {
190  "Cannot extract a (1x%d) row vector from a (1x%d) "
191  "row vector starting at index %d",
192  rowsize, colNum, c));
193  }
194 
195  return vpRowVector(*this, c, rowsize);
196  }
197 
198  double frobeniusNorm() const;
199  void init(const vpRowVector &v, unsigned int c, unsigned int ncols);
200  void insert(unsigned int i, const vpRowVector &v);
201 
202  std::ostream &maplePrint(std::ostream &os) const;
203  std::ostream &matlabPrint(std::ostream &os) const;
204 
205  vpRowVector &normalize();
206  vpRowVector &normalize(vpRowVector &x) const;
207 
209  inline double &operator[](unsigned int n) { return *(data + n); }
211  inline const double &operator[](unsigned int n) const { return *(data + n); }
212 
214  vpRowVector &operator=(const vpRowVector &v);
215  vpRowVector &operator=(const vpMatrix &M);
216  vpRowVector &operator=(const std::vector<double> &v);
217  vpRowVector &operator=(const std::vector<float> &v);
218  vpRowVector &operator=(double x);
219 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
221  vpRowVector &operator=(const std::initializer_list<double> &list);
222 #endif
224  bool operator==(const vpRowVector &v) const;
225  bool operator!=(const vpRowVector &v) const;
226 
227  double operator*(const vpColVector &x) const;
228  vpRowVector operator*(const vpMatrix &M) const;
229  vpRowVector operator*(double x) const;
230  vpRowVector &operator*=(double x);
231 
232  vpRowVector operator/(double x) const;
233  vpRowVector &operator/=(double x);
234 
235  vpRowVector operator+(const vpRowVector &v) const;
236  vpRowVector &operator+=(vpRowVector v);
237 
238  vpRowVector operator-(const vpRowVector &v) const;
239  vpRowVector &operator-=(vpRowVector v);
240  vpRowVector operator-() const;
241 
243  vpRowVector &operator<<(double val);
244  vpRowVector &operator,(double val);
245 
246  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
251  inline void rad2deg()
252  {
253  double r2d = 180.0 / M_PI;
254 
255  (*this) *= r2d;
256  }
257 
258  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
259  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
260 
266  inline void resize(unsigned int i, bool flagNullify = true) { vpArray2D<double>::resize(1, i, flagNullify); }
267 
278  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
279  {
280  if (nrows != 1)
282  "Cannot resize a row vector to a (%dx%d) dimension "
283  "vector that has more than one row",
284  nrows, ncols));
285  vpArray2D<double>::resize(nrows, ncols, flagNullify);
286  }
287 
288  void stack(double d);
289  void stack(const vpRowVector &v);
290 
291  double sum() const;
292  double sumSquare() const;
293  vpColVector t() const;
294  std::vector<double> toStdVector() const;
295  vpColVector transpose() const;
296  void transpose(vpColVector &v) const;
297 
298  static double mean(const vpRowVector &v);
299  static double median(const vpRowVector &v);
300  static vpRowVector stack(const vpRowVector &A, const vpRowVector &B);
301  static void stack(const vpRowVector &A, const vpRowVector &B, vpRowVector &C);
302  static double stdev(const vpRowVector &v, bool useBesselCorrection = false);
303 
304 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
313  vp_deprecated void init() {}
317  vp_deprecated void stackMatrices(const vpRowVector &r) { stack(r); }
322  vp_deprecated static vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B) { return stack(A, B); }
327  vp_deprecated static void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
328  {
329  stack(A, B, C);
330  }
331 
335  vp_deprecated void setIdentity(const double &val = 1.0);
336  vp_deprecated double euclideanNorm() const;
338 #endif
339 };
340 
341 VISP_EXPORT vpRowVector operator*(const double &x, const vpRowVector &v);
342 
343 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:131
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:417
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:305
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:529
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1059
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1227
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:445
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:383
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1173
Implementation of column vector and the associated operations.
Definition: vpColVector.h:167
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
VISP_EXPORT vpImagePoint operator*(const vpImagePoint &ip1, double scale)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:152
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:111
vp_deprecated void setIdentity(const double &val=1.0)
vpRowVector(const vpRowVector &v)
Definition: vpRowVector.h:122
vpRowVector(unsigned int n, double val)
Construct a row vector of size n. Each element is set to val.
Definition: vpRowVector.h:119
double & operator[](unsigned int n)
Operator that allows to set a value of an element : v[i] = x.
Definition: vpRowVector.h:209
vpRowVector(unsigned int n)
Definition: vpRowVector.h:117
static vp_deprecated vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B)
Definition: vpRowVector.h:322
vp_deprecated void stackMatrices(const vpRowVector &r)
Definition: vpRowVector.h:317
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:266
void deg2rad()
Definition: vpRowVector.h:162
void rad2deg()
Definition: vpRowVector.h:251
vpRowVector()
Basic constructor that creates an empty 0-size row vector.
Definition: vpRowVector.h:114
vpRowVector(const std::initializer_list< double > &list)
Definition: vpRowVector.h:130
vp_deprecated void init()
Definition: vpRowVector.h:313
static vp_deprecated void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
Definition: vpRowVector.h:327
void clear()
Definition: vpRowVector.h:141
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpRowVector.h:278
virtual ~vpRowVector()
Definition: vpRowVector.h:135
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpRowVector.h:211
vpRowVector extract(unsigned int c, unsigned int rowsize) const
Definition: vpRowVector.h:186
vpColVector operator*(const double &x, const vpColVector &v)