Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
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  vpColVector(const vpRotationVector &v);
210 
214  vpColVector(const vpPoseVector &p);
215 
220 
227  vpColVector(const vpMatrix &M);
228 
232  vpColVector(const vpMatrix &M, unsigned int j);
233 
237  vpColVector(const std::vector<double> &v);
238 
242  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 = colNum = dsize = 0;
274  }
275 
306  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
307 
334  std::ostream &csvPrint(std::ostream &os) const;
335 
343  {
344  double d2r = M_PI / 180.0;
345 
346  (*this) *= d2r;
347  return (*this);
348  }
349 
367  vpColVector extract(unsigned int r, unsigned int colsize) const
368  {
369  if (r >= rowNum || r + colsize > rowNum) {
371  "Cannot extract a (%dx1) column vector from a (%dx1) "
372  "column vector starting at index %d",
373  colsize, rowNum, r));
374  }
375 
376  return vpColVector(*this, r, colsize);
377  }
378 
389  double frobeniusNorm() const;
390 
398  vpColVector hadamard(const vpColVector &v) const;
399 
410  double infinityNorm() const;
411 
448  void init(const vpColVector &v, unsigned int r, unsigned int nrows);
449 
482  void insert(unsigned int i, const vpColVector &v);
483 
511  std::ostream &maplePrint(std::ostream &os) const;
512 
551  std::ostream &matlabPrint(std::ostream &os) const;
552 
563  vpColVector &normalize();
564 
576  vpColVector &normalize(vpColVector &x) const;
577 
581  inline double &operator[](unsigned int n) { return *(data + n); }
582 
586  inline const double &operator[](unsigned int n) const { return *(data + n); }
587 
596  vpColVector &operator=(const vpColVector &v);
597 
602 
607 
612 
618  vpColVector &operator=(const vpMatrix &M);
619 
623  vpColVector &operator=(const std::vector<double> &v);
624 
628  vpColVector &operator=(const std::vector<float> &v);
629 
633  vpColVector &operator=(double x);
634 
635 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
640 
664  vpColVector &operator=(const std::initializer_list<double> &list);
665 #endif
666 
674  bool operator==(const vpColVector &v) const;
675 
683  bool operator==(double v) const;
684 
691  bool operator!=(const vpColVector &v) const;
692 
700  bool operator!=(double v) const;
701 
709  double operator*(const vpColVector &v) const;
710 
718  vpMatrix operator*(const vpRowVector &v) const;
719 
740  vpColVector operator*(double x) const;
741 
760  vpColVector &operator*=(double x);
761 
781  vpColVector operator/(double x) const;
782 
800  vpColVector &operator/=(double x);
801 
805  vpColVector operator+(const vpColVector &v) const;
806 
828 
833 
837  vpColVector operator-(const vpColVector &v) const;
838 
843 
854  vpColVector operator-() const;
855 
877 
902  vpColVector &operator<<(double *x);
903 
922  vpColVector &operator<<(double val);
923 
942  vpColVector &operator,(double val);
943 
962  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
963 
971  {
972  double r2d = 180.0 / M_PI;
973 
974  (*this) *= r2d;
975  return (*this);
976  }
977 
1034  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
1035 
1045  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
1046 
1056  void resize(unsigned int i, bool flagNullify = true)
1057  {
1058  vpArray2D<double>::resize(i, 1, flagNullify);
1059  }
1060 
1072  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
1073  {
1074  if (ncols != 1) {
1076  "Cannot resize a column vector to a (%dx%d) "
1077  "dimension vector that has more than one column",
1078  nrows, ncols));
1079  }
1080  vpArray2D<double>::resize(nrows, ncols, flagNullify);
1081  }
1082 
1098  void stack(double d);
1099 
1118  void stack(const vpColVector &v);
1119 
1126  double sum() const;
1127 
1134  double sumSquare() const;
1135 
1139  vpRowVector t() const;
1140 
1145  std::vector<double> toStdVector() const;
1146 
1151  vpRowVector transpose() const;
1152 
1157  void transpose(vpRowVector &v) const;
1158 
1172  inline static vpColVector cross(const vpColVector &a, const vpColVector &b) { return crossProd(a, b); }
1173 
1185  static vpColVector crossProd(const vpColVector &a, const vpColVector &b);
1186 
1196  static double dotProd(const vpColVector &a, const vpColVector &b);
1197 
1226  static vpColVector invSort(const vpColVector &v);
1227 
1231  static double median(const vpColVector &v);
1232 
1236  static double mean(const vpColVector &v);
1237 
1252  static vpMatrix skew(const vpColVector &v);
1253 
1281  static vpColVector sort(const vpColVector &v);
1282 
1301  static vpColVector stack(const vpColVector &A, const vpColVector &B);
1302 
1321  static void stack(const vpColVector &A, const vpColVector &B, vpColVector &C);
1322 
1326  static double stdev(const vpColVector &v, bool useBesselCorrection = false);
1327 
1328 #ifdef VISP_HAVE_NLOHMANN_JSON
1335  friend void to_json(nlohmann::json &j, const vpColVector &v);
1336 
1343  friend void from_json(const nlohmann::json &j, vpColVector &v);
1344 #endif
1345 
1346 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1355  vp_deprecated void init() { }
1356 
1392  vp_deprecated void insert(const vpColVector &v, unsigned int i);
1393 
1397  vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
1398  {
1399  return vpColVector(*this, first_row - 1, last_row - first_row + 1);
1400  }
1401 
1405  vp_deprecated void stackMatrices(const vpColVector &r) { stack(r); }
1406 
1410  vp_deprecated static vpColVector stackMatrices(const vpColVector &A, const vpColVector &B) { return stack(A, B); }
1411 
1415  vp_deprecated static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
1416  {
1417  stack(A, B, C);
1418  }
1419 
1433  vp_deprecated void insert(const vpColVector &v, unsigned int r, unsigned int c = 0);
1434 
1444  vp_deprecated double euclideanNorm() const;
1446 #endif
1447 };
1448 
1453 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1454 VISP_EXPORT
1455 #endif
1456 vpColVector operator*(const double &x, const vpColVector &v);
1457 
1458 #ifdef VISP_HAVE_NLOHMANN_JSON
1459 inline void to_json(nlohmann::json &j, const vpColVector &v)
1460 {
1461  const vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
1462  to_json(j, *asArray);
1463  j["type"] = "vpColVector";
1464 }
1465 inline void from_json(const nlohmann::json &j, vpColVector &v)
1466 {
1467  vpArray2D<double> *asArray = (vpArray2D<double>*) & v;
1468  from_json(j, *asArray);
1469  if (v.getCols() != 1) {
1470  throw vpException(vpException::badValue, "From JSON, tried to read a 2D array into a vpColVector");
1471  }
1472 }
1473 
1474 
1475 #endif
1476 
1477 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:125
unsigned int getCols() const
Definition: vpArray2D.h:274
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:138
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:132
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:411
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:299
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:128
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:523
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:134
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:286
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1055
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1223
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:439
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition: vpArray2D.h:1039
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:377
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:130
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1169
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:367
vpColVector(unsigned int n, double val)
Definition: vpColVector.h:182
vpColVector & rad2deg()
Definition: vpColVector.h:970
vpColVector(unsigned int n)
Definition: vpColVector.h:177
static vp_deprecated vpColVector stackMatrices(const vpColVector &A, const vpColVector &B)
Definition: vpColVector.h:1410
static vp_deprecated void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
Definition: vpColVector.h:1415
double & operator[](unsigned int n)
Definition: vpColVector.h:581
vpColVector(const vpColVector &v)
Definition: vpColVector.h:187
const double & operator[](unsigned int n) const
Definition: vpColVector.h:586
vp_deprecated void init()
Definition: vpColVector.h:1355
vpColVector operator*(const double &x, const vpColVector &v)
vpColVector & deg2rad()
Definition: vpColVector.h:342
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpColVector.h:1072
void clear()
Definition: vpColVector.h:262
vp_deprecated vpColVector rows(unsigned int first_row, unsigned int last_row) const
Definition: vpColVector.h:1397
static vpColVector cross(const vpColVector &a, const vpColVector &b)
Definition: vpColVector.h:1172
vp_deprecated void stackMatrices(const vpColVector &r)
Definition: vpColVector.h:1405
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1056
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:85
@ fatalError
Fatal error.
Definition: vpException.h:84
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:5840
vpMatrix operator-() const
Definition: vpMatrix.cpp:1597
vpMatrix & operator/=(double x)
Divide all the element of the matrix by x : Aij = Aij / x.
Definition: vpMatrix.cpp:1728
int print(std::ostream &s, unsigned int length, const std::string &intro="") const
Definition: vpMatrix.cpp:5588
std::ostream & maplePrint(std::ostream &os) const
Definition: vpMatrix.cpp:5751
vpMatrix operator*(const vpMatrix &B) const
Definition: vpMatrix.cpp:1176
vpMatrix operator/(double x) const
Cij = Aij / x (A is unchanged)
Definition: vpMatrix.cpp:1668
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:5865
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
Definition: vpMatrix.cpp:1556
vpMatrix & operator*=(double x)
Multiply all the element of the matrix by x : Aij = Aij * x.
Definition: vpMatrix.cpp:1714
vp_deprecated double euclideanNorm() const
Definition: vpMatrix.cpp:6796
vpMatrix operator+(const vpMatrix &B) const
Definition: vpMatrix.cpp:1450
double sum() const
Definition: vpMatrix.cpp:1604
vpMatrix t() const
Definition: vpMatrix.cpp:457
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
Definition: vpMatrix.cpp:1539
double infinityNorm() const
Definition: vpMatrix.cpp:6751
double frobeniusNorm() const
Definition: vpMatrix.cpp:6691
vpMatrix & operator,(double val)
Definition: vpMatrix.cpp:811
double sumSquare() const
Definition: vpMatrix.cpp:6772
vp_deprecated void init()
Definition: vpMatrix.h:989
vpMatrix transpose() const
Definition: vpMatrix.cpp:464
std::ostream & csvPrint(std::ostream &os) const
Definition: vpMatrix.cpp:5792
std::ostream & matlabPrint(std::ostream &os) const
Definition: vpMatrix.cpp:5706
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:189
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.