ViSP  2.9.0
vpRowVector.cpp
1 /****************************************************************************
2  *
3  * $Id: vpRowVector.cpp 4574 2014-01-09 08:48:51Z 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 ) {
118  vpERROR_TRACE("\n\t\t Illegal matrix operation") ;
120  "\n\t\t Illegal matrix operation, bad vector size")) ;
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 
148  vpRowVector c(A.getCols());
149 
150  if (colNum != A.getRows())
151  {
152  vpERROR_TRACE("vpMatrix mismatch in vpRowVector/matrix multiply") ;
154  "vpMatrix mismatch in vpRowVector/matrix multiply")) ;
155  }
156 
157  c = 0.0;
158 
159  for (unsigned int i=0;i<colNum;i++) {
160  {
161  double bi = data[i] ; // optimization em 5/12/2006
162  for (unsigned int j=0;j<A.getCols();j++) {
163  c[j]+=bi*A[i][j];
164  }
165  }
166  }
167 
168  return c ;
169 }
170 
179 {
180  vpColVector tmp(colNum);
181  memcpy(tmp.data, data, colNum*sizeof(double)) ;
182  /*
183  for (int i=0;i<colNum;i++)
184  tmp[i] = (*this)[i];
185  */
186  return tmp;
187 }
188 
191 {
192 }
193 
195 vpRowVector::vpRowVector (vpMatrix &m, unsigned int i) : vpMatrix(m, i, 0, 1, m.getCols())
196 {
197 }
198 
208 {
209  x = x/sqrt(x.sumSquare());
210 
211  return x;
212 }
213 
214 
224 {
225 
226  double sum = sumSquare() ;
227  *this /= sum ;
228 
229  return *this;
230 }
231 
238 vpMatrix vpRowVector::reshape(const unsigned int &nrows,const unsigned int &ncols){
239  vpMatrix m(nrows,ncols);
240  reshape(m,nrows,ncols);
241  return m;
242 }
243 
250 void vpRowVector::reshape(vpMatrix & m,const unsigned int &nrows,const unsigned int &ncols){
251  if(dsize!=nrows*ncols)
252  {
253  vpERROR_TRACE("\n\t\t vpSubRowVector mismatch size for reshape vpSubColVector in a vpMatrix") ;
255  "\n\t\t \n\t\t vpSubRowVector mismatch size for reshape vpSubColVector in a vpMatrix")) ;
256  }
257  try
258  {
259  if ((m.getRows() != nrows) || (m.getCols() != ncols)) m.resize(nrows,ncols);
260  }
261  catch(vpException me)
262  {
263  vpERROR_TRACE("Error caught") ;
264  std::cout << me << std::endl ;
265  throw ;
266  }
267  for(unsigned int i =0; i< nrows; i++)
268  for(unsigned int j =0; j< ncols; j++)
269  m[i][j]=data[i*nrows+j];
270 }
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:183
#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
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:809
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
unsigned int rowNum
number of rows
Definition: vpMatrix.h:112
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
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