ViSP  2.6.2
vpColVector.cpp
1 /****************************************************************************
2  *
3  * $Id: vpColVector.cpp 3629 2012-03-14 11:05:47Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 
497 {
498  *this = vpColVector::stack(*this, B);
499 }
500 
520 {
521  vpColVector C;
522  vpColVector::stack(A, B, C);
523  return C;
524 }
525 
545 {
546  unsigned int nrA = A.getRows();
547  unsigned int nrB = B.getRows();
548 
549  if (nrA == 0 && nrB == 0) {
550  C.resize(0);
551  return;
552  }
553 
554  if (nrB == 0) {
555  C = A;
556  return;
557  }
558 
559  if (nrA == 0) {
560  C = B;
561  return;
562  }
563 
564  // General case
565  C.resize(nrA + nrB);
566 
567  for (unsigned int i=0; i<nrA; i++)
568  C[i] = A[i];
569 
570  for (unsigned int i=0; i<nrB; i++)
571  C[nrA+i] = B[i];
572 }
573 
578 {
579  if (v.data==NULL)
580  {
581  vpERROR_TRACE("vpColVector v non initialized") ;
583  }
584  double mean = 0 ;
585  double *vd = v.data ;
586  for (unsigned int i=0 ; i < v.getRows() ; i++)
587  mean += *(vd++) ;
588 
589  /*
590  for (int i=0 ; i < v.getRows() ; i++)
591  {
592  mean += v[i] ;
593  }
594  mean /= v.rowNum ;
595  return mean;
596  */
597  return mean/v.getRows();
598 }
599 
603 double
605 {
606  if (v.data==NULL)
607  {
608  vpERROR_TRACE("vpColVector v non initialized") ;
610  }
611 
612  unsigned int i,j;
613  unsigned int inf, sup;
614  unsigned int n = v.getRows() ;
615  vpColVector infsup(n) ;
616 
617  for (i=0;i<v.getRows();i++)
618  {
619  // We compute the number of element gretear (sup) than the current
620  // value and the number of element smaller (inf) than the current
621  // value
622  inf = sup = 0;
623  for (j=0;j<v.getRows();j++)
624  {
625  if (i != j)
626  {
627  if (v[i] <= v[j]) inf++;
628  if (v[i] >= v[j]) sup++;
629  }
630  }
631  // We compute then difference between inf and sup
632  // the median should be for |inf-sup| = 0 (1 if an even number of element)
633  // which means that there are the same number of element in the array
634  // that are greater and smaller that this value.
635  infsup[i] = fabs((double)(inf-sup)); //EM gcc 4.3
636  }
637 
638  // seek for the smaller value of |inf-sup| (should be 0 or 1)
639  unsigned int imin=0 ; // index of the median in the array
640  // min cannot be greater than the number of element
641  double min = v.getRows();
642  for (i=0;i<v.getRows();i++)
643  if (infsup[i] < min)
644  {
645  min = infsup[i];
646  imin = i ;
647  }
648 
649  // return the median
650  return(v[imin]);
651 
652 }
653 
668 vpMatrix
670 {
671 
672  vpMatrix M ;
673  if (v.getRows() != 3)
674  {
675  vpERROR_TRACE("Cannot compute skew kinematic matrix,"
676  "v has not 3 rows") ;
678  "\n\t\t Cannot compute skew kinematic matrix"
679  "v has not 3 rows")) ;
680  }
681 
682 
683  M.resize(3,3) ;
684  M[0][0] = 0 ; M[0][1] = -v[2] ; M[0][2] = v[1] ;
685  M[1][0] = v[2] ; M[1][1] = 0 ; M[1][2] = -v[0] ;
686  M[2][0] = -v[1] ; M[2][1] = v[0] ; M[2][2] = 0 ;
687 
688 
689  return M ;
690 }
691 
699 {
700 
701  if (a.getRows() != 3)
702  {
703  vpERROR_TRACE("Cannot compute cross product,"
704  "a has not 3 rows") ;
706  "\n\t\t Cannot compute cross product"
707  "a has not 3 rows")) ;
708  }
709  if (b.getRows() != 3)
710  {
711 
712  vpERROR_TRACE("Cannot compute cross product,"
713  "b has not 3 rows") ;
715  "\n\t\t Cannot compute cross product"
716  "b has not 3 rows")) ;;
717  }
718 
719  return vpColVector::skew(a) * b;
720 }
721 
722 
729 vpMatrix vpColVector::reshape(const unsigned int &nrows,const unsigned int &ncols){
730  vpMatrix m(nrows,ncols);
731  reshape(m,nrows,ncols);
732  return m;
733 }
734 
741 void vpColVector::reshape(vpMatrix & m,const unsigned int &nrows,const unsigned int &ncols){
742  if(dsize!=nrows*ncols)
743  {
744  vpERROR_TRACE("\n\t\t vpColVector mismatch size for reshape vpSubColVector in a vpMatrix") ;
746  "\n\t\t \n\t\t vpColVector mismatch size for reshape vpSubColVector in a vpMatrix")) ;
747  }
748  try
749  {
750  if ((m.getRows() != nrows) || (m.getCols() != ncols)) m.resize(nrows,ncols);
751  }
752  catch(vpException me)
753  {
754  vpERROR_TRACE("Error caught") ;
755  std::cout << me << std::endl ;
756  throw ;
757  }
758 
759  for(unsigned int j =0; j< ncols; j++)
760  for(unsigned int i =0; i< nrows; i++)
761  m[i][j]=data[j*ncols+i];
762 }
763 
764 /*
765  * Local variables:
766  * c-basic-offset: 2
767  * End:
768  */
something is not initialized
void stack(const vpColVector &B)
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 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
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:758
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