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
146 unsigned int r,
unsigned int c,
147 unsigned int nrows,
unsigned int ncols)
150 if (((r + nrows) > m.
rowNum) || ((c + ncols) > m.
colNum))
154 "\n\t\t SubvpMatrix larger than vpMatrix")) ;
157 init(m,r,c,nrows,ncols);
184 const bool flagNullify)
190 { memset(this->
data,0,this->
dsize*
sizeof(
double)) ;}
194 const bool recopyNeeded = (ncols !=
this ->colNum);
195 double * copyTmp = NULL;
196 unsigned int rowTmp = 0, colTmp=0;
198 vpDEBUG_TRACE (25,
"Recopy case per case is required iff number of "
199 "cols has changed (structure of double array is not "
200 "the same in this case.");
203 copyTmp =
new double[this->
dsize];
204 memcpy (copyTmp,
this ->
data,
sizeof(
double)*this->
dsize);
209 this->
dsize = nrows*ncols;
210 this->
data = (
double*)realloc(this->
data, this->
dsize*
sizeof(
double));
211 if ((NULL == this->
data) && (0 != this->
dsize))
213 if (copyTmp != NULL)
delete [] copyTmp;
214 vpERROR_TRACE(
"\n\t\tMemory allocation error when allocating data") ;
216 "\n\t\t Memory allocation error when "
217 "allocating data")) ;
225 if (copyTmp != NULL)
delete [] copyTmp;
226 vpERROR_TRACE(
"\n\t\tMemory allocation error when allocating rowPtrs") ;
228 "\n\t\t Memory allocation error when "
229 "allocating rowPtrs")) ;
232 vpDEBUG_TRACE (25,
"Recomputation this->trsize array values.");
235 for (
unsigned int i=0; i<
dsize; i+=ncols) { *t_++ = this->
data + i; }
240 vpDEBUG_TRACE (25,
"Recopy of this->data array values or nullify.");
242 { memset(this->
data,0,this->
dsize*
sizeof(
double)) ;}
248 const unsigned int minRow = (this->
rowNum<rowTmp)?this->
rowNum:rowTmp;
249 const unsigned int minCol = (this->
colNum<colTmp)?this->
colNum:colTmp;
250 for (
unsigned int i=0; i<this->
rowNum; ++i)
251 for (
unsigned int j=0; j<this->
colNum; ++j)
253 if ((minRow > i) && (minCol > j))
255 (*this)[i][j] = copyTmp [i*colTmp+j];
256 vpCDEBUG (25) << i <<
"x" << j <<
"<- " << i*colTmp+j
257 <<
"=" << copyTmp [i*colTmp+j] << std::endl;
259 else {(*this)[i][j] = 0;}
262 else {
vpDEBUG_TRACE (25,
"Nothing to do: already done by realloc.");}
265 if (copyTmp != NULL)
delete [] copyTmp;
280 std::cout << me << std::endl ;
284 unsigned int rnrows = r+nrows ;
285 unsigned int cncols = c+ncols ;
286 for (
unsigned int i=r ; i < rnrows; i++)
287 for (
unsigned int j=c ; j < cncols; j++)
288 (*
this)[i-r][j-c] = m[i][j] ;
335 std::cout << me << std::endl ;
355 for(
unsigned int i=0; i<3; i++)
356 for(
unsigned int j=0; j<3; j++)
357 (*
this)[i][j] = H[i][j];
378 for (
unsigned int i=0;i<
rowNum;i++)
379 for(
unsigned int j=0;j<
colNum;j++)
398 for (
unsigned int i=0; i<
rowNum; i++) {
399 for (
unsigned int j=0; j<
colNum; j++) {
429 std::cout << me << std::endl ;
435 vpERROR_TRACE(
"\n\t\tvpMatrix mismatch in vpMatrix/vpMatrix multiply") ;
437 "\n\t\tvpMatrix mismatch in "
438 "vpMatrix/vpMatrix multiply")) ;
442 unsigned int BcolNum = B.
colNum;
443 unsigned int BrowNum = B.
rowNum;
448 double *rowptri = A.
rowPtrs[i];
450 for (j=0;j<BcolNum;j++)
453 for (k=0;k<BrowNum;k++) s += rowptri[k] * BrowPtrs[k][j];
482 for (
unsigned int i=0;i<
rowNum;i++){
483 for (
unsigned int j=0;j<3;j++) {
485 for (
unsigned int k=0;k<3;k++) s += (*
this)[i][k] * H[k][j];
511 std::cout << me << std::endl ;
517 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix/vpMatrix addition") ;
519 "\n\t\t vpMatrix mismatch in "
520 "vpMatrix/vpMatrix addition")) ;
527 for (
unsigned int i=0;i<A.
rowNum;i++)
528 for(
unsigned int j=0;j<A.
colNum;j++)
529 CrowPtrs[i][j] = wB*BrowPtrs[i][j]+wA*ArowPtrs[i][j];
551 std::cout << me << std::endl ;
557 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix/vpMatrix addition") ;
559 "\n\t\t vpMatrix mismatch in "
560 "vpMatrix/vpMatrix addition")) ;
577 for (
unsigned int i=0;i<A.
rowNum;i++)
578 for(
unsigned int j=0;j<A.
colNum;j++)
581 CrowPtrs[i][j] = BrowPtrs[i][j]+ArowPtrs[i][j];
620 std::cout << me << std::endl ;
626 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix/vpMatrix substraction") ;
628 "\n\t\t vpMatrix mismatch in "
629 "vpMatrix/vpMatrix substraction")) ;
649 for (
unsigned int i=0;i<A.
rowNum;i++)
650 for(
unsigned int j=0;j<A.
colNum;j++)
652 CrowPtrs[i][j] = ArowPtrs[i][j]-BrowPtrs[i][j];
677 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix += addition") ;
679 "\n\t\t vpMatrix mismatch in "
680 "vpMatrix += addition")) ;
698 for (
unsigned int i=0;i<
rowNum;i++)
699 for(
unsigned int j=0;j<
colNum;j++)
700 rowPtrs[i][j] += BrowPtrs[i][j];
717 vpERROR_TRACE(
"\n\t\t vpMatrix mismatch in vpMatrix -= substraction") ;
719 "\n\t\t vpMatrix mismatch in "
720 "vpMatrix -= substraction")) ;
737 for (
unsigned int i=0;i<
rowNum;i++)
738 for(
unsigned int j=0;j<
colNum;j++)
739 rowPtrs[i][j] -= BrowPtrs[i][j];
767 std::cout << me << std::endl ;
787 for (
unsigned int i=0;i<A.
rowNum;i++)
788 for(
unsigned int j=0;j<A.
colNum;j++)
789 CrowPtrs[i][j]= -ArowPtrs[i][j];
831 for (
unsigned int i=0;i<
rowNum;i++)
832 for(
unsigned int j=0;j<
colNum;j++)
864 vpERROR_TRACE(
"vpMatrix mismatch in vpMatrix/vector multiply") ;
875 std::cout << me << std::endl ;
880 for (
unsigned int j=0;j<A.
colNum;j++)
883 for (
unsigned int i=0;i<A.
rowNum;i++)
910 vpERROR_TRACE(
"vpMatrix mismatch in vpMatrix::operator*(const vpTranslationVector)") ;
914 for (
unsigned int j=0;j<3;j++) c[j]=0 ;
916 for (
unsigned int j=0;j<3;j++) {
919 for (
unsigned int i=0;i<3;i++) {
946 std::cout << me << std::endl ;
951 unsigned int Brow = B.
getRows() ;
952 unsigned int Bcol = B.
getCols() ;
967 for (
unsigned int i=0;i<Brow;i++)
968 for(
unsigned int j=0;j<Bcol;j++)
989 std::cout << me << std::endl ;
1004 for (
unsigned int i=0;i<
rowNum;i++)
1005 for(
unsigned int j=0;j<
colNum;j++)
1006 CrowPtrs[i][j]=
rowPtrs[i][j]*x;
1029 if (std::fabs(x) <= std::numeric_limits<double>::epsilon()) {
1047 for (
unsigned int i=0;i<
rowNum;i++)
1048 for(
unsigned int j=0;j<
colNum;j++)
1072 for (
unsigned int i=0;i<
rowNum;i++)
1073 for(
unsigned int j=0;j<
colNum;j++)
1097 for (
unsigned int i=0;i<
rowNum;i++)
1098 for(
unsigned int j=0;j<
colNum;j++)
1114 for (
unsigned int i=0;i<
rowNum;i++)
1115 for(
unsigned int j=0;j<
colNum;j++)
1125 if (std::fabs(x) <= std::numeric_limits<double>::epsilon())
1140 for (
unsigned int i=0;i<
rowNum;i++)
1141 for(
unsigned int j=0;j<
colNum;j++)
1170 if (i==j) (*this)[i][j] = val ;
else (*
this)[i][j] = 0;
1214 for (
unsigned int i=0; i<
rowNum; i++)
1215 for (
unsigned int j=0; j<
colNum; j++)
1216 if (i == j) (*this)[i][j] = 1;
1217 else (*
this)[i][j] = 0;
1242 double *coli = (*this)[i] ;
1280 double ** AtRowPtrs = At.
rowPtrs;
1282 for(
unsigned int i = 0; i <
colNum; i++ )
1284 double * row_ = AtRowPtrs[i];
1286 for(
unsigned int j = 0; j <
rowNum; j++, col+=A_step )
1330 for(
unsigned int i=0;i<
rowNum;i++){
1331 for(
unsigned int j=i;j<
rowNum;j++){
1337 for(
unsigned int k=0; k <
colNum ;k++)
1338 ssum += *(pi++)* *(pj++);
1385 s +=(*(ptr+i)) * (*(ptr+j));
1395 s +=(*(ptr+i)) * (*(ptr+i));
1434 double *optr=out.
data;
1435 for(
unsigned int j =0;j<
colNum ; j++){
1436 for(
unsigned int i =0;i<
rowNum ; i++){
1470 double *optr=out.
data;
1471 for(
unsigned int i =0;i<
dsize ; i++){
1472 *(optr++)=*(mdata++);
1493 unsigned int r1= m1.
getRows();
1494 unsigned int c1= m1.
getCols();
1495 unsigned int r2= m2.
getRows();
1496 unsigned int c2= m2.
getCols();
1501 vpERROR_TRACE(
"Kronecker prodect bad dimension of output vpMatrix") ;
1505 for(
unsigned int r =0;r<r1 ; r++){
1506 for(
unsigned int c =0;c<c1 ; c++){
1507 double alpha = m1[r][c];
1508 double *m2ptr = m2[0];
1509 unsigned int roffset= r*r2;
1510 unsigned int coffset= c*c2;
1511 for(
unsigned int rr =0;rr<r2 ; rr++){
1512 for(
unsigned int cc =0;cc<c2 ;cc++){
1513 out[roffset+rr][coffset+cc]= alpha* *(m2ptr++);
1538 unsigned int r1= m1.
getRows();
1539 unsigned int c1= m1.
getCols();
1540 unsigned int r2= m2.
getRows();
1541 unsigned int c2= m2.
getCols();
1545 for(
unsigned int r =0;r<r1 ; r++){
1546 for(
unsigned int c =0;c<c1 ; c++){
1547 double alpha = m1[r][c];
1548 double *m2ptr = m2[0];
1549 unsigned int roffset= r*r2;
1550 unsigned int coffset= c*c2;
1551 for(
unsigned int rr =0;rr<r2 ; rr++){
1552 for(
unsigned int cc =0;cc<c2 ;cc++){
1553 out[roffset+rr ][coffset+cc]= alpha* *(m2ptr++);
1568 return kron(*
this,m);
1753 #if (DEBUG_LEVEL1 == 0)
1758 #if defined (VISP_HAVE_LAPACK)
1760 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
1762 #elif defined (VISP_HAVE_GSL)
1773 unsigned int i,j,k,nrows,ncols;
1780 #ifdef VISP_HAVE_GSL
1789 Asvd.
resize(nrows,ncols);
1791 for (i = 0 ; i < nrows ; i++)
1793 for (j = 0 ; j < ncols ; j++)
1796 for (k=0 ; k < ncols ; k++) Asvd[i][j] += (*
this)[i][k]*w[k]*v[j][k];
1799 for (i=0;i<nrows;i++)
1801 for (j=0;j<ncols;j++)
if (fabs(A[i][j]-Asvd[i][j]) > 1e-6) pb = 1;
1805 printf(
"pb in SVD\n");
1806 std::cout <<
" A : " << std::endl << A << std::endl;
1807 std::cout <<
" Asvd : " << std::endl << Asvd << std::endl;
1922 unsigned int i, j, k ;
1924 unsigned int nrows, ncols;
1925 unsigned int nrows_orig =
getRows() ;
1926 unsigned int ncols_orig =
getCols() ;
1927 Ap.
resize(ncols_orig,nrows_orig) ;
1929 if (nrows_orig >= ncols_orig)
1945 if (nrows_orig >= ncols_orig) a = *
this;
1946 else a = (*this).
t();
1952 for (i=0 ; i < ncols ; i++)
1953 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
1955 unsigned int rank = 0 ;
1956 for (i=0 ; i < ncols ; i++)
1957 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
1962 for (i = 0 ; i < ncols ; i++)
1964 for (j = 0 ; j < nrows ; j++)
1968 for (k=0 ; k < ncols ; k++)
1969 if (fabs(sv[k]) > maxsv*svThreshold)
1971 a1[i][j] += v[i][k]*a[j][k]/sv[k];
1975 if (nrows_orig >= ncols_orig) Ap = a1;
1978 if (nrows_orig >= ncols_orig)
1981 imAt.
resize(ncols_orig,rank) ;
1982 for (i=0 ; i < ncols_orig ; i++)
1983 for (j=0 ; j < rank ; j++)
1984 imAt[i][j] = v[i][j] ;
1987 imA.
resize(nrows_orig,rank) ;
1988 for (i=0 ; i < nrows_orig ; i++)
1989 for (j=0 ; j < rank ; j++)
1990 imA[i][j] = a[i][j] ;
1995 imAt.
resize(ncols_orig,rank) ;
1996 for (i=0 ; i < ncols_orig ; i++)
1997 for (j=0 ; j < rank ; j++)
1998 imAt[i][j] = a[i][j] ;
2000 imA.
resize(nrows_orig,rank) ;
2001 for (i=0 ; i < nrows_orig ; i++)
2002 for (j=0 ; j < rank ; j++)
2003 imA[i][j] = v[i][j] ;
2010 vpMatrix A, ApA, AAp, AApA, ApAAp ;
2023 for (i=0;i<nrows;i++)
2025 for (j=0;j<ncols;j++)
if (fabs(AApA[i][j]-A[i][j]) > 1e-6) pb = 1;
2027 for (i=0;i<ncols;i++)
2029 for (j=0;j<nrows;j++)
if (fabs(ApAAp[i][j]-Ap[i][j]) > 1e-6) pb = 1;
2031 for (i=0;i<nrows;i++)
2033 for (j=0;j<nrows;j++)
if (fabs(AAp[i][j]-AAp[j][i]) > 1e-6) pb = 1;
2035 for (i=0;i<ncols;i++)
2037 for (j=0;j<ncols;j++)
if (fabs(ApA[i][j]-ApA[j][i]) > 1e-6) pb = 1;
2041 printf(
"pb in pseudo inverse\n");
2042 std::cout <<
" A : " << std::endl << A << std::endl;
2043 std::cout <<
" Ap : " << std::endl << Ap << std::endl;
2044 std::cout <<
" A - AApA : " << std::endl << A - AApA << std::endl;
2045 std::cout <<
" Ap - ApAAp : " << std::endl << Ap - ApAAp << std::endl;
2046 std::cout <<
" AAp - (AAp)^T : " << std::endl << AAp - AAp.
t() << std::endl;
2047 std::cout <<
" ApA - (ApA)^T : " << std::endl << ApA - ApA.
t() << std::endl;
2101 unsigned int i, j, k ;
2103 unsigned int nrows, ncols;
2104 unsigned int nrows_orig =
getRows() ;
2105 unsigned int ncols_orig =
getCols() ;
2106 Ap.
resize(ncols_orig,nrows_orig) ;
2108 if (nrows_orig >= ncols_orig)
2124 if (nrows_orig >= ncols_orig) a = *
this;
2125 else a = (*this).
t();
2131 for (i=0 ; i < ncols ; i++)
2132 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
2134 unsigned int rank = 0 ;
2135 for (i=0 ; i < ncols ; i++)
2136 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
2141 for (i = 0 ; i < ncols ; i++)
2143 for (j = 0 ; j < nrows ; j++)
2147 for (k=0 ; k < ncols ; k++)
2148 if (fabs(sv[k]) > maxsv*svThreshold)
2150 a1[i][j] += v[i][k]*a[j][k]/sv[k];
2154 if (nrows_orig >= ncols_orig) Ap = a1;
2157 if (nrows_orig >= ncols_orig)
2160 imAt.
resize(ncols_orig,rank) ;
2161 for (i=0 ; i < ncols_orig ; i++)
2162 for (j=0 ; j < rank ; j++)
2163 imAt[i][j] = v[i][j] ;
2166 imA.
resize(nrows_orig,rank) ;
2167 for (i=0 ; i < nrows_orig ; i++)
2168 for (j=0 ; j < rank ; j++)
2169 imA[i][j] = a[i][j] ;
2174 imAt.
resize(ncols_orig,rank) ;
2175 for (i=0 ; i < ncols_orig ; i++)
2176 for (j=0 ; j < rank ; j++)
2177 imAt[i][j] = a[i][j] ;
2179 imA.
resize(nrows_orig,rank) ;
2180 for (i=0 ; i < nrows_orig ; i++)
2181 for (j=0 ; j < rank ; j++)
2182 imA[i][j] = v[i][j] ;
2186 vpMatrix cons(ncols_orig, ncols_orig);
2189 for (j = 0; j < ncols_orig; j++)
2191 for (i = 0; i < ncols_orig; i++)
2193 if (fabs(sv[i]) <= maxsv*svThreshold)
2195 cons[i][j] = v[j][i];
2200 vpMatrix Ker (ncols_orig-rank, ncols_orig);
2202 for (j = 0; j < ncols_orig ; j++)
2205 if ( std::fabs(cons.
row(j+1).
sumSquare()) > std::numeric_limits<double>::epsilon())
2207 for (i = 0; i < cons.
getCols(); i++)
2208 Ker[k][i] = cons[j][i];
2218 vpMatrix A, ApA, AAp, AApA, ApAAp ;
2231 for (i=0;i<nrows;i++)
2233 for (j=0;j<ncols;j++)
if (fabs(AApA[i][j]-A[i][j]) > 1e-6) pb = 1;
2235 for (i=0;i<ncols;i++)
2237 for (j=0;j<nrows;j++)
if (fabs(ApAAp[i][j]-Ap[i][j]) > 1e-6) pb = 1;
2239 for (i=0;i<nrows;i++)
2241 for (j=0;j<nrows;j++)
if (fabs(AAp[i][j]-AAp[j][i]) > 1e-6) pb = 1;
2243 for (i=0;i<ncols;i++)
2245 for (j=0;j<ncols;j++)
if (fabs(ApA[i][j]-ApA[j][i]) > 1e-6) pb = 1;
2249 printf(
"pb in pseudo inverse\n");
2250 std::cout <<
" A : " << std::endl << A << std::endl;
2251 std::cout <<
" Ap : " << std::endl << Ap << std::endl;
2252 std::cout <<
" A - AApA : " << std::endl << A - AApA << std::endl;
2253 std::cout <<
" Ap - ApAAp : " << std::endl << Ap - ApAAp << std::endl;
2254 std::cout <<
" AAp - (AAp)^T : " << std::endl << AAp - AAp.
t() << std::endl;
2255 std::cout <<
" ApA - (ApA)^T : " << std::endl << ApA - ApA.
t() << std::endl;
2256 std::cout <<
" KerA : " << std::endl << kerA << std::endl;
2278 for (
unsigned int i =0 ; i <
getCols() ; i++) c[i] = (*
this)[j-1][i] ;
2293 for (
unsigned int i =0 ; i <
getRows() ; i++) c[i] = (*
this)[i][j-1] ;
2343 unsigned int nra = A.
getRows() ;
2344 unsigned int nrb = B.
getRows() ;
2351 "\n\t\t incorrect matrices size")) ;
2365 for (i=0 ; i < nra ; i++)
2366 for (j=0 ; j < A.
getCols() ; j++)
2370 for (i=0 ; i < nrb ; i++)
2371 for (j=0 ; j < B.
getCols() ; j++)
2373 C[i+nra][j] = B[i][j] ;
2398 const unsigned int r,
const unsigned int c)
2429 const unsigned int r,
const unsigned int c)
2442 for(
unsigned int i=0; i<A.
getRows(); i++){
2443 for(
unsigned int j=0; j<A.
getCols(); j++){
2444 if(i >= r && i < (r + B.
getRows()) && j >= c && j < (c+B.
getCols())){
2445 C[i][j] = B[i-r][j-c];
2455 "\n\t\tIncorrect size of the matrix to insert data.");
2502 unsigned int nca = A.
getCols() ;
2503 unsigned int ncb = B.
getCols() ;
2510 "\n\t\t incorrect matrices size")) ;
2524 for (i=0 ; i < C.
getRows(); i++)
2525 for (j=0 ; j < nca ; j++)
2529 for (i=0 ; i < C.
getRows() ; i++)
2530 for (j=0 ; j < ncb ; j++)
2532 C[i][nca+j] = B[i][j] ;
2574 unsigned int rows = A.
getRows() ;
2576 this->
resize(rows,rows) ;
2585 for (
unsigned int i=0 ; i< rows ; i++ )
2586 (*
this)[i][i] = A[i] ;
2602 unsigned int rows = A.
getRows() ;
2613 for (
unsigned int i=0 ; i< rows ; i++ )
2627 std::ios_base::fmtflags original_flags = s.flags();
2630 for (
unsigned int i=0;i<m.
getRows();i++) {
2631 for (
unsigned int j=0;j<m.
getCols();j++){
2632 s << m[i][j] <<
" ";
2639 s.flags(original_flags);
2670 typedef std::string::size_type size_type;
2675 std::vector<std::string> values(m*n);
2676 std::ostringstream oss;
2677 std::ostringstream ossFixed;
2678 std::ios_base::fmtflags original_flags = oss.flags();
2681 ossFixed.setf ( std::ios::fixed, std::ios::floatfield );
2683 size_type maxBefore=0;
2684 size_type maxAfter=0;
2686 for (
unsigned int i=0;i<m;++i) {
2687 for (
unsigned int j=0;j<n;++j){
2689 oss << (*this)[i][j];
2690 if (oss.str().find(
"e")!=std::string::npos){
2692 ossFixed << (*this)[i][j];
2693 oss.str(ossFixed.str());
2696 values[i*n+j]=oss.str();
2697 size_type thislen=values[i*n+j].size();
2698 size_type p=values[i*n+j].find(
'.');
2700 if (p==std::string::npos){
2710 size_type totalLength=length;
2714 maxAfter=std::min(maxAfter, totalLength-maxBefore);
2715 if (maxAfter==1) maxAfter=0;
2718 std::cerr <<totalLength <<
" " <<maxBefore <<
" " <<maxAfter <<
"\n";
2720 if (intro) s <<intro;
2721 s <<
"["<<m<<
","<<n<<
"]=\n";
2723 for (
unsigned int i=0;i<m;i++) {
2725 for (
unsigned int j=0;j<n;j++){
2726 size_type p=values[i*n+j].find(
'.');
2727 s.setf(std::ios::right, std::ios::adjustfield);
2728 s.width((std::streamsize)maxBefore);
2729 s <<values[i*n+j].substr(0,p).c_str();
2732 s.setf(std::ios::left, std::ios::adjustfield);
2733 if (p!=std::string::npos){
2734 s.width((std::streamsize)maxAfter);
2735 s <<values[i*n+j].substr(p,maxAfter).c_str();
2738 s.width((std::streamsize)maxAfter);
2748 s.flags(original_flags);
2750 return (
int)(maxBefore+maxAfter);
2769 for (i=0; i < this->
getRows(); ++ i)
2771 for (j=0; j <
this ->getCols(); ++ j)
2773 os << (*this)[i][j] <<
", ";
2775 if (
this ->
getRows() != i+1) { os <<
";" << std::endl; }
2776 else { os <<
"]" << std::endl; }
2796 os <<
"([ " << std::endl;
2797 for (i=0; i < this->
getRows(); ++ i)
2799 for (j=0; j < this->
getCols(); ++ j)
2801 os << (*this)[i][j] <<
", ";
2803 os <<
"]," << std::endl;
2805 os <<
"])" << std::endl;
2822 for (i=0; i < this->
getRows(); ++ i)
2824 for (j=0; j < this->
getCols(); ++ j)
2826 os << (*this)[i][j];
2827 if (!(j==(this->
getCols()-1)))
2851 cppPrint(std::ostream & os,
const char * matrixName,
bool octet)
const
2855 const char defaultName [] =
"A";
2856 if (NULL == matrixName)
2858 matrixName = defaultName;
2860 os <<
"vpMatrix " << defaultName
2861 <<
" (" <<
this ->getRows ()
2862 <<
", " <<
this ->getCols () <<
"); " <<std::endl;
2864 for (i=0; i < this->
getRows(); ++ i)
2866 for (j=0; j <
this ->getCols(); ++ j)
2870 os << defaultName <<
"[" << i <<
"][" << j
2871 <<
"] = " << (*this)[i][j] <<
"; " << std::endl;
2875 for (
unsigned int k = 0; k <
sizeof(double); ++ k)
2877 os <<
"((unsigned char*)&(" << defaultName
2878 <<
"[" << i <<
"][" << j <<
"]) )[" << k
2879 <<
"] = 0x" <<std::hex<<
2880 (
unsigned int)((
unsigned char*)& ((*this)[i][j])) [k]
2881 <<
"; " << std::endl;
2903 for (
unsigned int i=0;i<
dsize;i++) {
2904 x = *(
data +i); norm += x*x;
2927 for (
unsigned int i=0;i<
rowNum;i++){
2929 for (
unsigned int j=0; j<
colNum;j++){
2930 x += fabs (*(*(
rowPtrs + i)+j)) ;
2947 double vpMatrix::detByLU()
const
2964 unsigned int * perm =
new unsigned int[
rowNum];
2966 tmp.LUDcmp(perm, d);
2971 for(
unsigned int i=0;i<
rowNum;i++)
2978 vpERROR_TRACE(
"Determinant Computation : ERR Matrix not squared") ;
2980 "\n\t\tDeterminant Computation : ERR Matrix not squared")) ;
3023 const unsigned int c)
3027 for(
unsigned int i=r; i<(r+A.
getRows()); i++){
3028 for(
unsigned int j=c; j<(c+A.
getCols()); j++){
3029 (*this)[i][j] = A[i-r][j-c];
3035 "\n\t\tIncorrect size of the matrix to insert data.");
3082 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not square") ;
3084 "\n\t\tEigen values computation: ERR Matrix not square")) ;
3087 #ifdef VISP_HAVE_GSL
3091 for (
unsigned int i=0; i <
rowNum; i++) {
3092 for (
unsigned int j=0; j <
rowNum; j++) {
3094 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
3095 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not symmetric") ;
3097 "\n\t\tEigen values computation: "
3098 "ERR Matrix not symmetric")) ;
3106 gsl_vector *eval = gsl_vector_alloc (rowNum);
3107 gsl_matrix *evec = gsl_matrix_alloc (rowNum,
colNum);
3109 gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (rowNum);
3110 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
3112 unsigned int Atda = m->tda ;
3113 for (
unsigned int i=0 ; i <
rowNum ; i++){
3114 unsigned int k = i*Atda ;
3115 for (
unsigned int j=0 ; j <
colNum ; j++)
3116 m->data[k+j] = (*
this)[i][j] ;
3118 gsl_eigen_symmv (m, eval, evec, w);
3120 gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
3122 for (
unsigned int i=0; i <
rowNum; i++) {
3123 evalue[i] = gsl_vector_get (eval, i);
3126 gsl_eigen_symmv_free (w);
3127 gsl_vector_free (eval);
3128 gsl_matrix_free (m);
3129 gsl_matrix_free (evec);
3135 vpERROR_TRACE(
"Not implemented since the GSL library is not detected.") ;
3137 "\n\t\tEigen values Computation: Not implemented "
3138 "since the GSL library is not detected")) ;
3197 #ifdef VISP_HAVE_GSL
3204 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not square") ;
3206 "\n\t\tEigen values computation: ERR Matrix not square")) ;
3209 #ifdef VISP_HAVE_GSL
3213 for (
unsigned int i=0; i <
rowNum; i++) {
3214 for (
unsigned int j=0; j <
rowNum; j++) {
3216 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
3217 vpERROR_TRACE(
"Eigen values computation: ERR Matrix not symmetric") ;
3219 "\n\t\tEigen values computation: "
3220 "ERR Matrix not symmetric")) ;
3229 gsl_vector *eval = gsl_vector_alloc (rowNum);
3230 gsl_matrix *evec = gsl_matrix_alloc (rowNum,
colNum);
3232 gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (rowNum);
3233 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
3235 unsigned int Atda = m->tda ;
3236 for (
unsigned int i=0 ; i <
rowNum ; i++){
3237 unsigned int k = i*Atda ;
3238 for (
unsigned int j=0 ; j <
colNum ; j++)
3239 m->data[k+j] = (*
this)[i][j] ;
3241 gsl_eigen_symmv (m, eval, evec, w);
3243 gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
3245 for (
unsigned int i=0; i <
rowNum; i++) {
3246 evalue[i] = gsl_vector_get (eval, i);
3249 for (
unsigned int i=0; i <
rowNum; i++) {
3250 unsigned int k = i*Atda ;
3251 for (
unsigned int j=0; j <
rowNum; j++) {
3252 evector[i][j] = evec->
data[k+j];
3274 gsl_eigen_symmv_free (w);
3275 gsl_vector_free (eval);
3276 gsl_matrix_free (m);
3277 gsl_matrix_free (evec);
3281 vpERROR_TRACE(
"Not implemented since the GSL library is not detected.") ;
3283 "\n\t\tEigen values Computation: Not implemented "
3284 "since the GSL library is not detected")) ;
3303 std::cout <<
"begin Kernel" << std::endl ;
3306 unsigned int ncaptor =
getRows() ;
3307 unsigned int ddl =
getCols() ;
3311 std::cout <<
"Erreur Matrice non initialise" << std::endl ;
3315 std::cout <<
"Interaction matrix L" << std::endl ;
3316 std::cout << *this ;
3317 std::cout <<
"signaux capteurs : " << ncaptor << std::endl ;
3324 if (ncaptor > ddl) min = ddl ;
else min = ncaptor ;
3345 for (i=0 ; i < ncaptor ; i++)
3346 for (j=0 ; j < ddl ; j++)
3348 a1[i][j] = (*this)[i][j] ;
3349 a2[i][j] = (*this)[i][j] ;
3355 for (i=ncaptor ; i < ddl ; i++)
3356 for (j=0 ; j < ddl ; j++)
3370 for (i=0 ; i < ddl ; i++)
3371 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
3375 std::cout <<
"Singular Value : (" ;
3376 for (i=0 ; i < ddl ; i++) std::cout << sv[i] <<
" , " ;
3377 std::cout <<
")" << std::endl ;
3381 unsigned int rank = 0 ;
3382 for (i=0 ; i < ddl ; i++)
3383 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
3388 for (i = 0 ; i < ddl ; i++)
3390 for (j = 0 ; j < ncaptor ; j++)
3398 for (k=0 ; k < ddl ; k++) if (fabs(sv[k]) > maxsv*svThreshold)
3400 C[i][j] += v[i][k]*a1[j][k]/sv[k];
3406 std::cout << C << std::endl ;
3423 for (i=0 ; i < ddl ; i++)
3424 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
3426 for (i=0 ; i < ddl ; i++)
3427 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
3431 for (j = 0 ; j < ddl ; j++)
3433 for (i = 0 ; i < ddl ; i++)
3436 if (fabs(sv[i]) <= maxsv*svThreshold)
3438 cons[i][j] = v[j][i];
3444 for (j = 0 ; j < ddl ; j++)
3449 if (std::fabs(cons.
row(j+1).
sumSquare()) > std::numeric_limits<double>::epsilon())
3451 for (i=0 ; i < cons.
getCols() ; i++)
3452 Ker[k][i] = cons[j][i] ;
3461 std::cout <<
"end Kernel" << std::endl ;
3502 det_ = this->detByLU();
3524 const bool binary,
const char *header)
3529 file.open(filename, std::fstream::out);
3531 file.open(filename, std::fstream::out|std::fstream::binary);
3545 while (header[i] !=
'\0')
3548 if (header[i] ==
'\n')
3555 file << M << std::endl;
3560 while (header[headerSize] !=
'\0') headerSize++;
3561 file.write(header,headerSize+1);
3562 unsigned int matrixSize;
3564 file.write((
char*)&matrixSize,
sizeof(
int));
3566 file.write((
char*)&matrixSize,
sizeof(
int));
3568 for(
unsigned int i = 0; i < M.
getRows(); i++)
3570 for(
unsigned int j = 0; j < M.
getCols(); j++)
3573 file.write((
char*)&value,
sizeof(
double));
3627 file.open(filename, std::fstream::out);
3636 bool inIndent =
false;
3637 std::string indent =
"";
3638 bool checkIndent =
true;
3639 while (header[i] !=
'\0')
3646 if(header[i] ==
' ')
3648 else if (indent.length() > 0)
3649 checkIndent =
false;
3651 if (header[i] ==
'\n' || (inIndent && header[i] ==
' '))
3661 file <<
"rows: " << M.
getRows() << std::endl;
3662 file <<
"cols: " << M.
getCols() << std::endl;
3664 if(indent.length() == 0)
3667 file <<
"data: " << std::endl;
3671 file << indent <<
"- [";
3673 file << M[i][j] <<
", ";
3674 file << M[i][j] <<
"]" << std::endl;
3700 file.open(filename, std::fstream::in);
3702 file.open(filename, std::fstream::in|std::fstream::binary);
3716 while ((c !=
'\0') && (c !=
'\n'))
3722 strncpy(header, h.c_str(), h.size() + 1);
3724 unsigned int rows, cols;
3728 if (rows > std::numeric_limits<unsigned int>::max()
3729 || cols > std::numeric_limits<unsigned int>::max())
3735 for(
unsigned int i = 0; i < rows; i++)
3737 for(
unsigned int j = 0; j < cols; j++)
3748 while ((c !=
'\0') && (c !=
'\n'))
3754 strncpy(header, h.c_str(), h.size() + 1);
3756 unsigned int rows, cols;
3757 file.read((
char*)&rows,
sizeof(
unsigned int));
3758 file.read((
char*)&cols,
sizeof(
unsigned int));
3762 for(
unsigned int i = 0; i < rows; i++)
3764 for(
unsigned int j = 0; j < cols; j++)
3766 file.read((
char*)&value,
sizeof(
double));
3795 file.open(filename, std::fstream::in);
3803 unsigned int rows = 0,cols = 0;
3805 std::string line,subs;
3806 bool inheader =
true;
3807 unsigned int i=0, j;
3808 unsigned int lineStart = 0;
3810 while ( getline (file,line) )
3814 if(rows == 0 && line.compare(0,5,
"rows:") == 0)
3816 std::stringstream ss(line);
3820 else if (cols == 0 && line.compare(0,5,
"cols:") == 0)
3822 std::stringstream ss(line);
3826 else if (line.compare(0,5,
"data:") == 0)
3836 if(rows == 0 || cols == 0)
3843 lineStart = (
unsigned int)line.find(
"[") + 1;
3845 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
3847 while(getline(ss, subs,
','))
3848 M[i][j++] = atof(subs.c_str());
3854 strncpy(header, h.substr(0,h.length()-1).c_str(), h.size());
3872 vpTRACE(
"The matrix must be square");
3874 "The matrix must be square" ));
3878 #ifdef VISP_HAVE_GSL
3880 double *b =
new double [size];
3881 for (
size_t i=0; i< size; i++)
3883 gsl_matrix_view m = gsl_matrix_view_array(this->
data,
rowNum, colNum);
3884 gsl_matrix_view em = gsl_matrix_view_array(b,
rowNum, colNum);
3885 gsl_linalg_exponential_ss(&m.matrix, &em.matrix, 0);
3888 memcpy(expA.
data, b, size *
sizeof(
double));
3909 for (
unsigned int i = 0; i <
rowNum;i++)
3912 for (
unsigned int j=0; j <
colNum; j++)
3914 sum += fabs((*
this)[i][j]);
3926 double sca = 1.0 / pow(2.0,s);
3931 for (
int k=2;k<=q;k++)
3933 c = c * ((double)(q-k+1)) / ((
double)(k*(2*q-k+1)));
3938 if (p) _expD=_expD+_expcX;
3939 else _expD=_expD- _expcX;
3944 for (
int k=1;k<=s;k++)
3957 double *dataptr =
data;
3958 double min = *dataptr;
3960 for (
unsigned int i = 0; i <
dsize-1; i++)
3962 if (*dataptr < min) min = *dataptr;
3971 double *dataptr =
data;
3972 double max = *dataptr;
3974 for (
unsigned int i = 0; i <
dsize-1; i++)
3976 if (*dataptr > max) max = *dataptr;
4006 for (
unsigned int i = 0 ; i < col ; i++)
4008 for (
unsigned int j = 0 ; j <
row ; j++)
4009 M_comp[i][j]=M[i][j];
4010 for (
unsigned int j = row+1 ; j < M.
getRows() ; j++)
4011 M_comp[i][j-1]=M[i][j];
4013 for (
unsigned int i = col+1 ; i < M.
getCols(); i++)
4015 for (
unsigned int j = 0 ; j <
row ; j++)
4016 M_comp[i-1][j]=M[i][j];
4017 for (
unsigned int j = row+1 ; j < M.
getRows() ; j++)
4018 M_comp[i-1][j-1]=M[i][j];
4037 for(
unsigned int i=0;i<M.
getCols();i++)
4039 if(min>w[i])min=w[i];
4040 if(max<w[i])max=w[i];
4055 vpTRACE(
"The matrix must be square");
4057 "The matrix must be square" ));
4061 for(
unsigned int i=0;i<H.
getCols();i++)
4063 for(
unsigned int j=0;j<H.
getCols();j++)
4067 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)
vpRowVector row(const unsigned int i)
Row extraction.
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.
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.
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)
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)
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 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
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)