Visual Servoing Platform  version 3.5.1 under development (2023-06-06)
vpColVector.cpp
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 
45 #include <assert.h>
46 #include <cmath> // std::fabs
47 #include <limits> // numeric_limits
48 #include <math.h>
49 #include <sstream>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 
54 #include <visp3/core/vpCPUFeatures.h>
55 #include <visp3/core/vpColVector.h>
56 #include <visp3/core/vpDebug.h>
57 #include <visp3/core/vpException.h>
58 #include <visp3/core/vpMath.h>
59 #include <visp3/core/vpRotationVector.h>
60 
61 #include <Simd/SimdLib.hpp>
62 
65 {
66  if (getRows() != v.getRows()) {
67  throw(vpException(vpException::dimensionError, "Cannot add (%dx1) column vector to (%dx1) column vector", getRows(),
68  v.getRows()));
69  }
71 
72  for (unsigned int i = 0; i < rowNum; i++)
73  r[i] = (*this)[i] + v[i];
74  return r;
75 }
98 {
99  if (getRows() != 3) {
100  throw(vpException(vpException::dimensionError, "Cannot add %d-dimension column vector to a translation vector",
101  getRows()));
102  }
104 
105  for (unsigned int i = 0; i < 3; i++)
106  s[i] = (*this)[i] + t[i];
107 
108  return s;
109 }
110 
113 {
114  if (getRows() != v.getRows()) {
115  throw(vpException(vpException::dimensionError, "Cannot add (%dx1) column vector to (%dx1) column vector", getRows(),
116  v.getRows()));
117  }
118 
119  for (unsigned int i = 0; i < rowNum; i++)
120  (*this)[i] += v[i];
121  return (*this);
122 }
125 {
126  if (getRows() != v.getRows()) {
127  throw(vpException(vpException::dimensionError, "Cannot subtract (%dx1) column vector to (%dx1) column vector",
128  getRows(), v.getRows()));
129  }
130 
131  for (unsigned int i = 0; i < rowNum; i++)
132  (*this)[i] -= v[i];
133  return (*this);
134 }
135 
143 double vpColVector::operator*(const vpColVector &v) const
144 {
145  if (size() != v.size()) {
147  "Cannot compute the dot product between column vectors "
148  "with different dimensions (%d) and (%d)",
149  size(), v.size()));
150  }
151  double r = 0;
152 
153  for (unsigned int i = 0; i < rowNum; i++)
154  r += (*this)[i] * v[i];
155  return r;
156 }
157 
168 {
169  vpMatrix M(rowNum, v.getCols());
170  for (unsigned int i = 0; i < rowNum; i++) {
171  for (unsigned int j = 0; j < v.getCols(); j++) {
172  M[i][j] = (*this)[i] * v[j];
173  }
174  }
175  return M;
176 }
177 
180 {
181  if (getRows() != m.getRows()) {
183  "Bad size during vpColVector (%dx1) and vpColVector "
184  "(%dx1) subtraction",
185  getRows(), m.getRows()));
186  }
187  vpColVector v(rowNum);
188 
189  for (unsigned int i = 0; i < rowNum; i++)
190  v[i] = (*this)[i] - m[i];
191  return v;
192 }
193 
207 vpColVector::vpColVector(const vpColVector &v, unsigned int r, unsigned int nrows) : vpArray2D<double>(nrows, 1)
208 {
209  init(v, r, nrows);
210 }
211 
249 void vpColVector::init(const vpColVector &v, unsigned int r, unsigned int nrows)
250 {
251  unsigned int rnrows = r + nrows;
252 
253  if (rnrows > v.getRows())
254  throw(vpException(vpException::dimensionError, "Bad row dimension (%d > %d) used to initialize vpColVector", rnrows,
255  v.getRows()));
256  resize(nrows, false);
257 
258  if (this->rowPtrs == NULL) // Fix coverity scan: explicit null dereferenced
259  return; // Nothing to do
260  for (unsigned int i = r; i < rnrows; i++)
261  (*this)[i - r] = v[i];
262 }
263 
264 vpColVector::vpColVector(const vpRotationVector &v) : vpArray2D<double>(v.size(), 1)
265 {
266  for (unsigned int i = 0; i < v.size(); i++)
267  (*this)[i] = v[i];
268 }
269 
270 vpColVector::vpColVector(const vpPoseVector &p) : vpArray2D<double>(p.size(), 1)
271 {
272  for (unsigned int i = 0; i < p.size(); i++)
273  (*this)[i] = p[i];
274 }
275 
277 {
278  for (unsigned int i = 0; i < v.size(); i++)
279  (*this)[i] = v[i];
280 }
281 
283 vpColVector::vpColVector(const vpMatrix &M, unsigned int j) : vpArray2D<double>(M.getRows(), 1)
284 {
285  for (unsigned int i = 0; i < M.getCols(); i++)
286  (*this)[i] = M[i][j];
287 }
288 
295 vpColVector::vpColVector(const vpMatrix &M) : vpArray2D<double>(M.getRows(), 1)
296 {
297  if (M.getCols() != 1) {
298  throw(vpException(vpException::dimensionError, "Cannot construct a (%dx1) row vector from a (%dx%d) matrix",
299  M.getRows(), M.getRows(), M.getCols()));
300  }
301 
302  for (unsigned int i = 0; i < M.getRows(); i++)
303  (*this)[i] = M[i][0];
304 }
305 
309 vpColVector::vpColVector(const std::vector<double> &v) : vpArray2D<double>((unsigned int)v.size(), 1)
310 {
311  for (unsigned int i = 0; i < v.size(); i++)
312  (*this)[i] = v[i];
313 }
317 vpColVector::vpColVector(const std::vector<float> &v) : vpArray2D<double>((unsigned int)v.size(), 1)
318 {
319  for (unsigned int i = 0; i < v.size(); i++)
320  (*this)[i] = (double)(v[i]);
321 }
322 
323 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
328 {
329  rowNum = v.rowNum;
330  colNum = v.colNum;
331  rowPtrs = v.rowPtrs;
332  dsize = v.dsize;
333  data = v.data;
334 
335  v.rowNum = 0;
336  v.colNum = 0;
337  v.rowPtrs = NULL;
338  v.dsize = 0;
339  v.data = NULL;
340 }
341 #endif
342 
354 {
355  vpColVector A;
356  A.resize(rowNum, false);
357 
358  double *vd = A.data;
359  double *d = data;
360 
361  for (unsigned int i = 0; i < rowNum; i++)
362  *(vd++) = -(*d++);
363 
364  return A;
365 }
366 
388 {
389  vpColVector v(rowNum);
390 
391  double *vd = v.data;
392  double *d = data;
393 
394  for (unsigned int i = 0; i < rowNum; i++)
395  *(vd++) = (*d++) * x;
396  return v;
397 }
398 
418 {
419  for (unsigned int i = 0; i < rowNum; i++)
420  (*this)[i] *= x;
421  return (*this);
422 }
423 
442 {
443  for (unsigned int i = 0; i < rowNum; i++)
444  (*this)[i] /= x;
445  return (*this);
446 }
447 
468 {
469  vpColVector v(rowNum);
470 
471  double *vd = v.data;
472  double *d = data;
473 
474  for (unsigned int i = 0; i < rowNum; i++)
475  *(vd++) = (*d++) / x;
476  return v;
477 }
478 
485 {
486  if (M.getCols() != 1) {
487  throw(vpException(vpException::dimensionError, "Cannot transform a (%dx%d) matrix into a column vector",
488  M.getRows(), M.getCols()));
489  }
490 
491  resize(M.getRows(), false);
492  memcpy(data, M.data, rowNum * sizeof(double));
493 
494  return (*this);
495 }
496 
500 vpColVector &vpColVector::operator=(const std::vector<double> &v)
501 {
502  resize((unsigned int)v.size(), false);
503  for (unsigned int i = 0; i < v.size(); i++)
504  (*this)[i] = v[i];
505  return *this;
506 }
510 vpColVector &vpColVector::operator=(const std::vector<float> &v)
511 {
512  resize((unsigned int)v.size(), false);
513  for (unsigned int i = 0; i < v.size(); i++)
514  (*this)[i] = (float)v[i];
515  return *this;
516 }
517 
519 {
520  unsigned int k = v.rowNum;
521  if (rowNum != k) {
522  resize(k, false);
523  }
524 
525  memcpy(data, v.data, rowNum * sizeof(double));
526  return *this;
527 }
528 
533 {
534  unsigned int k = tv.getRows();
535  if (rowNum != k) {
536  resize(k, false);
537  }
538 
539  memcpy(data, tv.data, rowNum * sizeof(double));
540  return *this;
541 }
546 {
547  unsigned int k = rv.getRows();
548  if (rowNum != k) {
549  resize(k, false);
550  }
551 
552  memcpy(data, rv.data, rowNum * sizeof(double));
553  return *this;
554 }
559 {
560  unsigned int k = p.getRows();
561  if (rowNum != k) {
562  resize(k, false);
563  }
564 
565  memcpy(data, p.data, rowNum * sizeof(double));
566  return *this;
567 }
568 
590 {
591  *this = v;
592  return *this;
593 }
594 
620 {
621  for (unsigned int i = 0; i < rowNum; i++) {
622  for (unsigned int j = 0; j < colNum; j++) {
623  rowPtrs[i][j] = *x++;
624  }
625  }
626  return *this;
627 }
628 
648 {
649  resize(1, false);
650  data[0] = val;
651  return *this;
652 }
653 
673 {
674  resize(rowNum + 1, false);
675  data[rowNum - 1] = val;
676  return *this;
677 }
678 
681 {
682  double *d = data;
683 
684  for (unsigned int i = 0; i < rowNum; i++)
685  *(d++) = x;
686  return *this;
687 }
688 
693 std::vector<double> vpColVector::toStdVector() const
694 {
695  std::vector<double> v(this->size());
696 
697  for (unsigned int i = 0; i < this->size(); i++)
698  v[i] = data[i];
699  return v;
700 }
701 
702 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
707 {
708  if (this != &other) {
709  free(data);
710  free(rowPtrs);
711 
712  rowNum = other.rowNum;
713  colNum = other.colNum;
714  rowPtrs = other.rowPtrs;
715  dsize = other.dsize;
716  data = other.data;
717 
718  other.rowNum = 0;
719  other.colNum = 0;
720  other.rowPtrs = NULL;
721  other.dsize = 0;
722  other.data = NULL;
723  }
724 
725  return *this;
726 }
727 
751 vpColVector &vpColVector::operator=(const std::initializer_list<double> &list)
752 {
753  resize(static_cast<unsigned int>(list.size()), false);
754  std::copy(list.begin(), list.end(), data);
755  return *this;
756 }
757 #endif
758 
766 {
767  if (rowNum != v.rowNum || colNum != v.colNum /* should not happen */)
768  return false;
769 
770  for (unsigned int i = 0; i < rowNum; i++) {
771  if (!vpMath::equal(data[i], v.data[i], std::numeric_limits<double>::epsilon()))
772  return false;
773  }
774 
775  return true;
776 }
777 
784 bool vpColVector::operator==(double v) const
785 {
786  for (unsigned int i = 0; i < rowNum; i++) {
787  if (!vpMath::equal(data[i], v, std::numeric_limits<double>::epsilon()))
788  return false;
789  }
790 
791  return true;
792 }
793 
799 bool vpColVector::operator!=(const vpColVector &v) const { return !(*this == v); }
800 
807 bool vpColVector::operator!=(double v) const { return !(*this == v); }
808 
813 {
814  vpRowVector v(rowNum);
815  memcpy(v.data, data, rowNum * sizeof(double));
816  return v;
817 }
818 
823 vpRowVector vpColVector::transpose() const { return t(); }
824 
829 void vpColVector::transpose(vpRowVector &v) const { v = t(); }
830 
835 vpColVector operator*(const double &x, const vpColVector &v)
836 {
837  vpColVector vout;
838  vout = v * x;
839  return vout;
840 }
841 
849 double vpColVector::dotProd(const vpColVector &a, const vpColVector &b)
850 {
851  if (a.data == NULL) {
852  throw(vpException(vpException::fatalError, "Cannot compute the dot product: first vector empty"));
853  }
854  if (b.data == NULL) {
855  throw(vpException(vpException::fatalError, "Cannot compute the dot product: second vector empty"));
856  }
857  if (a.size() != b.size()) {
859  "Cannot compute the dot product between column vectors "
860  "with different dimensions (%d) and (%d)",
861  a.size(), b.size()));
862  }
863 
864  double *ad = a.data;
865  double *bd = b.data;
866 
867  double c = 0;
868  for (unsigned int i = 0; i < a.getRows(); i++)
869  c += *(ad++) * *(bd++);
870  // vpMatrix c = (a.t() * b);
871  // return c[0][0];
872  return c;
873 }
874 
887 {
888  x /= sqrt(x.sumSquare());
889 
890  return x;
891 }
892 
904 {
905 
906  double sum_square = sumSquare();
907 
908  // if (sum != 0.0)
909  if (std::fabs(sum_square) > std::numeric_limits<double>::epsilon())
910  *this /= sqrt(sum_square);
911 
912  // If sum = 0, we have a nul vector. So we return just.
913  return *this;
914 }
915 
945 {
946  if (v.data == NULL) {
947  throw(vpException(vpException::fatalError, "Cannot sort content of column vector: vector empty"));
948  }
949  vpColVector tab;
950  tab = v;
951  unsigned int nb_permutation = 1;
952  unsigned int i = 0;
953  while (nb_permutation != 0) {
954  nb_permutation = 0;
955  for (unsigned int j = v.getRows() - 1; j >= i + 1; j--) {
956  if ((tab[j] > tab[j - 1])) {
957  double tmp = tab[j];
958  tab[j] = tab[j - 1];
959  tab[j - 1] = tmp;
960  nb_permutation++;
961  }
962  }
963  i++;
964  }
965 
966  return tab;
967 }
968 
997 {
998  if (v.data == NULL) {
999  throw(vpException(vpException::fatalError, "Cannot sort content of column vector: vector empty"));
1000  }
1001  vpColVector tab;
1002  tab = v;
1003  unsigned int nb_permutation = 1;
1004  unsigned int i = 0;
1005  while (nb_permutation != 0) {
1006  nb_permutation = 0;
1007  for (unsigned int j = v.getRows() - 1; j >= i + 1; j--) {
1008  if ((tab[j] < tab[j - 1])) {
1009  double tmp = tab[j];
1010  tab[j] = tab[j - 1];
1011  tab[j - 1] = tmp;
1012  nb_permutation++;
1013  }
1014  }
1015  i++;
1016  }
1017 
1018  return tab;
1019 }
1020 
1037 void vpColVector::stack(double d)
1038 {
1039  this->resize(rowNum + 1, false);
1040  (*this)[rowNum - 1] = d;
1041 }
1042 
1062 void vpColVector::stack(const vpColVector &v) { *this = vpColVector::stack(*this, v); }
1063 
1083 {
1084  vpColVector C;
1085  vpColVector::stack(A, B, C);
1086  return C;
1087 }
1088 
1108 {
1109  unsigned int nrA = A.getRows();
1110  unsigned int nrB = B.getRows();
1111 
1112  if (nrA == 0 && nrB == 0) {
1113  C.resize(0);
1114  return;
1115  }
1116 
1117  if (nrB == 0) {
1118  C = A;
1119  return;
1120  }
1121 
1122  if (nrA == 0) {
1123  C = B;
1124  return;
1125  }
1126 
1127  // General case
1128  C.resize(nrA + nrB, false);
1129 
1130  for (unsigned int i = 0; i < nrA; i++)
1131  C[i] = A[i];
1132 
1133  for (unsigned int i = 0; i < nrB; i++)
1134  C[nrA + i] = B[i];
1135 }
1136 
1141 {
1142  if (v.data == NULL || v.size() == 0) {
1143  throw(vpException(vpException::dimensionError, "Cannot compute column vector mean: vector empty"));
1144  }
1145 
1146  // Use directly sum() function
1147  double mean = v.sum();
1148 
1149  // Old code used
1150  // double *vd = v.data;
1151  // for (unsigned int i=0 ; i < v.getRows() ; i++)
1152  // mean += *(vd++);
1153 
1154  return mean / v.getRows();
1155 }
1156 
1161 {
1162  if (v.data == NULL || v.size() == 0) {
1163  throw(vpException(vpException::dimensionError, "Cannot compute column vector median: vector empty"));
1164  }
1165 
1166  std::vector<double> vectorOfDoubles(v.data, v.data + v.rowNum);
1167 
1168  return vpMath::getMedian(vectorOfDoubles);
1169 }
1170 
1174 double vpColVector::stdev(const vpColVector &v, bool useBesselCorrection)
1175 {
1176  if (v.data == NULL || v.size() == 0) {
1177  throw(vpException(vpException::dimensionError, "Cannot compute column vector stdev: vector empty"));
1178  }
1179 
1180  return SimdVectorStdev(v.data, v.rowNum, useBesselCorrection);
1181 }
1182 
1198 {
1199  vpMatrix M;
1200  if (v.getRows() != 3) {
1201  throw(vpException(vpException::dimensionError, "Cannot compute skew vector of a non 3-dimention vector (%d)",
1202  v.getRows()));
1203  }
1204 
1205  M.resize(3, 3, false, false);
1206  M[0][0] = 0;
1207  M[0][1] = -v[2];
1208  M[0][2] = v[1];
1209  M[1][0] = v[2];
1210  M[1][1] = 0;
1211  M[1][2] = -v[0];
1212  M[2][0] = -v[1];
1213  M[2][1] = v[0];
1214  M[2][2] = 0;
1215 
1216  return M;
1217 }
1218 
1229 {
1230  if (a.getRows() != 3 || b.getRows() != 3) {
1232  "Cannot compute the cross product between column "
1233  "vector with dimension %d and %d",
1234  a.getRows(), b.getRows()));
1235  }
1236 
1237  return vpColVector::skew(a) * b;
1238 }
1239 
1248 vpMatrix vpColVector::reshape(unsigned int nrows, unsigned int ncols)
1249 {
1250  vpMatrix M(nrows, ncols);
1251  reshape(M, nrows, ncols);
1252  return M;
1253 }
1254 
1310 void vpColVector::reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols)
1311 {
1312  if (dsize != nrows * ncols) {
1313  throw(vpException(vpException::dimensionError, "Cannot reshape (%dx1) column vector in (%dx%d) matrix", rowNum,
1314  M.getRows(), M.getCols()));
1315  }
1316  if ((M.getRows() != nrows) || (M.getCols() != ncols))
1317  M.resize(nrows, ncols, false, false);
1318 
1319  for (unsigned int j = 0; j < ncols; j++)
1320  for (unsigned int i = 0; i < nrows; i++)
1321  M[i][j] = data[j * nrows + i];
1322 }
1323 
1356 void vpColVector::insert(unsigned int i, const vpColVector &v)
1357 {
1358  if (i + v.size() > this->size())
1359  throw(vpException(vpException::dimensionError, "Unable to insert a column vector"));
1360 
1361  if (data != NULL && v.data != NULL && v.rowNum > 0) {
1362  memcpy(data + i, v.data, sizeof(double) * v.rowNum);
1363  }
1364 }
1365 
1385 int vpColVector::print(std::ostream &s, unsigned int length, char const *intro) const
1386 {
1387  typedef std::string::size_type size_type;
1388 
1389  unsigned int m = getRows();
1390  unsigned int n = 1;
1391 
1392  std::vector<std::string> values(m * n);
1393  std::ostringstream oss;
1394  std::ostringstream ossFixed;
1395  std::ios_base::fmtflags original_flags = oss.flags();
1396 
1397  // ossFixed <<std::fixed;
1398  ossFixed.setf(std::ios::fixed, std::ios::floatfield);
1399 
1400  size_type maxBefore = 0; // the length of the integral part
1401  size_type maxAfter = 0; // number of decimals plus
1402  // one place for the decimal point
1403  for (unsigned int i = 0; i < m; ++i) {
1404  oss.str("");
1405  oss << (*this)[i];
1406  if (oss.str().find("e") != std::string::npos) {
1407  ossFixed.str("");
1408  ossFixed << (*this)[i];
1409  oss.str(ossFixed.str());
1410  }
1411 
1412  values[i] = oss.str();
1413  size_type thislen = values[i].size();
1414  size_type p = values[i].find('.');
1415 
1416  if (p == std::string::npos) {
1417  maxBefore = vpMath::maximum(maxBefore, thislen);
1418  // maxAfter remains the same
1419  } else {
1420  maxBefore = vpMath::maximum(maxBefore, p);
1421  maxAfter = vpMath::maximum(maxAfter, thislen - p - 1);
1422  }
1423  }
1424 
1425  size_type totalLength = length;
1426  // increase totalLength according to maxBefore
1427  totalLength = vpMath::maximum(totalLength, maxBefore);
1428  // decrease maxAfter according to totalLength
1429  maxAfter = (std::min)(maxAfter, totalLength - maxBefore);
1430  if (maxAfter == 1)
1431  maxAfter = 0;
1432 
1433  // the following line is useful for debugging
1434  // std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
1435 
1436  if (intro)
1437  s << intro;
1438  s << "[" << m << "," << n << "]=\n";
1439 
1440  for (unsigned int i = 0; i < m; i++) {
1441  s << " ";
1442  size_type p = values[i].find('.');
1443  s.setf(std::ios::right, std::ios::adjustfield);
1444  s.width((std::streamsize)maxBefore);
1445  s << values[i].substr(0, p).c_str();
1446 
1447  if (maxAfter > 0) {
1448  s.setf(std::ios::left, std::ios::adjustfield);
1449  if (p != std::string::npos) {
1450  s.width((std::streamsize)maxAfter);
1451  s << values[i].substr(p, maxAfter).c_str();
1452  } else {
1453  assert(maxAfter > 1);
1454  s.width((std::streamsize)maxAfter);
1455  s << ".0";
1456  }
1457  }
1458 
1459  s << ' ';
1460 
1461  s << std::endl;
1462  }
1463 
1464  s.flags(original_flags); // restore s to standard state
1465 
1466  return (int)(maxBefore + maxAfter);
1467 }
1468 
1474 double vpColVector::sum() const { return SimdVectorSum(data, rowNum); }
1475 
1482 double vpColVector::sumSquare() const { return SimdVectorSumSquare(data, rowNum); }
1483 
1494 double vpColVector::euclideanNorm() const { return frobeniusNorm(); }
1495 
1505 {
1506  double norm = sumSquare();
1507 
1508  return sqrt(norm);
1509 }
1510 
1518 {
1519  if (v.getRows() != rowNum || v.getCols() != colNum) {
1520  throw(vpException(vpException::dimensionError, "Hadamard product: bad dimensions!"));
1521  }
1522 
1523  vpColVector out;
1524  out.resize(rowNum, false);
1525 
1526  SimdVectorHadamard(data, v.data, rowNum, out.data);
1527 
1528  return out;
1529 }
1530 
1543 {
1544  double norm = 0.0;
1545  for (unsigned int i = 0; i < rowNum; i++) {
1546  double x = fabs((*this)[i]);
1547  if (x > norm) {
1548  norm = x;
1549  }
1550  }
1551  return norm;
1552 }
1553 
1582 std::ostream &vpColVector::cppPrint(std::ostream &os, const std::string &matrixName, bool octet) const
1583 {
1584  os << "vpColVector " << matrixName << " (" << this->getRows() << "); " << std::endl;
1585 
1586  for (unsigned int i = 0; i < this->getRows(); ++i) {
1587 
1588  if (!octet) {
1589  os << matrixName << "[" << i << "] = " << (*this)[i] << "; " << std::endl;
1590  } else {
1591  for (unsigned int k = 0; k < sizeof(double); ++k) {
1592  os << "((unsigned char*)&(" << matrixName << "[" << i << "]) )[" << k << "] = 0x" << std::hex
1593  << (unsigned int)((unsigned char *)&((*this)[i]))[k] << "; " << std::endl;
1594  }
1595  }
1596  }
1597  std::cout << std::endl;
1598  return os;
1599 };
1600 
1627 std::ostream &vpColVector::csvPrint(std::ostream &os) const
1628 {
1629  for (unsigned int i = 0; i < this->getRows(); ++i) {
1630  os << (*this)[i];
1631 
1632  os << std::endl;
1633  }
1634  return os;
1635 };
1636 
1662 std::ostream &vpColVector::maplePrint(std::ostream &os) const
1663 {
1664  os << "([ " << std::endl;
1665  for (unsigned int i = 0; i < this->getRows(); ++i) {
1666  os << "[";
1667  os << (*this)[i] << ", ";
1668  os << "]," << std::endl;
1669  }
1670  os << "])" << std::endl;
1671  return os;
1672 };
1673 
1710 std::ostream &vpColVector::matlabPrint(std::ostream &os) const
1711 {
1712  os << "[ ";
1713  for (unsigned int i = 0; i < this->getRows(); ++i) {
1714  os << (*this)[i] << ", ";
1715  if (this->getRows() != i + 1) {
1716  os << ";" << std::endl;
1717  } else {
1718  os << "]" << std::endl;
1719  }
1720  }
1721  return os;
1722 };
1723 
1724 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1739 void vpColVector::insert(const vpColVector &v, unsigned int r, unsigned int c)
1740 {
1741  (void)c;
1742  insert(r, v);
1743 }
1744 #endif // defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:133
unsigned int getCols() const
Definition: vpArray2D.h:282
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:146
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:140
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:307
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:136
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:142
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:294
unsigned int getRows() const
Definition: vpArray2D.h:292
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:138
Implementation of column vector and the associated operations.
Definition: vpColVector.h:172
void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols)
static double dotProd(const vpColVector &a, const vpColVector &b)
vpColVector operator-() const
vpColVector & operator*=(double x)
std::ostream & matlabPrint(std::ostream &os) const
vp_deprecated double euclideanNorm() const
vpColVector & operator=(const vpColVector &v)
Copy operator. Allow operation such as A = v.
vpColVector operator/(double x) const
vpColVector & normalize()
static double median(const vpColVector &v)
vpColVector hadamard(const vpColVector &v) const
double sumSquare() const
std::ostream & csvPrint(std::ostream &os) const
std::ostream & maplePrint(std::ostream &os) const
vpColVector & operator,(double val)
int print(std::ostream &s, unsigned int length, char const *intro=0) const
bool operator==(const vpColVector &v) const
Comparison operator.
vpColVector & operator/=(double x)
static vpColVector invSort(const vpColVector &v)
void stack(double d)
bool operator!=(const vpColVector &v) const
static vpMatrix skew(const vpColVector &v)
vp_deprecated void init()
Definition: vpColVector.h:428
double operator*(const vpColVector &x) const
vpColVector()
Basic constructor that creates an empty 0-size column vector.
Definition: vpColVector.h:177
vpColVector operator+(const vpColVector &v) const
Operator that allows to add two column vectors.
Definition: vpColVector.cpp:64
std::vector< double > toStdVector() const
static double mean(const vpColVector &v)
vpColVector operator*(const double &x, const vpColVector &v)
vpColVector & operator+=(vpColVector v)
Operator that allows to add two column vectors.
vpRowVector t() const
static vpColVector crossProd(const vpColVector &a, const vpColVector &b)
double infinityNorm() const
vpColVector & operator<<(const vpColVector &v)
vpRowVector transpose() const
static double stdev(const vpColVector &v, bool useBesselCorrection=false)
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
double frobeniusNorm() const
vpColVector & operator-=(vpColVector v)
Operator that allows to subtract two column vectors.
static vpColVector sort(const vpColVector &v)
void insert(unsigned int i, const vpColVector &v)
double sum() const
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:357
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ dimensionError
Bad dimension.
Definition: vpException.h:95
@ fatalError
Fatal error.
Definition: vpException.h:96
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:312
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:170
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:367
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:194
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.