57 #include <visp/vpMatrix.h>
58 #include <visp/vpMath.h>
59 #include <visp/vpTranslationVector.h>
62 #include <visp/vpException.h>
63 #include <visp/vpMatrixException.h>
66 #include <visp/vpDebug.h>
80 #define DEBUG_LEVEL1 0
81 #define DEBUG_LEVEL2 0
135 unsigned int r,
unsigned int c,
136 unsigned int nrows,
unsigned int ncols)
140 if (((r + nrows) > m.
rowNum) || ((c + ncols) > m.
colNum))
144 "\n\t\t SubvpMatrix larger than vpMatrix")) ;
147 init(m,r,c,nrows,ncols);
175 const bool flagNullify)
181 { memset(this->
data,0,this->
dsize*
sizeof(
double)) ;}
185 const bool recopyNeeded = (ncols !=
this ->colNum);
186 double * copyTmp = NULL;
187 unsigned int rowTmp = 0, colTmp=0;
189 vpDEBUG_TRACE (25,
"Recopy case per case is required iff number of "
190 "cols has changed (structure of double array is not "
191 "the same in this case.");
194 copyTmp =
new double[this->
dsize];
195 memcpy (copyTmp,
this ->
data,
sizeof(
double)*this->
dsize);
200 this->
dsize = nrows*ncols;
201 this->
data = (
double*)realloc(this->
data, this->
dsize*
sizeof(
double));
202 if ((NULL == this->
data) && (0 != this->
dsize))
204 if (copyTmp != NULL)
delete [] copyTmp;
205 vpERROR_TRACE(
"\n\t\tMemory allocation error when allocating data") ;
207 "\n\t\t Memory allocation error when "
208 "allocating data")) ;
216 if (copyTmp != NULL)
delete [] copyTmp;
217 vpERROR_TRACE(
"\n\t\tMemory allocation error when allocating rowPtrs") ;
219 "\n\t\t Memory allocation error when "
220 "allocating rowPtrs")) ;
223 vpDEBUG_TRACE (25,
"Recomputation this->trsize array values.");
226 for (
unsigned int i=0; i<
dsize; i+=ncols) { *t++ = this->
data + i; }
231 vpDEBUG_TRACE (25,
"Recopy of this->data array values or nullify.");
233 { memset(this->
data,0,this->
dsize*
sizeof(
double)) ;}
239 const unsigned int minRow = (this->
rowNum<rowTmp)?this->
rowNum:rowTmp;
240 const unsigned int minCol = (this->
colNum<colTmp)?this->
colNum:colTmp;
241 for (
unsigned int i=0; i<this->
rowNum; ++i)
242 for (
unsigned int j=0; j<this->
colNum; ++j)
244 if ((minRow > i) && (minCol > j))
246 (*this)[i][j] = copyTmp [i*colTmp+j];
247 vpCDEBUG (25) << i <<
"x" << j <<
"<- " << i*colTmp+j
248 <<
"=" << copyTmp [i*colTmp+j] << std::endl;
250 else {(*this)[i][j] = 0;}
253 else {
vpDEBUG_TRACE (25,
"Nothing to do: already done by realloc.");}
256 if (copyTmp != NULL)
delete [] copyTmp;
271 std::cout << me << std::endl ;
275 unsigned int rnrows = r+nrows ;
276 unsigned int cncols = c+ncols ;
277 for (
unsigned int i=r ; i < rnrows; i++)
278 for (
unsigned int j=c ; j < cncols; j++)
279 (*
this)[i-r][j-c] = m[i][j] ;
326 std::cout << me << std::endl ;
351 for (
unsigned int i=0;i<
rowNum;i++)
352 for(
unsigned int j=0;j<
colNum;j++)
371 for (
unsigned int i=0; i<
rowNum; i++) {
372 for (
unsigned int j=0; j<
colNum; j++) {
402 std::cout << me << std::endl ;
408 vpERROR_TRACE(
"\n\t\tvpMatrix mismatch in vpMatrix/vpMatrix multiply") ;
410 "\n\t\tvpMatrix mismatch in "
411 "vpMatrix/vpMatrix multiply")) ;
415 unsigned int BcolNum = B.
colNum;
416 unsigned int BrowNum = B.
rowNum;
421 double *rowptri = A.
rowPtrs[i];
423 for (j=0;j<BcolNum;j++)
426 for (k=0;k<BrowNum;k++) s += rowptri[k] * BrowPtrs[k][j];
462 std::cout << me << std::endl ;
468 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix/vpMatrix addition") ;
470 "\n\t\t vpMatrix mismatch in "
471 "vpMatrix/vpMatrix addition")) ;
478 for (
unsigned int i=0;i<A.
rowNum;i++)
479 for(
unsigned int j=0;j<A.
colNum;j++)
480 CrowPtrs[i][j] = wB*BrowPtrs[i][j]+wA*ArowPtrs[i][j];
502 std::cout << me << std::endl ;
508 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix/vpMatrix addition") ;
510 "\n\t\t vpMatrix mismatch in "
511 "vpMatrix/vpMatrix addition")) ;
528 for (
unsigned int i=0;i<A.
rowNum;i++)
529 for(
unsigned int j=0;j<A.
colNum;j++)
532 CrowPtrs[i][j] = BrowPtrs[i][j]+ArowPtrs[i][j];
571 std::cout << me << std::endl ;
577 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix/vpMatrix substraction") ;
579 "\n\t\t vpMatrix mismatch in "
580 "vpMatrix/vpMatrix substraction")) ;
600 for (
unsigned int i=0;i<A.
rowNum;i++)
601 for(
unsigned int j=0;j<A.
colNum;j++)
603 CrowPtrs[i][j] = ArowPtrs[i][j]-BrowPtrs[i][j];
628 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix += addition") ;
630 "\n\t\t vpMatrix mismatch in "
631 "vpMatrix += addition")) ;
649 for (
unsigned int i=0;i<
rowNum;i++)
650 for(
unsigned int j=0;j<
colNum;j++)
651 rowPtrs[i][j] += BrowPtrs[i][j];
668 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix -= substraction") ;
670 "\n\t\t vpMatrix mismatch in "
671 "vpMatrix -= substraction")) ;
688 for (
unsigned int i=0;i<
rowNum;i++)
689 for(
unsigned int j=0;j<
colNum;j++)
690 rowPtrs[i][j] -= BrowPtrs[i][j];
718 std::cout << me << std::endl ;
738 for (
unsigned int i=0;i<A.
rowNum;i++)
739 for(
unsigned int j=0;j<A.
colNum;j++)
740 CrowPtrs[i][j]= -ArowPtrs[i][j];
782 for (
unsigned int i=0;i<
rowNum;i++)
783 for(
unsigned int j=0;j<
colNum;j++)
815 vpERROR_TRACE(
"vpMatrix mismatch in vpMatrix/vector multiply") ;
826 std::cout << me << std::endl ;
831 for (
unsigned int j=0;j<A.
colNum;j++)
834 for (
unsigned int i=0;i<A.
rowNum;i++)
861 vpERROR_TRACE(
"vpMatrix mismatch in vpMatrix::operator*(const vpTranslationVector)") ;
865 for (
unsigned int j=0;j<3;j++) c[j]=0 ;
867 for (
unsigned int j=0;j<3;j++) {
870 for (
unsigned int i=0;i<3;i++) {
897 std::cout << me << std::endl ;
902 unsigned int Brow = B.
getRows() ;
903 unsigned int Bcol = B.
getCols() ;
918 for (
unsigned int i=0;i<Brow;i++)
919 for(
unsigned int j=0;j<Bcol;j++)
940 std::cout << me << std::endl ;
955 for (
unsigned int i=0;i<
rowNum;i++)
956 for(
unsigned int j=0;j<
colNum;j++)
957 CrowPtrs[i][j]=
rowPtrs[i][j]*x;
980 if (std::fabs(x) <= std::numeric_limits<double>::epsilon()) {
998 for (
unsigned int i=0;i<
rowNum;i++)
999 for(
unsigned int j=0;j<
colNum;j++)
1023 for (
unsigned int i=0;i<
rowNum;i++)
1024 for(
unsigned int j=0;j<
colNum;j++)
1048 for (
unsigned int i=0;i<
rowNum;i++)
1049 for(
unsigned int j=0;j<
colNum;j++)
1065 for (
unsigned int i=0;i<
rowNum;i++)
1066 for(
unsigned int j=0;j<
colNum;j++)
1076 if (std::fabs(x) <= std::numeric_limits<double>::epsilon())
1091 for (
unsigned int i=0;i<
rowNum;i++)
1092 for(
unsigned int j=0;j<
colNum;j++)
1121 if (i==j) (*this)[i][j] = val ;
else (*
this)[i][j] = 0;
1165 for (
unsigned int i=0; i<
rowNum; i++)
1166 for (
unsigned int j=0; j<
colNum; j++)
1167 if (i == j) (*this)[i][j] = 1;
1168 else (*
this)[i][j] = 0;
1193 double *coli = (*this)[i] ;
1231 double ** AtRowPtrs = At.
rowPtrs;
1233 for(
unsigned int i = 0; i <
colNum; i++ )
1235 double *
row = AtRowPtrs[i];
1237 for(
unsigned int j = 0; j <
rowNum; j++, col+=A_step )
1281 for(
unsigned int i=0;i<
rowNum;i++){
1282 for(
unsigned int j=i;j<
rowNum;j++){
1288 for(
unsigned int k=0; k <
colNum ;k++)
1289 ssum += *(pi++)* *(pj++);
1336 s +=(*(ptr+i)) * (*(ptr+j));
1346 s +=(*(ptr+i)) * (*(ptr+i));
1385 double *optr=out.
data;
1386 for(
unsigned int j =0;j<
colNum ; j++){
1387 for(
unsigned int i =0;i<
rowNum ; i++){
1421 double *optr=out.
data;
1422 for(
unsigned int i =0;i<
dsize ; i++){
1423 *(optr++)=*(mdata++);
1444 unsigned int r1= m1.
getRows();
1445 unsigned int c1= m1.
getCols();
1446 unsigned int r2= m2.
getRows();
1447 unsigned int c2= m2.
getCols();
1452 vpERROR_TRACE(
"Kronecker prodect bad dimension of output vpMatrix") ;
1456 for(
unsigned int r =0;r<r1 ; r++){
1457 for(
unsigned int c =0;c<c1 ; c++){
1458 double alpha = m1[r][c];
1459 double *m2ptr = m2[0];
1460 unsigned int roffset= r*r2;
1461 unsigned int coffset= c*c2;
1462 for(
unsigned int rr =0;rr<r2 ; rr++){
1463 for(
unsigned int cc =0;cc<c2 ;cc++){
1464 out[roffset+rr][coffset+cc]= alpha* *(m2ptr++);
1489 unsigned int r1= m1.
getRows();
1490 unsigned int c1= m1.
getCols();
1491 unsigned int r2= m2.
getRows();
1492 unsigned int c2= m2.
getCols();
1496 for(
unsigned int r =0;r<r1 ; r++){
1497 for(
unsigned int c =0;c<c1 ; c++){
1498 double alpha = m1[r][c];
1499 double *m2ptr = m2[0];
1500 unsigned int roffset= r*r2;
1501 unsigned int coffset= c*c2;
1502 for(
unsigned int rr =0;rr<r2 ; rr++){
1503 for(
unsigned int cc =0;cc<c2 ;cc++){
1504 out[roffset+rr ][coffset+cc]= alpha* *(m2ptr++);
1519 return kron(*
this,m);
1704 #if (DEBUG_LEVEL1 == 0)
1709 #if defined (VISP_HAVE_LAPACK)
1711 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
1713 #elif defined (VISP_HAVE_GSL)
1724 unsigned int i,j,k,nrows,ncols;
1731 #ifdef VISP_HAVE_GSL
1740 Asvd.
resize(nrows,ncols);
1742 for (i = 0 ; i < nrows ; i++)
1744 for (j = 0 ; j < ncols ; j++)
1747 for (k=0 ; k < ncols ; k++) Asvd[i][j] += (*
this)[i][k]*w[k]*v[j][k];
1750 for (i=0;i<nrows;i++)
1752 for (j=0;j<ncols;j++)
if (fabs(A[i][j]-Asvd[i][j]) > 1e-6) pb = 1;
1756 printf(
"pb in SVD\n");
1757 std::cout <<
" A : " << std::endl << A << std::endl;
1758 std::cout <<
" Asvd : " << std::endl << Asvd << std::endl;
1873 unsigned int i, j, k ;
1875 unsigned int nrows, ncols;
1876 unsigned int nrows_orig =
getRows() ;
1877 unsigned int ncols_orig =
getCols() ;
1878 Ap.
resize(ncols_orig,nrows_orig) ;
1880 if (nrows_orig >= ncols_orig)
1896 if (nrows_orig >= ncols_orig) a = *
this;
1897 else a = (*this).
t();
1903 for (i=0 ; i < ncols ; i++)
1904 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
1906 unsigned int rank = 0 ;
1907 for (i=0 ; i < ncols ; i++)
1908 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
1913 for (i = 0 ; i < ncols ; i++)
1915 for (j = 0 ; j < nrows ; j++)
1919 for (k=0 ; k < ncols ; k++)
1920 if (fabs(sv[k]) > maxsv*svThreshold)
1922 a1[i][j] += v[i][k]*a[j][k]/sv[k];
1926 if (nrows_orig >= ncols_orig) Ap = a1;
1929 if (nrows_orig >= ncols_orig)
1932 imAt.
resize(ncols_orig,rank) ;
1933 for (i=0 ; i < ncols_orig ; i++)
1934 for (j=0 ; j < rank ; j++)
1935 imAt[i][j] = v[i][j] ;
1938 imA.
resize(nrows_orig,rank) ;
1939 for (i=0 ; i < nrows_orig ; i++)
1940 for (j=0 ; j < rank ; j++)
1941 imA[i][j] = a[i][j] ;
1946 imAt.
resize(ncols_orig,rank) ;
1947 for (i=0 ; i < ncols_orig ; i++)
1948 for (j=0 ; j < rank ; j++)
1949 imAt[i][j] = a[i][j] ;
1951 imA.
resize(nrows_orig,rank) ;
1952 for (i=0 ; i < nrows_orig ; i++)
1953 for (j=0 ; j < rank ; j++)
1954 imA[i][j] = v[i][j] ;
1961 vpMatrix A, ApA, AAp, AApA, ApAAp ;
1974 for (i=0;i<nrows;i++)
1976 for (j=0;j<ncols;j++)
if (fabs(AApA[i][j]-A[i][j]) > 1e-6) pb = 1;
1978 for (i=0;i<ncols;i++)
1980 for (j=0;j<nrows;j++)
if (fabs(ApAAp[i][j]-Ap[i][j]) > 1e-6) pb = 1;
1982 for (i=0;i<nrows;i++)
1984 for (j=0;j<nrows;j++)
if (fabs(AAp[i][j]-AAp[j][i]) > 1e-6) pb = 1;
1986 for (i=0;i<ncols;i++)
1988 for (j=0;j<ncols;j++)
if (fabs(ApA[i][j]-ApA[j][i]) > 1e-6) pb = 1;
1992 printf(
"pb in pseudo inverse\n");
1993 std::cout <<
" A : " << std::endl << A << std::endl;
1994 std::cout <<
" Ap : " << std::endl << Ap << std::endl;
1995 std::cout <<
" A - AApA : " << std::endl << A - AApA << std::endl;
1996 std::cout <<
" Ap - ApAAp : " << std::endl << Ap - ApAAp << std::endl;
1997 std::cout <<
" AAp - (AAp)^T : " << std::endl << AAp - AAp.
t() << std::endl;
1998 std::cout <<
" ApA - (ApA)^T : " << std::endl << ApA - ApA.
t() << std::endl;
2052 unsigned int i, j, k ;
2054 unsigned int nrows, ncols;
2055 unsigned int nrows_orig =
getRows() ;
2056 unsigned int ncols_orig =
getCols() ;
2057 Ap.
resize(ncols_orig,nrows_orig) ;
2059 if (nrows_orig >= ncols_orig)
2075 if (nrows_orig >= ncols_orig) a = *
this;
2076 else a = (*this).
t();
2082 for (i=0 ; i < ncols ; i++)
2083 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
2085 unsigned int rank = 0 ;
2086 for (i=0 ; i < ncols ; i++)
2087 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
2092 for (i = 0 ; i < ncols ; i++)
2094 for (j = 0 ; j < nrows ; j++)
2098 for (k=0 ; k < ncols ; k++)
2099 if (fabs(sv[k]) > maxsv*svThreshold)
2101 a1[i][j] += v[i][k]*a[j][k]/sv[k];
2105 if (nrows_orig >= ncols_orig) Ap = a1;
2108 if (nrows_orig >= ncols_orig)
2111 imAt.
resize(ncols_orig,rank) ;
2112 for (i=0 ; i < ncols_orig ; i++)
2113 for (j=0 ; j < rank ; j++)
2114 imAt[i][j] = v[i][j] ;
2117 imA.
resize(nrows_orig,rank) ;
2118 for (i=0 ; i < nrows_orig ; i++)
2119 for (j=0 ; j < rank ; j++)
2120 imA[i][j] = a[i][j] ;
2125 imAt.
resize(ncols_orig,rank) ;
2126 for (i=0 ; i < ncols_orig ; i++)
2127 for (j=0 ; j < rank ; j++)
2128 imAt[i][j] = a[i][j] ;
2130 imA.
resize(nrows_orig,rank) ;
2131 for (i=0 ; i < nrows_orig ; i++)
2132 for (j=0 ; j < rank ; j++)
2133 imA[i][j] = v[i][j] ;
2137 vpMatrix cons(ncols_orig, ncols_orig);
2140 for (j = 0; j < ncols_orig; j++)
2142 for (i = 0; i < ncols_orig; i++)
2144 if (fabs(sv[i]) <= maxsv*svThreshold)
2146 cons[i][j] = v[j][i];
2151 vpMatrix Ker (ncols_orig-rank, ncols_orig);
2153 for (j = 0; j < ncols_orig ; j++)
2156 if ( std::fabs(cons.
row(j+1).
sumSquare()) > std::numeric_limits<double>::epsilon())
2158 for (i = 0; i < cons.
getCols(); i++)
2159 Ker[k][i] = cons[j][i];
2169 vpMatrix A, ApA, AAp, AApA, ApAAp ;
2182 for (i=0;i<nrows;i++)
2184 for (j=0;j<ncols;j++)
if (fabs(AApA[i][j]-A[i][j]) > 1e-6) pb = 1;
2186 for (i=0;i<ncols;i++)
2188 for (j=0;j<nrows;j++)
if (fabs(ApAAp[i][j]-Ap[i][j]) > 1e-6) pb = 1;
2190 for (i=0;i<nrows;i++)
2192 for (j=0;j<nrows;j++)
if (fabs(AAp[i][j]-AAp[j][i]) > 1e-6) pb = 1;
2194 for (i=0;i<ncols;i++)
2196 for (j=0;j<ncols;j++)
if (fabs(ApA[i][j]-ApA[j][i]) > 1e-6) pb = 1;
2200 printf(
"pb in pseudo inverse\n");
2201 std::cout <<
" A : " << std::endl << A << std::endl;
2202 std::cout <<
" Ap : " << std::endl << Ap << std::endl;
2203 std::cout <<
" A - AApA : " << std::endl << A - AApA << std::endl;
2204 std::cout <<
" Ap - ApAAp : " << std::endl << Ap - ApAAp << std::endl;
2205 std::cout <<
" AAp - (AAp)^T : " << std::endl << AAp - AAp.
t() << std::endl;
2206 std::cout <<
" ApA - (ApA)^T : " << std::endl << ApA - ApA.
t() << std::endl;
2207 std::cout <<
" KerA : " << std::endl << kerA << std::endl;
2229 for (
unsigned int i =0 ; i <
getCols() ; i++) c[i] = (*
this)[j-1][i] ;
2244 for (
unsigned int i =0 ; i <
getRows() ; i++) c[i] = (*
this)[i][j-1] ;
2294 unsigned int nra = A.
getRows() ;
2295 unsigned int nrb = B.
getRows() ;
2302 "\n\t\t incorrect matrices size")) ;
2316 for (i=0 ; i < nra ; i++)
2317 for (j=0 ; j < A.
getCols() ; j++)
2321 for (i=0 ; i < nrb ; i++)
2322 for (j=0 ; j < B.
getCols() ; j++)
2324 C[i+nra][j] = B[i][j] ;
2349 const unsigned int r,
const unsigned int c)
2380 const unsigned int r,
const unsigned int c)
2393 for(
unsigned int i=0; i<A.
getRows(); i++){
2394 for(
unsigned int j=0; j<A.
getCols(); j++){
2395 if(i >= r && i < (r + B.
getRows()) && j >= c && j < (c+B.
getCols())){
2396 C[i][j] = B[i-r][j-c];
2406 "\n\t\tIncorrect size of the matrix to insert data.");
2453 unsigned int nca = A.
getCols() ;
2454 unsigned int ncb = B.
getCols() ;
2461 "\n\t\t incorrect matrices size")) ;
2475 for (i=0 ; i < C.
getRows(); i++)
2476 for (j=0 ; j < nca ; j++)
2480 for (i=0 ; i < C.
getRows() ; i++)
2481 for (j=0 ; j < ncb ; j++)
2483 C[i][nca+j] = B[i][j] ;
2525 unsigned int rows = A.
getRows() ;
2527 this->
resize(rows,rows) ;
2536 for (
unsigned int i=0 ; i< rows ; i++ )
2537 (*
this)[i][i] = A[i] ;
2553 unsigned int rows = A.
getRows() ;
2564 for (
unsigned int i=0 ; i< rows ; i++ )
2579 for (
unsigned int i=0;i<m.
getRows();i++) {
2580 for (
unsigned int j=0;j<m.
getCols();j++){
2581 s << m[i][j] <<
" ";
2617 typedef std::string::size_type size_type;
2622 std::vector<std::string> values(m*n);
2623 std::ostringstream oss;
2624 std::ostringstream ossFixed;
2626 ossFixed.setf ( std::ios::fixed, std::ios::floatfield );
2628 size_type maxBefore=0;
2629 size_type maxAfter=0;
2631 for (
unsigned int i=0;i<m;++i) {
2632 for (
unsigned int j=0;j<n;++j){
2634 oss << (*this)[i][j];
2635 if (oss.str().find(
"e")!=std::string::npos){
2637 ossFixed << (*this)[i][j];
2638 oss.str(ossFixed.str());
2641 values[i*n+j]=oss.str();
2642 size_type thislen=values[i*n+j].size();
2643 size_type p=values[i*n+j].find(
'.');
2645 if (p==std::string::npos){
2655 size_type totalLength=length;
2659 maxAfter=std::min(maxAfter, totalLength-maxBefore);
2660 if (maxAfter==1) maxAfter=0;
2663 std::cerr <<totalLength <<
" " <<maxBefore <<
" " <<maxAfter <<
"\n";
2665 if (intro) s <<intro;
2666 s <<
"["<<m<<
","<<n<<
"]=\n";
2668 for (
unsigned int i=0;i<m;i++) {
2670 for (
unsigned int j=0;j<n;j++){
2671 size_type p=values[i*n+j].find(
'.');
2672 s.setf(std::ios::right, std::ios::adjustfield);
2673 s.width((std::streamsize)maxBefore);
2674 s <<values[i*n+j].substr(0,p).c_str();
2677 s.setf(std::ios::left, std::ios::adjustfield);
2678 if (p!=std::string::npos){
2679 s.width((std::streamsize)maxAfter);
2680 s <<values[i*n+j].substr(p,maxAfter).c_str();
2683 s.width((std::streamsize)maxAfter);
2693 return (
int)(maxBefore+maxAfter);
2712 for (i=0; i < this->
getRows(); ++ i)
2714 for (j=0; j <
this ->getCols(); ++ j)
2716 os << (*this)[i][j] <<
", ";
2718 if (
this ->
getRows() != i+1) { os <<
";" << std::endl; }
2719 else { os <<
"]" << std::endl; }
2739 os <<
"([ " << std::endl;
2740 for (i=0; i < this->
getRows(); ++ i)
2742 for (j=0; j < this->
getCols(); ++ j)
2744 os << (*this)[i][j] <<
", ";
2746 os <<
"]," << std::endl;
2748 os <<
"])" << std::endl;
2768 cppPrint(std::ostream & os,
const char * matrixName,
bool octet)
2772 const char defaultName [] =
"A";
2773 if (NULL == matrixName)
2775 matrixName = defaultName;
2777 os <<
"vpMatrix " << defaultName
2778 <<
" (" <<
this ->getRows ()
2779 <<
", " <<
this ->getCols () <<
"); " <<std::endl;
2781 for (i=0; i < this->
getRows(); ++ i)
2783 for (j=0; j <
this ->getCols(); ++ j)
2787 os << defaultName <<
"[" << i <<
"][" << j
2788 <<
"] = " << (*this)[i][j] <<
"; " << std::endl;
2792 for (
unsigned int k = 0; k <
sizeof(double); ++ k)
2794 os <<
"((unsigned char*)&(" << defaultName
2795 <<
"[" << i <<
"][" << j <<
"]) )[" << k
2796 <<
"] = 0x" <<std::hex<<
2797 (
unsigned int)((
unsigned char*)& ((*this)[i][j])) [k]
2798 <<
"; " << std::endl;
2820 for (
unsigned int i=0;i<
dsize;i++) {
2821 x = *(
data +i); norm += x*x;
2844 for (
unsigned int i=0;i<
rowNum;i++){
2846 for (
unsigned int j=0; j<
colNum;j++){
2847 x += fabs (*(*(
rowPtrs + i)+j)) ;
2864 double vpMatrix::detByLU()
const
2881 unsigned int * perm =
new unsigned int[
rowNum];
2883 tmp.LUDcmp(perm, d);
2888 for(
unsigned int i=0;i<
rowNum;i++)
2895 vpERROR_TRACE(
"Determinant Computation : ERR Matrix not squared") ;
2897 "\n\t\tDeterminant Computation : ERR Matrix not squared")) ;
2940 const unsigned int c)
2944 for(
unsigned int i=r; i<(r+A.
getRows()); i++){
2945 for(
unsigned int j=c; j<(c+A.
getCols()); j++){
2946 (*this)[i][j] = A[i-r][j-c];
2952 "\n\t\tIncorrect size of the matrix to insert data.");
2999 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not square") ;
3001 "\n\t\tEigen values computation: ERR Matrix not square")) ;
3004 #ifdef VISP_HAVE_GSL
3008 for (
unsigned int i=0; i <
rowNum; i++) {
3009 for (
unsigned int j=0; j <
rowNum; j++) {
3011 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
3012 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not symmetric") ;
3014 "\n\t\tEigen values computation: "
3015 "ERR Matrix not symmetric")) ;
3023 gsl_vector *eval = gsl_vector_alloc (rowNum);
3024 gsl_matrix *evec = gsl_matrix_alloc (rowNum,
colNum);
3026 gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (rowNum);
3027 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
3029 unsigned int Atda = m->tda ;
3030 for (
unsigned int i=0 ; i <
rowNum ; i++){
3031 unsigned int k = i*Atda ;
3032 for (
unsigned int j=0 ; j <
colNum ; j++)
3033 m->data[k+j] = (*
this)[i][j] ;
3035 gsl_eigen_symmv (m, eval, evec, w);
3037 gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
3039 for (
unsigned int i=0; i <
rowNum; i++) {
3040 evalue[i] = gsl_vector_get (eval, i);
3043 gsl_eigen_symmv_free (w);
3044 gsl_vector_free (eval);
3045 gsl_matrix_free (m);
3046 gsl_matrix_free (evec);
3052 vpERROR_TRACE(
"Not implemented since the GSL library is not detected.") ;
3054 "\n\t\tEigen values Computation: Not implemented "
3055 "since the GSL library is not detected")) ;
3114 #ifdef VISP_HAVE_GSL
3121 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not square") ;
3123 "\n\t\tEigen values computation: ERR Matrix not square")) ;
3126 #ifdef VISP_HAVE_GSL
3130 for (
unsigned int i=0; i <
rowNum; i++) {
3131 for (
unsigned int j=0; j <
rowNum; j++) {
3133 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
3134 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not symmetric") ;
3136 "\n\t\tEigen values computation: "
3137 "ERR Matrix not symmetric")) ;
3146 gsl_vector *eval = gsl_vector_alloc (rowNum);
3147 gsl_matrix *evec = gsl_matrix_alloc (rowNum,
colNum);
3149 gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (rowNum);
3150 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
3152 unsigned int Atda = m->tda ;
3153 for (
unsigned int i=0 ; i <
rowNum ; i++){
3154 unsigned int k = i*Atda ;
3155 for (
unsigned int j=0 ; j <
colNum ; j++)
3156 m->data[k+j] = (*
this)[i][j] ;
3158 gsl_eigen_symmv (m, eval, evec, w);
3160 gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
3162 for (
unsigned int i=0; i <
rowNum; i++) {
3163 evalue[i] = gsl_vector_get (eval, i);
3166 for (
unsigned int i=0; i <
rowNum; i++) {
3167 unsigned int k = i*Atda ;
3168 for (
unsigned int j=0; j <
rowNum; j++) {
3169 evector[i][j] = evec->
data[k+j];
3191 gsl_eigen_symmv_free (w);
3192 gsl_vector_free (eval);
3193 gsl_matrix_free (m);
3194 gsl_matrix_free (evec);
3198 vpERROR_TRACE(
"Not implemented since the GSL library is not detected.") ;
3200 "\n\t\tEigen values Computation: Not implemented "
3201 "since the GSL library is not detected")) ;
3220 std::cout <<
"begin Kernel" << std::endl ;
3223 unsigned int ncaptor =
getRows() ;
3224 unsigned int ddl =
getCols() ;
3228 std::cout <<
"Erreur Matrice non initialise" << std::endl ;
3232 std::cout <<
"Interaction matrix L" << std::endl ;
3233 std::cout << *this ;
3234 std::cout <<
"signaux capteurs : " << ncaptor << std::endl ;
3241 if (ncaptor > ddl) min = ddl ;
else min = ncaptor ;
3262 for (i=0 ; i < ncaptor ; i++)
3263 for (j=0 ; j < ddl ; j++)
3265 a1[i][j] = (*this)[i][j] ;
3266 a2[i][j] = (*this)[i][j] ;
3272 for (i=ncaptor ; i < ddl ; i++)
3273 for (j=0 ; j < ddl ; j++)
3287 for (i=0 ; i < ddl ; i++)
3288 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
3292 std::cout <<
"Singular Value : (" ;
3293 for (i=0 ; i < ddl ; i++) std::cout << sv[i] <<
" , " ;
3294 std::cout <<
")" << std::endl ;
3298 unsigned int rank = 0 ;
3299 for (i=0 ; i < ddl ; i++)
3300 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
3305 for (i = 0 ; i < ddl ; i++)
3307 for (j = 0 ; j < ncaptor ; j++)
3315 for (k=0 ; k < ddl ; k++) if (fabs(sv[k]) > maxsv*svThreshold)
3317 C[i][j] += v[i][k]*a1[j][k]/sv[k];
3323 std::cout << C << std::endl ;
3340 for (i=0 ; i < ddl ; i++)
3341 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
3342 unsigned int rank = 0 ;
3343 for (i=0 ; i < ddl ; i++)
3344 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
3348 for (j = 0 ; j < ddl ; j++)
3350 for (i = 0 ; i < ddl ; i++)
3353 if (fabs(sv[i]) <= maxsv*svThreshold)
3355 cons[i][j] = v[j][i];
3361 for (j = 0 ; j < ddl ; j++)
3366 if (std::fabs(cons.
row(j+1).
sumSquare()) > std::numeric_limits<double>::epsilon())
3368 for (i=0 ; i < cons.
getCols() ; i++)
3369 Ker[k][i] = cons[j][i] ;
3378 std::cout <<
"end Kernel" << std::endl ;
3419 det = this->detByLU();
3441 const bool binary,
const char *Header)
3446 file.open(filename, std::fstream::out);
3448 file.open(filename, std::fstream::out|std::fstream::binary);
3462 while (Header[i] !=
'\0')
3465 if (Header[i] ==
'\n')
3472 file << M << std::endl;
3477 while (Header[headerSize] !=
'\0') headerSize++;
3478 file.write(Header,headerSize+1);
3479 unsigned int matrixSize;
3481 file.write((
char*)&matrixSize,
sizeof(
int));
3483 file.write((
char*)&matrixSize,
sizeof(
int));
3485 for(
unsigned int i = 0; i < M.
getRows(); i++)
3487 for(
unsigned int j = 0; j < M.
getCols(); j++)
3490 file.write((
char*)&value,
sizeof(
double));
3519 file.open(filename, std::fstream::in);
3521 file.open(filename, std::fstream::in|std::fstream::binary);
3535 while ((c !=
'\0') && (c !=
'\n'))
3541 strncpy(Header, h.c_str(), h.size() + 1);
3543 unsigned int rows, cols;
3549 for(
unsigned int i = 0; i < rows; i++)
3551 for(
unsigned int j = 0; j < cols; j++)
3562 while ((c !=
'\0') && (c !=
'\n'))
3568 strncpy(Header, h.c_str(), h.size() + 1);
3570 unsigned int rows, cols;
3571 file.read((
char*)&rows,
sizeof(
unsigned int));
3572 file.read((
char*)&cols,
sizeof(
unsigned int));
3576 for(
unsigned int i = 0; i < rows; i++)
3578 for(
unsigned int j = 0; j < cols; j++)
3580 file.read((
char*)&value,
sizeof(
double));
3604 vpTRACE(
"The matrix must be square");
3606 "The matrix must be square" ));
3626 for (
unsigned int i = 0; i <
rowNum;i++)
3629 for (
unsigned int j=0; j <
colNum; j++)
3631 sum += fabs((*
this)[i][j]);
3643 double sca = 1.0 / pow(2.0,s);
3648 for (
int k=2;k<=q;k++)
3650 c = c * ((double)(q-k+1)) / ((
double)(k*(2*q-k+1)));
3655 if (p) _expD=_expD+_expcX;
3656 else _expD=_expD- _expcX;
3661 for (
int k=1;k<=s;k++)
3673 double *dataptr =
data;
3674 double min = *dataptr;
3676 for (
unsigned int i = 0; i <
dsize-1; i++)
3678 if (*dataptr < min) min = *dataptr;
3687 double *dataptr =
data;
3688 double max = *dataptr;
3690 for (
unsigned int i = 0; i <
dsize-1; i++)
3692 if (*dataptr > max) max = *dataptr;
3722 for (
unsigned int i = 0 ; i < col ; i++)
3724 for (
unsigned int j = 0 ; j <
row ; j++)
3725 M_comp[i][j]=M[i][j];
3726 for (
unsigned int j = row+1 ; j < M.
getRows() ; j++)
3727 M_comp[i][j-1]=M[i][j];
3729 for (
unsigned int i = col+1 ; i < M.
getCols(); i++)
3731 for (
unsigned int j = 0 ; j <
row ; j++)
3732 M_comp[i-1][j]=M[i][j];
3733 for (
unsigned int j = row+1 ; j < M.
getRows() ; j++)
3734 M_comp[i-1][j-1]=M[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
Definition of the vpMatrix class.
unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6)
static vpMatrix juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B)
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
vpRowVector row(const unsigned int i)
Row extraction.
double infinityNorm() const
void init()
Initialization of the object matrix.
Definition of the row vector class.
static bool saveMatrix(const char *filename, const vpMatrix &M, const bool binary=false, const char *Header="")
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.
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
vpMatrix()
Basic constructor.
int print(std::ostream &s, unsigned int length, char const *intro=0)
vpColVector column(const unsigned int j)
Column extraction.
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
static Type maximum(const Type &a, const Type &b)
double * data
address of the first element of the data array
std::ostream & cppPrint(std::ostream &os, const char *matrixName=NULL, bool octet=false)
Print to be used as part of a C++ code later.
unsigned int trsize
Total row space.
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 bool loadMatrix(const char *filename, vpMatrix &M, const bool binary=false, char *Header=NULL)
static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B, const double &wB, vpMatrix &C)
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
VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpImagePoint &ip)
static vpMatrix stackMatrices(const vpMatrix &A, const vpMatrix &B)
Stack two Matrices C = [ A B ]^T.
void diag(const vpColVector &A)
unsigned int rowNum
number of rows
double getMaxValue() const
void resize(unsigned int i)
Set the size of the Row vector.
Class that provides a data structure for the column vectors as well as a set of operations on these v...
double euclideanNorm() const
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
std::ostream & maplePrint(std::ostream &os)
Print using MAPLE matrix input format.
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Compute the pseudo inverse of the matrix using the SVD.
unsigned int dsize
Current size (rowNum * colNum)
void kron(const vpMatrix &m1, vpMatrix &out)
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)
Class that consider the case of a translation vector.
virtual ~vpMatrix()
Destructor (Memory de-allocation)
std::ostream & matlabPrint(std::ostream &os)
Print using matlab syntax, to be put in matlab later.
void resize(const unsigned int i, const bool flagNullify=true)