Visual Servoing Platform  version 3.1.0
vpRowVector.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
72 class VISP_EXPORT vpRowVector : public vpArray2D<double>
73 {
74 public:
76  vpRowVector() : vpArray2D<double>() {}
79  explicit vpRowVector(unsigned int n) : vpArray2D<double>(1, n) {}
81  vpRowVector(unsigned int n, double val) : vpArray2D<double>(1, n, val) {}
84  vpRowVector(const vpRowVector &v) : vpArray2D<double>(v) {}
85  vpRowVector(const vpRowVector &v, unsigned int c, unsigned int ncols);
86  vpRowVector(const vpMatrix &M);
87  vpRowVector(const vpMatrix &M, unsigned int i);
88  vpRowVector(const std::vector<double> &v);
89  vpRowVector(const std::vector<float> &v);
93  virtual ~vpRowVector() {}
94 
99  void clear()
100  {
101  if (data != NULL) {
102  free(data);
103  data = NULL;
104  }
105 
106  if (rowPtrs != NULL) {
107  free(rowPtrs);
108  rowPtrs = NULL;
109  }
110  rowNum = colNum = dsize = 0;
111  }
112 
113  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
114  std::ostream &csvPrint(std::ostream &os) const;
115 
120  inline void deg2rad()
121  {
122  double d2r = M_PI / 180.0;
123 
124  (*this) *= d2r;
125  }
126 
127  double euclideanNorm() const;
144  vpRowVector extract(unsigned int c, unsigned int rowsize) const
145  {
146  if (c >= colNum || c + rowsize > colNum) {
148  "Cannot extract a (1x%d) row vector from a (1x%d) "
149  "row vector starting at index %d",
150  rowsize, colNum, c));
151  }
152 
153  return vpRowVector(*this, c, rowsize);
154  }
155 
156  void init(const vpRowVector &v, unsigned int c, unsigned int ncols);
157  void insert(unsigned int i, const vpRowVector &v);
158 
159  std::ostream &maplePrint(std::ostream &os) const;
160  std::ostream &matlabPrint(std::ostream &os) const;
161 
162  vpRowVector &normalize();
163  vpRowVector &normalize(vpRowVector &x) const;
164 
166  inline double &operator[](unsigned int n) { return *(data + n); }
168  inline const double &operator[](unsigned int n) const { return *(data + n); }
169 
171  vpRowVector &operator=(const vpRowVector &v);
172  vpRowVector &operator=(const vpMatrix &M);
173  vpRowVector &operator=(const std::vector<double> &v);
174  vpRowVector &operator=(const std::vector<float> &v);
175  vpRowVector &operator=(const double x);
176 
177  double operator*(const vpColVector &x) const;
178  vpRowVector operator*(const vpMatrix &M) const;
179  vpRowVector operator*(const double x) const;
180  vpRowVector &operator*=(double x);
181 
182  vpRowVector operator/(const double x) const;
183  vpRowVector &operator/=(double x);
184 
185  vpRowVector operator+(const vpRowVector &v) const;
186  vpRowVector &operator+=(vpRowVector v);
187 
188  vpRowVector operator-(const vpRowVector &v) const;
189  vpRowVector &operator-=(vpRowVector v);
190  vpRowVector operator-() const;
191 
193 
194  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
199  inline void rad2deg()
200  {
201  double r2d = 180.0 / M_PI;
202 
203  (*this) *= r2d;
204  }
205  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
206  vpMatrix reshape(const unsigned int &nrows, const unsigned int &ncols);
207 
213  inline void resize(const unsigned int i, const bool flagNullify = true)
214  {
215  vpArray2D<double>::resize(1, i, flagNullify);
216  }
217 
228  void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify)
229  {
230  if (nrows != 1)
232  "Cannot resize a row vector to a (%dx%d) dimension "
233  "vector that has more than one row",
234  nrows, ncols));
235  vpArray2D<double>::resize(nrows, ncols, flagNullify);
236  }
237 
238  void stack(const double &d);
239  void stack(const vpRowVector &v);
240 
241  double sum() const;
242  double sumSquare() const;
243  vpColVector t() const;
244  vpColVector transpose() const;
245  void transpose(vpColVector &v) const;
246 
247  static double mean(const vpRowVector &v);
248  static double median(const vpRowVector &v);
249  static vpRowVector stack(const vpRowVector &A, const vpRowVector &B);
250  static void stack(const vpRowVector &A, const vpRowVector &B, vpRowVector &C);
251  static double stdev(const vpRowVector &v, const bool useBesselCorrection = false);
252 
253 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
254 
262  vp_deprecated void init() {}
266  vp_deprecated void stackMatrices(const vpRowVector &r) { stack(r); }
271  vp_deprecated static vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B) { return stack(A, B); }
276  vp_deprecated static void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
277  {
278  stack(A, B, C);
279  }
280 
284  vp_deprecated void setIdentity(const double &val = 1.0);
286 #endif
287 };
288 
289 VISP_EXPORT vpRowVector operator*(const double &x, const vpRowVector &v);
290 
291 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
static vp_deprecated vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B)
Definition: vpRowVector.h:271
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:244
vpRowVector(const vpRowVector &v)
Definition: vpRowVector.h:84
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:72
static vp_deprecated void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
Definition: vpRowVector.h:276
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:171
vp_deprecated void stackMatrices(const vpRowVector &r)
Definition: vpRowVector.h:266
error that can be emited by ViSP classes.
Definition: vpException.h:71
vp_deprecated void init()
Definition: vpRowVector.h:262
Implementation of a generic 2D array used as vase class of matrices and vectors.
Definition: vpArray2D.h:70
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify)
Definition: vpRowVector.h:228
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:166
vpRowVector extract(unsigned int c, unsigned int rowsize) const
Definition: vpRowVector.h:144
virtual ~vpRowVector()
Definition: vpRowVector.h:93
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:273
void clear()
Definition: vpRowVector.h:99
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpRowVector.h:213
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpRowVector(unsigned int n, double val)
Construct a row vector of size n. Each element is set to val.
Definition: vpRowVector.h:81
void deg2rad()
Definition: vpRowVector.h:120
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpRowVector.h:168
vpRowVector(unsigned int n)
Definition: vpRowVector.h:79
vpRowVector()
Basic constructor that creates an empty 0-size row vector.
Definition: vpRowVector.h:76
void rad2deg()
Definition: vpRowVector.h:199