57 #include <visp/vpMatrix.h>
58 #include <visp/vpMath.h>
59 #include <visp/vpHomography.h>
60 #include <visp/vpTranslationVector.h>
63 #include <visp/vpException.h>
64 #include <visp/vpMatrixException.h>
67 #include <visp/vpDebug.h>
82 #include <gsl/gsl_linalg.h>
85 #define DEBUG_LEVEL1 0
86 #define DEBUG_LEVEL2 0
162 unsigned int r,
unsigned int c,
163 unsigned int nrows,
unsigned int ncols)
166 if (((r + nrows) > m.
rowNum) || ((c + ncols) > m.
colNum))
170 "\n\t\t SubvpMatrix larger than vpMatrix")) ;
173 init(m,r,c,nrows,ncols);
200 const bool flagNullify)
205 if (flagNullify && this->
data != NULL)
206 { memset(this->
data,0,this->
dsize*
sizeof(
double)) ;}
210 const bool recopyNeeded = (ncols !=
this ->colNum);
211 double * copyTmp = NULL;
212 unsigned int rowTmp = 0, colTmp=0;
214 vpDEBUG_TRACE (25,
"Recopy case per case is required iff number of "
215 "cols has changed (structure of double array is not "
216 "the same in this case.");
217 if (recopyNeeded && this->
data != NULL)
219 copyTmp =
new double[this->
dsize];
220 memcpy (copyTmp,
this ->
data,
sizeof(
double)*this->
dsize);
225 this->
dsize = nrows*ncols;
226 this->
data = (
double*)realloc(this->
data, this->
dsize*
sizeof(
double));
227 if ((NULL == this->
data) && (0 != this->
dsize))
229 if (copyTmp != NULL)
delete [] copyTmp;
230 vpERROR_TRACE(
"\n\t\tMemory allocation error when allocating data") ;
232 "\n\t\t Memory allocation error when "
233 "allocating data")) ;
241 if (copyTmp != NULL)
delete [] copyTmp;
242 vpERROR_TRACE(
"\n\t\tMemory allocation error when allocating rowPtrs") ;
244 "\n\t\t Memory allocation error when "
245 "allocating rowPtrs")) ;
248 vpDEBUG_TRACE (25,
"Recomputation this->trsize array values.");
251 for (
unsigned int i=0; i<
dsize; i+=ncols) { *t_++ = this->
data + i; }
256 vpDEBUG_TRACE (25,
"Recopy of this->data array values or nullify.");
258 { memset(this->
data,0,this->
dsize*
sizeof(
double)) ;}
261 if (recopyNeeded && this->
rowPtrs != NULL)
264 const unsigned int minRow = (this->
rowNum<rowTmp)?this->
rowNum:rowTmp;
265 const unsigned int minCol = (this->
colNum<colTmp)?this->
colNum:colTmp;
266 for (
unsigned int i=0; i<this->
rowNum; ++i)
267 for (
unsigned int j=0; j<this->
colNum; ++j)
269 if ((minRow > i) && (minCol > j))
271 (*this)[i][j] = copyTmp [i*colTmp+j];
272 vpCDEBUG (25) << i <<
"x" << j <<
"<- " << i*colTmp+j
273 <<
"=" << copyTmp [i*colTmp+j] << std::endl;
275 else {(*this)[i][j] = 0;}
278 else {
vpDEBUG_TRACE (25,
"Nothing to do: already done by realloc.");}
281 if (copyTmp != NULL)
delete [] copyTmp;
333 unsigned int rnrows = r+nrows ;
334 unsigned int cncols = c+ncols ;
338 "Bad row dimension (%d > %d) used to initialize vpMatrix", rnrows, M.
getRows()));
341 "Bad column dimension (%d > %d) used to initialize vpMatrix", cncols, M.
getCols()));
346 for (
unsigned int i=r ; i < rnrows; i++)
347 for (
unsigned int j=c ; j < cncols; j++)
348 (*
this)[i-r][j-c] = M[i][j] ;
395 std::cout << me << std::endl ;
415 for(
unsigned int i=0; i<3; i++)
416 for(
unsigned int j=0; j<3; j++)
417 (*
this)[i][j] = H[i][j];
438 for (
unsigned int i=0;i<
rowNum;i++)
439 for(
unsigned int j=0;j<
colNum;j++)
458 for (
unsigned int i=0; i<
rowNum; i++) {
459 for (
unsigned int j=0; j<
colNum; j++) {
489 std::cout << me << std::endl ;
495 vpERROR_TRACE(
"\n\t\tvpMatrix mismatch in vpMatrix/vpMatrix multiply") ;
497 "\n\t\tvpMatrix mismatch in "
498 "vpMatrix/vpMatrix multiply")) ;
502 unsigned int BcolNum = B.
colNum;
503 unsigned int BrowNum = B.
rowNum;
508 double *rowptri = A.
rowPtrs[i];
510 for (j=0;j<BcolNum;j++)
513 for (k=0;k<BrowNum;k++) s += rowptri[k] * BrowPtrs[k][j];
542 for (
unsigned int i=0;i<
rowNum;i++){
543 for (
unsigned int j=0;j<3;j++) {
545 for (
unsigned int k=0;k<3;k++) s += (*
this)[i][k] * H[k][j];
571 std::cout << me << std::endl ;
577 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix/vpMatrix addition") ;
579 "\n\t\t vpMatrix mismatch in "
580 "vpMatrix/vpMatrix addition")) ;
587 for (
unsigned int i=0;i<A.
rowNum;i++)
588 for(
unsigned int j=0;j<A.
colNum;j++)
589 CrowPtrs[i][j] = wB*BrowPtrs[i][j]+wA*ArowPtrs[i][j];
611 std::cout << me << std::endl ;
617 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix/vpMatrix addition") ;
619 "\n\t\t vpMatrix mismatch in "
620 "vpMatrix/vpMatrix addition")) ;
637 for (
unsigned int i=0;i<A.
rowNum;i++)
638 for(
unsigned int j=0;j<A.
colNum;j++)
641 CrowPtrs[i][j] = BrowPtrs[i][j]+ArowPtrs[i][j];
680 std::cout << me << std::endl ;
686 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix/vpMatrix substraction") ;
688 "\n\t\t vpMatrix mismatch in "
689 "vpMatrix/vpMatrix substraction")) ;
709 for (
unsigned int i=0;i<A.
rowNum;i++)
710 for(
unsigned int j=0;j<A.
colNum;j++)
712 CrowPtrs[i][j] = ArowPtrs[i][j]-BrowPtrs[i][j];
737 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix += addition") ;
739 "\n\t\t vpMatrix mismatch in "
740 "vpMatrix += addition")) ;
758 for (
unsigned int i=0;i<
rowNum;i++)
759 for(
unsigned int j=0;j<
colNum;j++)
760 rowPtrs[i][j] += BrowPtrs[i][j];
777 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix -= substraction") ;
779 "\n\t\t vpMatrix mismatch in "
780 "vpMatrix -= substraction")) ;
797 for (
unsigned int i=0;i<
rowNum;i++)
798 for(
unsigned int j=0;j<
colNum;j++)
799 rowPtrs[i][j] -= BrowPtrs[i][j];
827 std::cout << me << std::endl ;
847 for (
unsigned int i=0;i<A.
rowNum;i++)
848 for(
unsigned int j=0;j<A.
colNum;j++)
849 CrowPtrs[i][j]= -ArowPtrs[i][j];
870 double sum_square=0.0;
890 for (
unsigned int i=0;i<
rowNum;i++)
891 for(
unsigned int j=0;j<
colNum;j++)
906 for (
unsigned int i=0;i<
rowNum;i++)
908 for(
unsigned int j=0;j<
colNum;j++)
935 vpERROR_TRACE(
"vpMatrix mismatch in vpMatrix/vector multiply") ;
946 std::cout << me << std::endl ;
951 for (
unsigned int j=0;j<A.
colNum;j++)
954 for (
unsigned int i=0;i<A.
rowNum;i++)
981 vpERROR_TRACE(
"vpMatrix mismatch in vpMatrix::operator*(const vpTranslationVector)") ;
985 for (
unsigned int j=0;j<3;j++) c[j]=0 ;
987 for (
unsigned int j=0;j<3;j++) {
990 for (
unsigned int i=0;i<3;i++) {
1017 std::cout << me << std::endl ;
1022 unsigned int Brow = B.
getRows() ;
1023 unsigned int Bcol = B.
getCols() ;
1038 for (
unsigned int i=0;i<Brow;i++)
1039 for(
unsigned int j=0;j<Bcol;j++)
1060 std::cout << me << std::endl ;
1075 for (
unsigned int i=0;i<
rowNum;i++)
1076 for(
unsigned int j=0;j<
colNum;j++)
1077 CrowPtrs[i][j]=
rowPtrs[i][j]*x;
1100 if (std::fabs(x) <= std::numeric_limits<double>::epsilon()) {
1118 for (
unsigned int i=0;i<
rowNum;i++)
1119 for(
unsigned int j=0;j<
colNum;j++)
1143 for (
unsigned int i=0;i<
rowNum;i++)
1144 for(
unsigned int j=0;j<
colNum;j++)
1168 for (
unsigned int i=0;i<
rowNum;i++)
1169 for(
unsigned int j=0;j<
colNum;j++)
1185 for (
unsigned int i=0;i<
rowNum;i++)
1186 for(
unsigned int j=0;j<
colNum;j++)
1196 if (std::fabs(x) <= std::numeric_limits<double>::epsilon())
1211 for (
unsigned int i=0;i<
rowNum;i++)
1212 for(
unsigned int j=0;j<
colNum;j++)
1241 if (i==j) (*this)[i][j] = val ;
else (*
this)[i][j] = 0;
1285 for (
unsigned int i=0; i<
rowNum; i++)
1286 for (
unsigned int j=0; j<
colNum; j++)
1287 if (i == j) (*this)[i][j] = 1;
1288 else (*
this)[i][j] = 0;
1313 double *coli = (*this)[i] ;
1351 double ** AtRowPtrs = At.
rowPtrs;
1353 for(
unsigned int i = 0; i <
colNum; i++ )
1355 double * row_ = AtRowPtrs[i];
1357 for(
unsigned int j = 0; j <
rowNum; j++, col+=A_step )
1401 for(
unsigned int i=0;i<
rowNum;i++){
1402 for(
unsigned int j=i;j<
rowNum;j++){
1408 for(
unsigned int k=0; k <
colNum ;k++)
1409 ssum += *(pi++)* *(pj++);
1456 s +=(*(ptr+i)) * (*(ptr+j));
1466 s +=(*(ptr+i)) * (*(ptr+i));
1505 double *optr=out.
data;
1506 for(
unsigned int j =0;j<
colNum ; j++){
1507 for(
unsigned int i =0;i<
rowNum ; i++){
1541 double *optr=out.
data;
1542 for(
unsigned int i =0;i<
dsize ; i++){
1543 *(optr++)=*(mdata++);
1564 unsigned int r1= m1.
getRows();
1565 unsigned int c1= m1.
getCols();
1566 unsigned int r2= m2.
getRows();
1567 unsigned int c2= m2.
getCols();
1572 vpERROR_TRACE(
"Kronecker prodect bad dimension of output vpMatrix") ;
1576 for(
unsigned int r =0;r<r1 ; r++){
1577 for(
unsigned int c =0;c<c1 ; c++){
1578 double alpha = m1[r][c];
1579 double *m2ptr = m2[0];
1580 unsigned int roffset= r*r2;
1581 unsigned int coffset= c*c2;
1582 for(
unsigned int rr =0;rr<r2 ; rr++){
1583 for(
unsigned int cc =0;cc<c2 ;cc++){
1584 out[roffset+rr][coffset+cc]= alpha* *(m2ptr++);
1609 unsigned int r1= m1.
getRows();
1610 unsigned int c1= m1.
getCols();
1611 unsigned int r2= m2.
getRows();
1612 unsigned int c2= m2.
getCols();
1616 for(
unsigned int r =0;r<r1 ; r++){
1617 for(
unsigned int c =0;c<c1 ; c++){
1618 double alpha = m1[r][c];
1619 double *m2ptr = m2[0];
1620 unsigned int roffset= r*r2;
1621 unsigned int coffset= c*c2;
1622 for(
unsigned int rr =0;rr<r2 ; rr++){
1623 for(
unsigned int cc =0;cc<c2 ;cc++){
1624 out[roffset+rr ][coffset+cc]= alpha* *(m2ptr++);
1639 return kron(*
this,m);
1824 #if (DEBUG_LEVEL1 == 0)
1829 #if defined (VISP_HAVE_LAPACK)
1831 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
1833 #elif defined (VISP_HAVE_GSL)
1844 unsigned int i,j,k,nrows,ncols;
1851 #ifdef VISP_HAVE_GSL
1860 Asvd.
resize(nrows,ncols);
1862 for (i = 0 ; i < nrows ; i++)
1864 for (j = 0 ; j < ncols ; j++)
1867 for (k=0 ; k < ncols ; k++) Asvd[i][j] += (*
this)[i][k]*w[k]*v[j][k];
1870 for (i=0;i<nrows;i++)
1872 for (j=0;j<ncols;j++)
if (fabs(A[i][j]-Asvd[i][j]) > 1e-6) pb = 1;
1876 printf(
"pb in SVD\n");
1877 std::cout <<
" A : " << std::endl << A << std::endl;
1878 std::cout <<
" Asvd : " << std::endl << Asvd << std::endl;
1993 unsigned int i, j, k ;
1995 unsigned int nrows, ncols;
1996 unsigned int nrows_orig =
getRows() ;
1997 unsigned int ncols_orig =
getCols() ;
1998 Ap.
resize(ncols_orig,nrows_orig) ;
2000 if (nrows_orig >= ncols_orig)
2016 if (nrows_orig >= ncols_orig) a = *
this;
2017 else a = (*this).
t();
2023 for (i=0 ; i < ncols ; i++)
2024 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
2026 unsigned int rank = 0 ;
2027 for (i=0 ; i < ncols ; i++)
2028 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
2033 for (i = 0 ; i < ncols ; i++)
2035 for (j = 0 ; j < nrows ; j++)
2039 for (k=0 ; k < ncols ; k++)
2040 if (fabs(sv[k]) > maxsv*svThreshold)
2042 a1[i][j] += v[i][k]*a[j][k]/sv[k];
2046 if (nrows_orig >= ncols_orig) Ap = a1;
2049 if (nrows_orig >= ncols_orig)
2052 imAt.
resize(ncols_orig,rank) ;
2053 for (i=0 ; i < ncols_orig ; i++)
2054 for (j=0 ; j < rank ; j++)
2055 imAt[i][j] = v[i][j] ;
2058 imA.
resize(nrows_orig,rank) ;
2059 for (i=0 ; i < nrows_orig ; i++)
2060 for (j=0 ; j < rank ; j++)
2061 imA[i][j] = a[i][j] ;
2066 imAt.
resize(ncols_orig,rank) ;
2067 for (i=0 ; i < ncols_orig ; i++)
2068 for (j=0 ; j < rank ; j++)
2069 imAt[i][j] = a[i][j] ;
2071 imA.
resize(nrows_orig,rank) ;
2072 for (i=0 ; i < nrows_orig ; i++)
2073 for (j=0 ; j < rank ; j++)
2074 imA[i][j] = v[i][j] ;
2081 vpMatrix A, ApA, AAp, AApA, ApAAp ;
2094 for (i=0;i<nrows;i++)
2096 for (j=0;j<ncols;j++)
if (fabs(AApA[i][j]-A[i][j]) > 1e-6) pb = 1;
2098 for (i=0;i<ncols;i++)
2100 for (j=0;j<nrows;j++)
if (fabs(ApAAp[i][j]-Ap[i][j]) > 1e-6) pb = 1;
2102 for (i=0;i<nrows;i++)
2104 for (j=0;j<nrows;j++)
if (fabs(AAp[i][j]-AAp[j][i]) > 1e-6) pb = 1;
2106 for (i=0;i<ncols;i++)
2108 for (j=0;j<ncols;j++)
if (fabs(ApA[i][j]-ApA[j][i]) > 1e-6) pb = 1;
2112 printf(
"pb in pseudo inverse\n");
2113 std::cout <<
" A : " << std::endl << A << std::endl;
2114 std::cout <<
" Ap : " << std::endl << Ap << std::endl;
2115 std::cout <<
" A - AApA : " << std::endl << A - AApA << std::endl;
2116 std::cout <<
" Ap - ApAAp : " << std::endl << Ap - ApAAp << std::endl;
2117 std::cout <<
" AAp - (AAp)^T : " << std::endl << AAp - AAp.
t() << std::endl;
2118 std::cout <<
" ApA - (ApA)^T : " << std::endl << ApA - ApA.
t() << std::endl;
2172 unsigned int i, j, k ;
2174 unsigned int nrows, ncols;
2175 unsigned int nrows_orig =
getRows() ;
2176 unsigned int ncols_orig =
getCols() ;
2177 Ap.
resize(ncols_orig,nrows_orig) ;
2179 if (nrows_orig >= ncols_orig)
2195 if (nrows_orig >= ncols_orig) a = *
this;
2196 else a = (*this).
t();
2202 for (i=0 ; i < ncols ; i++)
2203 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
2205 unsigned int rank = 0 ;
2206 for (i=0 ; i < ncols ; i++)
2207 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
2212 for (i = 0 ; i < ncols ; i++)
2214 for (j = 0 ; j < nrows ; j++)
2218 for (k=0 ; k < ncols ; k++)
2219 if (fabs(sv[k]) > maxsv*svThreshold)
2221 a1[i][j] += v[i][k]*a[j][k]/sv[k];
2225 if (nrows_orig >= ncols_orig) Ap = a1;
2228 if (nrows_orig >= ncols_orig)
2231 imAt.
resize(ncols_orig,rank) ;
2232 for (i=0 ; i < ncols_orig ; i++)
2233 for (j=0 ; j < rank ; j++)
2234 imAt[i][j] = v[i][j] ;
2237 imA.
resize(nrows_orig,rank) ;
2238 for (i=0 ; i < nrows_orig ; i++)
2239 for (j=0 ; j < rank ; j++)
2240 imA[i][j] = a[i][j] ;
2245 imAt.
resize(ncols_orig,rank) ;
2246 for (i=0 ; i < ncols_orig ; i++)
2247 for (j=0 ; j < rank ; j++)
2248 imAt[i][j] = a[i][j] ;
2250 imA.
resize(nrows_orig,rank) ;
2251 for (i=0 ; i < nrows_orig ; i++)
2252 for (j=0 ; j < rank ; j++)
2253 imA[i][j] = v[i][j] ;
2257 vpMatrix cons(ncols_orig, ncols_orig);
2260 for (j = 0; j < ncols_orig; j++)
2262 for (i = 0; i < ncols_orig; i++)
2264 if (fabs(sv[i]) <= maxsv*svThreshold)
2266 cons[i][j] = v[j][i];
2271 vpMatrix Ker (ncols_orig-rank, ncols_orig);
2273 for (j = 0; j < ncols_orig ; j++)
2276 if ( std::fabs(cons.
getRow(j).
sumSquare()) > std::numeric_limits<double>::epsilon())
2278 for (i = 0; i < cons.
getCols(); i++)
2279 Ker[k][i] = cons[j][i];
2289 vpMatrix A, ApA, AAp, AApA, ApAAp ;
2302 for (i=0;i<nrows;i++)
2304 for (j=0;j<ncols;j++)
if (fabs(AApA[i][j]-A[i][j]) > 1e-6) pb = 1;
2306 for (i=0;i<ncols;i++)
2308 for (j=0;j<nrows;j++)
if (fabs(ApAAp[i][j]-Ap[i][j]) > 1e-6) pb = 1;
2310 for (i=0;i<nrows;i++)
2312 for (j=0;j<nrows;j++)
if (fabs(AAp[i][j]-AAp[j][i]) > 1e-6) pb = 1;
2314 for (i=0;i<ncols;i++)
2316 for (j=0;j<ncols;j++)
if (fabs(ApA[i][j]-ApA[j][i]) > 1e-6) pb = 1;
2320 printf(
"pb in pseudo inverse\n");
2321 std::cout <<
" A : " << std::endl << A << std::endl;
2322 std::cout <<
" Ap : " << std::endl << Ap << std::endl;
2323 std::cout <<
" A - AApA : " << std::endl << A - AApA << std::endl;
2324 std::cout <<
" Ap - ApAAp : " << std::endl << Ap - ApAAp << std::endl;
2325 std::cout <<
" AAp - (AAp)^T : " << std::endl << AAp - AAp.
t() << std::endl;
2326 std::cout <<
" ApA - (ApA)^T : " << std::endl << ApA - ApA.
t() << std::endl;
2327 std::cout <<
" KerA : " << std::endl << kerA << std::endl;
2338 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
2350 for (
unsigned int j =0 ; j <
getCols() ; j++) c[j] = (*
this)[i-1][j] ;
2366 for (
unsigned int i =0 ; i <
getRows() ; i++) c[i] = (*
this)[i][j-1] ;
2413 vpMatrix::getCol(
const unsigned int j,
const unsigned int i_begin,
const unsigned int column_size)
const
2418 for (
unsigned int i=0 ; i < column_size ; i++)
2419 c[i] = (*
this)[i_begin+i][j];
2467 unsigned int nb_rows =
getRows();
2469 for (
unsigned int i=0 ; i < nb_rows ; i++)
2470 c[i] = (*
this)[i][j];
2514 unsigned int nb_cols =
getCols();
2516 for (
unsigned int j=0 ; j < nb_cols ; j++)
2517 r[j] = (*
this)[i][j];
2559 vpMatrix::getRow(
const unsigned int i,
const unsigned int j_begin,
const unsigned int row_size)
const
2564 for (
unsigned int j=0 ; j < row_size ; j++)
2565 r[j] = (*
this)[i][j_begin+i];
2612 unsigned int nra = A.
getRows() ;
2613 unsigned int nrb = B.
getRows() ;
2620 "\n\t\t incorrect matrices size")) ;
2634 for (i=0 ; i < nra ; i++)
2635 for (j=0 ; j < A.
getCols() ; j++)
2639 for (i=0 ; i < nrb ; i++)
2640 for (j=0 ; j < B.
getCols() ; j++)
2642 C[i+nra][j] = B[i][j] ;
2667 const unsigned int r,
const unsigned int c)
2698 const unsigned int r,
const unsigned int c)
2711 for(
unsigned int i=0; i<A.
getRows(); i++){
2712 for(
unsigned int j=0; j<A.
getCols(); j++){
2713 if(i >= r && i < (r + B.
getRows()) && j >= c && j < (c+B.
getCols())){
2714 C[i][j] = B[i-r][j-c];
2724 "\n\t\tIncorrect size of the matrix to insert data.");
2771 unsigned int nca = A.
getCols() ;
2772 unsigned int ncb = B.
getCols() ;
2779 "\n\t\t incorrect matrices size")) ;
2793 for (i=0 ; i < C.
getRows(); i++)
2794 for (j=0 ; j < nca ; j++)
2798 for (i=0 ; i < C.
getRows() ; i++)
2799 for (j=0 ; j < ncb ; j++)
2801 C[i][nca+j] = B[i][j] ;
2843 unsigned int rows = A.
getRows() ;
2845 this->
resize(rows,rows) ;
2854 for (
unsigned int i=0 ; i< rows ; i++ )
2855 (*
this)[i][i] = A[i] ;
2871 unsigned int rows = A.
getRows() ;
2882 for (
unsigned int i=0 ; i< rows ; i++ )
2896 std::ios_base::fmtflags original_flags = s.flags();
2899 for (
unsigned int i=0;i<m.
getRows();i++) {
2900 for (
unsigned int j=0;j<m.
getCols();j++){
2901 s << m[i][j] <<
" ";
2908 s.flags(original_flags);
2939 typedef std::string::size_type size_type;
2944 std::vector<std::string> values(m*n);
2945 std::ostringstream oss;
2946 std::ostringstream ossFixed;
2947 std::ios_base::fmtflags original_flags = oss.flags();
2950 ossFixed.setf ( std::ios::fixed, std::ios::floatfield );
2952 size_type maxBefore=0;
2953 size_type maxAfter=0;
2955 for (
unsigned int i=0;i<m;++i) {
2956 for (
unsigned int j=0;j<n;++j){
2958 oss << (*this)[i][j];
2959 if (oss.str().find(
"e")!=std::string::npos){
2961 ossFixed << (*this)[i][j];
2962 oss.str(ossFixed.str());
2965 values[i*n+j]=oss.str();
2966 size_type thislen=values[i*n+j].size();
2967 size_type p=values[i*n+j].find(
'.');
2969 if (p==std::string::npos){
2979 size_type totalLength=length;
2983 maxAfter=std::min(maxAfter, totalLength-maxBefore);
2984 if (maxAfter==1) maxAfter=0;
2989 if (intro) s <<intro;
2990 s <<
"["<<m<<
","<<n<<
"]=\n";
2992 for (
unsigned int i=0;i<m;i++) {
2994 for (
unsigned int j=0;j<n;j++){
2995 size_type p=values[i*n+j].find(
'.');
2996 s.setf(std::ios::right, std::ios::adjustfield);
2997 s.width((std::streamsize)maxBefore);
2998 s <<values[i*n+j].substr(0,p).c_str();
3001 s.setf(std::ios::left, std::ios::adjustfield);
3002 if (p!=std::string::npos){
3003 s.width((std::streamsize)maxAfter);
3004 s <<values[i*n+j].substr(p,maxAfter).c_str();
3007 s.width((std::streamsize)maxAfter);
3017 s.flags(original_flags);
3019 return (
int)(maxBefore+maxAfter);
3038 for (i=0; i < this->
getRows(); ++ i)
3040 for (j=0; j <
this ->getCols(); ++ j)
3042 os << (*this)[i][j] <<
", ";
3044 if (
this ->
getRows() != i+1) { os <<
";" << std::endl; }
3045 else { os <<
"]" << std::endl; }
3065 os <<
"([ " << std::endl;
3066 for (i=0; i < this->
getRows(); ++ i)
3068 for (j=0; j < this->
getCols(); ++ j)
3070 os << (*this)[i][j] <<
", ";
3072 os <<
"]," << std::endl;
3074 os <<
"])" << std::endl;
3091 for (i=0; i < this->
getRows(); ++ i)
3093 for (j=0; j < this->
getCols(); ++ j)
3095 os << (*this)[i][j];
3096 if (!(j==(this->
getCols()-1)))
3120 cppPrint(std::ostream & os,
const char * matrixName,
bool octet)
const
3124 const char defaultName [] =
"A";
3125 if (NULL == matrixName)
3127 matrixName = defaultName;
3129 os <<
"vpMatrix " << defaultName
3130 <<
" (" <<
this ->getRows ()
3131 <<
", " <<
this ->getCols () <<
"); " <<std::endl;
3133 for (i=0; i < this->
getRows(); ++ i)
3135 for (j=0; j <
this ->getCols(); ++ j)
3139 os << defaultName <<
"[" << i <<
"][" << j
3140 <<
"] = " << (*this)[i][j] <<
"; " << std::endl;
3144 for (
unsigned int k = 0; k <
sizeof(double); ++ k)
3146 os <<
"((unsigned char*)&(" << defaultName
3147 <<
"[" << i <<
"][" << j <<
"]) )[" << k
3148 <<
"] = 0x" <<std::hex<<
3149 (
unsigned int)((
unsigned char*)& ((*this)[i][j])) [k]
3150 <<
"; " << std::endl;
3172 for (
unsigned int i=0;i<
dsize;i++) {
3173 x = *(
data +i); norm += x*x;
3196 for (
unsigned int i=0;i<
rowNum;i++){
3198 for (
unsigned int j=0; j<
colNum;j++){
3199 x += fabs (*(*(
rowPtrs + i)+j)) ;
3216 double vpMatrix::detByLU()
const
3233 unsigned int * perm =
new unsigned int[
rowNum];
3235 tmp.LUDcmp(perm, d);
3240 for(
unsigned int i=0;i<
rowNum;i++)
3247 vpERROR_TRACE(
"Determinant Computation : ERR Matrix not squared") ;
3249 "\n\t\tDeterminant Computation : ERR Matrix not squared")) ;
3292 const unsigned int c)
3296 for(
unsigned int i=r; i<(r+A.
getRows()); i++){
3297 for(
unsigned int j=c; j<(c+A.
getCols()); j++){
3298 (*this)[i][j] = A[i-r][j-c];
3304 "\n\t\tIncorrect size of the matrix to insert data.");
3351 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not square") ;
3353 "\n\t\tEigen values computation: ERR Matrix not square")) ;
3356 #ifdef VISP_HAVE_GSL
3360 for (
unsigned int i=0; i <
rowNum; i++) {
3361 for (
unsigned int j=0; j <
rowNum; j++) {
3363 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
3364 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not symmetric") ;
3366 "\n\t\tEigen values computation: "
3367 "ERR Matrix not symmetric")) ;
3375 gsl_vector *eval = gsl_vector_alloc (rowNum);
3376 gsl_matrix *evec = gsl_matrix_alloc (rowNum,
colNum);
3378 gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (rowNum);
3379 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
3381 unsigned int Atda = m->tda ;
3382 for (
unsigned int i=0 ; i <
rowNum ; i++){
3383 unsigned int k = i*Atda ;
3384 for (
unsigned int j=0 ; j <
colNum ; j++)
3385 m->data[k+j] = (*
this)[i][j] ;
3387 gsl_eigen_symmv (m, eval, evec, w);
3389 gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
3391 for (
unsigned int i=0; i <
rowNum; i++) {
3392 evalue[i] = gsl_vector_get (eval, i);
3395 gsl_eigen_symmv_free (w);
3396 gsl_vector_free (eval);
3397 gsl_matrix_free (m);
3398 gsl_matrix_free (evec);
3404 vpERROR_TRACE(
"Not implemented since the GSL library is not detected.") ;
3406 "\n\t\tEigen values Computation: Not implemented "
3407 "since the GSL library is not detected")) ;
3466 #ifdef VISP_HAVE_GSL
3473 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not square") ;
3475 "\n\t\tEigen values computation: ERR Matrix not square")) ;
3478 #ifdef VISP_HAVE_GSL
3482 for (
unsigned int i=0; i <
rowNum; i++) {
3483 for (
unsigned int j=0; j <
rowNum; j++) {
3485 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
3486 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not symmetric") ;
3488 "\n\t\tEigen values computation: "
3489 "ERR Matrix not symmetric")) ;
3498 gsl_vector *eval = gsl_vector_alloc (rowNum);
3499 gsl_matrix *evec = gsl_matrix_alloc (rowNum,
colNum);
3501 gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (rowNum);
3502 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
3504 unsigned int Atda = m->tda ;
3505 for (
unsigned int i=0 ; i <
rowNum ; i++){
3506 unsigned int k = i*Atda ;
3507 for (
unsigned int j=0 ; j <
colNum ; j++)
3508 m->data[k+j] = (*
this)[i][j] ;
3510 gsl_eigen_symmv (m, eval, evec, w);
3512 gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
3514 for (
unsigned int i=0; i <
rowNum; i++) {
3515 evalue[i] = gsl_vector_get (eval, i);
3518 for (
unsigned int i=0; i <
rowNum; i++) {
3519 unsigned int k = i*Atda ;
3520 for (
unsigned int j=0; j <
rowNum; j++) {
3521 evector[i][j] = evec->
data[k+j];
3543 gsl_eigen_symmv_free (w);
3544 gsl_vector_free (eval);
3545 gsl_matrix_free (m);
3546 gsl_matrix_free (evec);
3550 vpERROR_TRACE(
"Not implemented since the GSL library is not detected.") ;
3552 "\n\t\tEigen values Computation: Not implemented "
3553 "since the GSL library is not detected")) ;
3572 std::cout <<
"begin Kernel" << std::endl ;
3575 unsigned int ncaptor =
getRows() ;
3576 unsigned int ddl =
getCols() ;
3580 std::cout <<
"Erreur Matrice non initialise" << std::endl ;
3584 std::cout <<
"Interaction matrix L" << std::endl ;
3585 std::cout << *this ;
3586 std::cout <<
"signaux capteurs : " << ncaptor << std::endl ;
3593 if (ncaptor > ddl) min = ddl ;
else min = ncaptor ;
3614 for (i=0 ; i < ncaptor ; i++)
3615 for (j=0 ; j < ddl ; j++)
3617 a1[i][j] = (*this)[i][j] ;
3618 a2[i][j] = (*this)[i][j] ;
3624 for (i=ncaptor ; i < ddl ; i++)
3625 for (j=0 ; j < ddl ; j++)
3639 for (i=0 ; i < ddl ; i++)
3640 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
3644 std::cout <<
"Singular Value : (" ;
3645 for (i=0 ; i < ddl ; i++) std::cout << sv[i] <<
" , " ;
3646 std::cout <<
")" << std::endl ;
3650 unsigned int rank = 0 ;
3651 for (i=0 ; i < ddl ; i++)
3652 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
3657 for (i = 0 ; i < ddl ; i++)
3659 for (j = 0 ; j < ncaptor ; j++)
3667 for (k=0 ; k < ddl ; k++) if (fabs(sv[k]) > maxsv*svThreshold)
3669 C[i][j] += v[i][k]*a1[j][k]/sv[k];
3675 std::cout << C << std::endl ;
3692 for (i=0 ; i < ddl ; i++)
3693 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
3695 for (i=0 ; i < ddl ; i++)
3696 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
3700 for (j = 0 ; j < ddl ; j++)
3702 for (i = 0 ; i < ddl ; i++)
3705 if (fabs(sv[i]) <= maxsv*svThreshold)
3707 cons[i][j] = v[j][i];
3713 for (j = 0 ; j < ddl ; j++)
3718 if (std::fabs(cons.
getRow(j).
sumSquare()) > std::numeric_limits<double>::epsilon())
3720 for (i=0 ; i < cons.
getCols() ; i++)
3721 Ker[k][i] = cons[j][i] ;
3730 std::cout <<
"end Kernel" << std::endl ;
3771 det_ = this->detByLU();
3793 const bool binary,
const char *header)
3798 file.open(filename, std::fstream::out);
3800 file.open(filename, std::fstream::out|std::fstream::binary);
3814 while (header[i] !=
'\0')
3817 if (header[i] ==
'\n')
3824 file << M << std::endl;
3829 while (header[headerSize] !=
'\0') headerSize++;
3830 file.write(header,headerSize+1);
3831 unsigned int matrixSize;
3833 file.write((
char*)&matrixSize,
sizeof(
int));
3835 file.write((
char*)&matrixSize,
sizeof(
int));
3837 for(
unsigned int i = 0; i < M.
getRows(); i++)
3839 for(
unsigned int j = 0; j < M.
getCols(); j++)
3842 file.write((
char*)&value,
sizeof(
double));
3896 file.open(filename, std::fstream::out);
3905 bool inIndent =
false;
3906 std::string indent =
"";
3907 bool checkIndent =
true;
3908 while (header[i] !=
'\0')
3915 if(header[i] ==
' ')
3917 else if (indent.length() > 0)
3918 checkIndent =
false;
3920 if (header[i] ==
'\n' || (inIndent && header[i] ==
' '))
3930 file <<
"rows: " << M.
getRows() << std::endl;
3931 file <<
"cols: " << M.
getCols() << std::endl;
3933 if(indent.length() == 0)
3936 file <<
"data: " << std::endl;
3940 file << indent <<
"- [";
3942 file << M[i][j] <<
", ";
3943 file << M[i][j] <<
"]" << std::endl;
3969 file.open(filename, std::fstream::in);
3971 file.open(filename, std::fstream::in|std::fstream::binary);
3985 while ((c !=
'\0') && (c !=
'\n'))
3991 strncpy(header, h.c_str(), h.size() + 1);
3993 unsigned int rows, cols;
3997 if (rows > std::numeric_limits<unsigned int>::max()
3998 || cols > std::numeric_limits<unsigned int>::max())
4004 for(
unsigned int i = 0; i < rows; i++)
4006 for(
unsigned int j = 0; j < cols; j++)
4017 while ((c !=
'\0') && (c !=
'\n'))
4023 strncpy(header, h.c_str(), h.size() + 1);
4025 unsigned int rows, cols;
4026 file.read((
char*)&rows,
sizeof(
unsigned int));
4027 file.read((
char*)&cols,
sizeof(
unsigned int));
4031 for(
unsigned int i = 0; i < rows; i++)
4033 for(
unsigned int j = 0; j < cols; j++)
4035 file.read((
char*)&value,
sizeof(
double));
4064 file.open(filename, std::fstream::in);
4072 unsigned int rows = 0,cols = 0;
4074 std::string line,subs;
4075 bool inheader =
true;
4076 unsigned int i=0, j;
4077 unsigned int lineStart = 0;
4079 while ( getline (file,line) )
4083 if(rows == 0 && line.compare(0,5,
"rows:") == 0)
4085 std::stringstream ss(line);
4089 else if (cols == 0 && line.compare(0,5,
"cols:") == 0)
4091 std::stringstream ss(line);
4095 else if (line.compare(0,5,
"data:") == 0)
4105 if(rows == 0 || cols == 0)
4112 lineStart = (
unsigned int)line.find(
"[") + 1;
4114 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
4116 while(getline(ss, subs,
','))
4117 M[i][j++] = atof(subs.c_str());
4123 strncpy(header, h.substr(0,h.length()-1).c_str(), h.size());
4141 vpTRACE(
"The matrix must be square");
4143 "The matrix must be square" ));
4147 #ifdef VISP_HAVE_GSL
4149 double *b =
new double [size_];
4150 for (
size_t i=0; i< size_; i++)
4152 gsl_matrix_view m = gsl_matrix_view_array(this->
data,
rowNum, colNum);
4153 gsl_matrix_view em = gsl_matrix_view_array(b,
rowNum, colNum);
4154 gsl_linalg_exponential_ss(&m.matrix, &em.matrix, 0);
4157 memcpy(expA.
data, b, size_ *
sizeof(
double));
4178 for (
unsigned int i = 0; i <
rowNum;i++)
4181 for (
unsigned int j=0; j <
colNum; j++)
4183 sum += fabs((*
this)[i][j]);
4195 double sca = 1.0 / pow(2.0,s);
4200 for (
int k=2;k<=q;k++)
4202 c = c * ((double)(q-k+1)) / ((
double)(k*(2*q-k+1)));
4207 if (p) _expD=_expD+_expcX;
4208 else _expD=_expD- _expcX;
4213 for (
int k=1;k<=s;k++)
4226 double *dataptr =
data;
4227 double min = *dataptr;
4229 for (
unsigned int i = 0; i <
dsize-1; i++)
4231 if (*dataptr < min) min = *dataptr;
4240 double *dataptr =
data;
4241 double max = *dataptr;
4243 for (
unsigned int i = 0; i <
dsize-1; i++)
4245 if (*dataptr > max) max = *dataptr;
4275 for (
unsigned int i = 0 ; i < col ; i++)
4277 for (
unsigned int j = 0 ; j <
row ; j++)
4278 M_comp[i][j]=M[i][j];
4279 for (
unsigned int j = row+1 ; j < M.
getRows() ; j++)
4280 M_comp[i][j-1]=M[i][j];
4282 for (
unsigned int i = col+1 ; i < M.
getCols(); i++)
4284 for (
unsigned int j = 0 ; j <
row ; j++)
4285 M_comp[i-1][j]=M[i][j];
4286 for (
unsigned int j = row+1 ; j < M.
getRows() ; j++)
4287 M_comp[i-1][j-1]=M[i][j];
4306 for(
unsigned int i=0;i<M.
getCols();i++)
4308 if(min>w[i])min=w[i];
4309 if(max<w[i])max=w[i];
4324 vpTRACE(
"The matrix must be square");
4326 "The matrix must be square" ));
4330 for(
unsigned int i=0;i<H.
getCols();i++)
4332 for(
unsigned int j=0;j<H.
getCols();j++)
4336 HLM[i][j]+= alpha*H[i][j];
vpMatrix operator*(const double &x, const vpMatrix &B)
friend VISP_EXPORT std::ostream & operator<<(std::ostream &s, const vpMatrix &m)
std::cout a matrix
std::ostream & csvPrint(std::ostream &os) const
Print matrix in csv format.
Definition of the vpMatrix class.
unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6)
static vpMatrix juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B)
static bool loadMatrix(std::string filename, vpMatrix &M, const bool binary=false, char *Header=NULL)
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
vp_deprecated vpRowVector row(const unsigned int i)
double infinityNorm() const
static bool saveMatrixYAML(std::string filename, const vpMatrix &M, const char *header="")
std::ostream & cppPrint(std::ostream &os, const char *matrixName=NULL, bool octet=false) const
Print to be used as part of a C++ code later.
static void kron(const vpMatrix &m1, const vpMatrix &m2, vpMatrix &out)
void init()
Initialization of the object matrix.
Definition of the row vector class.
static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
double getMinValue() const
vpMatrix & operator*=(const double x)
Multiply all the element of the matrix by x : Aij = Aij * x.
vpMatrix & operator/=(double x)
Divide all the element of the matrix by x : Aij = Aij / x.
error that can be emited by ViSP classes.
static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
vpMatrix()
Basic constructor.
int print(std::ostream &s, unsigned int length, char const *intro=0)
vp_deprecated vpColVector column(const unsigned int j)
std::ostream & maplePrint(std::ostream &os) const
Print using MAPLE matrix input format.
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
static Type maximum(const Type &a, const Type &b)
vpRowVector getRow(const unsigned int i) const
double * data
address of the first element of the data array
unsigned int trsize
Total row space.
This class aims to compute the homography wrt.two images.
vpColVector eigenValues()
void insert(const vpMatrix &A, const unsigned int r, const unsigned int c)
void solveBySVD(const vpColVector &B, vpColVector &x) const
void setIdentity(const double &val=1.0)
double ** rowPtrs
address of the first element of each rows
void svd(vpColVector &w, vpMatrix &v)
vpMatrix & operator=(const vpMatrix &B)
Copy operator. Allow operation such as A = B.
static void multMatrixVector(const vpMatrix &A, const vpColVector &b, vpColVector &c)
static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B, const double &wB, vpMatrix &C)
static bool loadMatrixYAML(std::string filename, vpMatrix &M, char *header=NULL)
vpColVector getCol(const unsigned int j) const
static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
vpMatrix operator+(const vpMatrix &B) const
vpMatrix transpose() const
void kill()
Destruction of the matrix (Memory de-allocation)
double det(vpDetMethod method=LU_DECOMPOSITION) const
void stackMatrices(const vpMatrix &A)
void resize(const unsigned int i, const bool flagNullify=true)
void diag(const vpColVector &A)
unsigned int rowNum
number of rows
double getMaxValue() const
Class that provides a data structure for the column vectors as well as a set of operations on these v...
double euclideanNorm() const
std::ostream & matlabPrint(std::ostream &os) const
Print using matlab syntax, to be put in matlab later.
vpMatrix inverseByLU() const
unsigned int getCols() const
Return the number of columns of the matrix.
vpMatrix operator/(const double x) const
Cij = Aij / x (A is unchanged)
static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA)
vpMatrix operator*(const vpMatrix &B) const
error that can be emited by the vpMatrix class and its derivates
vpColVector stackColumns()
vpMatrix operator-() const
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Compute the pseudo inverse of the matrix using the SVD.
unsigned int dsize
Current size (rowNum * colNum)
unsigned int colNum
number of columns
unsigned int getRows() const
Return the number of rows of the matrix.
static void negateMatrix(const vpMatrix &A, vpMatrix &C)
static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM)
Class that consider the case of a translation vector.
virtual ~vpMatrix()
Destructor (Memory de-allocation)
static bool saveMatrix(std::string filename, const vpMatrix &M, const bool binary=false, const char *Header="")
void resize(const unsigned int i, const bool flagNullify=true)