Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpColVector.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  * Provide some simple operation on column vectors.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
39 #ifndef vpColVector_H
40 #define vpColVector_H
41 
42 #include <visp3/core/vpArray2D.h>
43 #include <visp3/core/vpMath.h>
44 #include <visp3/core/vpPoseVector.h>
45 #include <visp3/core/vpRotationVector.h>
46 #include <visp3/core/vpRowVector.h>
47 
48 class vpMatrix;
49 class vpRowVector;
50 class vpRotationVector;
52 class vpPoseVector;
53 
72 class VISP_EXPORT vpColVector : public vpArray2D<double>
73 {
74  friend class vpMatrix;
75 
76 public:
78  vpColVector() : vpArray2D<double>() {}
82  explicit vpColVector(unsigned int n) : vpArray2D<double>(n, 1) {}
84  vpColVector(unsigned int n, double val) : vpArray2D<double>(n, 1, val) {}
87  vpColVector(const vpColVector &v) : vpArray2D<double>(v) {}
88  vpColVector(const vpColVector &v, unsigned int r, unsigned int nrows);
91  vpColVector(const vpRotationVector &v);
93  vpColVector(const vpPoseVector &p);
97  vpColVector(const vpMatrix &M);
98  vpColVector(const vpMatrix &M, unsigned int j);
99  vpColVector(const std::vector<double> &v);
100  vpColVector(const std::vector<float> &v);
101 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
103 #endif
104 
107  virtual ~vpColVector() {}
108 
113  void clear()
114  {
115  if (data != NULL) {
116  free(data);
117  data = NULL;
118  }
119 
120  if (rowPtrs != NULL) {
121  free(rowPtrs);
122  rowPtrs = NULL;
123  }
124  rowNum = colNum = dsize = 0;
125  }
126 
127  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
128  std::ostream &csvPrint(std::ostream &os) const;
129 
134  inline void deg2rad()
135  {
136  double d2r = M_PI / 180.0;
137 
138  (*this) *= d2r;
139  }
140 
141  double euclideanNorm() const;
158  vpColVector extract(unsigned int r, unsigned int colsize) const
159  {
160  if (r >= rowNum || r + colsize > rowNum) {
162  "Cannot extract a (%dx1) column vector from a (%dx1) "
163  "column vector starting at index %d",
164  colsize, rowNum, r));
165  }
166 
167  return vpColVector(*this, r, colsize);
168  }
169 
170  vpColVector hadamard(const vpColVector &v) const;
171 
172  double infinityNorm() const;
173  void init(const vpColVector &v, unsigned int r, unsigned int nrows);
174  void insert(unsigned int i, const vpColVector &v);
175  void insert(const vpColVector &v, unsigned int i);
176 
177  std::ostream &maplePrint(std::ostream &os) const;
178  std::ostream &matlabPrint(std::ostream &os) const;
179 
180  vpColVector &normalize();
181  vpColVector &normalize(vpColVector &x) const;
182 
184  inline double &operator[](unsigned int n) { return *(data + n); }
186  inline const double &operator[](unsigned int n) const { return *(data + n); }
188  vpColVector &operator=(const vpColVector &v);
192  vpColVector &operator=(const vpMatrix &M);
193  vpColVector &operator=(const std::vector<double> &v);
194  vpColVector &operator=(const std::vector<float> &v);
195  vpColVector &operator=(double x);
196 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
198 #endif
199  bool operator==(const vpColVector &v) const;
201  bool operator!=(const vpColVector &v) const;
202 
203  double operator*(const vpColVector &x) const;
204  vpMatrix operator*(const vpRowVector &v) const;
205  vpColVector operator*(const double x) const;
206  vpColVector &operator*=(double x);
207 
208  vpColVector operator/(const double x) const;
209  vpColVector &operator/=(double x);
210 
211  vpColVector operator+(const vpColVector &v) const;
214 
215  vpColVector operator-(const vpColVector &v) const;
217  vpColVector operator-() const;
218 
220  vpColVector &operator<<(double *);
221 
222  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
223 
228  inline void rad2deg()
229  {
230  double r2d = 180.0 / M_PI;
231 
232  (*this) *= r2d;
233  }
234 
235  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
236  vpMatrix reshape(const unsigned int &nrows, const unsigned int &ncols);
237 
244  void resize(const unsigned int i, const bool flagNullify = true) { vpArray2D<double>::resize(i, 1, flagNullify); }
255  void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify)
256  {
257  if (ncols != 1) {
259  "Cannot resize a column vector to a (%dx%d) "
260  "dimension vector that has more than one column",
261  nrows, ncols));
262  }
263  vpArray2D<double>::resize(nrows, ncols, flagNullify);
264  }
265 
266  void stack(double d);
267  void stack(const vpColVector &v);
268 
269  double sum() const;
270  double sumSquare() const;
271  vpRowVector t() const;
272  std::vector<double> toStdVector();
273  vpRowVector transpose() const;
274  void transpose(vpRowVector &v) const;
275 
285  inline static vpColVector cross(const vpColVector &a, const vpColVector &b) { return crossProd(a, b); }
286  static vpColVector crossProd(const vpColVector &a, const vpColVector &b);
287 
288  static double dotProd(const vpColVector &a, const vpColVector &b);
289  static vpColVector invSort(const vpColVector &v);
290  static double median(const vpColVector &v);
291  static double mean(const vpColVector &v);
292  // Compute the skew matrix [v]x
293  static vpMatrix skew(const vpColVector &v);
294 
295  static vpColVector sort(const vpColVector &v);
296 
297  static vpColVector stack(const vpColVector &A, const vpColVector &B);
298  static void stack(const vpColVector &A, const vpColVector &B, vpColVector &C);
299 
300  static double stdev(const vpColVector &v, const bool useBesselCorrection = false);
301 
302 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
303 
311  vp_deprecated void init() {}
315  vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
316  {
317  return vpColVector(*this, first_row - 1, last_row - first_row + 1);
318  }
322  vp_deprecated void setIdentity(const double &val = 1.0);
326  vp_deprecated void stackMatrices(const vpColVector &r) { stack(r); }
331  vp_deprecated static vpColVector stackMatrices(const vpColVector &A, const vpColVector &B) { return stack(A, B); }
336  vp_deprecated static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
337  {
338  stack(A, B, C);
339  }
340 
341  vp_deprecated void insert(const vpColVector &v, const unsigned int r, const unsigned int c = 0);
343 #endif
344 };
345 
346 #ifndef DOXYGEN_SHOULD_SKIP_THIS
347 VISP_EXPORT
348 #endif
349 vpColVector operator*(const double &x, const vpColVector &v);
350 
351 #endif
std::ostream & csvPrint(std::ostream &os) const
Definition: vpMatrix.cpp:4389
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
Implementation of a generic rotation vector.
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:158
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:247
double infinityNorm() const
Definition: vpMatrix.cpp:5117
vp_deprecated void init()
Definition: vpColVector.h:311
static vpColVector cross(const vpColVector &a, const vpColVector &b)
Definition: vpColVector.h:285
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:72
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
Definition: vpMatrix.cpp:4438
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:171
vpMatrix & operator*=(const double x)
Multiply all the element of the matrix by x : Aij = Aij * x.
Definition: vpMatrix.cpp:1424
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:4462
vpMatrix & operator/=(double x)
Divide all the element of the matrix by x : Aij = Aij / x.
Definition: vpMatrix.cpp:1434
error that can be emited by ViSP classes.
Definition: vpException.h:71
double & operator[](unsigned int n)
Operator that allows to set a value of an element : v[i] = x.
Definition: vpColVector.h:184
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
Implementation of a generic 2D array used as vase class of matrices and vectors.
Definition: vpArray2D.h:70
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
Definition: vpMatrix.cpp:1260
static vp_deprecated void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
Definition: vpColVector.h:336
double sumSquare() const
Definition: vpMatrix.cpp:5138
std::ostream & maplePrint(std::ostream &os) const
Definition: vpMatrix.cpp:4348
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
Definition: vpMatrix.cpp:1277
void deg2rad()
Definition: vpColVector.h:134
vpColVector(unsigned int n, double val)
Construct a column vector of size n. Each element is set to val.
Definition: vpColVector.h:84
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:74
void insert(const vpMatrix &A, const unsigned int r, const unsigned int c)
Definition: vpMatrix.cpp:4570
vp_deprecated void setIdentity(const double &val=1.0)
Definition: vpMatrix.cpp:5317
vpColVector(unsigned int n)
Definition: vpColVector.h:82
vp_deprecated void init()
Definition: vpMatrix.h:686
void clear()
Definition: vpColVector.h:113
virtual ~vpColVector()
Definition: vpColVector.h:107
vp_deprecated void stackMatrices(const vpColVector &r)
Definition: vpColVector.h:326
vpMatrix operator+(const vpMatrix &B) const
Definition: vpMatrix.cpp:1171
vpMatrix transpose() const
Definition: vpMatrix.cpp:394
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:275
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:76
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition: vpArray2D.h:714
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify)
Definition: vpColVector.h:255
int print(std::ostream &s, unsigned int length, char const *intro=0) const
Definition: vpMatrix.cpp:4181
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpColVector.h:186
vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
Definition: vpColVector.h:315
vpMatrix t() const
Definition: vpMatrix.cpp:375
double sum() const
Definition: vpMatrix.cpp:1326
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
double euclideanNorm() const
Definition: vpMatrix.cpp:5096
std::ostream & matlabPrint(std::ostream &os) const
Definition: vpMatrix.cpp:4304
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
vpMatrix operator/(const double x) const
Cij = Aij / x (A is unchanged)
Definition: vpMatrix.cpp:1380
vpMatrix operator*(const vpMatrix &B) const
Definition: vpMatrix.cpp:941
vpColVector()
Basic constructor that creates an empty 0-size column vector.
Definition: vpColVector.h:78
static vp_deprecated vpColVector stackMatrices(const vpColVector &A, const vpColVector &B)
Definition: vpColVector.h:331
vpMatrix operator-() const
Definition: vpMatrix.cpp:1319
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:80
Class that consider the case of a translation vector.
void rad2deg()
Definition: vpColVector.h:228
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:78
vpColVector(const vpColVector &v)
Definition: vpColVector.h:87
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:244