Visual Servoing Platform  version 3.6.1 under development (2024-12-03)
vpRowVector.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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  * Operation on row vectors.
32  */
33 
34 #ifndef VP_ROW_VECTOR_H
35 #define VP_ROW_VECTOR_H
36 
37 #include <vector>
38 
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpArray2D.h>
41 #include <visp3/core/vpColVector.h>
42 #include <visp3/core/vpMath.h>
43 #include <visp3/core/vpMatrix.h>
44 
45 BEGIN_VISP_NAMESPACE
46 
47 class vpMatrix;
48 class vpColVector;
49 
123 class VISP_EXPORT vpRowVector : public vpArray2D<double>
124 {
125 public:
127  vpRowVector() : vpArray2D<double>() { }
130  VP_EXPLICIT vpRowVector(unsigned int n) : vpArray2D<double>(1, n) { }
132  vpRowVector(unsigned int n, double val) : vpArray2D<double>(1, n, val) { }
135  vpRowVector(const vpRowVector &v) : vpArray2D<double>(v) { }
136  vpRowVector(const vpRowVector &v, unsigned int c, unsigned int ncols);
137  VP_EXPLICIT vpRowVector(const vpMatrix &M);
138  vpRowVector(const vpMatrix &M, unsigned int i);
139  VP_EXPLICIT vpRowVector(const std::vector<double> &v);
140  VP_EXPLICIT vpRowVector(const std::vector<float> &v);
141 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
143  VP_EXPLICIT vpRowVector(const std::initializer_list<double> &list) : vpArray2D<double>(list) { }
144 #endif
145 
150  void clear()
151  {
152  if (data != nullptr) {
153  free(data);
154  data = nullptr;
155  }
156 
157  if (rowPtrs != nullptr) {
158  free(rowPtrs);
159  rowPtrs = nullptr;
160  }
161  rowNum = 0;
162  colNum = 0;
163  dsize = 0;
164  }
165 
166  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
167  std::ostream &csvPrint(std::ostream &os) const;
168 
173  inline void deg2rad()
174  {
175  double d2r = M_PI / 180.0;
176 
177  (*this) *= d2r;
178  }
179 
197  vpRowVector extract(unsigned int c, unsigned int rowsize) const
198  {
199  if ((c >= colNum) || ((c + rowsize) > colNum)) {
201  "Cannot extract a (1x%d) row vector from a (1x%d) "
202  "row vector starting at index %d",
203  rowsize, colNum, c));
204  }
205 
206  return vpRowVector(*this, c, rowsize);
207  }
208 
209  double frobeniusNorm() const;
210 
218  vpRowVector hadamard(const vpRowVector &v) const;
219 
220  void init(const vpRowVector &v, unsigned int c, unsigned int ncols);
221  void insert(unsigned int i, const vpRowVector &v);
222 
223  std::ostream &maplePrint(std::ostream &os) const;
224  std::ostream &matlabPrint(std::ostream &os) const;
225 
226  vpRowVector &normalize();
227  vpRowVector &normalize(vpRowVector &x) const;
228 
230  inline double &operator[](unsigned int n) { return *(data + n); }
232  inline const double &operator[](unsigned int n) const { return *(data + n); }
233 
235  vpRowVector &operator=(const vpRowVector &v);
236  vpRowVector &operator=(const vpMatrix &M);
237  vpRowVector &operator=(const std::vector<double> &v);
238  vpRowVector &operator=(const std::vector<float> &v);
239  vpRowVector &operator=(double x);
240 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
242  vpRowVector &operator=(const std::initializer_list<double> &list);
243 #endif
245  bool operator==(const vpRowVector &v) const;
246  bool operator!=(const vpRowVector &v) const;
247 
248  double operator*(const vpColVector &x) const;
249  vpRowVector operator*(const vpMatrix &M) const;
250  vpRowVector operator*(double x) const;
251  vpRowVector &operator*=(double x);
252 
253  vpRowVector operator/(double x) const;
254  vpRowVector &operator/=(double x);
255 
256  vpRowVector operator+(const vpRowVector &v) const;
257  vpRowVector &operator+=(vpRowVector v);
258 
259  vpRowVector operator-(const vpRowVector &v) const;
260  vpRowVector &operator-=(vpRowVector v);
261  vpRowVector operator-() const;
262 
264  vpRowVector &operator<<(double val);
265  vpRowVector &operator,(double val);
266 
267  int print(std::ostream &s, unsigned int length, char const *intro = 0) const;
272  inline void rad2deg()
273  {
274  double r2d = 180.0 / M_PI;
275 
276  (*this) *= r2d;
277  }
278 
279  void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols);
280  vpMatrix reshape(unsigned int nrows, unsigned int ncols);
281 
287  inline void resize(unsigned int i, bool flagNullify = true) { vpArray2D<double>::resize(1, i, flagNullify); }
288 
299  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
300  {
301  if (nrows != 1) {
303  "Cannot resize a row vector to a (%dx%d) dimension "
304  "vector that has more than one row",
305  nrows, ncols));
306  }
307  vpArray2D<double>::resize(nrows, ncols, flagNullify);
308  }
309 
310  void stack(double d);
311  void stack(const vpRowVector &v);
312 
313  double sum() const;
314  double sumSquare() const;
315  vpColVector t() const;
316  std::vector<double> toStdVector() const;
317  vpColVector transpose() const;
318  void transpose(vpColVector &v) const;
319 
320  static double mean(const vpRowVector &v);
321  static double median(const vpRowVector &v);
322  static vpRowVector stack(const vpRowVector &A, const vpRowVector &B);
323  static void stack(const vpRowVector &A, const vpRowVector &B, vpRowVector &C);
324  static double stdev(const vpRowVector &v, bool useBesselCorrection = false);
325 
326 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
335  VP_DEPRECATED void init() { }
339  VP_DEPRECATED void stackMatrices(const vpRowVector &r) { stack(r); }
344  VP_DEPRECATED static vpRowVector stackMatrices(const vpRowVector &A, const vpRowVector &B) { return stack(A, B); }
349  VP_DEPRECATED static void stackMatrices(const vpRowVector &A, const vpRowVector &B, vpRowVector &C)
350  {
351  stack(A, B, C);
352  }
353 
357  VP_DEPRECATED void setIdentity(const double &val = 1.0);
358  VP_DEPRECATED double euclideanNorm() const;
360 #endif
361 };
362 
363 VISP_EXPORT vpRowVector operator*(const double &x, const vpRowVector &v);
364 
365 END_VISP_NAMESPACE
366 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:145
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:497
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:362
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:614
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1166
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1351
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:526
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition: vpArray2D.h:1150
void reshape(unsigned int nrows, unsigned int ncols)
Definition: vpArray2D.h:456
bool operator==(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1294
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpColVector operator*(const double &x, const vpColVector &v)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ fatalError
Fatal error.
Definition: vpException.h:72
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:124
vpRowVector(const vpRowVector &v)
Definition: vpRowVector.h:135
vpRowVector(unsigned int n, double val)
Construct a row vector of size n. Each element is set to val.
Definition: vpRowVector.h:132
double & operator[](unsigned int n)
Operator that allows to set a value of an element : v[i] = x.
Definition: vpRowVector.h:230
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:287
void deg2rad()
Definition: vpRowVector.h:173
void rad2deg()
Definition: vpRowVector.h:272
vpRowVector()
Basic constructor that creates an empty 0-size row vector.
Definition: vpRowVector.h:127
VP_EXPLICIT vpRowVector(const std::initializer_list< double > &list)
Definition: vpRowVector.h:143
void clear()
Definition: vpRowVector.h:150
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify)
Definition: vpRowVector.h:299
VP_EXPLICIT vpRowVector(unsigned int n)
Definition: vpRowVector.h:130
const double & operator[](unsigned int n) const
Operator that allows to get the value of an element : x = v[i].
Definition: vpRowVector.h:232
vpRowVector extract(unsigned int c, unsigned int rowsize) const
Definition: vpRowVector.h:197
vpMatrix operator*(const double &x, const vpMatrix &A)