Visual Servoing Platform  version 3.6.1 under development (2024-05-28)
vpColVector.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Provide some simple operation on column vectors.
32  */
33 
34 #ifndef _vpColVector_h_
35 #define _vpColVector_h_
36 
37 #include <visp3/core/vpArray2D.h>
38 #include <visp3/core/vpMath.h>
39 #include <visp3/core/vpPoseVector.h>
40 #include <visp3/core/vpRotationVector.h>
41 #include <visp3/core/vpRowVector.h>
42 
43 class vpMatrix;
44 class vpRowVector;
45 class vpRotationVector;
47 class vpPoseVector;
48 
162 class VISP_EXPORT vpColVector : public vpArray2D<double>
163 {
164  friend class vpMatrix;
165 
166 public:
170  vpColVector() : vpArray2D<double>() { }
171 
177  explicit vpColVector(unsigned int n) : vpArray2D<double>(n, 1) { }
178 
182  vpColVector(unsigned int n, double val) : vpArray2D<double>(n, 1, val) { }
183 
187  vpColVector(const vpColVector &v) : vpArray2D<double>(v) { }
188 
203  vpColVector(const vpColVector &v, unsigned int r, unsigned int nrows);
204 
209  explicit vpColVector(const vpRotationVector &v);
210 
214  explicit vpColVector(const vpPoseVector &p);
215 
219  explicit vpColVector(const vpTranslationVector &t);
220 
227  explicit vpColVector(const vpMatrix &M);
228 
232  vpColVector(const vpMatrix &M, unsigned int j);
233 
237  explicit vpColVector(const std::vector<double> &v);
238 
242  explicit vpColVector(const std::vector<float> &v);
243 
244 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
249 #endif
250 
251 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))) // Check if cxx11 or higher
252  vpColVector(const std::initializer_list<double> &list) : vpArray2D<double>(static_cast<unsigned int>(list.size()), 1)
253  {
254  std::copy(list.begin(), list.end(), data);
255  }
256 #endif
257 
262  void clear()
263  {
264  if (data != nullptr) {
265  free(data);
266  data = nullptr;
267  }
268 
269  if (rowPtrs != nullptr) {
270  free(rowPtrs);
271  rowPtrs = nullptr;
272  }
273  rowNum = 0;
274  colNum = 0;
275  dsize = 0;
276  }
277 
308  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
309 
336  std::ostream &csvPrint(std::ostream &os) const;
337 
345  {
346  double d2r = M_PI / 180.0;
347 
348  (*this) *= d2r;
349  return (*this);
350  }
351 
369  vpColVector extract(unsigned int r, unsigned int colsize) const
370  {
371  if ((r >= rowNum) || ((r + colsize) > rowNum)) {
373  "Cannot extract a (%dx1) column vector from a (%dx1) "
374  "column vector starting at index %d",
375  colsize, rowNum, r));
376  }
377 
378  return vpColVector(*this, r, colsize);
379  }
380 
391  double frobeniusNorm() const;
392 
400  vpColVector hadamard(const vpColVector &v) const;
401 
412  double infinityNorm() const;
413 
450  void init(const vpColVector &v, unsigned int r, unsigned int nrows);
451 
484  void insert(unsigned int i, const vpColVector &v);
485 
513  std::ostream &maplePrint(std::ostream &os) const;
514 
553  std::ostream &matlabPrint(std::ostream &os) const;
554 
565  vpColVector &normalize();
566 
578  vpColVector &normalize(vpColVector &x) const;
579 
583  inline double &operator[](unsigned int n) { return *(data + n); }
584 
588  inline const double &operator[](unsigned int n) const { return *(data + n); }
589 
598  vpColVector &operator=(const vpColVector &v);
599 
604 
609 
614 
620  vpColVector &operator=(const vpMatrix &M);
621 
625  vpColVector &operator=(const std::vector<double> &v);
626 
630  vpColVector &operator=(const std::vector<float> &v);
631 
635  vpColVector &operator=(double x);
636 
637 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
642 
666  vpColVector &operator=(const std::initializer_list<double> &list);
667 #endif
668 
676  bool operator==(const vpColVector &v) const;
677 
685  bool operator==(double v) const;
686 
693  bool operator!=(const vpColVector &v) const;
694 
702  bool operator!=(double v) const;
703 
711  double operator*(const vpColVector &v) const;
712 
720  vpMatrix operator*(const vpRowVector &v) const;
721 
742  vpColVector operator*(double x) const;
743 
762  vpColVector &operator*=(double x);
763 
783  vpColVector operator/(double x) const;
784 
802  vpColVector &operator/=(double x);
803 
807  vpColVector operator+(const vpColVector &v) const;
808 
830 
835 
839  vpColVector operator-(const vpColVector &v) const;
840 
845 
856  vpColVector operator-() const;
857 
879 
904  vpColVector &operator<<(double *x);
905 
924  vpColVector &operator<<(double val);
925 
944  vpColVector &operator,(double val);
945 
964  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
965 
973  {
974  double r2d = 180.0 / M_PI;
975 
976  (*this) *= r2d;
977  return (*this);
978  }
979 
1036  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
1037 
1047  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
1048 
1058  void resize(unsigned int i, bool flagNullify = true)
1059  {
1060  vpArray2D<double>::resize(i, 1, flagNullify);
1061  }
1062 
1074  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
1075  {
1076  if (ncols != 1) {
1078  "Cannot resize a column vector to a (%dx%d) "
1079  "dimension vector that has more than one column",
1080  nrows, ncols));
1081  }
1082  vpArray2D<double>::resize(nrows, ncols, flagNullify);
1083  }
1084 
1100  void stack(double d);
1101 
1120  void stack(const vpColVector &v);
1121 
1128  double sum() const;
1129 
1136  double sumSquare() const;
1137 
1141  vpRowVector t() const;
1142 
1147  std::vector<double> toStdVector() const;
1148 
1153  vpRowVector transpose() const;
1154 
1159  void transpose(vpRowVector &v) const;
1160 
1174  inline static vpColVector cross(const vpColVector &a, const vpColVector &b) { return crossProd(a, b); }
1175 
1187  static vpColVector crossProd(const vpColVector &a, const vpColVector &b);
1188 
1198  static double dotProd(const vpColVector &a, const vpColVector &b);
1199 
1228  static vpColVector invSort(const vpColVector &v);
1229 
1233  static double median(const vpColVector &v);
1234 
1238  static double mean(const vpColVector &v);
1239 
1254  static vpMatrix skew(const vpColVector &v);
1255 
1283  static vpColVector sort(const vpColVector &v);
1284 
1303  static vpColVector stack(const vpColVector &A, const vpColVector &B);
1304 
1323  static void stack(const vpColVector &A, const vpColVector &B, vpColVector &C);
1324 
1328  static double stdev(const vpColVector &v, bool useBesselCorrection = false);
1329 
1330 #ifdef VISP_HAVE_NLOHMANN_JSON
1337  friend void to_json(nlohmann::json &j, const vpColVector &v);
1338 
1345  friend void from_json(const nlohmann::json &j, vpColVector &v);
1346 #endif
1347 
1348 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1357  vp_deprecated void init() { }
1358 
1394  vp_deprecated void insert(const vpColVector &v, unsigned int i);
1395 
1399  vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
1400  {
1401  return vpColVector(*this, first_row - 1, last_row - first_row + 1);
1402  }
1403 
1407  vp_deprecated void stackMatrices(const vpColVector &r) { stack(r); }
1408 
1412  vp_deprecated static vpColVector stackMatrices(const vpColVector &A, const vpColVector &B) { return stack(A, B); }
1413 
1417  vp_deprecated static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
1418  {
1419  stack(A, B, C);
1420  }
1421 
1435  vp_deprecated void insert(const vpColVector &v, unsigned int r, unsigned int c = 0);
1436 
1446  vp_deprecated double euclideanNorm() const;
1448 #endif
1449 };
1450 
1455 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1456 VISP_EXPORT
1457 #endif
1458 vpColVector operator*(const double &x, const vpColVector &v);
1459 
1460 #ifdef VISP_HAVE_NLOHMANN_JSON
1461 inline void to_json(nlohmann::json &j, const vpColVector &v)
1462 {
1463  const vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
1464  to_json(j, *asArray);
1465  j["type"] = "vpColVector";
1466 }
1467 inline void from_json(const nlohmann::json &j, vpColVector &v)
1468 {
1469  vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
1470  from_json(j, *asArray);
1471  if (v.getCols() != 1) {
1472  throw vpException(vpException::badValue, "From JSON, tried to read a 2D array into a vpColVector");
1473  }
1474 }
1475 
1476 
1477 #endif
1478 
1479 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:126
unsigned int getCols() const
Definition: vpArray2D.h:329
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:133
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:486
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:354
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:129
friend void to_json(nlohmann::json &j, const vpArray2D< T > &array)
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:603
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:135
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:341
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1141
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1325
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:515
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition: vpArray2D.h:1125
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:445
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:131
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1268
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:369
vpColVector(unsigned int n, double val)
Definition: vpColVector.h:182
vpColVector & rad2deg()
Definition: vpColVector.h:972
vpColVector(unsigned int n)
Definition: vpColVector.h:177
static vp_deprecated vpColVector stackMatrices(const vpColVector &A, const vpColVector &B)
Definition: vpColVector.h:1412
static vp_deprecated void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
Definition: vpColVector.h:1417
double & operator[](unsigned int n)
Definition: vpColVector.h:583
vpColVector(const vpColVector &v)
Definition: vpColVector.h:187
const double & operator[](unsigned int n) const
Definition: vpColVector.h:588
vp_deprecated void init()
Definition: vpColVector.h:1357
vpColVector operator*(const double &x, const vpColVector &v)
vpColVector & deg2rad()
Definition: vpColVector.h:344
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpColVector.h:1074
void clear()
Definition: vpColVector.h:262
vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
Definition: vpColVector.h:1399
vpColVector(const std::initializer_list< double > &list)
Definition: vpColVector.h:252
static vpColVector cross(const vpColVector &a, const vpColVector &b)
Definition: vpColVector.h:1174
vp_deprecated void stackMatrices(const vpColVector &r)
Definition: vpColVector.h:1407
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1058
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:72
@ fatalError
Fatal error.
Definition: vpException.h:71
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
Definition: vpMatrix.cpp:5642
vpMatrix operator-() const
Definition: vpMatrix.cpp:1637
vpMatrix & operator/=(double x)
Divide all the element of the matrix by x : Aij = Aij / x.
Definition: vpMatrix.cpp:1780
int print(std::ostream &s, unsigned int length, const std::string &intro="") const
Definition: vpMatrix.cpp:5382
std::ostream & maplePrint(std::ostream &os) const
Definition: vpMatrix.cpp:5548
vpMatrix operator*(const vpMatrix &B) const
Definition: vpMatrix.cpp:1198
vpMatrix operator/(double x) const
Cij = Aij / x (A is unchanged)
Definition: vpMatrix.cpp:1712
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:5669
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
Definition: vpMatrix.cpp:1591
vpMatrix & operator*=(double x)
Multiply all the element of the matrix by x : Aij = Aij * x.
Definition: vpMatrix.cpp:1764
vp_deprecated double euclideanNorm() const
Definition: vpMatrix.cpp:6633
vpMatrix operator+(const vpMatrix &B) const
Definition: vpMatrix.cpp:1481
double sum() const
Definition: vpMatrix.cpp:1644
vpMatrix t() const
Definition: vpMatrix.cpp:465
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
Definition: vpMatrix.cpp:1572
double infinityNorm() const
Definition: vpMatrix.cpp:6588
double frobeniusNorm() const
Definition: vpMatrix.cpp:6528
vpMatrix & operator,(double val)
Definition: vpMatrix.cpp:827
double sumSquare() const
Definition: vpMatrix.cpp:6609
vp_deprecated void init()
Definition: vpMatrix.h:1001
vpMatrix transpose() const
Definition: vpMatrix.cpp:472
std::ostream & csvPrint(std::ostream &os) const
Definition: vpMatrix.cpp:5591
std::ostream & matlabPrint(std::ostream &os) const
Definition: vpMatrix.cpp:5501
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:190
Implementation of a generic rotation vector.
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:107
Class that consider the case of a translation vector.