Visual Servoing Platform  version 3.1.0
vpColVector.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 #if defined __SSE2__ || defined _M_X64 || (defined _M_IX86_FP && _M_IX86_FP >= 2)
62 #include <emmintrin.h>
63 #define VISP_HAVE_SSE2 1
64 #endif
65 
68 {
69  if (getRows() != v.getRows()) {
70  throw(vpException(vpException::dimensionError, "Cannot add (%dx1) column vector to (%dx1) column vector", getRows(),
71  v.getRows()));
72  }
74 
75  for (unsigned int i = 0; i < rowNum; i++)
76  r[i] = (*this)[i] + v[i];
77  return r;
78 }
101 {
102  if (getRows() != 3) {
103  throw(vpException(vpException::dimensionError, "Cannot add %d-dimension column vector to a translation vector",
104  getRows()));
105  }
107 
108  for (unsigned int i = 0; i < 3; i++)
109  s[i] = (*this)[i] + t[i];
110 
111  return s;
112 }
113 
116 {
117  if (getRows() != v.getRows()) {
118  throw(vpException(vpException::dimensionError, "Cannot add (%dx1) column vector to (%dx1) column vector", getRows(),
119  v.getRows()));
120  }
121 
122  for (unsigned int i = 0; i < rowNum; i++)
123  (*this)[i] += v[i];
124  return (*this);
125 }
128 {
129  if (getRows() != v.getRows()) {
130  throw(vpException(vpException::dimensionError, "Cannot substract (%dx1) column vector to (%dx1) column vector",
131  getRows(), v.getRows()));
132  }
133 
134  for (unsigned int i = 0; i < rowNum; i++)
135  (*this)[i] -= v[i];
136  return (*this);
137 }
138 
146 double vpColVector::operator*(const vpColVector &v) const
147 {
148  if (size() != v.size()) {
150  "Cannot compute the dot product between column vectors "
151  "with different dimensions (%d) and (%d)",
152  size(), v.size()));
153  }
154  double r = 0;
155 
156  for (unsigned int i = 0; i < rowNum; i++)
157  r += (*this)[i] * v[i];
158  return r;
159 }
160 
171 {
172  vpMatrix M(rowNum, v.getCols());
173  for (unsigned int i = 0; i < rowNum; i++) {
174  for (unsigned int j = 0; j < v.getCols(); j++) {
175  M[i][j] = (*this)[i] * v[j];
176  }
177  }
178  return M;
179 }
180 
183 {
184  if (getRows() != m.getRows()) {
186  "Bad size during vpColVector (%dx1) and vpColVector "
187  "(%dx1) substraction",
188  getRows(), m.getRows()));
189  }
190  vpColVector v(rowNum);
191 
192  for (unsigned int i = 0; i < rowNum; i++)
193  v[i] = (*this)[i] - m[i];
194  return v;
195 }
196 
210 vpColVector::vpColVector(const vpColVector &v, unsigned int r, unsigned int nrows) : vpArray2D<double>(nrows, 1)
211 {
212  init(v, r, nrows);
213 }
214 
252 void vpColVector::init(const vpColVector &v, unsigned int r, unsigned int nrows)
253 {
254  unsigned int rnrows = r + nrows;
255 
256  if (rnrows > v.getRows())
257  throw(vpException(vpException::dimensionError, "Bad row dimension (%d > %d) used to initialize vpColVector", rnrows,
258  v.getRows()));
259  resize(nrows, false);
260 
261  if (this->rowPtrs == NULL) // Fix coverity scan: explicit null dereferenced
262  return; // Nothing to do
263  for (unsigned int i = r; i < rnrows; i++)
264  (*this)[i - r] = v[i];
265 }
266 
268 {
269  for (unsigned int i = 0; i < v.size(); i++)
270  (*this)[i] = v[i];
271 }
272 
274 {
275  for (unsigned int i = 0; i < p.size(); i++)
276  (*this)[i] = p[i];
277 }
278 
280 {
281  for (unsigned int i = 0; i < v.size(); i++)
282  (*this)[i] = v[i];
283 }
284 
286 vpColVector::vpColVector(const vpMatrix &M, unsigned int j) : vpArray2D<double>(M.getRows(), 1)
287 {
288  for (unsigned int i = 0; i < M.getCols(); i++)
289  (*this)[i] = M[i][j];
290 }
291 
299 {
300  if (M.getCols() != 1) {
301  throw(vpException(vpException::dimensionError, "Cannot construct a (%dx1) row vector from a (%dx%d) matrix",
302  M.getRows(), M.getRows(), M.getCols()));
303  }
304 
305  for (unsigned int i = 0; i < M.getRows(); i++)
306  (*this)[i] = M[i][0];
307 }
308 
312 vpColVector::vpColVector(const std::vector<double> &v) : vpArray2D<double>((unsigned int)v.size(), 1)
313 {
314  for (unsigned int i = 0; i < v.size(); i++)
315  (*this)[i] = v[i];
316 }
320 vpColVector::vpColVector(const std::vector<float> &v) : vpArray2D<double>((unsigned int)v.size(), 1)
321 {
322  for (unsigned int i = 0; i < v.size(); i++)
323  (*this)[i] = (double)(v[i]);
324 }
325 
326 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
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 
631 {
632  double *d = data;
633 
634  for (unsigned int i = 0; i < rowNum; i++)
635  *(d++) = x;
636  return *this;
637 }
638 
639 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
641 {
642  if (this != &other) {
643  free(data);
644  free(rowPtrs);
645 
646  rowNum = other.rowNum;
647  colNum = other.colNum;
648  rowPtrs = other.rowPtrs;
649  dsize = other.dsize;
650  data = other.data;
651 
652  other.rowNum = 0;
653  other.colNum = 0;
654  other.rowPtrs = NULL;
655  other.dsize = 0;
656  other.data = NULL;
657  }
658 
659  return *this;
660 }
661 #endif
662 
667 {
668  vpRowVector v(rowNum);
669  memcpy(v.data, data, rowNum * sizeof(double));
670  return v;
671 }
672 
677 vpRowVector vpColVector::transpose() const { return t(); }
678 
683 void vpColVector::transpose(vpRowVector &v) const { v = t(); }
684 
689 vpColVector operator*(const double &x, const vpColVector &v)
690 {
691  vpColVector vout;
692  vout = v * x;
693  return vout;
694 }
695 
703 double vpColVector::dotProd(const vpColVector &a, const vpColVector &b)
704 {
705  if (a.data == NULL) {
706  throw(vpException(vpException::fatalError, "Cannot compute the dot product: first vector empty"));
707  }
708  if (b.data == NULL) {
709  throw(vpException(vpException::fatalError, "Cannot compute the dot product: second vector empty"));
710  }
711  if (a.size() != b.size()) {
713  "Cannot compute the dot product between column vectors "
714  "with different dimensions (%d) and (%d)",
715  a.size(), b.size()));
716  }
717 
718  double *ad = a.data;
719  double *bd = b.data;
720 
721  double c = 0;
722  for (unsigned int i = 0; i < a.getRows(); i++)
723  c += *(ad++) * *(bd++);
724  // vpMatrix c = (a.t() * b);
725  // return c[0][0];
726  return c;
727 }
728 
737 {
738  x = x / sqrt(x.sumSquare());
739 
740  return x;
741 }
742 
751 {
752 
753  double sum_square = sumSquare();
754 
755  // if (sum != 0.0)
756  if (std::fabs(sum_square) > std::numeric_limits<double>::epsilon())
757  *this /= sqrt(sum_square);
758 
759  // If sum = 0, we have a nul vector. So we return just.
760  return *this;
761 }
762 
768 {
769  if (v.data == NULL) {
770  throw(vpException(vpException::fatalError, "Cannot sort content of column vector: vector empty"));
771  }
772  vpColVector tab;
773  tab = v;
774  unsigned int nb_permutation = 1;
775  unsigned int i = 0;
776  while (nb_permutation != 0) {
777  nb_permutation = 0;
778  for (unsigned int j = v.getRows() - 1; j >= i + 1; j--) {
779  if ((tab[j] > tab[j - 1])) {
780  double tmp = tab[j];
781  tab[j] = tab[j - 1];
782  tab[j - 1] = tmp;
783  nb_permutation++;
784  }
785  }
786  i++;
787  }
788 
789  return tab;
790 }
791 
797 {
798  if (v.data == NULL) {
799  throw(vpException(vpException::fatalError, "Cannot sort content of column vector: vector empty"));
800  }
801  vpColVector tab;
802  tab = v;
803  unsigned int nb_permutation = 1;
804  unsigned int i = 0;
805  while (nb_permutation != 0) {
806  nb_permutation = 0;
807  for (unsigned int j = v.getRows() - 1; j >= i + 1; j--) {
808  if ((tab[j] < tab[j - 1])) {
809  double tmp = tab[j];
810  tab[j] = tab[j - 1];
811  tab[j - 1] = tmp;
812  nb_permutation++;
813  }
814  }
815  i++;
816  }
817 
818  return tab;
819 }
820 
837 void vpColVector::stack(const double &d)
838 {
839  this->resize(rowNum + 1, false);
840  (*this)[rowNum - 1] = d;
841 }
842 
862 void vpColVector::stack(const vpColVector &v) { *this = vpColVector::stack(*this, v); }
863 
883 {
884  vpColVector C;
885  vpColVector::stack(A, B, C);
886  return C;
887 }
888 
908 {
909  unsigned int nrA = A.getRows();
910  unsigned int nrB = B.getRows();
911 
912  if (nrA == 0 && nrB == 0) {
913  C.resize(0);
914  return;
915  }
916 
917  if (nrB == 0) {
918  C = A;
919  return;
920  }
921 
922  if (nrA == 0) {
923  C = B;
924  return;
925  }
926 
927  // General case
928  C.resize(nrA + nrB, false);
929 
930  for (unsigned int i = 0; i < nrA; i++)
931  C[i] = A[i];
932 
933  for (unsigned int i = 0; i < nrB; i++)
934  C[nrA + i] = B[i];
935 }
936 
941 {
942  if (v.data == NULL || v.size() == 0) {
943  throw(vpException(vpException::dimensionError, "Cannot compute column vector mean: vector empty"));
944  }
945 
946  // Use directly sum() function
947  double mean = v.sum();
948 
949  // Old code used
950  // double *vd = v.data;
951  // for (unsigned int i=0 ; i < v.getRows() ; i++)
952  // mean += *(vd++);
953 
954  return mean / v.getRows();
955 }
956 
961 {
962  if (v.data == NULL || v.size() == 0) {
963  throw(vpException(vpException::dimensionError, "Cannot compute column vector median: vector empty"));
964  }
965 
966  std::vector<double> vectorOfDoubles(v.data, v.data + v.rowNum);
967 
968  return vpMath::getMedian(vectorOfDoubles);
969 }
970 
974 double vpColVector::stdev(const vpColVector &v, const bool useBesselCorrection)
975 {
976  if (v.data == NULL || v.size() == 0) {
977  throw(vpException(vpException::dimensionError, "Cannot compute column vector stdev: vector empty"));
978  }
979 
980  double mean_value = mean(v);
981  double sum_squared_diff = 0.0;
982  unsigned int i = 0;
983 
984 #if VISP_HAVE_SSE2
985  if (vpCPUFeatures::checkSSE2()) {
986  __m128d v_sub, v_mul, v_sum = _mm_setzero_pd();
987  __m128d v_mean = _mm_set1_pd(mean_value);
988 
989  if (v.getRows() >= 4) {
990  for (; i <= v.getRows() - 4; i += 4) {
991  v_sub = _mm_sub_pd(_mm_loadu_pd(v.data + i), v_mean);
992  v_mul = _mm_mul_pd(v_sub, v_sub);
993  v_sum = _mm_add_pd(v_mul, v_sum);
994 
995  v_sub = _mm_sub_pd(_mm_loadu_pd(v.data + i + 2), v_mean);
996  v_mul = _mm_mul_pd(v_sub, v_sub);
997  v_sum = _mm_add_pd(v_mul, v_sum);
998  }
999  }
1000 
1001  double res[2];
1002  _mm_storeu_pd(res, v_sum);
1003 
1004  sum_squared_diff = res[0] + res[1];
1005  }
1006 // Old code used before SSE
1007 //#else
1008 // for(unsigned int i = 0; i < v.size(); i++) {
1009 // sum_squared_diff += (v[i]-mean_value) * (v[i]-mean_value);
1010 // }
1011 #endif
1012 
1013  for (; i < v.getRows(); i++) {
1014  sum_squared_diff += (v[i] - mean_value) * (v[i] - mean_value);
1015  }
1016 
1017  double divisor = (double)v.size();
1018  if (useBesselCorrection && v.size() > 1) {
1019  divisor = divisor - 1;
1020  }
1021 
1022  return std::sqrt(sum_squared_diff / divisor);
1023 }
1024 
1040 {
1041  vpMatrix M;
1042  if (v.getRows() != 3) {
1043  throw(vpException(vpException::dimensionError, "Cannot compute skew vector of a non 3-dimention vector (%d)",
1044  v.getRows()));
1045  }
1046 
1047  M.resize(3, 3, false, false);
1048  M[0][0] = 0;
1049  M[0][1] = -v[2];
1050  M[0][2] = v[1];
1051  M[1][0] = v[2];
1052  M[1][1] = 0;
1053  M[1][2] = -v[0];
1054  M[2][0] = -v[1];
1055  M[2][1] = v[0];
1056  M[2][2] = 0;
1057 
1058  return M;
1059 }
1060 
1072 {
1073  if (a.getRows() != 3 || b.getRows() != 3) {
1075  "Cannot compute the cross product between column "
1076  "vector with dimension %d and %d",
1077  a.getRows(), b.getRows()));
1078  }
1079 
1080  return vpColVector::skew(a) * b;
1081 }
1082 
1091 vpMatrix vpColVector::reshape(const unsigned int &nrows, const unsigned int &ncols)
1092 {
1093  vpMatrix M(nrows, ncols);
1094  reshape(M, nrows, ncols);
1095  return M;
1096 }
1097 
1153 void vpColVector::reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols)
1154 {
1155  if (dsize != nrows * ncols) {
1156  throw(vpException(vpException::dimensionError, "Cannot reshape (%dx1) column vector in (%dx%d) matrix", rowNum,
1157  M.getRows(), M.getCols()));
1158  }
1159  if ((M.getRows() != nrows) || (M.getCols() != ncols))
1160  M.resize(nrows, ncols, false, false);
1161 
1162  for (unsigned int j = 0; j < ncols; j++)
1163  for (unsigned int i = 0; i < nrows; i++)
1164  M[i][j] = data[j * nrows + i];
1165 }
1166 
1199 void vpColVector::insert(unsigned int i, const vpColVector &v)
1200 {
1201  if (i + v.size() > this->size())
1202  throw(vpException(vpException::dimensionError, "Unable to insert a column vector"));
1203 
1204  if (data != NULL && v.data != NULL && v.rowNum > 0) {
1205  memcpy(data + i, v.data, sizeof(double) * v.rowNum);
1206  }
1207 }
1208 
1228 int vpColVector::print(std::ostream &s, unsigned int length, char const *intro) const
1229 {
1230  typedef std::string::size_type size_type;
1231 
1232  unsigned int m = getRows();
1233  unsigned int n = 1;
1234 
1235  std::vector<std::string> values(m * n);
1236  std::ostringstream oss;
1237  std::ostringstream ossFixed;
1238  std::ios_base::fmtflags original_flags = oss.flags();
1239 
1240  // ossFixed <<std::fixed;
1241  ossFixed.setf(std::ios::fixed, std::ios::floatfield);
1242 
1243  size_type maxBefore = 0; // the length of the integral part
1244  size_type maxAfter = 0; // number of decimals plus
1245  // one place for the decimal point
1246  for (unsigned int i = 0; i < m; ++i) {
1247  oss.str("");
1248  oss << (*this)[i];
1249  if (oss.str().find("e") != std::string::npos) {
1250  ossFixed.str("");
1251  ossFixed << (*this)[i];
1252  oss.str(ossFixed.str());
1253  }
1254 
1255  values[i] = oss.str();
1256  size_type thislen = values[i].size();
1257  size_type p = values[i].find('.');
1258 
1259  if (p == std::string::npos) {
1260  maxBefore = vpMath::maximum(maxBefore, thislen);
1261  // maxAfter remains the same
1262  } else {
1263  maxBefore = vpMath::maximum(maxBefore, p);
1264  maxAfter = vpMath::maximum(maxAfter, thislen - p - 1);
1265  }
1266  }
1267 
1268  size_type totalLength = length;
1269  // increase totalLength according to maxBefore
1270  totalLength = vpMath::maximum(totalLength, maxBefore);
1271  // decrease maxAfter according to totalLength
1272  maxAfter = (std::min)(maxAfter, totalLength - maxBefore);
1273  if (maxAfter == 1)
1274  maxAfter = 0;
1275 
1276  // the following line is useful for debugging
1277  // std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
1278 
1279  if (intro)
1280  s << intro;
1281  s << "[" << m << "," << n << "]=\n";
1282 
1283  for (unsigned int i = 0; i < m; i++) {
1284  s << " ";
1285  size_type p = values[i].find('.');
1286  s.setf(std::ios::right, std::ios::adjustfield);
1287  s.width((std::streamsize)maxBefore);
1288  s << values[i].substr(0, p).c_str();
1289 
1290  if (maxAfter > 0) {
1291  s.setf(std::ios::left, std::ios::adjustfield);
1292  if (p != std::string::npos) {
1293  s.width((std::streamsize)maxAfter);
1294  s << values[i].substr(p, maxAfter).c_str();
1295  } else {
1296  assert(maxAfter > 1);
1297  s.width((std::streamsize)maxAfter);
1298  s << ".0";
1299  }
1300  }
1301 
1302  s << ' ';
1303 
1304  s << std::endl;
1305  }
1306 
1307  s.flags(original_flags); // restore s to standard state
1308 
1309  return (int)(maxBefore + maxAfter);
1310 }
1311 
1317 double vpColVector::sum() const
1318 {
1319  double sum = 0.0;
1320  unsigned int i = 0;
1321 
1322 #if VISP_HAVE_SSE2
1323  if (vpCPUFeatures::checkSSE2()) {
1324  __m128d v_sum1 = _mm_setzero_pd(), v_sum2 = _mm_setzero_pd(), v_sum;
1325 
1326  if (rowNum >= 4) {
1327  for (; i <= rowNum - 4; i += 4) {
1328  v_sum1 = _mm_add_pd(_mm_loadu_pd(data + i), v_sum1);
1329  v_sum2 = _mm_add_pd(_mm_loadu_pd(data + i + 2), v_sum2);
1330  }
1331  }
1332 
1333  v_sum = _mm_add_pd(v_sum1, v_sum2);
1334 
1335  double res[2];
1336  _mm_storeu_pd(res, v_sum);
1337 
1338  sum = res[0] + res[1];
1339  }
1340 // Old code used before SSE
1341 //#else
1342 // for (unsigned int i=0;i<rowNum;i++) {
1343 // sum += rowPtrs[i][0];
1344 // }
1345 #endif
1346 
1347  for (; i < rowNum; i++) {
1348  sum += (*this)[i];
1349  }
1350 
1351  return sum;
1352 }
1353 
1361 {
1362  double sum_square = 0.0;
1363  unsigned int i = 0;
1364 
1365 #if VISP_HAVE_SSE2
1366  if (vpCPUFeatures::checkSSE2()) {
1367  __m128d v_mul1, v_mul2;
1368  __m128d v_sum = _mm_setzero_pd();
1369 
1370  if (rowNum >= 4) {
1371  for (; i <= rowNum - 4; i += 4) {
1372  v_mul1 = _mm_mul_pd(_mm_loadu_pd(data + i), _mm_loadu_pd(data + i));
1373  v_mul2 = _mm_mul_pd(_mm_loadu_pd(data + i + 2), _mm_loadu_pd(data + i + 2));
1374 
1375  v_sum = _mm_add_pd(v_mul1, v_sum);
1376  v_sum = _mm_add_pd(v_mul2, v_sum);
1377  }
1378  }
1379 
1380  double res[2];
1381  _mm_storeu_pd(res, v_sum);
1382 
1383  sum_square = res[0] + res[1];
1384  }
1385 // Old code used before SSE
1386 //#else
1387 // for (unsigned int i=0;i<rowNum;i++) {
1388 // double x=rowPtrs[i][0];
1389 // sum_square += x*x;
1390 // }
1391 #endif
1392 
1393  for (; i < rowNum; i++) {
1394  sum_square += (*this)[i] * (*this)[i];
1395  }
1396 
1397  return sum_square;
1398 }
1399 
1407 {
1408  // Use directly sumSquare() function
1409  double norm = sumSquare();
1410 
1411  // Old code used
1412  // for (unsigned int i=0;i<dsize;i++) {
1413  // double x = *(data +i); norm += x*x;
1414  // }
1415 
1416  return sqrt(norm);
1417 }
1418 
1426 {
1427  if (v.getRows() != rowNum || v.getCols() != colNum) {
1428  throw(vpException(vpException::dimensionError, "Hadamard product: bad dimensions!"));
1429  }
1430 
1431  vpColVector out;
1432  out.resize(rowNum, false);
1433 
1434  unsigned int i = 0;
1435 
1436 #if VISP_HAVE_SSE2
1437  if (vpCPUFeatures::checkSSE2() && dsize >= 2) {
1438  for (; i <= dsize - 2; i += 2) {
1439  __m128d vout = _mm_mul_pd(_mm_loadu_pd(data + i), _mm_loadu_pd(v.data + i));
1440  _mm_storeu_pd(out.data + i, vout);
1441  }
1442  }
1443 #endif
1444 
1445  for (; i < dsize; i++) {
1446  out.data[i] = data[i] * v.data[i];
1447  }
1448 
1449  return out;
1450 }
1451 
1464 {
1465  double norm = 0.0;
1466  for (unsigned int i = 0; i < rowNum; i++) {
1467  double x = fabs((*this)[i]);
1468  if (x > norm) {
1469  norm = x;
1470  }
1471  }
1472  return norm;
1473 }
1474 
1503 std::ostream &vpColVector::cppPrint(std::ostream &os, const std::string &matrixName, bool octet) const
1504 {
1505  os << "vpColVector " << matrixName << " (" << this->getRows() << "); " << std::endl;
1506 
1507  for (unsigned int i = 0; i < this->getRows(); ++i) {
1508 
1509  if (!octet) {
1510  os << matrixName << "[" << i << "] = " << (*this)[i] << "; " << std::endl;
1511  } else {
1512  for (unsigned int k = 0; k < sizeof(double); ++k) {
1513  os << "((unsigned char*)&(" << matrixName << "[" << i << "]) )[" << k << "] = 0x" << std::hex
1514  << (unsigned int)((unsigned char *)&((*this)[i]))[k] << "; " << std::endl;
1515  }
1516  }
1517  }
1518  std::cout << std::endl;
1519  return os;
1520 };
1521 
1548 std::ostream &vpColVector::csvPrint(std::ostream &os) const
1549 {
1550  for (unsigned int i = 0; i < this->getRows(); ++i) {
1551  os << (*this)[i];
1552 
1553  os << std::endl;
1554  }
1555  return os;
1556 };
1557 
1583 std::ostream &vpColVector::maplePrint(std::ostream &os) const
1584 {
1585  os << "([ " << std::endl;
1586  for (unsigned int i = 0; i < this->getRows(); ++i) {
1587  os << "[";
1588  os << (*this)[i] << ", ";
1589  os << "]," << std::endl;
1590  }
1591  os << "])" << std::endl;
1592  return os;
1593 };
1594 
1631 std::ostream &vpColVector::matlabPrint(std::ostream &os) const
1632 {
1633  os << "[ ";
1634  for (unsigned int i = 0; i < this->getRows(); ++i) {
1635  os << (*this)[i] << ", ";
1636  if (this->getRows() != i + 1) {
1637  os << ";" << std::endl;
1638  } else {
1639  os << "]" << std::endl;
1640  }
1641  }
1642  return os;
1643 };
1644 
1645 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1646 
1660 void vpColVector::insert(const vpColVector &v, const unsigned int r, const unsigned int c)
1661 {
1662  (void)c;
1663  insert(r, v);
1664 }
1665 #endif // defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
double euclideanNorm() const
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
Implementation of a generic rotation vector.
double operator*(const vpColVector &x) const
vp_deprecated void init()
Definition: vpColVector.h:306
static vpColVector invSort(const vpColVector &v)
void stack(const double &d)
static vpColVector sort(const vpColVector &v)
static double stdev(const vpColVector &v, const bool useBesselCorrection=false)
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:72
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:222
vpColVector operator*(const double &x, const vpColVector &v)
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:171
error that can be emited by ViSP classes.
Definition: vpException.h:71
unsigned int getRows() const
Definition: vpArray2D.h:156
vpRowVector t() const
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
Implementation of a generic 2D array used as vase class of matrices and vectors.
Definition: vpArray2D.h:70
vpColVector & operator*=(double x)
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:158
double sum() const
static double median(const vpColVector &v)
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:137
unsigned int getCols() const
Definition: vpArray2D.h:146
vpColVector & operator/=(double x)
vpColVector hadamard(const vpColVector &v) const
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:74
vpRowVector transpose() const
vpColVector & normalize()
std::ostream & csvPrint(std::ostream &os) const
double infinityNorm() const
VISP_EXPORT bool checkSSE2()
std::ostream & matlabPrint(std::ostream &os) const
vpColVector & operator<<(const vpColVector &v)
static double mean(const vpColVector &v)
vpColVector & operator-=(vpColVector v)
Operator that allows to substract two column vectors.
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:76
void insert(unsigned int i, const vpColVector &v)
vpColVector & operator=(const vpColVector &v)
Copy operator. Allow operation such as A = v.
int print(std::ostream &s, unsigned int length, char const *intro=0) const
vpColVector & operator+=(vpColVector v)
Operator that allows to add two column vectors.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpColVector operator/(const double x) const
static double dotProd(const vpColVector &a, const vpColVector &b)
vpColVector operator-() const
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
double sumSquare() const
vpColVector()
Basic constructor that creates an empty 0-size column vector.
Definition: vpColVector.h:78
std::ostream & maplePrint(std::ostream &os) const
vpColVector operator+(const vpColVector &v) const
Operator that allows to add two column vectors.
Definition: vpColVector.cpp:67
static vpMatrix skew(const vpColVector &v)
unsigned int dsize
Current array size (rowNum * colNum)
Definition: vpArray2D.h:80
void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols)
static vpColVector crossProd(const vpColVector &a, const vpColVector &b)
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
Class that consider the case of a translation vector.
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:78
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:241