ViSP  2.9.0
vpColVector.cpp
1 /****************************************************************************
2  *
3  * $Id: vpColVector.cpp 4649 2014-02-07 14:57:11Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Provide some simple operation on column vectors.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
49 #include <visp/vpColVector.h>
50 #include <visp/vpException.h>
51 #include <visp/vpMatrixException.h>
52 #include <visp/vpDebug.h>
53 #include <visp/vpRotationVector.h>
54 
55 #include <stdio.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <cmath> // std::fabs
59 #include <limits> // numeric_limits
60 #include <string.h> //EM gcc 4.3
61 #include <math.h> //EM gcc 4.3
62 
66 {
67  vpColVector v ;
68 
69  try {
70  v.resize(rowNum) ;
71  }
72  catch(vpException me)
73  {
74  vpERROR_TRACE("Error caught") ;
75  throw ;
76  }
77 
78  double *vd = v.data ; double *d = data ; double *md = m.data ;
79 
80  for (unsigned int i=0;i<rowNum;i++)
81  *(vd++) = *(d++) + *(md++);
82 
83  return v;
84 }
85 
87 double
89 {
90  double v = 0 ;
91 
92  for (unsigned int i=0;i<rowNum;i++)
93  v += (*this)[i] * m[i];
94  return v;
95 }
96 
99 {
100 
101  return (vpMatrix)*this*(vpMatrix)m;
102 }
103 
106 {
107  vpColVector v ;
108 
109  try {
110  v.resize(rowNum) ;
111  }
112  catch(vpException me)
113  {
114  vpERROR_TRACE("Error caught") ;
115  throw ;
116  }
117 
118  for (unsigned int i=0;i<rowNum;i++)
119  v[i] = (*this)[i] - m[i];
120  return v;
121 }
122 
123 vpColVector::vpColVector (vpColVector &m, unsigned int r, unsigned int nrows)
124 {
125  if ( (r+nrows) > m.getRows() )
126  {
127  vpERROR_TRACE("\n\t\t SubvpMatrix larger than vpMatrix") ;
129  "\n\t\t SubvpMatrix larger than vpColVector")) ;
130  }
131  init(m, r, 0, nrows, 1);
132 }
133 
134 
136  resize(v._size);
137  memcpy(data, v.r, v._size*sizeof(double)) ;
138 }
139 
140 
143 {
144  vpColVector A ;
145  try {
146  A.resize(rowNum) ;
147  }
148  catch(vpException me)
149  {
150  vpERROR_TRACE("Error caught") ;
151  throw ;
152  }
153 
154  double *vd = A.data ; double *d = data ;
155 
156  for (unsigned int i=0; i<rowNum; i++)
157  *(vd++)= - (*d++);
158 
159  return A;
160 }
161 
164 {
165  vpColVector v;
166  try {
167  v.resize(rowNum) ;
168  }
169  catch(vpException me)
170  {
171  vpERROR_TRACE("Error caught") ;
172  throw ;
173  }
174 
175  double *vd = v.data ; double *d = data ;
176 
177  for (unsigned int i=0;i<rowNum;i++)
178  *(vd++)= (*d++) * x;
179  return v;
180 }
181 
187 {
188  if (m.getCols() !=1)
189  {
190  vpTRACE(" m should be a 1 cols matrix ") ;
191  throw (vpException(vpException::dimensionError,"m should be a 1 cols matrix "));
192  }
193 
194  try {
195  resize(m.getRows());
196  }
197  catch(vpException me)
198  {
199  vpERROR_TRACE("Error caught") ;
200  throw ;
201  }
202 
203  memcpy(data, m.data, rowNum*sizeof(double)) ;
204 
205  /*
206  double *md = m.data ; double *d = data ;
207  for (int i=0;i<rowNum;i++)
208  *(d++)= *(md++) ;
209  */
210  /*
211  for (int i=0; i<rowNum; i++) {
212  for (int j=0; j<colNum; j++) {
213  rowPtrs[i][j] = m.rowPtrs[i][j];
214  }
215  }*/
216  return *this;
217 }
218 
221 {
222  unsigned int k = v.rowNum ;
223  if (rowNum != k){
224  try {
225  resize(k);
226  }
227  catch(vpException me)
228  {
229  vpERROR_TRACE("Error caught") ;
230  throw ;
231  }
232  }
233  //
234 
235  memcpy(data, v.data, rowNum*sizeof(double)) ;
236  /*
237  double *vd = m.data ; double *d = data ;
238  for (int i=0;i<rowNum;i++)
239  *(d++)= *(vd++) ;
240 
241 
242  for (int i=0; i<rowNum; i++) {
243  for (int j=0; j<colNum; j++) {
244  rowPtrs[i][j] = v.rowPtrs[i][j];
245  }
246  }
247 */
248  return *this;
249 }
250 
253 {
254  try {
255  resize(v.getRows());
256  }
257  catch(vpException me)
258  {
259  vpERROR_TRACE("Error caught") ;
260  throw ;
261  }
262 
263  for (unsigned int i=0; i<rowNum; i++) {
264  for (unsigned int j=0; j<colNum; j++) {
265  rowPtrs[i][j] = v.rowPtrs[i][j];
266  }
267  }
268  return *this;
269 }
270 
273 {
274  for (unsigned int i=0; i<rowNum; i++) {
275  for (unsigned int j=0; j<colNum; j++) {
276  rowPtrs[i][j] = *x++;
277  }
278  }
279  return *this;
280 }
281 
284 {
285  double *d = data ;
286 
287  for (unsigned int i=0;i<rowNum;i++)
288  *(d++)= x ;
289  /*
290  for (int i=0; i<rowNum; i++) {
291  for (int j=0; j<colNum; j++) {
292  rowPtrs[i][j] = x;
293  }
294  }*/
295  return *this;
296 }
297 
298 /*
299  \brief Transpose the row vector A
300 
301  A is defined inside the class
302 
303  \return A^T
304 */
306 {
307  vpRowVector v ;
308  try {
309  v.resize(rowNum);
310  }
311  catch(vpException me)
312  {
313  vpERROR_TRACE("Error caught") ;
314  throw ;
315  }
316  memcpy(v.data, data, rowNum*sizeof(double)) ;
317  /*
318  // premiere optimisation
319  double *vd = m.data ; double *d = data ;
320  for (int i=0;i<rowNum;i++)
321  *(d++)= *(vd++) ;
322  */
323 
324  /*
325  // solution brute
326  for (int i=0;i<rowNum;i++)
327  v[i] = (*this)[i];
328  */
329  return v;
330 }
335 vpColVector operator*(const double &x,const vpColVector &B)
336 {
337  vpColVector v1 ;
338  v1 = B*x ;
339  return v1 ;
340 }
341 
343 {
344 }
345 
346 vpColVector::vpColVector (vpMatrix &m, unsigned int j) : vpMatrix(m, 0, j, m.getRows(), 1)
347 {
348 }
349 
350 
355 double
357 {
358  if (a.data==NULL)
359  {
360  vpERROR_TRACE("vpColVector a non initialized") ;
362  }
363  if (b.data==NULL)
364  {
365  vpERROR_TRACE("vpColVector b non initialized") ;
367  }
368  double *ad = a.data ; double *bd = b.data ;
369 
370  double c = 0 ;
371  for (unsigned int i=0 ; i < a.getRows() ; i++)
372  c += *(ad++)* *(bd++) ;
373  // vpMatrix c = (a.t() * b);
374  // return c[0][0];
375  return c ;
376 }
377 
386 // vpColVector &vpColVector::normalize(vpColVector &x) const
387 // {
388 // x = x/sqrt(x.sumSquare());
389 
390 // return x;
391 // }
392 
393 
403 {
404 
405  double sum = sumSquare() ;
406 
407  //if (sum != 0.0)
408  if (std::fabs(sum) > std::numeric_limits<double>::epsilon())
409  *this /= sqrt(sum) ;
410 
411  // If sum = 0, we have a nul vector. So we return just.
412  return *this;
413 }
414 
415 
416 
419 {
420  if (v.data==NULL)
421  {
422  vpERROR_TRACE("vpColVector v non initialized") ;
424  }
425  vpColVector tab ;
426  tab = v ;
427  unsigned int nb_permutation = 1 ;
428  unsigned int i = 0 ;
429  while (nb_permutation !=0 )
430  {
431  nb_permutation = 0 ;
432  for (unsigned int j = v.getRows()-1 ; j >= i+1 ; j--)
433  {
434  if ((tab[j]>tab[j-1]))
435  {
436  double tmp = tab[j] ;
437  tab[j] = tab[j-1] ;
438  tab[j-1] = tmp ;
439  nb_permutation++ ;
440  }
441  }
442  i++ ;
443  }
444 
445  return tab ;
446 }
447 
450 {
451  if (v.data==NULL) {
452  vpERROR_TRACE("vpColVector a non initialized") ;
454  }
455  vpColVector tab ;
456  tab = v ;
457  unsigned int nb_permutation = 1 ;
458  unsigned int i = 0 ;
459  while (nb_permutation !=0 )
460  {
461  nb_permutation = 0 ;
462  for (unsigned int j = v.getRows()-1 ; j >= i+1 ; j--)
463  {
464  if ((tab[j]<tab[j-1]))
465  {
466  double tmp = tab[j] ;
467  tab[j] = tab[j-1] ;
468  tab[j-1] = tmp ;
469  nb_permutation++ ;
470  }
471  }
472  i++ ;
473  }
474 
475  return tab ;
476 }
477 
494 void vpColVector::stack(const double &b)
495 {
496  this->resize(rowNum+1,false);
497  (*this)[rowNum-1] = b;
498 }
499 
518 {
519  *this = vpColVector::stack(*this, B);
520 }
521 
541 {
542  vpColVector C;
543  vpColVector::stack(A, B, C);
544  return C;
545 }
546 
566 {
567  unsigned int nrA = A.getRows();
568  unsigned int nrB = B.getRows();
569 
570  if (nrA == 0 && nrB == 0) {
571  C.resize(0);
572  return;
573  }
574 
575  if (nrB == 0) {
576  C = A;
577  return;
578  }
579 
580  if (nrA == 0) {
581  C = B;
582  return;
583  }
584 
585  // General case
586  C.resize(nrA + nrB);
587 
588  for (unsigned int i=0; i<nrA; i++)
589  C[i] = A[i];
590 
591  for (unsigned int i=0; i<nrB; i++)
592  C[nrA+i] = B[i];
593 }
594 
599 {
600  if (v.data==NULL)
601  {
602  vpERROR_TRACE("vpColVector v non initialized") ;
604  }
605  double mean = 0 ;
606  double *vd = v.data ;
607  for (unsigned int i=0 ; i < v.getRows() ; i++)
608  mean += *(vd++) ;
609 
610  /*
611  for (int i=0 ; i < v.getRows() ; i++)
612  {
613  mean += v[i] ;
614  }
615  mean /= v.rowNum ;
616  return mean;
617  */
618  return mean/v.getRows();
619 }
620 
624 double
626 {
627  if (v.data==NULL)
628  {
629  vpERROR_TRACE("vpColVector v non initialized") ;
631  }
632 
633  unsigned int i,j;
634  unsigned int inf, sup;
635  unsigned int n = v.getRows() ;
636  vpColVector infsup(n) ;
637 
638  for (i=0;i<v.getRows();i++)
639  {
640  // We compute the number of element gretear (sup) than the current
641  // value and the number of element smaller (inf) than the current
642  // value
643  inf = sup = 0;
644  for (j=0;j<v.getRows();j++)
645  {
646  if (i != j)
647  {
648  if (v[i] <= v[j]) inf++;
649  if (v[i] >= v[j]) sup++;
650  }
651  }
652  // We compute then difference between inf and sup
653  // the median should be for |inf-sup| = 0 (1 if an even number of element)
654  // which means that there are the same number of element in the array
655  // that are greater and smaller that this value.
656  infsup[i] = fabs((double)(inf-sup)); //EM gcc 4.3
657  }
658 
659  // seek for the smaller value of |inf-sup| (should be 0 or 1)
660  unsigned int imin=0 ; // index of the median in the array
661  // min cannot be greater than the number of element
662  double min = v.getRows();
663  for (i=0;i<v.getRows();i++)
664  if (infsup[i] < min)
665  {
666  min = infsup[i];
667  imin = i ;
668  }
669 
670  // return the median
671  return(v[imin]);
672 
673 }
674 
689 vpMatrix
691 {
692 
693  vpMatrix M ;
694  if (v.getRows() != 3)
695  {
696  vpERROR_TRACE("Cannot compute skew kinematic matrix,"
697  "v has not 3 rows") ;
699  "\n\t\t Cannot compute skew kinematic matrix"
700  "v has not 3 rows")) ;
701  }
702 
703 
704  M.resize(3,3) ;
705  M[0][0] = 0 ; M[0][1] = -v[2] ; M[0][2] = v[1] ;
706  M[1][0] = v[2] ; M[1][1] = 0 ; M[1][2] = -v[0] ;
707  M[2][0] = -v[1] ; M[2][1] = v[0] ; M[2][2] = 0 ;
708 
709 
710  return M ;
711 }
712 
720 {
721 
722  if (a.getRows() != 3)
723  {
724  vpERROR_TRACE("Cannot compute cross product,"
725  "a has not 3 rows") ;
727  "\n\t\t Cannot compute cross product"
728  "a has not 3 rows")) ;
729  }
730  if (b.getRows() != 3)
731  {
732 
733  vpERROR_TRACE("Cannot compute cross product,"
734  "b has not 3 rows") ;
736  "\n\t\t Cannot compute cross product"
737  "b has not 3 rows")) ;;
738  }
739 
740  return vpColVector::skew(a) * b;
741 }
742 
743 
750 vpMatrix vpColVector::reshape(const unsigned int &nrows,const unsigned int &ncols){
751  vpMatrix m(nrows,ncols);
752  reshape(m,nrows,ncols);
753  return m;
754 }
755 
762 void vpColVector::reshape(vpMatrix & m,const unsigned int &nrows,const unsigned int &ncols){
763  if(dsize!=nrows*ncols)
764  {
765  vpERROR_TRACE("\n\t\t vpColVector mismatch size for reshape vpSubColVector in a vpMatrix") ;
767  "\n\t\t \n\t\t vpColVector mismatch size for reshape vpSubColVector in a vpMatrix")) ;
768  }
769  try
770  {
771  if ((m.getRows() != nrows) || (m.getCols() != ncols)) m.resize(nrows,ncols);
772  }
773  catch(vpException me)
774  {
775  vpERROR_TRACE("Error caught") ;
776  std::cout << me << std::endl ;
777  throw ;
778  }
779 
780  for(unsigned int j =0; j< ncols; j++)
781  for(unsigned int i =0; i< nrows; i++)
782  m[i][j]=data[j*ncols+i];
783 }
784 
785 /*
786  * Local variables:
787  * c-basic-offset: 2
788  * End:
789  */
something is not initialized
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
Class that consider the case of a generic rotation vector (cannot be used as is !) consisting in thre...
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:183
static vpColVector invSort(const vpColVector &v)
reverse sorting of the elements of vector v
static vpColVector sort(const vpColVector &v)
sort the elements of vector v
void stack(const double &b)
void init()
Initialization of the object matrix.
Definition: vpMatrix.cpp:98
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpTRACE
Definition: vpDebug.h:418
Definition of the row vector class.
Definition: vpRowVector.h:73
vpColVector operator+(const vpColVector &v) const
operator addition of two vectors V = A+v
Definition: vpColVector.cpp:65
error that can be emited by ViSP classes.
Definition: vpException.h:76
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:809
vpMatrix()
Basic constructor.
Definition: vpMatrix.cpp:116
vpColVector operator-() const
operator A = -A
static double median(const vpColVector &v)
compute the median
vpColVector operator*(const double &x, const vpColVector &B)
multiplication by a scalar Ci = x*Bi
double * data
address of the first element of the data array
Definition: vpMatrix.h:118
double ** rowPtrs
address of the first element of each rows
Definition: vpMatrix.h:121
vpRowVector t() const
transpose of Vector
vpColVector & operator<<(const vpColVector &v)
Copy operator. Allow operation such as A = v.
static double mean(const vpColVector &v)
compute the mean
void reshape(vpMatrix &m, const unsigned int &nrows, const unsigned int &ncols)
Reshape methods.
unsigned int rowNum
number of rows
Definition: vpMatrix.h:112
vpColVector & operator=(const vpColVector &v)
Copy operator. Allow operation such as A = v.
void resize(unsigned int i)
Set the size of the Row vector.
Definition: vpRowVector.h:91
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
unsigned int getCols() const
Return the number of columns of the matrix.
Definition: vpMatrix.h:163
static double dotProd(const vpColVector &a, const vpColVector &b)
Dot Product.
error that can be emited by the vpMatrix class and its derivates
vpColVector()
basic constructor
Definition: vpColVector.h:82
static vpMatrix skew(const vpColVector &v)
static vpColVector crossProd(const vpColVector &a, const vpColVector &b)
normalise the vector
unsigned int dsize
Current size (rowNum * colNum)
Definition: vpMatrix.h:124
unsigned int colNum
number of columns
Definition: vpMatrix.h:114
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:161
vpColVector & normalize()
normalise the vector
double operator*(const vpColVector &x) const
operator dot product
Definition: vpColVector.cpp:88
unsigned int _size
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94