ViSP  2.8.0
vpColVector.cpp
1 /****************************************************************************
2  *
3  * $Id: vpColVector.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
189 
190  if (m.getCols() !=1)
191  {
192  vpTRACE(" m should be a 1 cols matrix ") ;
193  throw (vpException(vpException::dimensionError)," m should be a 1 cols matrix ");
194  }
195 
196  try {
197  resize(m.getRows());
198  }
199  catch(vpException me)
200  {
201  vpERROR_TRACE("Error caught") ;
202  throw ;
203  }
204 
205  memcpy(data, m.data, rowNum*sizeof(double)) ;
206 
207  /*
208  double *md = m.data ; double *d = data ;
209  for (int i=0;i<rowNum;i++)
210  *(d++)= *(md++) ;
211  */
212  /*
213  for (int i=0; i<rowNum; i++) {
214  for (int j=0; j<colNum; j++) {
215  rowPtrs[i][j] = m.rowPtrs[i][j];
216  }
217  }*/
218  return *this;
219 }
220 
223 {
224  unsigned int k = v.rowNum ;
225  if (rowNum != k){
226  try {
227  resize(k);
228  }
229  catch(vpException me)
230  {
231  vpERROR_TRACE("Error caught") ;
232  throw ;
233  }
234  }
235  //
236 
237  memcpy(data, v.data, rowNum*sizeof(double)) ;
238  /*
239  double *vd = m.data ; double *d = data ;
240  for (int i=0;i<rowNum;i++)
241  *(d++)= *(vd++) ;
242 
243 
244  for (int i=0; i<rowNum; i++) {
245  for (int j=0; j<colNum; j++) {
246  rowPtrs[i][j] = v.rowPtrs[i][j];
247  }
248  }
249 */
250  return *this;
251 }
252 
255 {
256  try {
257  resize(v.getRows());
258  }
259  catch(vpException me)
260  {
261  vpERROR_TRACE("Error caught") ;
262  throw ;
263  }
264 
265  for (unsigned int i=0; i<rowNum; i++) {
266  for (unsigned int j=0; j<colNum; j++) {
267  rowPtrs[i][j] = v.rowPtrs[i][j];
268  }
269  }
270  return *this;
271 }
272 
275 {
276  for (unsigned int i=0; i<rowNum; i++) {
277  for (unsigned int j=0; j<colNum; j++) {
278  rowPtrs[i][j] = *x++;
279  }
280  }
281  return *this;
282 }
283 
286 {
287  double *d = data ;
288 
289  for (unsigned int i=0;i<rowNum;i++)
290  *(d++)= x ;
291  /*
292  for (int i=0; i<rowNum; i++) {
293  for (int j=0; j<colNum; j++) {
294  rowPtrs[i][j] = x;
295  }
296  }*/
297  return *this;
298 }
299 
300 /*
301  \brief Transpose the row vector A
302 
303  A is defined inside the class
304 
305  \return A^T
306 */
308 {
309  vpRowVector v ;
310  try {
311  v.resize(rowNum);
312  }
313  catch(vpException me)
314  {
315  vpERROR_TRACE("Error caught") ;
316  throw ;
317  }
318  memcpy(v.data, data, rowNum*sizeof(double)) ;
319  /*
320  // premiere optimisation
321  double *vd = m.data ; double *d = data ;
322  for (int i=0;i<rowNum;i++)
323  *(d++)= *(vd++) ;
324  */
325 
326  /*
327  // solution brute
328  for (int i=0;i<rowNum;i++)
329  v[i] = (*this)[i];
330  */
331  return v;
332 }
337 vpColVector operator*(const double &x,const vpColVector &B)
338 {
339  vpColVector v1 ;
340  v1 = B*x ;
341  return v1 ;
342 }
343 
345 {
346 }
347 
348 vpColVector::vpColVector (vpMatrix &m, unsigned int j) : vpMatrix(m, 0, j, m.getRows(), 1)
349 {
350 }
351 
352 
357 double
359 {
360  if (a.data==NULL)
361  {
362  vpERROR_TRACE("vpColVector a non initialized") ;
364  }
365  if (b.data==NULL)
366  {
367  vpERROR_TRACE("vpColVector b non initialized") ;
369  }
370  double *ad = a.data ; double *bd = b.data ;
371 
372  double c = 0 ;
373  for (unsigned int i=0 ; i < a.getRows() ; i++)
374  c += *(ad++)* *(bd++) ;
375  // vpMatrix c = (a.t() * b);
376  // return c[0][0];
377  return c ;
378 }
379 
388 // vpColVector &vpColVector::normalize(vpColVector &x) const
389 // {
390 // x = x/sqrt(x.sumSquare());
391 
392 // return x;
393 // }
394 
395 
405 {
406 
407  double sum = sumSquare() ;
408 
409  //if (sum != 0.0)
410  if (std::fabs(sum) > std::numeric_limits<double>::epsilon())
411  *this /= sqrt(sum) ;
412 
413  // If sum = 0, we have a nul vector. So we return just.
414  return *this;
415 }
416 
417 
418 
421 {
422  if (v.data==NULL)
423  {
424  vpERROR_TRACE("vpColVector v non initialized") ;
426  }
427  vpColVector tab ;
428  tab = v ;
429  unsigned int nb_permutation = 1 ;
430  unsigned int i = 0 ;
431  while (nb_permutation !=0 )
432  {
433  nb_permutation = 0 ;
434  for (unsigned int j = v.getRows()-1 ; j >= i+1 ; j--)
435  {
436  if ((tab[j]>tab[j-1]))
437  {
438  double tmp = tab[j] ;
439  tab[j] = tab[j-1] ;
440  tab[j-1] = tmp ;
441  nb_permutation++ ;
442  }
443  }
444  i++ ;
445  }
446 
447  return tab ;
448 }
449 
452 {
453  if (v.data==NULL) {
454  vpERROR_TRACE("vpColVector a non initialized") ;
456  }
457  vpColVector tab ;
458  tab = v ;
459  unsigned int nb_permutation = 1 ;
460  unsigned int i = 0 ;
461  while (nb_permutation !=0 )
462  {
463  nb_permutation = 0 ;
464  for (unsigned int j = v.getRows()-1 ; j >= i+1 ; j--)
465  {
466  if ((tab[j]<tab[j-1]))
467  {
468  double tmp = tab[j] ;
469  tab[j] = tab[j-1] ;
470  tab[j-1] = tmp ;
471  nb_permutation++ ;
472  }
473  }
474  i++ ;
475  }
476 
477  return tab ;
478 }
479 
496 void vpColVector::stack(const double &b)
497 {
498  this->resize(rowNum+1,false);
499  (*this)[rowNum-1] = b;
500 }
501 
520 {
521  *this = vpColVector::stack(*this, B);
522 }
523 
543 {
544  vpColVector C;
545  vpColVector::stack(A, B, C);
546  return C;
547 }
548 
568 {
569  unsigned int nrA = A.getRows();
570  unsigned int nrB = B.getRows();
571 
572  if (nrA == 0 && nrB == 0) {
573  C.resize(0);
574  return;
575  }
576 
577  if (nrB == 0) {
578  C = A;
579  return;
580  }
581 
582  if (nrA == 0) {
583  C = B;
584  return;
585  }
586 
587  // General case
588  C.resize(nrA + nrB);
589 
590  for (unsigned int i=0; i<nrA; i++)
591  C[i] = A[i];
592 
593  for (unsigned int i=0; i<nrB; i++)
594  C[nrA+i] = B[i];
595 }
596 
601 {
602  if (v.data==NULL)
603  {
604  vpERROR_TRACE("vpColVector v non initialized") ;
606  }
607  double mean = 0 ;
608  double *vd = v.data ;
609  for (unsigned int i=0 ; i < v.getRows() ; i++)
610  mean += *(vd++) ;
611 
612  /*
613  for (int i=0 ; i < v.getRows() ; i++)
614  {
615  mean += v[i] ;
616  }
617  mean /= v.rowNum ;
618  return mean;
619  */
620  return mean/v.getRows();
621 }
622 
626 double
628 {
629  if (v.data==NULL)
630  {
631  vpERROR_TRACE("vpColVector v non initialized") ;
633  }
634 
635  unsigned int i,j;
636  unsigned int inf, sup;
637  unsigned int n = v.getRows() ;
638  vpColVector infsup(n) ;
639 
640  for (i=0;i<v.getRows();i++)
641  {
642  // We compute the number of element gretear (sup) than the current
643  // value and the number of element smaller (inf) than the current
644  // value
645  inf = sup = 0;
646  for (j=0;j<v.getRows();j++)
647  {
648  if (i != j)
649  {
650  if (v[i] <= v[j]) inf++;
651  if (v[i] >= v[j]) sup++;
652  }
653  }
654  // We compute then difference between inf and sup
655  // the median should be for |inf-sup| = 0 (1 if an even number of element)
656  // which means that there are the same number of element in the array
657  // that are greater and smaller that this value.
658  infsup[i] = fabs((double)(inf-sup)); //EM gcc 4.3
659  }
660 
661  // seek for the smaller value of |inf-sup| (should be 0 or 1)
662  unsigned int imin=0 ; // index of the median in the array
663  // min cannot be greater than the number of element
664  double min = v.getRows();
665  for (i=0;i<v.getRows();i++)
666  if (infsup[i] < min)
667  {
668  min = infsup[i];
669  imin = i ;
670  }
671 
672  // return the median
673  return(v[imin]);
674 
675 }
676 
691 vpMatrix
693 {
694 
695  vpMatrix M ;
696  if (v.getRows() != 3)
697  {
698  vpERROR_TRACE("Cannot compute skew kinematic matrix,"
699  "v has not 3 rows") ;
701  "\n\t\t Cannot compute skew kinematic matrix"
702  "v has not 3 rows")) ;
703  }
704 
705 
706  M.resize(3,3) ;
707  M[0][0] = 0 ; M[0][1] = -v[2] ; M[0][2] = v[1] ;
708  M[1][0] = v[2] ; M[1][1] = 0 ; M[1][2] = -v[0] ;
709  M[2][0] = -v[1] ; M[2][1] = v[0] ; M[2][2] = 0 ;
710 
711 
712  return M ;
713 }
714 
722 {
723 
724  if (a.getRows() != 3)
725  {
726  vpERROR_TRACE("Cannot compute cross product,"
727  "a has not 3 rows") ;
729  "\n\t\t Cannot compute cross product"
730  "a has not 3 rows")) ;
731  }
732  if (b.getRows() != 3)
733  {
734 
735  vpERROR_TRACE("Cannot compute cross product,"
736  "b has not 3 rows") ;
738  "\n\t\t Cannot compute cross product"
739  "b has not 3 rows")) ;;
740  }
741 
742  return vpColVector::skew(a) * b;
743 }
744 
745 
752 vpMatrix vpColVector::reshape(const unsigned int &nrows,const unsigned int &ncols){
753  vpMatrix m(nrows,ncols);
754  reshape(m,nrows,ncols);
755  return m;
756 }
757 
764 void vpColVector::reshape(vpMatrix & m,const unsigned int &nrows,const unsigned int &ncols){
765  if(dsize!=nrows*ncols)
766  {
767  vpERROR_TRACE("\n\t\t vpColVector mismatch size for reshape vpSubColVector in a vpMatrix") ;
769  "\n\t\t \n\t\t vpColVector mismatch size for reshape vpSubColVector in a vpMatrix")) ;
770  }
771  try
772  {
773  if ((m.getRows() != nrows) || (m.getCols() != ncols)) m.resize(nrows,ncols);
774  }
775  catch(vpException me)
776  {
777  vpERROR_TRACE("Error caught") ;
778  std::cout << me << std::endl ;
779  throw ;
780  }
781 
782  for(unsigned int j =0; j< ncols; j++)
783  for(unsigned int i =0; i< nrows; i++)
784  m[i][j]=data[j*ncols+i];
785 }
786 
787 /*
788  * Local variables:
789  * c-basic-offset: 2
790  * End:
791  */
something is not initialized
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
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:174
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:93
#define vpERROR_TRACE
Definition: vpDebug.h:379
#define vpTRACE
Definition: vpDebug.h:401
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:75
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:760
vpColVector operator-()
operator A = -A
vpMatrix()
Basic constructor.
Definition: vpMatrix.cpp:111
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:116
double ** rowPtrs
address of the first element of each rows
Definition: vpMatrix.h:119
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:110
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:159
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:122
unsigned int colNum
number of columns
Definition: vpMatrix.h:112
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:157
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