Visual Servoing Platform  version 3.5.1 under development (2023-03-29)
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 
130 class VISP_EXPORT vpColVector : public vpArray2D<double>
131 {
132  friend class vpMatrix;
133 
134 public:
136  vpColVector() : vpArray2D<double>() {}
140  explicit vpColVector(unsigned int n) : vpArray2D<double>(n, 1) {}
142  vpColVector(unsigned int n, double val) : vpArray2D<double>(n, 1, val) {}
145  vpColVector(const vpColVector &v) : vpArray2D<double>(v) {}
146  vpColVector(const vpColVector &v, unsigned int r, unsigned int nrows);
149  vpColVector(const vpRotationVector &v);
151  vpColVector(const vpPoseVector &p);
155  vpColVector(const vpMatrix &M);
156  vpColVector(const vpMatrix &M, unsigned int j);
157  vpColVector(const std::vector<double> &v);
158  vpColVector(const std::vector<float> &v);
159 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
161  vpColVector(const std::initializer_list<double> &list) : vpArray2D<double>(static_cast<unsigned int>(list.size()), 1)
162  {
163  std::copy(list.begin(), list.end(), data);
164  }
165 #endif
169  virtual ~vpColVector() {}
170 
175  void clear()
176  {
177  if (data != NULL) {
178  free(data);
179  data = NULL;
180  }
181 
182  if (rowPtrs != NULL) {
183  free(rowPtrs);
184  rowPtrs = NULL;
185  }
186  rowNum = colNum = dsize = 0;
187  }
188 
189  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
190  std::ostream &csvPrint(std::ostream &os) const;
191 
198  {
199  double d2r = M_PI / 180.0;
200 
201  (*this) *= d2r;
202  return (*this);
203  }
204 
205  vp_deprecated double euclideanNorm() const;
222  vpColVector extract(unsigned int r, unsigned int colsize) const
223  {
224  if (r >= rowNum || r + colsize > rowNum) {
226  "Cannot extract a (%dx1) column vector from a (%dx1) "
227  "column vector starting at index %d",
228  colsize, rowNum, r));
229  }
230 
231  return vpColVector(*this, r, colsize);
232  }
233 
234  double frobeniusNorm() const;
235  vpColVector hadamard(const vpColVector &v) const;
236 
237  double infinityNorm() const;
238  void init(const vpColVector &v, unsigned int r, unsigned int nrows);
239  void insert(unsigned int i, const vpColVector &v);
240  void insert(const vpColVector &v, unsigned int i);
241 
242  std::ostream &maplePrint(std::ostream &os) const;
243  std::ostream &matlabPrint(std::ostream &os) const;
244 
245  vpColVector &normalize();
246  vpColVector &normalize(vpColVector &x) const;
247 
249  inline double &operator[](unsigned int n) { return *(data + n); }
251  inline const double &operator[](unsigned int n) const { return *(data + n); }
253  vpColVector &operator=(const vpColVector &v);
257  vpColVector &operator=(const vpMatrix &M);
258  vpColVector &operator=(const std::vector<double> &v);
259  vpColVector &operator=(const std::vector<float> &v);
260  vpColVector &operator=(double x);
261 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
263  vpColVector &operator=(const std::initializer_list<double> &list);
264 #endif
266  bool operator==(const vpColVector &v) const;
267  bool operator!=(const vpColVector &v) const;
268 
269  double operator*(const vpColVector &x) const;
270  vpMatrix operator*(const vpRowVector &v) const;
271  vpColVector operator*(double x) const;
272  vpColVector &operator*=(double x);
273 
274  vpColVector operator/(double x) const;
275  vpColVector &operator/=(double x);
276 
277  vpColVector operator+(const vpColVector &v) const;
280 
281  vpColVector operator-(const vpColVector &v) const;
283  vpColVector operator-() const;
284 
286  vpColVector &operator<<(double *);
287  vpColVector &operator<<(double val);
288  vpColVector &operator,(double val);
289 
290  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
291 
298  {
299  double r2d = 180.0 / M_PI;
300 
301  (*this) *= r2d;
302  return (*this);
303  }
304 
305  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
306  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
307 
314  void resize(unsigned int i, bool flagNullify = true) { vpArray2D<double>::resize(i, 1, flagNullify); }
325  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
326  {
327  if (ncols != 1) {
329  "Cannot resize a column vector to a (%dx%d) "
330  "dimension vector that has more than one column",
331  nrows, ncols));
332  }
333  vpArray2D<double>::resize(nrows, ncols, flagNullify);
334  }
335 
336  void stack(double d);
337  void stack(const vpColVector &v);
338 
339  double sum() const;
340  double sumSquare() const;
341  vpRowVector t() const;
342  std::vector<double> toStdVector() const;
343  vpRowVector transpose() const;
344  void transpose(vpRowVector &v) const;
345 
354  inline static vpColVector cross(const vpColVector &a, const vpColVector &b) { return crossProd(a, b); }
355  static vpColVector crossProd(const vpColVector &a, const vpColVector &b);
356 
357  static double dotProd(const vpColVector &a, const vpColVector &b);
358  static vpColVector invSort(const vpColVector &v);
359  static double median(const vpColVector &v);
360  static double mean(const vpColVector &v);
361  // Compute the skew matrix [v]x
362  static vpMatrix skew(const vpColVector &v);
363 
364  static vpColVector sort(const vpColVector &v);
365 
366  static vpColVector stack(const vpColVector &A, const vpColVector &B);
367  static void stack(const vpColVector &A, const vpColVector &B, vpColVector &C);
368 
369  static double stdev(const vpColVector &v, bool useBesselCorrection = false);
370 
371 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
380  vp_deprecated void init() {}
384  vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
385  {
386  return vpColVector(*this, first_row - 1, last_row - first_row + 1);
387  }
391  vp_deprecated void setIdentity(const double &val = 1.0);
395  vp_deprecated void stackMatrices(const vpColVector &r) { stack(r); }
400  vp_deprecated static vpColVector stackMatrices(const vpColVector &A, const vpColVector &B) { return stack(A, B); }
405  vp_deprecated static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
406  {
407  stack(A, B, C);
408  }
409 
410  vp_deprecated void insert(const vpColVector &v, unsigned int r, unsigned int c = 0);
412 #endif
413 };
414 
415 #ifndef DOXYGEN_SHOULD_SKIP_THIS
416 VISP_EXPORT
417 #endif
418 vpColVector operator*(const double &x, const vpColVector &v);
419 
420 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:129
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:142
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:988
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:136
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:303
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:132
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:491
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:138
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:290
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:411
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition: vpArray2D.h:924
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:378
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:134
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:940
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:222
void insert(const vpColVector &v, unsigned int i)
vpColVector(unsigned int n, double val)
Construct a column vector of size n. Each element is set to val.
Definition: vpColVector.h:142
vpColVector & rad2deg()
Definition: vpColVector.h:297
vpColVector(unsigned int n)
Definition: vpColVector.h:140
static vp_deprecated vpColVector stackMatrices(const vpColVector &A, const vpColVector &B)
Definition: vpColVector.h:400
static vp_deprecated void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
Definition: vpColVector.h:405
double & operator[](unsigned int n)
Operator that allows to set a value of an element : v[i] = x.
Definition: vpColVector.h:249
vpColVector(const vpColVector &v)
Definition: vpColVector.h:145
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpColVector.h:251
vp_deprecated void init()
Definition: vpColVector.h:380
vpColVector()
Basic constructor that creates an empty 0-size column vector.
Definition: vpColVector.h:136
vpColVector & deg2rad()
Definition: vpColVector.h:197
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpColVector.h:325
virtual ~vpColVector()
Definition: vpColVector.h:169
vp_deprecated void setIdentity(const double &val=1.0)
void clear()
Definition: vpColVector.h:175
vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
Definition: vpColVector.h:384
vpColVector(const std::initializer_list< double > &list)
Definition: vpColVector.h:161
static vpColVector cross(const vpColVector &a, const vpColVector &b)
Definition: vpColVector.h:354
vp_deprecated void stackMatrices(const vpColVector &r)
Definition: vpColVector.h:395
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:314
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ fatalError
Fatal error.
Definition: vpException.h:96
VISP_EXPORT vpImagePoint operator*(const vpImagePoint &ip1, double scale)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
Definition: vpMatrix.cpp:5837
vpMatrix operator-() const
Definition: vpMatrix.cpp:1549
vpMatrix & operator/=(double x)
Divide all the element of the matrix by x : Aij = Aij / x.
Definition: vpMatrix.cpp:1680
int print(std::ostream &s, unsigned int length, const std::string &intro="") const
Definition: vpMatrix.cpp:5587
std::ostream & maplePrint(std::ostream &os) const
Definition: vpMatrix.cpp:5747
vpMatrix operator*(const vpMatrix &B) const
Definition: vpMatrix.cpp:1159
vpMatrix operator/(double x) const
Cij = Aij / x (A is unchanged)
Definition: vpMatrix.cpp:1620
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:5861
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
Definition: vpMatrix.cpp:1507
vpMatrix & operator*=(double x)
Multiply all the element of the matrix by x : Aij = Aij * x.
Definition: vpMatrix.cpp:1666
vpMatrix operator+(const vpMatrix &B) const
Definition: vpMatrix.cpp:1401
double sum() const
Definition: vpMatrix.cpp:1556
vpMatrix t() const
Definition: vpMatrix.cpp:462
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
Definition: vpMatrix.cpp:1490
double infinityNorm() const
Definition: vpMatrix.cpp:6739
double frobeniusNorm() const
Definition: vpMatrix.cpp:6680
vpMatrix & operator,(double val)
Definition: vpMatrix.cpp:797
double sumSquare() const
Definition: vpMatrix.cpp:6760
vp_deprecated void init()
Definition: vpMatrix.h:1011
void insert(const vpMatrix &A, unsigned int r, unsigned int c)
Definition: vpMatrix.cpp:5970
vpMatrix transpose() const
Definition: vpMatrix.cpp:469
std::ostream & csvPrint(std::ostream &os) const
Definition: vpMatrix.cpp:5788
std::ostream & matlabPrint(std::ostream &os) const
Definition: vpMatrix.cpp:5703
double euclideanNorm() const
Definition: vpMatrix.cpp:6876
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:152
Implementation of a generic rotation vector.
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:116
Class that consider the case of a translation vector.