57 #include <visp3/core/vpConfig.h>
60 #include <gsl/gsl_linalg.h>
63 #include <visp3/core/vpMatrix.h>
64 #include <visp3/core/vpMath.h>
65 #include <visp3/core/vpTranslationVector.h>
66 #include <visp3/core/vpColVector.h>
67 #include <visp3/core/vpException.h>
68 #include <visp3/core/vpDebug.h>
79 unsigned int r,
unsigned int c,
80 unsigned int nrows,
unsigned int ncols)
83 if (((r + nrows) > M.
rowNum) || ((c + ncols) > M.
colNum)) {
85 "Cannot construct a sub matrix (%dx%d) starting at position (%d,%d) that is not contained in the original matrix (%dx%d)",
89 init(M, r, c, nrows, ncols);
139 unsigned int rnrows = r+nrows ;
140 unsigned int cncols = c+ncols ;
144 "Bad row dimension (%d > %d) used to initialize vpMatrix", rnrows, M.
getRows()));
147 "Bad column dimension (%d > %d) used to initialize vpMatrix", cncols, M.
getCols()));
152 for (
unsigned int i=r ; i < rnrows; i++)
153 for (
unsigned int j=c ; j < cncols; j++)
154 (*
this)[i-r][j-c] = M[i][j] ;
196 for (
unsigned int i=0; i<
rowNum; i++) {
197 for (
unsigned int j=0; j<
colNum; j++) {
198 if (i == j) (*this)[i][j] = 1.0;
199 else (*
this)[i][j] = 0;
211 for (
unsigned int i=0;i<
rowNum;i++)
212 for (
unsigned int j=0;j<
colNum;j++)
213 if (i==j) (*this)[i][j] = val ;
214 else (*
this)[i][j] = 0;
233 for (
unsigned int i=0;i<
rowNum;i++) {
234 double *coli = (*this)[i] ;
235 for (
unsigned int j=0;j<
colNum;j++)
270 double ** AtRowPtrs = At.
rowPtrs;
272 for(
unsigned int i = 0; i <
colNum; i++ ) {
273 double * row_ = AtRowPtrs[i];
275 for(
unsigned int j = 0; j <
rowNum; j++, col+=A_step )
317 for(
unsigned int i=0;i<
rowNum;i++){
318 for(
unsigned int j=i;j<
rowNum;j++){
324 for(
unsigned int k=0; k <
colNum ;k++)
325 ssum += *(pi++)* *(pj++);
368 s +=(*(ptr+i)) * (*(ptr+j));
378 s +=(*(ptr+i)) * (*(ptr+i));
433 for (
unsigned int i=0;i<
rowNum;i++)
434 for(
unsigned int j=0;j<
colNum;j++)
448 for (
unsigned int i=0; i<
rowNum; i++) {
449 for (
unsigned int j=0; j<
colNum; j++) {
495 unsigned int rows = A.
getRows() ;
503 for (
unsigned int i=0 ; i< rows ; i++ )
504 (*
this)[i][i] = A[i] ;
543 for (
unsigned int i=0 ; i< min_ ; i++ )
544 (*
this)[i][i] = val;
562 unsigned int rows = A.
getRows() ;
571 for (
unsigned int i=0 ; i< rows ; i++ )
586 "Cannot multiply a (%dx%d) matrix by a (%dx%d) translation vector",
590 for (
unsigned int j=0;j<3;j++) t_out[j]=0 ;
592 for (
unsigned int j=0;j<3;j++) {
594 for (
unsigned int i=0;i<3;i++) {
625 "Cannot multiply a (%dx%d) matrix by a (%d) column vector",
637 for (
unsigned int j=0;j<A.
colNum;j++) {
639 for (
unsigned int i=0;i<A.
rowNum;i++) {
669 "Cannot multiply (%dx%d) matrix by (%dx%d) matrix",
674 unsigned int BcolNum = B.
colNum;
675 unsigned int BrowNum = B.
rowNum;
680 double *rowptri = A.
rowPtrs[i];
682 for (j=0;j<BcolNum;j++)
685 for (k=0;k<BrowNum;k++) s += rowptri[k] * BrowPtrs[k][j];
708 "Cannot multiply (%dx%d) matrix by (%dx%d) matrix as a rotation matrix",
713 unsigned int BcolNum = B.
colNum;
714 unsigned int BrowNum = B.
rowNum;
719 double *rowptri = A.
rowPtrs[i];
721 for (j=0;j<BcolNum;j++)
724 for (k=0;k<BrowNum;k++) s += rowptri[k] * BrowPtrs[k][j];
747 "Cannot multiply (%dx%d) matrix by (%dx%d) matrix as a rotation matrix",
752 unsigned int BcolNum = B.
colNum;
753 unsigned int BrowNum = B.
rowNum;
758 double *rowptri = A.
rowPtrs[i];
760 for (j=0;j<BcolNum;j++)
763 for (k=0;k<BrowNum;k++) s += rowptri[k] * BrowPtrs[k][j];
807 "Cannot multiply (%dx%d) matrix by (3x3) rotation matrix",
812 unsigned int RcolNum = R.
getCols();
813 unsigned int RrowNum = R.
getRows();
814 for (
unsigned int i=0;i<
rowNum;i++)
818 for (
unsigned int j=0;j<RcolNum;j++)
821 for (
unsigned int k=0;k<RrowNum;k++) s += rowptri[k] * R[k][j];
836 "Cannot multiply (%dx%d) matrix by (3x3) velocity twist matrix",
841 unsigned int VcolNum = V.
getCols();
842 unsigned int VrowNum = V.
getRows();
843 for (
unsigned int i=0;i<
rowNum;i++)
847 for (
unsigned int j=0;j<VcolNum;j++)
850 for (
unsigned int k=0;k<VrowNum;k++) s += rowptri[k] * V[k][j];
865 "Cannot multiply (%dx%d) matrix by (3x3) force/torque twist matrix",
870 unsigned int VcolNum = V.
getCols();
871 unsigned int VrowNum = V.
getRows();
872 for (
unsigned int i=0;i<
rowNum;i++)
876 for (
unsigned int j=0;j<VcolNum;j++)
879 for (
unsigned int k=0;k<VrowNum;k++) s += rowptri[k] * V[k][j];
908 "Cannot add (%dx%d) matrix with (%dx%d) matrix",
916 for (
unsigned int i=0;i<A.
rowNum;i++)
917 for(
unsigned int j=0;j<A.
colNum;j++)
918 CrowPtrs[i][j] = wB*BrowPtrs[i][j]+wA*ArowPtrs[i][j];
941 "Cannot add (%dx%d) matrix with (%dx%d) matrix",
949 for (
unsigned int i=0;i<A.
rowNum;i++) {
950 for(
unsigned int j=0;j<A.
colNum;j++) {
951 CrowPtrs[i][j] = BrowPtrs[i][j]+ArowPtrs[i][j];
979 "Cannot add (%dx%d) matrix with (%dx%d) matrix",
987 for (
unsigned int i=0;i<A.
rowNum;i++) {
988 for(
unsigned int j=0;j<A.
colNum;j++) {
989 CrowPtrs[i][j] = BrowPtrs[i][j]+ArowPtrs[i][j];
1031 "Cannot substract (%dx%d) matrix to (%dx%d) matrix",
1039 for (
unsigned int i=0;i<A.
rowNum;i++) {
1040 for(
unsigned int j=0;j<A.
colNum;j++) {
1041 CrowPtrs[i][j] = ArowPtrs[i][j]-BrowPtrs[i][j];
1068 "Cannot substract (%dx%d) matrix to (%dx%d) matrix",
1076 for (
unsigned int i=0;i<A.
rowNum;i++) {
1077 for(
unsigned int j=0;j<A.
colNum;j++) {
1078 CrowPtrs[i][j] = ArowPtrs[i][j]-BrowPtrs[i][j];
1100 "Cannot add (%dx%d) matrix to (%dx%d) matrix",
1106 for (
unsigned int i=0;i<
rowNum;i++)
1107 for(
unsigned int j=0;j<
colNum;j++)
1108 rowPtrs[i][j] += BrowPtrs[i][j];
1118 "Cannot substract (%dx%d) matrix to (%dx%d) matrix",
1123 for (
unsigned int i=0;i<
rowNum;i++)
1124 for(
unsigned int j=0;j<
colNum;j++)
1125 rowPtrs[i][j] -= BrowPtrs[i][j];
1152 for (
unsigned int i=0;i<A.
rowNum;i++)
1153 for(
unsigned int j=0;j<A.
colNum;j++)
1154 CrowPtrs[i][j]= -ArowPtrs[i][j];
1173 for (
unsigned int i=0;i<
rowNum;i++)
1175 for(
unsigned int j=0;j<
colNum;j++)
1204 unsigned int Brow = B.
getRows() ;
1205 unsigned int Bcol = B.
getCols() ;
1207 for (
unsigned int i=0;i<Brow;i++)
1208 for(
unsigned int j=0;j<Bcol;j++)
1222 for (
unsigned int i=0;i<
rowNum;i++)
1223 for(
unsigned int j=0;j<
colNum;j++)
1242 if (std::fabs(x) <= std::numeric_limits<double>::epsilon()) {
1248 for (
unsigned int i=0;i<
rowNum;i++)
1249 for(
unsigned int j=0;j<
colNum;j++)
1259 for (
unsigned int i=0;i<
rowNum;i++)
1260 for(
unsigned int j=0;j<
colNum;j++)
1270 for (
unsigned int i=0;i<
rowNum;i++)
1271 for(
unsigned int j=0;j<
colNum;j++)
1283 for (
unsigned int i=0;i<
rowNum;i++)
1284 for(
unsigned int j=0;j<
colNum;j++)
1294 if (std::fabs(x) <= std::numeric_limits<double>::epsilon())
1299 for (
unsigned int i=0;i<
rowNum;i++)
1300 for(
unsigned int j=0;j<
colNum;j++)
1329 double *optr=out.
data;
1330 for(
unsigned int j =0;j<
colNum ; j++){
1331 for(
unsigned int i =0;i<
rowNum ; i++){
1362 double *optr=out.
data;
1363 for(
unsigned int i =0;i<
dsize ; i++){
1364 *(optr++)=*(mdata++);
1386 unsigned int r1= m1.
getRows();
1387 unsigned int c1= m1.
getCols();
1388 unsigned int r2= m2.
getRows();
1389 unsigned int c2= m2.
getCols();
1393 vpERROR_TRACE(
"Kronecker prodect bad dimension of output vpMatrix") ;
1395 "In Kronecker product bad dimension of output matrix"));
1398 for(
unsigned int r =0;r<r1 ; r++){
1399 for(
unsigned int c =0;c<c1 ; c++){
1400 double alpha = m1[r][c];
1401 double *m2ptr = m2[0];
1402 unsigned int roffset= r*r2;
1403 unsigned int coffset= c*c2;
1404 for(
unsigned int rr =0;rr<r2 ; rr++){
1405 for(
unsigned int cc =0;cc<c2 ;cc++){
1406 out[roffset+rr][coffset+cc]= alpha* *(m2ptr++);
1432 unsigned int r1= m1.
getRows();
1433 unsigned int c1= m1.
getCols();
1434 unsigned int r2= m2.
getRows();
1435 unsigned int c2= m2.
getCols();
1439 for(
unsigned int r =0;r<r1 ; r++){
1440 for(
unsigned int c =0;c<c1 ; c++){
1441 double alpha = m1[r][c];
1442 double *m2ptr = m2[0];
1443 unsigned int roffset= r*r2;
1444 unsigned int coffset= c*c2;
1445 for(
unsigned int rr =0;rr<r2 ; rr++){
1446 for(
unsigned int cc =0;cc<c2 ;cc++){
1447 out[roffset+rr ][coffset+cc]= alpha* *(m2ptr++);
1463 return kron(*
this,m);
1653 #if defined (VISP_HAVE_LAPACK_C)
1655 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
1657 #elif defined (VISP_HAVE_GSL)
1668 unsigned int i,j,k,nrows,ncols;
1675 #ifdef VISP_HAVE_GSL
1684 Asvd.
resize(nrows,ncols);
1686 for (i = 0 ; i < nrows ; i++)
1688 for (j = 0 ; j < ncols ; j++)
1691 for (k=0 ; k < ncols ; k++) Asvd[i][j] += (*
this)[i][k]*w[k]*v[j][k];
1694 for (i=0;i<nrows;i++)
1696 for (j=0;j<ncols;j++)
if (fabs(A[i][j]-Asvd[i][j]) > 1e-6) pb = 1;
1700 printf(
"pb in SVD\n");
1701 std::cout <<
" A : " << std::endl << A << std::endl;
1702 std::cout <<
" Asvd : " << std::endl << Asvd << std::endl;
1816 unsigned int i, j, k ;
1818 unsigned int nrows, ncols;
1819 unsigned int nrows_orig =
getRows() ;
1820 unsigned int ncols_orig =
getCols() ;
1821 Ap.
resize(ncols_orig,nrows_orig) ;
1823 if (nrows_orig >= ncols_orig)
1839 if (nrows_orig >= ncols_orig) a = *
this;
1840 else a = (*this).
t();
1846 for (i=0 ; i < ncols ; i++)
1847 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
1849 unsigned int rank = 0 ;
1850 for (i=0 ; i < ncols ; i++)
1851 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
1854 for (i = 0 ; i < ncols ; i++)
1856 for (j = 0 ; j < nrows ; j++)
1860 for (k=0 ; k < ncols ; k++)
1861 if (fabs(sv[k]) > maxsv*svThreshold)
1863 a1[i][j] += v[i][k]*a[j][k]/sv[k];
1867 if (nrows_orig >= ncols_orig) Ap = a1;
1870 if (nrows_orig >= ncols_orig)
1873 imAt.
resize(ncols_orig,rank) ;
1874 for (i=0 ; i < ncols_orig ; i++)
1875 for (j=0 ; j < rank ; j++)
1876 imAt[i][j] = v[i][j] ;
1879 imA.
resize(nrows_orig,rank) ;
1880 for (i=0 ; i < nrows_orig ; i++)
1881 for (j=0 ; j < rank ; j++)
1882 imA[i][j] = a[i][j] ;
1887 imAt.
resize(ncols_orig,rank) ;
1888 for (i=0 ; i < ncols_orig ; i++)
1889 for (j=0 ; j < rank ; j++)
1890 imAt[i][j] = a[i][j] ;
1892 imA.
resize(nrows_orig,rank) ;
1893 for (i=0 ; i < nrows_orig ; i++)
1894 for (j=0 ; j < rank ; j++)
1895 imA[i][j] = v[i][j] ;
1902 vpMatrix A, ApA, AAp, AApA, ApAAp ;
1915 for (i=0;i<nrows;i++)
1917 for (j=0;j<ncols;j++)
if (fabs(AApA[i][j]-A[i][j]) > 1e-6) pb = 1;
1919 for (i=0;i<ncols;i++)
1921 for (j=0;j<nrows;j++)
if (fabs(ApAAp[i][j]-Ap[i][j]) > 1e-6) pb = 1;
1923 for (i=0;i<nrows;i++)
1925 for (j=0;j<nrows;j++)
if (fabs(AAp[i][j]-AAp[j][i]) > 1e-6) pb = 1;
1927 for (i=0;i<ncols;i++)
1929 for (j=0;j<ncols;j++)
if (fabs(ApA[i][j]-ApA[j][i]) > 1e-6) pb = 1;
1933 printf(
"pb in pseudo inverse\n");
1934 std::cout <<
" A : " << std::endl << A << std::endl;
1935 std::cout <<
" Ap : " << std::endl << Ap << std::endl;
1936 std::cout <<
" A - AApA : " << std::endl << A - AApA << std::endl;
1937 std::cout <<
" Ap - ApAAp : " << std::endl << Ap - ApAAp << std::endl;
1938 std::cout <<
" AAp - (AAp)^T : " << std::endl << AAp - AAp.
t() << std::endl;
1939 std::cout <<
" ApA - (ApA)^T : " << std::endl << ApA - ApA.
t() << 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] ;
2078 vpMatrix cons(ncols_orig, ncols_orig);
2081 for (j = 0; j < ncols_orig; j++)
2083 for (i = 0; i < ncols_orig; i++)
2085 if (fabs(sv[i]) <= maxsv*svThreshold)
2087 cons[i][j] = v[j][i];
2092 vpMatrix Ker (ncols_orig-rank, ncols_orig);
2094 for (j = 0; j < ncols_orig ; j++)
2097 if ( std::fabs(cons.
getRow(j).
sumSquare()) > std::numeric_limits<double>::epsilon())
2099 for (i = 0; i < cons.
getCols(); i++)
2100 Ker[k][i] = cons[j][i];
2110 vpMatrix A, ApA, AAp, AApA, ApAAp ;
2123 for (i=0;i<nrows;i++)
2125 for (j=0;j<ncols;j++)
if (fabs(AApA[i][j]-A[i][j]) > 1e-6) pb = 1;
2127 for (i=0;i<ncols;i++)
2129 for (j=0;j<nrows;j++)
if (fabs(ApAAp[i][j]-Ap[i][j]) > 1e-6) pb = 1;
2131 for (i=0;i<nrows;i++)
2133 for (j=0;j<nrows;j++)
if (fabs(AAp[i][j]-AAp[j][i]) > 1e-6) pb = 1;
2135 for (i=0;i<ncols;i++)
2137 for (j=0;j<ncols;j++)
if (fabs(ApA[i][j]-ApA[j][i]) > 1e-6) pb = 1;
2141 printf(
"pb in pseudo inverse\n");
2142 std::cout <<
" A : " << std::endl << A << std::endl;
2143 std::cout <<
" Ap : " << std::endl << Ap << std::endl;
2144 std::cout <<
" A - AApA : " << std::endl << A - AApA << std::endl;
2145 std::cout <<
" Ap - ApAAp : " << std::endl << Ap - ApAAp << std::endl;
2146 std::cout <<
" AAp - (AAp)^T : " << std::endl << AAp - AAp.
t() << std::endl;
2147 std::cout <<
" ApA - (ApA)^T : " << std::endl << ApA - ApA.
t() << std::endl;
2148 std::cout <<
" KerA : " << std::endl << kerA << std::endl;
2200 vpMatrix::getCol(
const unsigned int j,
const unsigned int i_begin,
const unsigned int column_size)
const
2205 for (
unsigned int i=0 ; i < column_size ; i++)
2206 c[i] = (*
this)[i_begin+i][j];
2254 unsigned int nb_rows =
getRows();
2256 for (
unsigned int i=0 ; i < nb_rows ; i++)
2257 c[i] = (*
this)[i][j];
2301 unsigned int nb_cols =
getCols();
2303 for (
unsigned int j=0 ; j < nb_cols ; j++)
2304 r[j] = (*
this)[i][j];
2346 vpMatrix::getRow(
const unsigned int i,
const unsigned int j_begin,
const unsigned int row_size)
const
2351 for (
unsigned int j=0 ; j < row_size ; j++)
2352 r[j] = (*
this)[i][j_begin+i];
2416 unsigned int nra = A.
getRows() ;
2417 unsigned int nrb = B.
getRows() ;
2422 "Cannot stack (%dx%d) matrix with (%dx%d) matrix",
2434 for (i=0 ; i < nra ; i++) {
2435 for (j=0 ; j < A.
getCols() ; j++)
2439 for (i=0 ; i < nrb ; i++) {
2440 for (j=0 ; j < B.
getCols() ; j++) {
2441 C[i+nra][j] = B[i][j] ;
2458 unsigned int nra = A.
getRows() ;
2463 "Cannot stack (%dx%d) matrix with (1x%d) row vector",
2475 for (i=0 ; i < nra ; i++) {
2476 for (j=0 ; j < A.
getCols() ; j++)
2480 for (j=0 ; j < r.
getCols() ; j++) {
2498 const unsigned int r,
const unsigned int c)
2527 const unsigned int r,
const unsigned int c)
2537 for(
unsigned int i=0; i<A.
getRows(); i++){
2538 for(
unsigned int j=0; j<A.
getCols(); j++){
2539 if(i >= r && i < (r + B.
getRows()) && j >= c && j < (c+B.
getCols())){
2540 C[i][j] = B[i-r][j-c];
2550 "Cannot insert (%dx%d) matrix in (%dx%d) matrix at position (%d,%d)",
2596 unsigned int nca = A.
getCols() ;
2597 unsigned int ncb = B.
getCols() ;
2602 "Cannot juxtapose (%dx%d) matrix with (%dx%d) matrix",
2614 for (i=0 ; i < C.
getRows(); i++)
2615 for (j=0 ; j < nca ; j++)
2618 for (i=0 ; i < C.
getRows() ; i++)
2619 for (j=0 ; j < ncb ; j++){
2620 C[i][nca+j] = B[i][j] ;
2651 typedef std::string::size_type size_type;
2656 std::vector<std::string> values(m*n);
2657 std::ostringstream oss;
2658 std::ostringstream ossFixed;
2659 std::ios_base::fmtflags original_flags = oss.flags();
2662 ossFixed.setf ( std::ios::fixed, std::ios::floatfield );
2664 size_type maxBefore=0;
2665 size_type maxAfter=0;
2667 for (
unsigned int i=0;i<m;++i) {
2668 for (
unsigned int j=0;j<n;++j){
2670 oss << (*this)[i][j];
2671 if (oss.str().find(
"e")!=std::string::npos){
2673 ossFixed << (*this)[i][j];
2674 oss.str(ossFixed.str());
2677 values[i*n+j]=oss.str();
2678 size_type thislen=values[i*n+j].size();
2679 size_type p=values[i*n+j].find(
'.');
2681 if (p==std::string::npos){
2691 size_type totalLength=length;
2695 maxAfter=std::min(maxAfter, totalLength-maxBefore);
2696 if (maxAfter==1) maxAfter=0;
2701 if (intro) s <<intro;
2702 s <<
"["<<m<<
","<<n<<
"]=\n";
2704 for (
unsigned int i=0;i<m;i++) {
2706 for (
unsigned int j=0;j<n;j++){
2707 size_type p=values[i*n+j].find(
'.');
2708 s.setf(std::ios::right, std::ios::adjustfield);
2709 s.width((std::streamsize)maxBefore);
2710 s <<values[i*n+j].substr(0,p).c_str();
2713 s.setf(std::ios::left, std::ios::adjustfield);
2714 if (p!=std::string::npos){
2715 s.width((std::streamsize)maxAfter);
2716 s <<values[i*n+j].substr(p,maxAfter).c_str();
2719 s.width((std::streamsize)maxAfter);
2729 s.flags(original_flags);
2731 return (
int)(maxBefore+maxAfter);
2748 for (
unsigned int i=0; i < this->
getRows(); ++ i) {
2749 for (
unsigned int j=0; j <
this ->getCols(); ++ j) {
2750 os << (*this)[i][j] <<
", ";
2752 if (
this ->
getRows() != i+1) { os <<
";" << std::endl; }
2753 else { os <<
"]" << std::endl; }
2772 os <<
"([ " << std::endl;
2773 for (
unsigned int i=0; i < this->
getRows(); ++ i) {
2775 for (
unsigned int j=0; j < this->
getCols(); ++ j) {
2776 os << (*this)[i][j] <<
", ";
2778 os <<
"]," << std::endl;
2780 os <<
"])" << std::endl;
2796 for (
unsigned int i=0; i < this->
getRows(); ++ i) {
2797 for (
unsigned int j=0; j < this->
getCols(); ++ j) {
2798 os << (*this)[i][j];
2799 if (!(j==(this->
getCols()-1)))
2826 const char defaultName [] =
"A";
2827 if (NULL == matrixName)
2829 matrixName = defaultName;
2831 os <<
"vpMatrix " << defaultName
2832 <<
" (" <<
this ->getRows ()
2833 <<
", " <<
this ->getCols () <<
"); " <<std::endl;
2835 for (
unsigned int i=0; i < this->
getRows(); ++ i)
2837 for (
unsigned int j=0; j <
this ->getCols(); ++ j)
2841 os << defaultName <<
"[" << i <<
"][" << j
2842 <<
"] = " << (*this)[i][j] <<
"; " << std::endl;
2846 for (
unsigned int k = 0; k <
sizeof(double); ++ k)
2848 os <<
"((unsigned char*)&(" << defaultName
2849 <<
"[" << i <<
"][" << j <<
"]) )[" << k
2850 <<
"] = 0x" <<std::hex<<
2851 (
unsigned int)((
unsigned char*)& ((*this)[i][j])) [k]
2852 <<
"; " << std::endl;
2869 double vpMatrix::detByLU()
const
2886 unsigned int * perm =
new unsigned int[
rowNum];
2888 tmp.LUDcmp(perm, d);
2893 for(
unsigned int i=0;i<
rowNum;i++)
2900 "Cannot compute LU decomposition on a non square matrix (%dx%d)",
2953 const unsigned int c)
2957 for(
unsigned int i=r; i<(r+A.
getRows()); i++){
2958 for(
unsigned int j=c; j<(c+A.
getCols()); j++){
2959 (*this)[i][j] = A[i-r][j-c];
2965 "Cannot insert (%dx%d) matrix in (%dx%d) matrix at position (%d,%d)",
3012 "Cannot compute eigen values on a non square matrix (%dx%d)",
3016 #ifdef VISP_HAVE_GSL
3020 for (
unsigned int i=0; i <
rowNum; i++) {
3021 for (
unsigned int j=0; j <
rowNum; j++) {
3023 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
3025 "Cannot compute eigen values on a non symetric matrix")) ;
3032 gsl_vector *eval = gsl_vector_alloc (rowNum);
3033 gsl_matrix *evec = gsl_matrix_alloc (rowNum,
colNum);
3035 gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (rowNum);
3036 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
3038 unsigned int Atda = m->tda ;
3039 for (
unsigned int i=0 ; i <
rowNum ; i++){
3040 unsigned int k = i*Atda ;
3041 for (
unsigned int j=0 ; j <
colNum ; j++)
3042 m->data[k+j] = (*
this)[i][j] ;
3044 gsl_eigen_symmv (m, eval, evec, w);
3046 gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
3048 for (
unsigned int i=0; i <
rowNum; i++) {
3049 evalue[i] = gsl_vector_get (eval, i);
3052 gsl_eigen_symmv_free (w);
3053 gsl_vector_free (eval);
3054 gsl_matrix_free (m);
3055 gsl_matrix_free (evec);
3062 "Eigen values computation is not implemented. You should install GSL rd party")) ;
3119 #ifdef VISP_HAVE_GSL
3125 if (rowNum != colNum) {
3127 "Cannot compute eigen values on a non square matrix (%dx%d)",
3131 #ifdef VISP_HAVE_GSL
3135 for (
unsigned int i=0; i < rowNum; i++) {
3136 for (
unsigned int j=0; j < rowNum; j++) {
3138 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
3140 "Cannot compute eigen values on a non symetric matrix")) ;
3147 evector.resize(rowNum, colNum);
3149 gsl_vector *eval = gsl_vector_alloc (rowNum);
3150 gsl_matrix *evec = gsl_matrix_alloc (rowNum, colNum);
3152 gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (rowNum);
3153 gsl_matrix *m = gsl_matrix_alloc(rowNum, colNum);
3155 unsigned int Atda = m->tda ;
3156 for (
unsigned int i=0 ; i < rowNum ; i++){
3157 unsigned int k = i*Atda ;
3158 for (
unsigned int j=0 ; j < colNum ; j++)
3159 m->data[k+j] = (*
this)[i][j] ;
3161 gsl_eigen_symmv (m, eval, evec, w);
3163 gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
3165 for (
unsigned int i=0; i < rowNum; i++) {
3166 evalue[i] = gsl_vector_get (eval, i);
3169 for (
unsigned int i=0; i < rowNum; i++) {
3170 unsigned int k = i*Atda ;
3171 for (
unsigned int j=0; j < rowNum; j++) {
3172 evector[i][j] = evec->data[k+j];
3176 gsl_eigen_symmv_free (w);
3177 gsl_vector_free (eval);
3178 gsl_matrix_free (m);
3179 gsl_matrix_free (evec);
3184 "Eigen values computation is not implemented. You should install GSL rd party")) ;
3204 unsigned int nbline =
getRows() ;
3205 unsigned int nbcol =
getCols() ;
3207 if ( (nbline <= 0) || (nbcol <= 0) ) {
3218 if (nbline < nbcol) A.
resize(nbcol,nbcol) ;
3219 else A.
resize(nbline,nbcol) ;
3221 for (i=0 ; i < nbline ; i++)
3223 for (j=0 ; j < nbcol ; j++)
3225 A[i][j] = (*this)[i][j] ;
3233 for (i=0 ; i < nbcol ; i++)
3234 if (fabs(sv[i]) > maxsv) maxsv = fabs(sv[i]) ;
3236 unsigned int rank = 0 ;
3237 for (i=0 ; i < nbcol ; i++)
3238 if (fabs(sv[i]) > maxsv*svThreshold) rank++ ;
3243 unsigned int k = 0 ;
3244 for (j = 0 ; j < nbcol ; j++)
3247 if ( (fabs(sv[j]) <= maxsv*svThreshold) && (std::fabs(v.
getCol(j).
sumSquare()) > std::numeric_limits<double>::epsilon()))
3250 for (i=0 ; i < v.
getRows() ; i++)
3252 Ker[k][i] = v[i][j];
3303 det_ = this->detByLU();
3321 "Cannot compute the exponential of a non square (%dx%d) matrix",
3326 #ifdef VISP_HAVE_GSL
3328 double *b =
new double [size_];
3329 for (
size_t i=0; i< size_; i++)
3331 gsl_matrix_view m = gsl_matrix_view_array(this->
data,
rowNum, colNum);
3332 gsl_matrix_view em = gsl_matrix_view_array(b,
rowNum, colNum);
3333 gsl_linalg_exponential_ss(&m.matrix, &em.matrix, 0);
3336 memcpy(expA.
data, b, size_ *
sizeof(
double));
3357 for (
unsigned int i = 0; i <
rowNum;i++)
3360 for (
unsigned int j=0; j <
colNum; j++)
3362 sum += fabs((*
this)[i][j]);
3374 double sca = 1.0 / pow(2.0,s);
3379 for (
int k=2;k<=q;k++)
3381 c = c * ((double)(q-k+1)) / ((
double)(k*(2*q-k+1)));
3386 if (p) _expD=_expD+_expcX;
3387 else _expD=_expD- _expcX;
3392 for (
int k=1;k<=s;k++)
3425 for (
unsigned int i = 0 ; i < col ; i++)
3427 for (
unsigned int j = 0 ; j < row ; j++)
3428 M_comp[i][j]=M[i][j];
3429 for (
unsigned int j = row+1 ; j < M.
getRows() ; j++)
3430 M_comp[i][j-1]=M[i][j];
3432 for (
unsigned int i = col+1 ; i < M.
getCols(); i++)
3434 for (
unsigned int j = 0 ; j < row ; j++)
3435 M_comp[i-1][j]=M[i][j];
3436 for (
unsigned int j = row+1 ; j < M.
getRows() ; j++)
3437 M_comp[i-1][j-1]=M[i][j];
3456 for(
unsigned int i=0;i<M.
getCols();i++)
3458 if(min>w[i])min=w[i];
3459 if(max<w[i])max=w[i];
3474 "Cannot compute HLM on a non square matrix (%dx%d)",
3479 for(
unsigned int i=0;i<H.
getCols();i++)
3481 for(
unsigned int j=0;j<H.
getCols();j++)
3485 HLM[i][j]+= alpha*H[i][j];
3501 for (
unsigned int i=0;i<
dsize;i++) {
3502 x = *(
data +i); norm += x*x;
3522 for (
unsigned int i=0;i<
rowNum;i++){
3524 for (
unsigned int j=0; j<
colNum;j++){
3525 x += fabs (*(*(
rowPtrs + i)+j)) ;
3541 double sum_square=0.0;
3544 for (
unsigned int i=0;i<
rowNum;i++) {
3545 for(
unsigned int j=0;j<
colNum;j++) {
3554 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
3574 #endif //#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
vpMatrix operator*(const double &x, const vpMatrix &B)
std::ostream & csvPrint(std::ostream &os) const
Implementation of a matrix and operations on matrices.
vpColVector eigenValues() const
static vpMatrix juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B)
double infinityNorm() const
unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6) const
void stack(const double &d)
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
std::ostream & cppPrint(std::ostream &os, const char *matrixName=NULL, bool octet=false) const
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of row vector and the associated operations.
static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
vpMatrix & operator*=(const double x)
Multiply all the element of the matrix by x : Aij = Aij * x.
void stack(const vpMatrix &A)
vpMatrix & operator/=(double x)
Divide all the element of the matrix by x : Aij = Aij / x.
error that can be emited by ViSP classes.
double * data
Address of the first element of the data array.
vp_deprecated void stackMatrices(const vpMatrix &A)
Implementation of a generic 2D array used as vase class of matrices and vectors.
static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
unsigned int getCols() const
Return the number of columns of the 2D array.
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
std::ostream & maplePrint(std::ostream &os) const
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
static Type maximum(const Type &a, const Type &b)
Implementation of a rotation matrix and operations on such kind of matrices.
vpRowVector getRow(const unsigned int i) const
unsigned int rowNum
Number of rows in the array.
void insert(const vpMatrix &A, const unsigned int r, const unsigned int c)
void solveBySVD(const vpColVector &B, vpColVector &x) const
vp_deprecated void setIdentity(const double &val=1.0)
void svd(vpColVector &w, vpMatrix &v)
void diag(const double &val=1.0)
vp_deprecated void init()
static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B, const double &wB, vpMatrix &C)
static void multMatrixVector(const vpMatrix &A, const vpColVector &v, vpColVector &w)
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
Implementation of a velocity twist matrix and operations on such kind of matrices.
unsigned int getRows() const
Return the number of rows of the 2D array.
double det(vpDetMethod method=LU_DECOMPOSITION) const
unsigned int colNum
Number of columns in the array.
int print(std::ostream &s, unsigned int length, char const *intro=0) const
void resize(const unsigned int i, const bool flagNullify=true)
Implementation of a force/torque twist matrix and operations on such kind of matrices.
void kron(const vpMatrix &m1, vpMatrix &out) const
Implementation of column vector and the associated operations.
double euclideanNorm() const
std::ostream & matlabPrint(std::ostream &os) const
vpMatrix inverseByLU() const
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
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 array size (rowNum * colNum)
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.
double ** rowPtrs
Address of the first element of each rows.
vpMatrix & operator=(const vpArray2D< double > &A)
void resize(const unsigned int i, const bool flagNullify=true)
vpMatrix & operator<<(double *)