ViSP  2.10.0
vpRowVector.cpp
1 /****************************************************************************
2  *
3  * $Id: vpRowVector.cpp 5185 2015-01-21 14:36:41Z 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  * Operation on row vectors.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
49 #include <visp/vpMatrix.h>
50 #include <visp/vpMatrixException.h>
51 #include <visp/vpRowVector.h>
52 #include <visp/vpColVector.h>
53 #include <visp/vpDebug.h>
54 #include <string.h>
55 #include <stdlib.h>
58 {
59  if (colNum==0)
60  resize(v.getCols());
61 
62  for (unsigned int i=0; i<rowNum; i++) {
63  for (unsigned int j=0; j<colNum; j++) {
64  rowPtrs[i][j] = v.rowPtrs[i][j];
65  }
66  }
67  return *this;
68 }
69 
75 {
76  if (m.getCols() != colNum)
77  resize(m.getCols());
78 
79  memcpy(data, m.data, colNum*sizeof(double)) ;
80  /*
81  for (int i=0; i<rowNum; i++) {
82  for (int j=0; j<colNum; j++) {
83  rowPtrs[i][j] = m.rowPtrs[i][j];
84  }
85  }*/
86  return *this;
87 }
88 
91 {
92  for (unsigned int i=0; i<rowNum; i++) {
93  for (unsigned int j=0; j<colNum; j++) {
94  rowPtrs[i][j] = x;
95  }
96  }
97  return *this;
98 }
99 
114 double vpRowVector::operator*(const vpColVector &x) const
115 {
116  unsigned int nelements = x.getRows();
117  if (getCols() != nelements ) {
119  "Bad size during vpRowVector (1x%d) by vpColVector (%dx1) multiplication",
120  colNum, x.getRows())) ;
121  }
122 
123  double scalar = 0.0;
124 
125  for (unsigned int i=0; i<nelements; i++) {
126  scalar += (*this)[i] * x[i];
127  }
128  return scalar;
129 }
146 {
147  vpRowVector c(A.getCols());
148 
149  if (colNum != A.getRows())
150  {
151  vpERROR_TRACE("vpMatrix mismatch in vpRowVector/matrix multiply") ;
153  "vpMatrix mismatch in vpRowVector/matrix multiply")) ;
154  }
155 
156  c = 0.0;
157 
158  for (unsigned int i=0;i<colNum;i++) {
159  {
160  double bi = data[i] ; // optimization em 5/12/2006
161  for (unsigned int j=0;j<A.getCols();j++) {
162  c[j]+=bi*A[i][j];
163  }
164  }
165  }
166 
167  return c ;
168 }
169 
172 {
173  vpRowVector A ;
174  try {
175  A.resize(colNum) ;
176  }
177  catch(vpException me)
178  {
179  vpERROR_TRACE("Error caught") ;
180  throw ;
181  }
182 
183  double *vd = A.data ; double *d = data ;
184 
185  for (unsigned int i=0; i<colNum; i++)
186  *(vd++)= - (*d++);
187 
188  return A;
189 }
190 
193 {
194  if (getCols() != m.getCols() ) {
196  "Bad size during vpRowVector (1x%d) and vpRowVector (1x%d) substraction",
197  getCols(), m.getCols())) ;
198  }
199 
200  vpRowVector v(colNum) ;
201 
202  for (unsigned int i=0;i<colNum;i++)
203  v[i] = (*this)[i] - m[i];
204  return v;
205 }
206 
209 {
210  if (getCols() != m.getCols() ) {
212  "Bad size during vpRowVector (1x%d) and vpRowVector (1x%d) substraction",
213  getCols(), m.getCols())) ;
214  }
215 
216  vpRowVector v(colNum) ;
217 
218  for (unsigned int i=0;i<colNum;i++)
219  v[i] = (*this)[i] + m[i];
220  return v;
221 }
222 
246 {
247  *this = v;
248  return *this;
249 }
250 
259 {
260  vpColVector tmp(colNum);
261  memcpy(tmp.data, data, colNum*sizeof(double)) ;
262  /*
263  for (int i=0;i<colNum;i++)
264  tmp[i] = (*this)[i];
265  */
266  return tmp;
267 }
268 
271 {
272 }
273 
275 vpRowVector::vpRowVector (vpMatrix &m, unsigned int i) : vpMatrix(m, i, 0, 1, m.getCols())
276 {
277 }
278 
288 {
289  x = x/sqrt(x.sumSquare());
290 
291  return x;
292 }
293 
294 
304 {
305  double sum_square = sumSquare() ;
306  *this /= sum_square ;
307 
308  return *this;
309 }
310 
317 vpMatrix vpRowVector::reshape(const unsigned int &nrows,const unsigned int &ncols){
318  vpMatrix m(nrows,ncols);
319  reshape(m,nrows,ncols);
320  return m;
321 }
322 
329 void vpRowVector::reshape(vpMatrix & m,const unsigned int &nrows,const unsigned int &ncols){
330  if(dsize!=nrows*ncols)
331  {
332  vpERROR_TRACE("\n\t\t vpSubRowVector mismatch size for reshape vpSubColVector in a vpMatrix") ;
334  "\n\t\t \n\t\t vpSubRowVector mismatch size for reshape vpSubColVector in a vpMatrix")) ;
335  }
336  try
337  {
338  if ((m.getRows() != nrows) || (m.getCols() != ncols)) m.resize(nrows,ncols);
339  }
340  catch(vpException me)
341  {
342  vpERROR_TRACE("Error caught") ;
343  std::cout << me << std::endl ;
344  throw ;
345  }
346  for(unsigned int i =0; i< nrows; i++)
347  for(unsigned int j =0; j< ncols; j++)
348  m[i][j]=data[i*nrows+j];
349 }
350 
386 void vpRowVector::insert(unsigned int i, const vpRowVector &v)
387 {
388  if (i+v.size() > this->size())
389  throw(vpException(vpException::dimensionError, "Unable to insert a row vector"));
390  for (unsigned int j=0; j < v.size(); j++)
391  (*this)[i+j] = v[j];
392 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:199
#define vpERROR_TRACE
Definition: vpDebug.h:395
Definition of the row vector class.
Definition: vpRowVector.h:73
vpRowVector & normalize()
Normalise the vector.
error that can be emited by ViSP classes.
Definition: vpException.h:76
vpRowVector operator+(const vpRowVector &v) const
Addition of two vectors V = A+v.
double sumSquare() const
Definition: vpMatrix.cpp:868
unsigned int size() const
Definition: vpRowVector.h:147
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
vpColVector t() const
Transpose the vector.
double operator*(const vpColVector &x) const
operator dot product
vpRowVector operator-() const
Operator A = -A.
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpRowVector.h:98
unsigned int rowNum
number of rows
Definition: vpMatrix.h:112
vpRowVector & operator<<(const vpRowVector &v)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void reshape(vpMatrix &m, const unsigned int &nrows, const unsigned int &ncols)
Reshape methods.
unsigned int getCols() const
Return the number of columns of the matrix.
Definition: vpMatrix.h:163
error that can be emited by the vpMatrix class and its derivates
vpRowVector & operator=(const vpRowVector &v)
Copy operator. Allow operation such as A = v.
Definition: vpRowVector.cpp:57
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
vpRowVector()
Basic constructor.
Definition: vpRowVector.h:84
void insert(unsigned int i, const vpRowVector &v)