ViSP  2.6.2
vpRowVector.cpp
1 /****************************************************************************
2  *
3  * $Id: vpRowVector.cpp 3530 2012-01-03 10:52:12Z 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  * 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  }
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 
178 {
179  vpColVector tmp(colNum);
180  memcpy(tmp.data, data, colNum*sizeof(double)) ;
181  /*
182  for (int i=0;i<colNum;i++)
183  tmp[i] = (*this)[i];
184  */
185  return tmp;
186 }
187 
190 {
191 }
192 
194 vpRowVector::vpRowVector (vpMatrix &m, unsigned int i) : vpMatrix(m, i, 0, 1, m.getCols())
195 {
196 }
197 
207 {
208  x = x/sqrt(x.sumSquare());
209 
210  return x;
211 }
212 
213 
223 {
224 
225  double sum = sumSquare() ;
226  *this /= sum ;
227 
228  return *this;
229 }
230 
237 vpMatrix vpRowVector::reshape(const unsigned int &nrows,const unsigned int &ncols){
238  vpMatrix m(nrows,ncols);
239  reshape(m,nrows,ncols);
240  return m;
241 }
242 
249 void vpRowVector::reshape(vpMatrix & m,const unsigned int &nrows,const unsigned int &ncols){
250  if(dsize!=nrows*ncols)
251  {
252  vpERROR_TRACE("\n\t\t vpSubRowVector mismatch size for reshape vpSubColVector in a vpMatrix") ;
254  "\n\t\t \n\t\t vpSubRowVector mismatch size for reshape vpSubColVector in a vpMatrix")) ;
255  }
256  try
257  {
258  if ((m.getRows() != nrows) || (m.getCols() != ncols)) m.resize(nrows,ncols);
259  }
260  catch(vpException me)
261  {
262  vpERROR_TRACE("Error caught") ;
263  std::cout << me << std::endl ;
264  throw ;
265  }
266  for(unsigned int i =0; i< nrows; i++)
267  for(unsigned int j =0; j< ncols; j++)
268  m[i][j]=data[i*nrows+j];
269 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:174
#define vpERROR_TRACE
Definition: vpDebug.h:379
Definition of the row vector class.
Definition: vpRowVector.h:73
vpRowVector & normalize()
normalise the vector
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:758
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
vpColVector t() const
Transpose the vector.
double operator*(const vpColVector &x) const
operator dot product
unsigned int rowNum
number of rows
Definition: vpMatrix.h:110
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:159
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: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
vpRowVector()
basic constructor
Definition: vpRowVector.h:84