Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
vpRowVector.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Operation on row vectors.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
39 #ifndef vpRowVector_H
40 #define vpRowVector_H
41 
42 #include <vector>
43 
44 #include <visp3/core/vpArray2D.h>
45 #include <visp3/core/vpColVector.h>
46 #include <visp3/core/vpMath.h>
47 #include <visp3/core/vpMatrix.h>
48 
49 class vpMatrix;
50 class vpColVector;
51 
115 class VISP_EXPORT vpRowVector : public vpArray2D<double>
116 {
117 public:
119  vpRowVector() : vpArray2D<double>() {}
122  explicit vpRowVector(unsigned int n) : vpArray2D<double>(1, n) {}
124  vpRowVector(unsigned int n, double val) : vpArray2D<double>(1, n, val) {}
127  vpRowVector(const vpRowVector &v) : vpArray2D<double>(v) {}
128  vpRowVector(const vpRowVector &v, unsigned int c, unsigned int ncols);
129  vpRowVector(const vpMatrix &M);
130  vpRowVector(const vpMatrix &M, unsigned int i);
131  vpRowVector(const std::vector<double> &v);
132  vpRowVector(const std::vector<float> &v);
133 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
135  vpRowVector(const std::initializer_list<double> &list) : vpArray2D<double>(list) { }
136 #endif
137 
140  virtual ~vpRowVector() {}
141 
146  void clear()
147  {
148  if (data != NULL) {
149  free(data);
150  data = NULL;
151  }
152 
153  if (rowPtrs != NULL) {
154  free(rowPtrs);
155  rowPtrs = NULL;
156  }
157  rowNum = colNum = dsize = 0;
158  }
159 
160  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
161  std::ostream &csvPrint(std::ostream &os) const;
162 
167  inline void deg2rad()
168  {
169  double d2r = M_PI / 180.0;
170 
171  (*this) *= d2r;
172  }
173 
174  vp_deprecated double euclideanNorm() const;
191  vpRowVector extract(unsigned int c, unsigned int rowsize) const
192  {
193  if (c >= colNum || c + rowsize > colNum) {
195  "Cannot extract a (1x%d) row vector from a (1x%d) "
196  "row vector starting at index %d",
197  rowsize, colNum, c));
198  }
199 
200  return vpRowVector(*this, c, rowsize);
201  }
202 
203  double frobeniusNorm() const;
204  void init(const vpRowVector &v, unsigned int c, unsigned int ncols);
205  void insert(unsigned int i, const vpRowVector &v);
206 
207  std::ostream &maplePrint(std::ostream &os) const;
208  std::ostream &matlabPrint(std::ostream &os) const;
209 
210  vpRowVector &normalize();
211  vpRowVector &normalize(vpRowVector &x) const;
212 
214  inline double &operator[](unsigned int n) { return *(data + n); }
216  inline const double &operator[](unsigned int n) const { return *(data + n); }
217 
219  vpRowVector &operator=(const vpRowVector &v);
220  vpRowVector &operator=(const vpMatrix &M);
221  vpRowVector &operator=(const std::vector<double> &v);
222  vpRowVector &operator=(const std::vector<float> &v);
223  vpRowVector &operator=(double x);
224 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
226  vpRowVector &operator=(const std::initializer_list<double> &list);
227 #endif
228  bool operator==(const vpRowVector &v) const;
230  bool operator!=(const vpRowVector &v) const;
231 
232  double operator*(const vpColVector &x) const;
233  vpRowVector operator*(const vpMatrix &M) const;
234  vpRowVector operator*(double x) const;
235  vpRowVector &operator*=(double x);
236 
237  vpRowVector operator/(double x) const;
238  vpRowVector &operator/=(double x);
239 
240  vpRowVector operator+(const vpRowVector &v) const;
241  vpRowVector &operator+=(vpRowVector v);
242 
243  vpRowVector operator-(const vpRowVector &v) const;
244  vpRowVector &operator-=(vpRowVector v);
245  vpRowVector operator-() const;
246 
248  vpRowVector& operator<<(double val);
249  vpRowVector& operator,(double val);
250 
251  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
256  inline void rad2deg()
257  {
258  double r2d = 180.0 / M_PI;
259 
260  (*this) *= r2d;
261  }
262 
263  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
264  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
265 
271  inline void resize(unsigned int i, bool flagNullify = true)
272  {
273  vpArray2D<double>::resize(1, i, flagNullify);
274  }
275 
286  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
287  {
288  if (nrows != 1)
290  "Cannot resize a row vector to a (%dx%d) dimension "
291  "vector that has more than one row",
292  nrows, ncols));
293  vpArray2D<double>::resize(nrows, ncols, flagNullify);
294  }
295 
296  void stack(double d);
297  void stack(const vpRowVector &v);
298 
299  double sum() const;
300  double sumSquare() const;
301  vpColVector t() const;
302  std::vector<double> toStdVector() const;
303  vpColVector transpose() const;
304  void transpose(vpColVector &v) const;
305 
306  static double mean(const vpRowVector &v);
307  static double median(const vpRowVector &v);
308  static vpRowVector stack(const vpRowVector &A, const vpRowVector &B);
309  static void stack(const vpRowVector &A, const vpRowVector &B, vpRowVector &C);
310  static double stdev(const vpRowVector &v, bool useBesselCorrection = false);
311 
312 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
313 
321  vp_deprecated void init() {}
325  vp_deprecated void stackMatrices(const vpRowVector &r) { stack(r); }
330  vp_deprecated static vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B) { return stack(A, B); }
335  vp_deprecated static void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
336  {
337  stack(A, B, C);
338  }
339 
343  vp_deprecated void setIdentity(const double &val = 1.0);
345 #endif
346 };
347 
348 VISP_EXPORT vpRowVector operator*(const double &x, const vpRowVector &v);
349 
350 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
static vp_deprecated vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B)
Definition: vpRowVector.h:330
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:304
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:413
vpRowVector(const vpRowVector &v)
Definition: vpRowVector.h:127
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:996
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:115
static vp_deprecated void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
Definition: vpRowVector.h:335
vp_deprecated void stackMatrices(const vpRowVector &r)
Definition: vpRowVector.h:325
error that can be emited by ViSP classes.
Definition: vpException.h:71
vp_deprecated void init()
Definition: vpRowVector.h:321
Implementation of a generic 2D array used as base class for matrices and vectors. ...
Definition: vpArray2D.h:131
vpRowVector(const std::initializer_list< double > &list)
Definition: vpRowVector.h:135
vpColVector operator*(const double &x, const vpColVector &v)
double & operator[](unsigned int n)
Operator that allows to set a value of an element : v[i] = x.
Definition: vpRowVector.h:214
vpRowVector extract(unsigned int c, unsigned int rowsize) const
Definition: vpRowVector.h:191
virtual ~vpRowVector()
Definition: vpRowVector.h:140
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:948
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:493
void clear()
Definition: vpRowVector.h:146
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
vpRowVector(unsigned int n, double val)
Construct a row vector of size n. Each element is set to val.
Definition: vpRowVector.h:124
void deg2rad()
Definition: vpRowVector.h:167
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:271
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:379
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpRowVector.h:216
vpRowVector(unsigned int n)
Definition: vpRowVector.h:122
vpRowVector()
Basic constructor that creates an empty 0-size row vector.
Definition: vpRowVector.h:119
void rad2deg()
Definition: vpRowVector.h:256
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpRowVector.h:286