Visual Servoing Platform  version 3.2.1 under development (2019-07-16)
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=(const double x);
224 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
226  vpRowVector &operator=(const std::initializer_list<double> &list);
227 #endif
228 
229  double operator*(const vpColVector &x) const;
230  vpRowVector operator*(const vpMatrix &M) const;
231  vpRowVector operator*(const double x) const;
232  vpRowVector &operator*=(double x);
233 
234  vpRowVector operator/(const double x) const;
235  vpRowVector &operator/=(double x);
236 
237  vpRowVector operator+(const vpRowVector &v) const;
238  vpRowVector &operator+=(vpRowVector v);
239 
240  vpRowVector operator-(const vpRowVector &v) const;
241  vpRowVector &operator-=(vpRowVector v);
242  vpRowVector operator-() const;
243 
245  vpRowVector& operator<<(double val);
246  vpRowVector& operator,(double val);
247 
248  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
253  inline void rad2deg()
254  {
255  double r2d = 180.0 / M_PI;
256 
257  (*this) *= r2d;
258  }
259 
260  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
261  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
262 
268  inline void resize(const unsigned int i, const bool flagNullify = true)
269  {
270  vpArray2D<double>::resize(1, i, flagNullify);
271  }
272 
283  void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify)
284  {
285  if (nrows != 1)
287  "Cannot resize a row vector to a (%dx%d) dimension "
288  "vector that has more than one row",
289  nrows, ncols));
290  vpArray2D<double>::resize(nrows, ncols, flagNullify);
291  }
292 
293  void stack(double d);
294  void stack(const vpRowVector &v);
295 
296  double sum() const;
297  double sumSquare() const;
298  vpColVector t() const;
299  std::vector<double> toStdVector();
300  vpColVector transpose() const;
301  void transpose(vpColVector &v) const;
302 
303  static double mean(const vpRowVector &v);
304  static double median(const vpRowVector &v);
305  static vpRowVector stack(const vpRowVector &A, const vpRowVector &B);
306  static void stack(const vpRowVector &A, const vpRowVector &B, vpRowVector &C);
307  static double stdev(const vpRowVector &v, const bool useBesselCorrection = false);
308 
309 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
310 
318  vp_deprecated void init() {}
322  vp_deprecated void stackMatrices(const vpRowVector &r) { stack(r); }
327  vp_deprecated static vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B) { return stack(A, B); }
332  vp_deprecated static void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
333  {
334  stack(A, B, C);
335  }
336 
340  vp_deprecated void setIdentity(const double &val = 1.0);
342 #endif
343 };
344 
345 VISP_EXPORT vpRowVector operator*(const double &x, const vpRowVector &v);
346 
347 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpRowVector.h:216
static vp_deprecated vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B)
Definition: vpRowVector.h:327
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:406
vpRowVector(const vpRowVector &v)
Definition: vpRowVector.h:127
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:332
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:305
vp_deprecated void stackMatrices(const vpRowVector &r)
Definition: vpRowVector.h:322
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpRowVector extract(unsigned int c, unsigned int rowsize) const
Definition: vpRowVector.h:191
vp_deprecated void init()
Definition: vpRowVector.h:318
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
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify)
Definition: vpRowVector.h:283
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
virtual ~vpRowVector()
Definition: vpRowVector.h:140
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:486
void clear()
Definition: vpRowVector.h:146
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpRowVector.h:268
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 reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:381
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:253