Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpColVector.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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
39
40#ifndef VP_COLVECTOR_H
41#define VP_COLVECTOR_H
42
43#include <visp3/core/vpConfig.h>
44
45#ifdef VISP_HAVE_NLOHMANN_JSON
46#include VISP_NLOHMANN_JSON(json.hpp)
47#endif
49class vpMatrix;
50class vpRowVector;
53class vpPoseVector;
54END_VISP_NAMESPACE
55
56#include <visp3/core/vpArray2D.h>
57#include <visp3/core/vpMath.h>
58#include <visp3/core/vpPoseVector.h>
59#include <visp3/core/vpRotationVector.h>
60#include <visp3/core/vpRowVector.h>
61
190class VISP_EXPORT vpColVector : public vpArray2D<double>
191{
192 friend class vpMatrix;
193
194public:
198 vpColVector() : vpArray2D<double>() { }
199
205 VP_EXPLICIT vpColVector(unsigned int n) : vpArray2D<double>(n, 1) { }
206
210 vpColVector(unsigned int n, double val) : vpArray2D<double>(n, 1, val) { }
211
215 vpColVector(const vpColVector &v) : vpArray2D<double>(v) { }
216
231 vpColVector(const vpColVector &v, unsigned int r, unsigned int nrows);
232
237 VP_EXPLICIT vpColVector(const vpRotationVector &v);
238
242 VP_EXPLICIT vpColVector(const vpPoseVector &p);
243
247 VP_EXPLICIT vpColVector(const vpTranslationVector &t);
248
255 VP_EXPLICIT vpColVector(const vpMatrix &M);
256
260 vpColVector(const vpMatrix &M, unsigned int j);
261
265 VP_EXPLICIT vpColVector(const std::vector<double> &v);
266
270 VP_EXPLICIT vpColVector(const std::vector<float> &v);
271
272#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
277#endif
278
279#if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))) // Check if cxx11 or higher
280 vpColVector(const std::initializer_list<double> &list) : vpArray2D<double>(static_cast<unsigned int>(list.size()), 1)
281 {
282 std::copy(list.begin(), list.end(), data);
283 }
284#endif
285
286 static vpColVector view(double *raw_data, unsigned int rows);
287 static void view(vpColVector &v, double *raw_data, unsigned int rows);
288
293 void clear()
294 {
295 if (!isMemoryOwner) {
296 throw vpException(vpException::fatalError, "Cannot clear a vector view");
297 }
298 if (data != nullptr) {
299 free(data);
300 data = nullptr;
301 }
302
303 if (rowPtrs != nullptr) {
304 free(rowPtrs);
305 rowPtrs = nullptr;
306 }
307 rowNum = 0;
308 colNum = 0;
309 dsize = 0;
310 }
311
346 std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
347
378 std::ostream &csvPrint(std::ostream &os) const;
379
387 {
388 double d2r = M_PI / 180.0;
389
390 (*this) *= d2r;
391 return (*this);
392 }
393
411 vpColVector extract(unsigned int r, unsigned int colsize) const
412 {
413 if ((r >= rowNum) || ((r + colsize) > rowNum)) {
415 "Cannot extract a (%dx1) column vector from a (%dx1) "
416 "column vector starting at index %d",
417 colsize, rowNum, r));
418 }
419
420 return vpColVector(*this, r, colsize);
421 }
422
433 double frobeniusNorm() const;
434
442 vpColVector hadamard(const vpColVector &v) const;
443
454 double infinityNorm() const;
455
496 void init(const vpColVector &v, unsigned int r, unsigned int nrows);
497
534 void insert(unsigned int i, const vpColVector &v);
535
567 std::ostream &maplePrint(std::ostream &os) const;
568
611 std::ostream &matlabPrint(std::ostream &os) const;
612
623 vpColVector &normalize();
624
636 vpColVector &normalize(vpColVector &x) const;
637
641 inline double &operator[](unsigned int n) { return *(data + n); }
642
646 inline const double &operator[](unsigned int n) const { return *(data + n); }
647
657
662
667
672
678 vpColVector &operator=(const vpMatrix &M);
679
683 vpColVector &operator=(const std::vector<double> &v);
684
688 vpColVector &operator=(const std::vector<float> &v);
689
693 vpColVector &operator=(double x);
694
695#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
700
728 vpColVector &operator=(const std::initializer_list<double> &list);
729#endif
730
738 bool operator==(const vpColVector &v) const;
739
747 bool operator==(double v) const;
748
755 bool operator!=(const vpColVector &v) const;
756
764 bool operator!=(double v) const;
765
773 double operator*(const vpColVector &v) const;
774
782 vpMatrix operator*(const vpRowVector &v) const;
783
791 vpMatrix operator*(const vpMatrix &M) const;
792
813 vpColVector operator*(double x) const;
814
833 vpColVector &operator*=(double x);
834
854 vpColVector operator/(double x) const;
855
873 vpColVector &operator/=(double x);
874
878 vpColVector operator+(const vpColVector &v) const;
879
900 vpTranslationVector operator+(const vpTranslationVector &t) const;
901
905 vpColVector &operator+=(const vpColVector &v);
906
910 vpColVector &operator+=(const vpTranslationVector &t);
911
915 vpColVector operator-(const vpColVector &v) const;
916
920 vpColVector &operator-=(const vpColVector &v);
921
925 vpColVector &operator-=(const vpTranslationVector &t);
926
937 vpColVector operator-() const;
938
964
993 vpColVector &operator<<(double *x);
994
1017 vpColVector &operator<<(double val);
1018
1041 vpColVector &operator,(double val);
1042
1061 int print(std::ostream &s, unsigned int length, char const *intro = nullptr) const;
1062
1070 {
1071 double r2d = 180.0 / M_PI;
1072
1073 (*this) *= r2d;
1074 return (*this);
1075 }
1076
1137 void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
1138
1148 vpMatrix reshape(unsigned int nrows, unsigned int ncols);
1149
1158
1159 void resize(unsigned int i, bool flagNullify = true)
1160 {
1161 vpArray2D<double>::resize(i, 1, flagNullify);
1162 }
1163
1175 void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
1176 {
1177 if (ncols != 1) {
1179 "Cannot resize a column vector to a (%dx%d) "
1180 "dimension vector that has more than one column",
1181 nrows, ncols));
1182 }
1183 vpArray2D<double>::resize(nrows, ncols, flagNullify);
1184 }
1185
1201 void stack(double d);
1202
1221 void stack(const vpColVector &v);
1222
1229 double sum() const;
1230
1237 double sumSquare() const;
1238
1242 vpRowVector t() const;
1243
1248 std::vector<double> toStdVector() const;
1249
1254 vpRowVector transpose() const;
1255
1260 void transpose(vpRowVector &v) const;
1261
1275 inline static vpColVector cross(const vpColVector &a, const vpColVector &b) { return crossProd(a, b); }
1276
1288 static vpColVector crossProd(const vpColVector &a, const vpColVector &b);
1289
1299 static double dotProd(const vpColVector &a, const vpColVector &b);
1300
1333 static vpColVector invSort(const vpColVector &v);
1334
1338 static double median(const vpColVector &v);
1339
1343 static double mean(const vpColVector &v);
1344
1359 static vpMatrix skew(const vpColVector &v);
1360
1392 static vpColVector sort(const vpColVector &v);
1393
1412 static vpColVector stack(const vpColVector &A, const vpColVector &B);
1413
1432 static void stack(const vpColVector &A, const vpColVector &B, vpColVector &C);
1433
1437 static double stdev(const vpColVector &v, bool useBesselCorrection = false);
1438
1439#ifdef VISP_HAVE_NLOHMANN_JSON
1446 friend void to_json(nlohmann::json &j, const vpColVector &v);
1447
1454 friend void from_json(const nlohmann::json &j, vpColVector &v);
1455#endif
1456
1457#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1462
1466 VP_DEPRECATED void init() { }
1467
1507 VP_DEPRECATED void insert(const vpColVector &v, unsigned int i);
1508
1512 VP_DEPRECATED vpColVector rows(unsigned int first_row, unsigned int last_row) const
1513 {
1514 return vpColVector(*this, first_row - 1, last_row - first_row + 1);
1515 }
1516
1520 VP_DEPRECATED void stackMatrices(const vpColVector &r) { stack(r); }
1521
1525 VP_DEPRECATED static vpColVector stackMatrices(const vpColVector &A, const vpColVector &B) { return stack(A, B); }
1526
1530 VP_DEPRECATED static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
1531 {
1532 stack(A, B, C);
1533 }
1534
1548 VP_DEPRECATED void insert(const vpColVector &v, unsigned int r, unsigned int c = 0);
1549
1559 VP_DEPRECATED double euclideanNorm() const;
1561#endif
1562};
1563
1568#ifndef DOXYGEN_SHOULD_SKIP_THIS
1569VISP_EXPORT
1570#endif
1571vpColVector operator*(const double &x, const vpColVector &v);
1572
1573
1574#ifdef VISP_HAVE_NLOHMANN_JSON
1575inline void to_json(nlohmann::json &j, const vpColVector &v)
1576{
1577 const vpArray2D<double> *asArray = &v;
1578 to_json(j, *asArray);
1579 j["type"] = "vpColVector";
1580}
1581
1582inline void from_json(const nlohmann::json &j, vpColVector &v)
1583{
1584 vpArray2D<double> *asArray = &v;
1585 from_json(j, *asArray);
1586 if (v.getCols() != 1) {
1587 throw vpException(vpException::badValue, "From JSON, tried to read a 2D array into a vpColVector");
1588 }
1589}
1590#endif
1591END_VISP_NAMESPACE
1592#endif
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition vpArray2D.h:615
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition vpArray2D.h:586
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition vpArray2D.h:448
static vpArray2D< Type > view(const vpArray2D< Type > &A)
Creates a view of the Matrix A. A view shares the same underlying memory as the original array....
Definition vpArray2D.h:324
unsigned int rowNum
Definition vpArray2D.h:1201
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition vpArray2D.h:705
friend void to_json(nlohmann::json &j, const vpArray2D< T > &array)
unsigned int dsize
Definition vpArray2D.h:1207
unsigned int size() const
Definition vpArray2D.h:435
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition vpArray2D.h:1273
bool operator!=(const vpArray2D< Type > &A) const
Definition vpArray2D.h:1458
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition vpArray2D.h:1257
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
void reshape(unsigned int nrows, unsigned int ncols)
Definition vpArray2D.h:545
unsigned int colNum
Definition vpArray2D.h:1203
bool operator==(const vpArray2D< Type > &A) const
Definition vpArray2D.h:1401
Implementation of column vector and the associated operations.
friend class vpMatrix
const double & operator[](unsigned int n) const
vpColVector extract(unsigned int r, unsigned int colsize) const
vpColVector(unsigned int n, double val)
VP_EXPLICIT vpColVector(unsigned int n)
static VP_DEPRECATED vpColVector stackMatrices(const vpColVector &A, const vpColVector &B)
VP_DEPRECATED void stackMatrices(const vpColVector &r)
double & operator[](unsigned int n)
vpColVector & rad2deg()
void stack(double d)
vpColVector(const vpColVector &v)
vpColVector & deg2rad()
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
static vpColVector crossProd(const vpColVector &a, const vpColVector &b)
VP_DEPRECATED vpColVector rows(unsigned int first_row, unsigned int last_row) const
static VP_DEPRECATED void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C)
vpColVector(const std::initializer_list< double > &list)
static vpColVector cross(const vpColVector &a, const vpColVector &b)
VP_DEPRECATED void init()
void resize(unsigned int i, bool flagNullify=true)
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:73
@ fatalError
Fatal error.
Definition vpException.h:72
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
vpMatrix operator*(const vpMatrix &B) const
Implementation of a pose vector and operations on poses.
Implementation of a generic rotation vector.
Implementation of row vector and the associated operations.
Class that consider the case of a translation vector.