Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <cmath> // std::fabs
49 #include <limits> // numeric_limits
50 #include <string.h>
51 #include <math.h>
52 #include <sstream>
53 #include <assert.h>
54 
55 #include <visp3/core/vpColVector.h>
56 #include <visp3/core/vpException.h>
57 #include <visp3/core/vpMath.h>
58 #include <visp3/core/vpDebug.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 
66 
70 {
71  if (getRows() != v.getRows() ) {
73  "Cannot add (%dx1) column vector to (%dx1) column vector",
74  getRows(), v.getRows())) ;
75  }
77 
78  for (unsigned int i=0;i<rowNum;i++)
79  r[i] = (*this)[i] + v[i];
80  return r;
81 }
102 {
103  if (getRows() != 3) {
105  "Cannot add %d-dimension column vector to a translation vector", getRows()));
106  }
108 
109  for (unsigned int i=0;i<3;i++) s[i] = (*this)[i]+t[i] ;
110 
111  return s;
112 }
113 
115 vpColVector &
117 {
118  if (getRows() != v.getRows() ) {
120  "Cannot add (%dx1) column vector to (%dx1) column vector",
121  getRows(), v.getRows())) ;
122  }
123 
124  for (unsigned int i=0;i<rowNum;i++)
125  (*this)[i] += v[i];
126  return (*this);
127 }
129 vpColVector &
131 {
132  if (getRows() != v.getRows() ) {
134  "Cannot substract (%dx1) column vector to (%dx1) column vector",
135  getRows(), v.getRows())) ;
136  }
137 
138  for (unsigned int i=0;i<rowNum;i++)
139  (*this)[i] -= v[i];
140  return (*this);
141 }
142 
143 
151 double
153 {
154  if (size() != v.size()) {
156  "Cannot compute the dot product between column vectors with different dimensions (%d) and (%d)",
157  size(), v.size()));
158  }
159  double r = 0 ;
160 
161  for (unsigned int i=0;i<rowNum;i++)
162  r += (*this)[i] * v[i];
163  return r;
164 }
165 
176 {
177  vpMatrix M(rowNum, v.getCols());
178  for (unsigned int i=0; i<rowNum; i++) {
179  for (unsigned int j=0; j<v.getCols(); j++) {
180  M[i][j] = (*this)[i] * v[j];
181  }
182  }
183  return M;
184 }
185 
188 {
189  if (getRows() != m.getRows() ) {
191  "Bad size during vpColVector (%dx1) and vpColVector (%dx1) substraction",
192  getRows(), m.getRows())) ;
193  }
194  vpColVector v(rowNum);
195 
196  for (unsigned int i=0;i<rowNum;i++)
197  v[i] = (*this)[i] - m[i];
198  return v;
199 }
200 
213 vpColVector::vpColVector (const vpColVector &v, unsigned int r, unsigned int nrows)
214  : vpArray2D<double>(nrows, 1)
215 {
216  init(v, r, nrows);
217 }
218 
254 void
255 vpColVector::init(const vpColVector &v, unsigned int r, unsigned int nrows)
256 {
257  unsigned int rnrows = r+nrows ;
258 
259  if (rnrows > v.getRows())
261  "Bad row dimension (%d > %d) used to initialize vpColVector",
262  rnrows, v.getRows()));
263  resize(nrows);
264 
265  if (this->rowPtrs == NULL) // Fix coverity scan: explicit null dereferenced
266  return; // Noting to do
267  for (unsigned int i=r ; i < rnrows; i++)
268  (*this)[i-r] = v[i];
269 }
270 
272  : vpArray2D<double>(v.size(), 1)
273 {
274  for (unsigned int i=0; i< v.size(); i++)
275  (*this)[i] = v[i];
276 }
277 
279  : vpArray2D<double>(p.size(), 1)
280 {
281  for (unsigned int i=0; i< p.size(); i++)
282  (*this)[i] = p[i];
283 }
284 
286  : vpArray2D<double>(v.size(), 1)
287 {
288  for (unsigned int i=0; i< v.size(); i++)
289  (*this)[i] = v[i];
290 }
291 
293 vpColVector::vpColVector (const vpMatrix &M, unsigned int j)
294  : vpArray2D<double>(M.getRows(), 1)
295 {
296  for(unsigned int i=0; i< M.getCols(); i++)
297  (*this)[i] = M[i][j];
298 }
299 
306  : vpArray2D<double>(M.getRows(), 1)
307 {
308  if(M.getCols()!=1) {
310  "Cannot construct a (%dx1) row vector from a (%dx%d) matrix",
311  M.getRows(), M.getRows(), M.getCols())) ;
312  }
313 
314  for(unsigned int i=0; i< M.getRows(); i++)
315  (*this)[i] = M[i][0];
316 }
317 
321 vpColVector::vpColVector (const std::vector<double> &v)
322  : vpArray2D<double>((unsigned int)v.size(), 1)
323 {
324  for(unsigned int i=0; i< v.size(); i++)
325  (*this)[i] = v[i];
326 }
330 vpColVector::vpColVector (const std::vector<float> &v)
331  : vpArray2D<double>((unsigned int)v.size(), 1)
332 {
333  for(unsigned int i=0; i< v.size(); i++)
334  (*this)[i] = (double)(v[i]);
335 }
336 
348 {
349  vpColVector A ;
350  try {
351  A.resize(rowNum) ;
352  }
353  catch(vpException &/*e*/)
354  {
355  vpERROR_TRACE("Error caught") ;
356  throw ;
357  }
358 
359  double *vd = A.data ; double *d = data ;
360 
361  for (unsigned int i=0; i<rowNum; i++)
362  *(vd++)= - (*d++);
363 
364  return A;
365 }
366 
387 {
388  vpColVector v(rowNum);
389 
390  double *vd = v.data ; double *d = data ;
391 
392  for (unsigned int i=0;i<rowNum;i++)
393  *(vd++) = (*d++) * x;
394  return v;
395 }
396 
415 {
416  for (unsigned int i=0;i<rowNum;i++)
417  (*this)[i] *= x;
418  return (*this);
419 }
420 
439 {
440  for (unsigned int i=0;i<rowNum;i++)
441  (*this)[i] /= x;
442  return (*this);
443 }
444 
465 {
466  vpColVector v(rowNum);
467 
468  double *vd = v.data ; double *d = data ;
469 
470  for (unsigned int i=0;i<rowNum;i++)
471  *(vd++) = (*d++) / x;
472  return v;
473 }
474 
481 {
482  if (M.getCols() !=1) {
484  "Cannot transform a (%dx%d) matrix into a column vector",
485  M.getRows(), M.getCols()));
486  }
487 
488  try {
489  resize(M.getRows());
490  }
491  catch(...) {
492  throw ;
493  }
494 
495  memcpy(data, M.data, rowNum*sizeof(double)) ;
496 
497  return (*this);
498 }
499 
503 vpColVector & vpColVector::operator=(const std::vector<double> &v)
504 {
505  resize((unsigned int)v.size());
506  for(unsigned int i=0; i<v.size(); i++)
507  (*this)[i] = v[i];
508  return *this;
509 }
513 vpColVector & vpColVector::operator=(const std::vector<float> &v)
514 {
515  resize((unsigned int)v.size());
516  for(unsigned int i=0; i<v.size(); i++)
517  (*this)[i] = (float)v[i];
518  return *this;
519 }
520 
522 {
523  unsigned int k = v.rowNum ;
524  if (rowNum != k){
525  try {
526  resize(k);
527  }
528  catch(...)
529  {
530  throw ;
531  }
532  }
533 
534  memcpy(data, v.data, rowNum*sizeof(double)) ;
535  return *this;
536 }
537 
542 {
543  unsigned int k = tv.getRows() ;
544  if (rowNum != k){
545  try {
546  resize(k);
547  }
548  catch(...)
549  {
550  throw ;
551  }
552  }
553 
554  memcpy(data, tv.data, rowNum*sizeof(double)) ;
555  return *this;
556 }
561 {
562  unsigned int k = rv.getRows() ;
563  if (rowNum != k){
564  try {
565  resize(k);
566  }
567  catch(...)
568  {
569  throw ;
570  }
571  }
572 
573  memcpy(data, rv.data, rowNum*sizeof(double)) ;
574  return *this;
575 }
580 {
581  unsigned int k = p.getRows() ;
582  if (rowNum != k){
583  try {
584  resize(k);
585  }
586  catch(...)
587  {
588  throw ;
589  }
590  }
591 
592  memcpy(data, p.data, rowNum*sizeof(double)) ;
593  return *this;
594 }
595 
617 {
618  *this = v;
619  return *this;
620 }
621 
647 {
648  for (unsigned int i=0; i<rowNum; i++) {
649  for (unsigned int j=0; j<colNum; j++) {
650  rowPtrs[i][j] = *x++;
651  }
652  }
653  return *this;
654 }
655 
658 {
659  double *d = data ;
660 
661  for (unsigned int i=0;i<rowNum;i++)
662  *(d++)= x ;
663  return *this;
664 }
665 
670 {
671  vpRowVector v(rowNum);
672  memcpy(v.data, data, rowNum*sizeof(double)) ;
673  return v;
674 }
675 
681 {
682  return t();
683 }
684 
690 {
691  v = t();
692 }
693 
694 
699 vpColVector operator*(const double &x, const vpColVector &v)
700 {
701  vpColVector vout ;
702  vout = v*x ;
703  return vout ;
704 }
705 
712 double
714 {
715  if (a.data==NULL) {
717  "Cannot compute the dot product: first vector empty")) ;
718  }
719  if (b.data==NULL) {
721  "Cannot compute the dot product: second vector empty")) ;
722  }
723  if (a.size() != b.size()) {
725  "Cannot compute the dot product between column vectors with different dimensions (%d) and (%d)",
726  a.size(), b.size()));
727  }
728 
729  double *ad = a.data ; double *bd = b.data ;
730 
731  double c = 0 ;
732  for (unsigned int i=0 ; i < a.getRows() ; i++)
733  c += *(ad++)* *(bd++) ;
734  // vpMatrix c = (a.t() * b);
735  // return c[0][0];
736  return c ;
737 }
738 
747  {
748  x = x/sqrt(x.sumSquare());
749 
750  return x;
751  }
752 
753 
762 {
763 
764  double sum_square = sumSquare();
765 
766  //if (sum != 0.0)
767  if (std::fabs(sum_square) > std::numeric_limits<double>::epsilon())
768  *this /= sqrt(sum_square) ;
769 
770  // If sum = 0, we have a nul vector. So we return just.
771  return *this;
772 }
773 
780 {
781  if (v.data==NULL) {
783  "Cannot sort content of column vector: vector empty")) ;
784  }
785  vpColVector tab ;
786  tab = v ;
787  unsigned int nb_permutation = 1 ;
788  unsigned int i = 0 ;
789  while (nb_permutation !=0 )
790  {
791  nb_permutation = 0 ;
792  for (unsigned int j = v.getRows()-1 ; j >= i+1 ; j--)
793  {
794  if ((tab[j]>tab[j-1]))
795  {
796  double tmp = tab[j] ;
797  tab[j] = tab[j-1] ;
798  tab[j-1] = tmp ;
799  nb_permutation++ ;
800  }
801  }
802  i++ ;
803  }
804 
805  return tab ;
806 }
807 
814 {
815  if (v.data==NULL) {
817  "Cannot sort content of column vector: vector empty")) ;
818  }
819  vpColVector tab ;
820  tab = v ;
821  unsigned int nb_permutation = 1 ;
822  unsigned int i = 0 ;
823  while (nb_permutation !=0 )
824  {
825  nb_permutation = 0 ;
826  for (unsigned int j = v.getRows()-1 ; j >= i+1 ; j--)
827  {
828  if ((tab[j]<tab[j-1]))
829  {
830  double tmp = tab[j] ;
831  tab[j] = tab[j-1] ;
832  tab[j-1] = tmp ;
833  nb_permutation++ ;
834  }
835  }
836  i++ ;
837  }
838 
839  return tab ;
840 }
841 
858 void vpColVector::stack(const double &d)
859 {
860  this->resize(rowNum+1,false);
861  (*this)[rowNum-1] = d;
862 }
863 
884 {
885  *this = vpColVector::stack(*this, v);
886 }
887 
907 {
908  vpColVector C;
909  vpColVector::stack(A, B, C);
910  return C;
911 }
912 
932 {
933  unsigned int nrA = A.getRows();
934  unsigned int nrB = B.getRows();
935 
936  if (nrA == 0 && nrB == 0) {
937  C.resize(0);
938  return;
939  }
940 
941  if (nrB == 0) {
942  C = A;
943  return;
944  }
945 
946  if (nrA == 0) {
947  C = B;
948  return;
949  }
950 
951  // General case
952  C.resize(nrA + nrB);
953 
954  for (unsigned int i=0; i<nrA; i++)
955  C[i] = A[i];
956 
957  for (unsigned int i=0; i<nrB; i++)
958  C[nrA+i] = B[i];
959 }
960 
965 {
966  if (v.data==NULL) {
968  "Cannot compute column vector mean: vector empty")) ;
969  }
970 
971  //Use directly sum() function
972  double mean = v.sum();
973 
974  //Old code used
975 // double *vd = v.data ;
976 // for (unsigned int i=0 ; i < v.getRows() ; i++)
977 // mean += *(vd++) ;
978 
979  return mean/v.getRows();
980 }
981 
985 double
987 {
988  if (v.data==NULL) {
990  "Cannot compute column vector median: vector empty")) ;
991  }
992 
993  std::vector<double> vectorOfDoubles(v.size());
994  for(unsigned int i = 0; i < v.size(); i++) {
995  vectorOfDoubles[i] = v[i];
996  }
997 
998  return vpMath::getMedian(vectorOfDoubles);
999 }
1000 
1004 double
1005 vpColVector::stdev(const vpColVector &v, const bool useBesselCorrection)
1006 {
1007  if (v.data==NULL) {
1009  "Cannot compute column vector stdev: vector empty")) ;
1010  }
1011 
1012  double mean_value = mean(v);
1013  double sum_squared_diff = 0.0;
1014  unsigned int i = 0;
1015 
1016 #if VISP_HAVE_SSE2
1017  __m128d v_sub, v_mul, v_sum = _mm_setzero_pd();
1018  //Compilation error with:
1019  //clang version 3.5.0 (tags/RELEASE_350/final)
1020  //Target: x86_64-unknown-linux-gnu
1021  //Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
1022  //Target: x86_64-apple-darwin13.4.0
1023  //error: use of undeclared identifier '_mm_set_pd1'; did you mean '_mm_set_ps1'?
1024 // __m128d v_mean = _mm_set_pd1(mean_value);
1025  __m128d v_mean = _mm_set_pd(mean_value, mean_value);
1026 
1027  if(v.getRows() >= 4) {
1028  for(; i <= v.getRows()- 4; i+=4) {
1029  v_sub = _mm_sub_pd(_mm_loadu_pd(v.data + i), v_mean);
1030  v_mul = _mm_mul_pd(v_sub, v_sub);
1031  v_sum = _mm_add_pd(v_mul, v_sum);
1032 
1033  v_sub = _mm_sub_pd(_mm_loadu_pd(v.data + i + 2), v_mean);
1034  v_mul = _mm_mul_pd(v_sub, v_sub);
1035  v_sum = _mm_add_pd(v_mul, v_sum);
1036  }
1037  }
1038 
1039  double res[2];
1040  _mm_storeu_pd(res, v_sum);
1041 
1042  sum_squared_diff = res[0]+res[1];
1043 
1044  //Old code used before SSE
1045 //#else
1046 // for(unsigned int i = 0; i < v.size(); i++) {
1047 // sum_squared_diff += (v[i]-mean_value) * (v[i]-mean_value);
1048 // }
1049 #endif
1050 
1051  for(; i < v.getRows(); i++) {
1052  sum_squared_diff += (v[i]-mean_value) * (v[i]-mean_value);
1053  }
1054 
1055  double divisor = (double) v.size();
1056  if(useBesselCorrection && v.size() > 1) {
1057  divisor = divisor-1;
1058  }
1059 
1060  return std::sqrt(sum_squared_diff / divisor);
1061 }
1062 
1077 vpMatrix
1079 {
1080  vpMatrix M ;
1081  if (v.getRows() != 3) {
1083  "Cannot compute skew vector of a non 3-dimention vector (%d)",
1084  v.getRows())) ;
1085  }
1086 
1087  M.resize(3,3) ;
1088  M[0][0] = 0 ; M[0][1] = -v[2] ; M[0][2] = v[1] ;
1089  M[1][0] = v[2] ; M[1][1] = 0 ; M[1][2] = -v[0] ;
1090  M[2][0] = -v[1] ; M[2][1] = v[0] ; M[2][2] = 0 ;
1091 
1092  return M ;
1093 }
1094 
1105 {
1106  if (a.getRows() != 3 || b.getRows() != 3) {
1108  "Cannot compute the cross product between column vector with dimension %d and %d",
1109  a.getRows(), b.getRows()));
1110  }
1111 
1112  return vpColVector::skew(a) * b;
1113 }
1114 
1115 
1124 vpMatrix vpColVector::reshape(const unsigned int &nrows, const unsigned int &ncols)
1125 {
1126  vpMatrix M(nrows, ncols);
1127  reshape(M, nrows, ncols);
1128  return M;
1129 }
1130 
1185 void vpColVector::reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols)
1186 {
1187  if(dsize!=nrows*ncols) {
1189  "Cannot reshape (%dx1) column vector in (%dx%d) matrix",
1190  rowNum, M.getRows(), M.getCols())) ;
1191  }
1192  try {
1193  if ((M.getRows() != nrows) || (M.getCols() != ncols)) M.resize(nrows,ncols);
1194  }
1195  catch(...) {
1196  throw ;
1197  }
1198 
1199  for(unsigned int j =0; j< ncols; j++)
1200  for(unsigned int i =0; i< nrows; i++)
1201  M[i][j]=data[j*nrows+i];
1202 }
1203 
1236 void vpColVector::insert(unsigned int i, const vpColVector &v)
1237 {
1238  if (i+v.size() > this->size())
1239  throw(vpException(vpException::dimensionError, "Unable to insert a column vector"));
1240  for (unsigned int j=0; j < v.size(); j++)
1241  (*this)[i+j] = v[j];
1242 }
1243 
1263 int
1264 vpColVector::print(std::ostream& s, unsigned int length, char const* intro) const
1265 {
1266  typedef std::string::size_type size_type;
1267 
1268  unsigned int m = getRows();
1269  unsigned int n = 1;
1270 
1271  std::vector<std::string> values(m*n);
1272  std::ostringstream oss;
1273  std::ostringstream ossFixed;
1274  std::ios_base::fmtflags original_flags = oss.flags();
1275 
1276  // ossFixed <<std::fixed;
1277  ossFixed.setf ( std::ios::fixed, std::ios::floatfield );
1278 
1279  size_type maxBefore=0; // the length of the integral part
1280  size_type maxAfter=0; // number of decimals plus
1281  // one place for the decimal point
1282  for (unsigned int i=0;i<m;++i) {
1283  oss.str("");
1284  oss << (*this)[i];
1285  if (oss.str().find("e")!=std::string::npos){
1286  ossFixed.str("");
1287  ossFixed << (*this)[i];
1288  oss.str(ossFixed.str());
1289  }
1290 
1291  values[i]=oss.str();
1292  size_type thislen=values[i].size();
1293  size_type p=values[i].find('.');
1294 
1295  if (p==std::string::npos){
1296  maxBefore=vpMath::maximum(maxBefore, thislen);
1297  // maxAfter remains the same
1298  } else{
1299  maxBefore=vpMath::maximum(maxBefore, p);
1300  maxAfter=vpMath::maximum(maxAfter, thislen-p-1);
1301  }
1302 
1303  }
1304 
1305  size_type totalLength=length;
1306  // increase totalLength according to maxBefore
1307  totalLength=vpMath::maximum(totalLength,maxBefore);
1308  // decrease maxAfter according to totalLength
1309  maxAfter=std::min(maxAfter, totalLength-maxBefore);
1310  if (maxAfter==1) maxAfter=0;
1311 
1312  // the following line is useful for debugging
1313  //std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
1314 
1315  if (intro) s <<intro;
1316  s <<"["<<m<<","<<n<<"]=\n";
1317 
1318  for (unsigned int i=0;i<m;i++) {
1319  s <<" ";
1320  size_type p=values[i].find('.');
1321  s.setf(std::ios::right, std::ios::adjustfield);
1322  s.width((std::streamsize)maxBefore);
1323  s <<values[i].substr(0,p).c_str();
1324 
1325  if (maxAfter>0){
1326  s.setf(std::ios::left, std::ios::adjustfield);
1327  if (p!=std::string::npos){
1328  s.width((std::streamsize)maxAfter);
1329  s <<values[i].substr(p,maxAfter).c_str();
1330  } else{
1331  assert(maxAfter>1);
1332  s.width((std::streamsize)maxAfter);
1333  s <<".0";
1334  }
1335  }
1336 
1337  s <<' ';
1338 
1339  s <<std::endl;
1340  }
1341 
1342  s.flags(original_flags); // restore s to standard state
1343 
1344  return (int)(maxBefore+maxAfter);
1345 }
1346 
1352 double vpColVector::sum() const
1353 {
1354  double sum = 0.0;
1355  unsigned int i = 0;
1356 
1357 #if VISP_HAVE_SSE2
1358  __m128d v_sum1 = _mm_setzero_pd(), v_sum2 = _mm_setzero_pd(), v_sum;
1359 
1360  if(rowNum >= 4) {
1361  for(; i <= rowNum- 4; i+=4) {
1362  v_sum1 = _mm_add_pd(_mm_loadu_pd(data + i), v_sum1);
1363  v_sum2 = _mm_add_pd(_mm_loadu_pd(data + i + 2), v_sum2);
1364  }
1365  }
1366 
1367  v_sum = _mm_add_pd(v_sum1, v_sum2);
1368 
1369  double res[2];
1370  _mm_storeu_pd(res, v_sum);
1371 
1372  sum = res[0]+res[1];
1373 
1374  //Old code used before SSE
1375 //#else
1376 // for (unsigned int i=0;i<rowNum;i++) {
1377 // sum += rowPtrs[i][0];
1378 // }
1379 #endif
1380 
1381  for(; i < rowNum; i++) {
1382  sum += (*this)[i];
1383  }
1384 
1385  return sum;
1386 }
1387 
1394 {
1395  double sum_square = 0.0;
1396  unsigned int i = 0;
1397 
1398 #if VISP_HAVE_SSE2
1399  __m128d v_mul1, v_mul2;
1400  __m128d v_sum = _mm_setzero_pd();
1401 
1402  if(rowNum >= 4) {
1403  for(; i <= rowNum- 4; i+=4) {
1404  v_mul1 = _mm_mul_pd(_mm_loadu_pd(data + i), _mm_loadu_pd(data + i));
1405  v_mul2 = _mm_mul_pd(_mm_loadu_pd(data + i + 2), _mm_loadu_pd(data + i + 2));
1406 
1407  v_sum = _mm_add_pd(v_mul1, v_sum);
1408  v_sum = _mm_add_pd(v_mul2, v_sum);
1409  }
1410  }
1411 
1412  double res[2];
1413  _mm_storeu_pd(res, v_sum);
1414 
1415  sum_square = res[0]+res[1];
1416 
1417  //Old code used before SSE
1418 //#else
1419 // for (unsigned int i=0;i<rowNum;i++) {
1420 // double x=rowPtrs[i][0];
1421 // sum_square += x*x;
1422 // }
1423 #endif
1424 
1425  for(; i < rowNum; i++) {
1426  sum_square += (*this)[i] * (*this)[i];
1427  }
1428 
1429  return sum_square;
1430 }
1431 
1439 {
1440  //Use directly sumSquare() function
1441  double norm = sumSquare();
1442 
1443  //Old code used
1444 // for (unsigned int i=0;i<dsize;i++) {
1445 // double x = *(data +i); norm += x*x;
1446 // }
1447 
1448  return sqrt(norm);
1449 }
1450 
1462 {
1463  double norm=0.0;
1464  for (unsigned int i=0;i<rowNum;i++){
1465  double x = fabs ( (*this)[i] ) ;
1466  if (x > norm) {
1467  norm = x;
1468  }
1469  }
1470  return norm;
1471 }
1472 
1502 std::ostream & vpColVector::cppPrint(std::ostream & os, const std::string &matrixName, bool octet) const
1503 {
1504  os << "vpColVector " << matrixName
1505  << " (" << 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  }
1512  else {
1513  for (unsigned int k = 0; k < sizeof(double); ++ k) {
1514  os << "((unsigned char*)&(" << matrixName
1515  << "[" << i << "]) )[" << k
1516  <<"] = 0x" <<std::hex<<
1517  (unsigned int)((unsigned char*)& ((*this)[i])) [k]
1518  << "; " << std::endl;
1519  }
1520  }
1521  }
1522  std::cout << std::endl;
1523  return os;
1524 };
1525 
1552 std::ostream & vpColVector::csvPrint(std::ostream & os) const
1553 {
1554  for (unsigned int i=0; i < this->getRows(); ++ i) {
1555  os << (*this)[i];
1556 
1557  os << std::endl;
1558  }
1559  return os;
1560 };
1561 
1587 std::ostream & vpColVector::maplePrint(std::ostream & os) const
1588 {
1589  os << "([ " << std::endl;
1590  for (unsigned int i=0; i < this->getRows(); ++ i) {
1591  os << "[";
1592  os << (*this)[i] << ", ";
1593  os << "]," << std::endl;
1594  }
1595  os << "])" << std::endl;
1596  return os;
1597 };
1598 
1635 std::ostream & vpColVector::matlabPrint(std::ostream & os) const
1636 {
1637  os << "[ ";
1638  for (unsigned int i=0; i < this->getRows(); ++ i) {
1639  os << (*this)[i] << ", ";
1640  if (this ->getRows() != i+1) { os << ";" << std::endl; }
1641  else { os << "]" << std::endl; }
1642  }
1643  return os;
1644 };
1645 
1646 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1647 
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)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
Implementation of a generic rotation vector.
vp_deprecated void init()
Definition: vpColVector.h:295
static vpColVector invSort(const vpColVector &v)
void stack(const double &d)
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
static vpColVector sort(const vpColVector &v)
static double stdev(const vpColVector &v, const bool useBesselCorrection=false)
double euclideanNorm() const
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:70
std::ostream & matlabPrint(std::ostream &os) const
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:217
vpColVector operator*(const double &x, const vpColVector &v)
vpColVector operator/(const double x) const
#define vpERROR_TRACE
Definition: vpDebug.h:391
vpColVector operator+(const vpColVector &v) const
Operator that allows to add two column vectors.
Definition: vpColVector.cpp:69
error that can be emited by ViSP classes.
Definition: vpException.h:73
std::ostream & csvPrint(std::ostream &os) const
Type * 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:156
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
vpColVector operator-() const
static double median(const vpColVector &v)
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:140
vpColVector & operator/=(double x)
int print(std::ostream &s, unsigned int length, char const *intro=0) const
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:74
double infinityNorm() const
vpColVector & normalize()
std::ostream & maplePrint(std::ostream &os) const
vpRowVector t() const
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
vpColVector & operator<<(const vpColVector &v)
static double mean(const vpColVector &v)
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
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)
double sumSquare() const
vpColVector & operator=(const vpColVector &v)
Copy operator. Allow operation such as A = v.
vpColVector & operator+=(vpColVector v)
Operator that allows to add two column vectors.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
static double dotProd(const vpColVector &a, const vpColVector &b)
vpRowVector transpose() const
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
vpColVector()
Basic constructor that creates an empty 0-size column vector.
Definition: vpColVector.h:78
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)
double operator*(const vpColVector &x) const
Class that consider the case of a translation vector.
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:78
double sum() const
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225