/*! Initialize a sub-row vector from a parent row vector. \param v : parent row vector. \param offset : offset where the sub-row vector starts in the parent row vector. \param ncols : size of the sub-row vector. */ void vpSubRowVector::init(vpRowVector &v, const unsigned int & offset,const unsigned int & ncols) { if (!v.data) { throw(vpException(vpException::fatalError, "Cannot initialize a sub-row vector from an empty parent row vector")) ; } if(offset+ncols<=v.getCols()){ data=v.data+offset; rowNum=1; colNum = ncols; pColNum=v.getCols(); parent=&v; if(rowPtrs) free(rowPtrs); rowPtrs=(double**) malloc(1 * sizeof(double*)); for(unsigned int i=0;i<1;i++) rowPtrs[i]=v.data+i+offset; dsize = colNum ; } else { throw(vpException(vpException::dimensionError, "Cannot create a sub-row vector that is not completely containt in the parrent row vector")) ; } }
/*! Stack row vectors. \param A : Initial vector. \param B : Vector to stack at the end of A. \param C : Resulting stacked vector \f$C = [A B]\f$. \code vpRowVector r1(3, 1); // r1 is equal to [1 1 1] vpRowVector r2(2, 3); // r2 is equal to [3 3] vpRowVector v; vpRowVector::stack(r1, r2, v); // v is equal to [1 1 1 3 3] \endcode \sa stack(const vpRowVector &) \sa stack(const vpRowVector &, const vpRowVector &) */ void vpRowVector::stack(const vpRowVector &A, const vpRowVector &B, vpRowVector &C) { unsigned int nrA = A.getCols(); unsigned int nrB = B.getCols(); if (nrA == 0 && nrB == 0) { C.resize(0); return; } if (nrB == 0) { C = A; return; } if (nrA == 0) { C = B; return; } // General case C.resize(nrA + nrB); for (unsigned int i=0; i<nrA; i++) C[i] = A[i]; for (unsigned int i=0; i<nrB; i++) C[nrA+i] = B[i]; }
/*! \brief Initialisation of a the subRowVector \param v : parent row vector \param offset : offset where subRowVector start in the parent vector \param ncols : size of the subRowVector */ void vpSubRowVector::init(vpRowVector &v, const unsigned int & offset,const unsigned int & ncols){ if(!v.data){ vpERROR_TRACE("\n\t\t vpSubColvector parent vpRowVector has been destroyed"); throw(vpMatrixException(vpMatrixException::incorrectMatrixSizeError, "\n\t\t \n\t\t vpSubColvector parent vpRowVector has been destroyed")) ; } if(offset+ncols<=v.getCols()){ data=v.data+offset; rowNum=1; colNum = ncols; pColNum=v.getCols(); parent=&v; if(rowPtrs) free(rowPtrs); rowPtrs=(double**) malloc(1 * sizeof(double*)); for(unsigned int i=0;i<1;i++) rowPtrs[i]=v.data+i+offset; dsize = colNum ; trsize =0 ; }else{ vpERROR_TRACE("SubRowVector cannot be contain in parent RowVector") ; throw(vpMatrixException(vpMatrixException::incorrectMatrixSizeError,"SubRowVector cannot be contain in parent RowVector")) ; } }
/*! Multiply a column vector by a row vector. \param v : Row vector. \return The resulting matrix. */ vpMatrix vpColVector::operator*(const vpRowVector &v) const { vpMatrix M(rowNum, v.getCols()); for (unsigned int i=0; i<rowNum; i++) { for (unsigned int j=0; j<v.getCols(); j++) { M[i][j] = (*this)[i] * v[j]; } } return M; }
/*! Compute the mean value of all the elements of the vector. */ double vpRowVector::mean(const vpRowVector &v) { if (v.data == NULL) { throw(vpException(vpException::fatalError, "Cannot compute mean value of an empty row vector")); } double mean = 0; double *vd = v.data; for (unsigned int i = 0; i < v.getCols(); i++) mean += *(vd++); return mean / v.getCols(); }
/*! Operator that allows to add to row vectors that have the same size. \exception vpException::dimensionError If the vectors size differ. */ vpRowVector vpRowVector::operator+(const vpRowVector &v) const { if (getCols() != v.getCols() ) { throw(vpException(vpException::dimensionError, "Cannot add (1x%d) row vector to (1x%d) row vector", getCols(), v.getCols())) ; } vpRowVector r(colNum) ; for (unsigned int i=0;i<colNum;i++) r[i] = (*this)[i] + v[i]; return r; }
/*! Operator that allows to substract to row vectors that have the same size. \exception vpException::dimensionError If the vectors size differ. */ vpRowVector vpRowVector::operator-(const vpRowVector &m) const { if (getCols() != m.getCols() ) { throw(vpException(vpException::dimensionError, "Cannot substract (1x%d) row vector to (1x%d) row vector", getCols(), m.getCols())) ; } vpRowVector v(colNum) ; for (unsigned int i=0;i<colNum;i++) v[i] = (*this)[i] - m[i]; return v; }
/*! Initialize the row vector from a part of an input row vector \e v. \param v : Input row vector used for initialization. \param c : column index in \e v that corresponds to the first element of the row vector to contruct. \param ncols : Number of columns of the constructed row vector. The sub-vector starting from v[c] element and ending on v[c+ncols-1] element is used to initialize the contructed row vector. The following code shows how to use this function: \code #include <visp3/core/vpRowVector.h> int main() { vpRowVector v(4); int val = 0; for(size_t i=0; i<v.getCols(); i++) { v[i] = val++; } std::cout << "v: " << v << std::endl; vpRowVector w; w.init(v, 1, 2); std::cout << "w: " << w << std::endl; } \endcode It produces the following output: \code v: 0 1 2 3 w: 1 2 \endcode */ void vpRowVector::init(const vpRowVector &v, unsigned int c, unsigned int ncols) { unsigned int cncols = c+ncols ; if (cncols > v.getCols()) throw(vpException(vpException::dimensionError, "Bad column dimension (%d > %d) used to initialize vpRowVector", cncols, v.getCols())); resize(ncols); if (this->rowPtrs == NULL) // Fix coverity scan: explicit null dereferenced return; // Noting to do for (unsigned int i=0 ; i < ncols; i++) (*this)[i] = v[i+c]; }
/*! Insert a row vector. \param i : Index of the first element to introduce. This index starts from 0. \param v : Row vector to insert. The following example shows how to use this function: \code #include <visp/vpRowVector.h> int main() { vpRowVector v(4); for (unsigned int i=0; i < v.size(); i++) v[i] = i; std::cout << "v: " << v << std::endl; vpRowVector w(2); for (unsigned int i=0; i < w.size(); i++) w[i] = i+10; std::cout << "w: " << w << std::endl; v.insert(1, w); std::cout << "v: " << v << std::endl; } \endcode It produces the following output: \code v: 0 1 2 3 w: 10 11 v: 0 10 11 3 \endcode */ void vpRowVector::insert(unsigned int i, const vpRowVector &v) { if (i+v.size() > this->size()) throw(vpException(vpException::dimensionError, "Unable to insert (1x%d) row vector in (1x%d) row vector at position (%d)", v.getCols(), colNum, i)); for (unsigned int j=0; j < v.size(); j++) (*this)[i+j] = v[j]; }