55 #include <visp3/core/vpConfig.h> 58 #include <gsl/gsl_linalg.h> 61 #include <visp3/core/vpCPUFeatures.h> 62 #include <visp3/core/vpColVector.h> 63 #include <visp3/core/vpDebug.h> 64 #include <visp3/core/vpException.h> 65 #include <visp3/core/vpMath.h> 66 #include <visp3/core/vpMatrix.h> 67 #include <visp3/core/vpTranslationVector.h> 69 #define USE_SSE_CODE 1 70 #if defined __SSE2__ || defined _M_X64 || (defined _M_IX86_FP && _M_IX86_FP >= 2) 71 #include <emmintrin.h> 72 #define VISP_HAVE_SSE2 1 75 #if VISP_HAVE_SSE2 && USE_SSE_CODE 83 unsigned int ncols,
unsigned int nrows_orig,
unsigned int ncols_orig,
double svThreshold,
90 for (
unsigned int i = 0; i < ncols; i++) {
91 if (fabs(sv[i]) > maxsv)
97 for (
unsigned int i = 0; i < ncols; i++) {
98 if (fabs(sv[i]) > maxsv * svThreshold) {
102 for (
unsigned int j = 0; j < nrows; j++) {
105 for (
unsigned int k = 0; k < ncols; k++) {
106 if (fabs(sv[k]) > maxsv * svThreshold) {
107 a1[i][j] += v[i][k] * a[j][k] / sv[k];
112 if (nrows_orig >= ncols_orig)
119 unsigned int ncols_orig,
double svThreshold,
vpMatrix &Ap,
unsigned int &rank,
122 Ap.
resize(ncols_orig, nrows_orig);
125 double maxsv = fabs(sv[0]);
129 for (
unsigned int i = 0; i < ncols_orig; i++) {
130 if (fabs(sv[i]) > maxsv * svThreshold) {
134 for (
unsigned int j = 0; j < nrows_orig; j++) {
137 for (
unsigned int k = 0; k < ncols_orig; k++) {
138 if (fabs(sv[k]) > maxsv * svThreshold) {
139 Ap[i][j] += V[i][k] * U[j][k] / sv[k];
146 imA.
resize(nrows_orig, rank);
147 imAt.
resize(ncols_orig, rank);
149 for (
unsigned int i = 0; i < nrows_orig; i++) {
150 for (
unsigned int j = 0; j < rank; j++) {
155 for (
unsigned int i = 0; i < ncols_orig; i++) {
156 for (
unsigned int j = 0; j < rank; j++) {
157 imAt[i][j] = V[i][j];
161 kerAt.
resize(ncols_orig - rank, ncols_orig);
162 if (rank != ncols_orig) {
163 for (
unsigned int j = 0, k = 0; j < ncols_orig; j++) {
165 if ((fabs(sv[j]) <= maxsv * svThreshold) &&
166 (std::fabs(V.
getCol(j).
sumSquare()) > std::numeric_limits<double>::epsilon())) {
167 for (
unsigned int i = 0; i < V.
getRows(); i++) {
168 kerAt[k][i] = V[i][j];
184 if (((r + nrows) > M.
rowNum) || ((c + ncols) > M.
colNum)) {
186 "Cannot construct a sub matrix (%dx%d) starting at " 187 "position (%d,%d) that is not contained in the " 188 "original matrix (%dx%d)",
192 init(M, r, c, nrows, ncols);
195 #ifdef VISP_HAVE_CPP11_COMPATIBILITY 260 unsigned int rnrows = r + nrows;
261 unsigned int cncols = c + ncols;
269 resize(nrows, ncols,
false,
false);
273 for (
unsigned int i = 0; i < nrows; i++) {
274 memcpy((*
this)[i], &M[i + r][c], ncols *
sizeof(
double));
321 unsigned int rnrows = r + nrows;
322 unsigned int cncols = c + ncols;
332 for (
unsigned int i = 0; i < nrows; i++) {
333 memcpy(M[i], &(*
this)[i + r][c], ncols *
sizeof(
double));
362 for (
unsigned int i = 0; i <
rowNum; i++) {
363 for (
unsigned int j = 0; j <
colNum; j++) {
381 for (
unsigned int i = 0; i <
rowNum; i++) {
382 double *coli = (*this)[i];
383 for (
unsigned int j = 0; j <
colNum; j++)
411 double **AtRowPtrs = At.
rowPtrs;
413 for (
unsigned int i = 0; i <
colNum; i++) {
414 double *row_ = AtRowPtrs[i];
416 for (
unsigned int j = 0; j <
rowNum; j++, col += A_step)
452 for (
unsigned int i = 0; i <
rowNum; i++) {
453 for (
unsigned int j = i; j <
rowNum; j++) {
459 for (
unsigned int k = 0; k <
colNum; k++)
460 ssum += *(pi++) * *(pj++);
485 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) 491 vpMatrix::blas_dgemm(transa, transb,
colNum,
colNum,
rowNum, alpha,
data,
colNum,
data,
colNum, beta, B.
data,
colNum);
493 unsigned int i, j, k;
496 for (i = 0; i <
colNum; i++) {
498 for (j = 0; j < i; j++) {
501 for (k = 0; k <
rowNum; k++) {
502 s += (*(ptr + i)) * (*(ptr + j));
510 for (k = 0; k <
rowNum; k++) {
511 s += (*(ptr + i)) * (*(ptr + i));
558 #ifdef VISP_HAVE_CPP11_COMPATIBILITY 570 if (
this != &other) {
582 other.rowPtrs = NULL;
594 for (
unsigned int i = 0; i <
rowNum; i++)
595 for (
unsigned int j = 0; j <
colNum; j++)
608 for (
unsigned int i = 0; i <
rowNum; i++) {
609 for (
unsigned int j = 0; j <
colNum; j++) {
654 unsigned int rows = A.
getRows();
658 for (
unsigned int i = 0; i < rows; i++)
659 (*
this)[i][i] = A[i];
696 for (
unsigned int i = 0; i < min_; i++)
713 unsigned int rows = A.
getRows();
716 for (
unsigned int i = 0; i < rows; i++)
733 for (
unsigned int j = 0; j < 3; j++)
736 for (
unsigned int j = 0; j < 3; j++) {
738 for (
unsigned int i = 0; i < 3; i++) {
739 t_out[i] +=
rowPtrs[i][j] * tj;
774 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) 780 vpMatrix::blas_dgemv(trans, A.
colNum, A.
rowNum, alpha, A.
data, A.
colNum, v.
data, incr, beta, w.
data, incr);
783 for (
unsigned int j = 0; j < A.
colNum; j++) {
785 for (
unsigned int i = 0; i < A.
rowNum; i++) {
815 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) 820 vpMatrix::blas_dgemm(trans, trans, B.
colNum, A.
rowNum, A.
colNum, alpha, B.
data, B.
colNum, A.
data, A.
colNum, beta,
824 unsigned int BcolNum = B.
colNum;
825 unsigned int BrowNum = B.
rowNum;
826 unsigned int i, j, k;
828 for (i = 0; i < A.
rowNum; i++) {
829 double *rowptri = A.
rowPtrs[i];
831 for (j = 0; j < BcolNum; j++) {
833 for (k = 0; k < BrowNum; k++)
834 s += rowptri[k] * BrowPtrs[k][j];
858 "Cannot multiply (%dx%d) matrix by (%dx%d) matrix as a " 864 unsigned int BcolNum = B.
colNum;
865 unsigned int BrowNum = B.
rowNum;
866 unsigned int i, j, k;
868 for (i = 0; i < A.
rowNum; i++) {
869 double *rowptri = A.
rowPtrs[i];
871 for (j = 0; j < BcolNum; j++) {
873 for (k = 0; k < BrowNum; k++)
874 s += rowptri[k] * BrowPtrs[k][j];
897 "Cannot multiply (%dx%d) matrix by (%dx%d) matrix as a " 903 unsigned int BcolNum = B.
colNum;
904 unsigned int BrowNum = B.
rowNum;
905 unsigned int i, j, k;
907 for (i = 0; i < A.
rowNum; i++) {
908 double *rowptri = A.
rowPtrs[i];
910 for (j = 0; j < BcolNum; j++) {
912 for (k = 0; k < BrowNum; k++)
913 s += rowptri[k] * BrowPtrs[k][j];
961 unsigned int RcolNum = R.
getCols();
962 unsigned int RrowNum = R.
getRows();
963 for (
unsigned int i = 0; i <
rowNum; i++) {
966 for (
unsigned int j = 0; j < RcolNum; j++) {
968 for (
unsigned int k = 0; k < RrowNum; k++)
969 s += rowptri[k] * R[k][j];
989 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) 994 vpMatrix::blas_dgemm(trans, trans, V.
colNum,
rowNum,
colNum, alpha, V.
data, V.
colNum,
data,
colNum, beta, M.
data,
1005 for (
unsigned int i = 0; i < 6; i++) {
1006 for (
unsigned int j = 0; j < 6; j++) {
1007 V_trans[i][j] = V[j][i];
1011 for (
unsigned int i = 0; i <
rowNum; i++) {
1015 for (
int j = 0; j < 6; j++) {
1016 __m128d v_mul = _mm_setzero_pd();
1017 for (
int k = 0; k < 6; k += 2) {
1018 v_mul = _mm_add_pd(v_mul, _mm_mul_pd(_mm_loadu_pd(&rowptri[k]), _mm_loadu_pd(&V_trans[j][k])));
1022 _mm_storeu_pd(v_tmp, v_mul);
1023 ci[j] = v_tmp[0] + v_tmp[1];
1028 unsigned int VcolNum = V.
getCols();
1029 unsigned int VrowNum = V.
getRows();
1030 for (
unsigned int i = 0; i <
rowNum; i++) {
1033 for (
unsigned int j = 0; j < VcolNum; j++) {
1035 for (
unsigned int k = 0; k < VrowNum; k++)
1036 s += rowptri[k] * V[k][j];
1057 unsigned int VcolNum = V.
getCols();
1058 unsigned int VrowNum = V.
getRows();
1059 for (
unsigned int i = 0; i <
rowNum; i++) {
1062 for (
unsigned int j = 0; j < VcolNum; j++) {
1064 for (
unsigned int k = 0; k < VrowNum; k++)
1065 s += rowptri[k] * V[k][j];
1094 double **ArowPtrs = A.
rowPtrs;
1095 double **BrowPtrs = B.
rowPtrs;
1096 double **CrowPtrs = C.
rowPtrs;
1098 for (
unsigned int i = 0; i < A.
rowNum; i++)
1099 for (
unsigned int j = 0; j < A.
colNum; j++)
1100 CrowPtrs[i][j] = wB * BrowPtrs[i][j] + wA * ArowPtrs[i][j];
1122 double **ArowPtrs = A.
rowPtrs;
1123 double **BrowPtrs = B.
rowPtrs;
1124 double **CrowPtrs = C.
rowPtrs;
1126 for (
unsigned int i = 0; i < A.
rowNum; i++) {
1127 for (
unsigned int j = 0; j < A.
colNum; j++) {
1128 CrowPtrs[i][j] = BrowPtrs[i][j] + ArowPtrs[i][j];
1155 double **ArowPtrs = A.
rowPtrs;
1156 double **BrowPtrs = B.
rowPtrs;
1157 double **CrowPtrs = C.
rowPtrs;
1159 for (
unsigned int i = 0; i < A.
rowNum; i++) {
1160 for (
unsigned int j = 0; j < A.
colNum; j++) {
1161 CrowPtrs[i][j] = BrowPtrs[i][j] + ArowPtrs[i][j];
1202 double **ArowPtrs = A.
rowPtrs;
1203 double **BrowPtrs = B.
rowPtrs;
1204 double **CrowPtrs = C.
rowPtrs;
1206 for (
unsigned int i = 0; i < A.
rowNum; i++) {
1207 for (
unsigned int j = 0; j < A.
colNum; j++) {
1208 CrowPtrs[i][j] = ArowPtrs[i][j] - BrowPtrs[i][j];
1235 double **ArowPtrs = A.
rowPtrs;
1236 double **BrowPtrs = B.
rowPtrs;
1237 double **CrowPtrs = C.
rowPtrs;
1239 for (
unsigned int i = 0; i < A.
rowNum; i++) {
1240 for (
unsigned int j = 0; j < A.
colNum; j++) {
1241 CrowPtrs[i][j] = ArowPtrs[i][j] - BrowPtrs[i][j];
1266 double **BrowPtrs = B.
rowPtrs;
1268 for (
unsigned int i = 0; i <
rowNum; i++)
1269 for (
unsigned int j = 0; j <
colNum; j++)
1270 rowPtrs[i][j] += BrowPtrs[i][j];
1283 double **BrowPtrs = B.
rowPtrs;
1284 for (
unsigned int i = 0; i <
rowNum; i++)
1285 for (
unsigned int j = 0; j <
colNum; j++)
1286 rowPtrs[i][j] -= BrowPtrs[i][j];
1305 double **ArowPtrs = A.
rowPtrs;
1306 double **CrowPtrs = C.
rowPtrs;
1309 for (
unsigned int i = 0; i < A.
rowNum; i++)
1310 for (
unsigned int j = 0; j < A.
colNum; j++)
1311 CrowPtrs[i][j] = -ArowPtrs[i][j];
1328 for (
unsigned int i = 0; i <
rowNum; i++) {
1329 for (
unsigned int j = 0; j <
colNum; j++) {
1353 unsigned int Brow = B.
getRows();
1354 unsigned int Bcol = B.
getCols();
1356 for (
unsigned int i = 0; i < Brow; i++)
1357 for (
unsigned int j = 0; j < Bcol; j++)
1358 C[i][j] = B[i][j] * x;
1371 for (
unsigned int i = 0; i <
rowNum; i++)
1372 for (
unsigned int j = 0; j <
colNum; j++)
1386 if (std::fabs(x) <= std::numeric_limits<double>::epsilon()) {
1390 double xinv = 1 / x;
1392 for (
unsigned int i = 0; i <
rowNum; i++)
1393 for (
unsigned int j = 0; j <
colNum; j++)
1394 C[i][j] =
rowPtrs[i][j] * xinv;
1402 for (
unsigned int i = 0; i <
rowNum; i++)
1403 for (
unsigned int j = 0; j <
colNum; j++)
1412 for (
unsigned int i = 0; i <
rowNum; i++)
1413 for (
unsigned int j = 0; j <
colNum; j++)
1425 for (
unsigned int i = 0; i <
rowNum; i++)
1426 for (
unsigned int j = 0; j <
colNum; j++)
1436 if (std::fabs(x) <= std::numeric_limits<double>::epsilon())
1439 double xinv = 1 / x;
1441 for (
unsigned int i = 0; i <
rowNum; i++)
1442 for (
unsigned int j = 0; j <
colNum; j++)
1461 double *optr = out.
data;
1462 for (
unsigned int j = 0; j <
colNum; j++) {
1463 for (
unsigned int i = 0; i <
rowNum; i++) {
1489 double *mdata =
data;
1490 double *optr = out.
data;
1491 for (
unsigned int i = 0; i <
dsize; i++) {
1492 *(optr++) = *(mdata++);
1525 for (; i <=
dsize - 2; i += 2) {
1526 __m128d vout = _mm_mul_pd(_mm_loadu_pd(
data + i), _mm_loadu_pd(m.
data + i));
1527 _mm_storeu_pd(out.
data + i, vout);
1532 for (; i <
dsize; i++) {
1547 unsigned int r1 = m1.
getRows();
1548 unsigned int c1 = m1.
getCols();
1549 unsigned int r2 = m2.
getRows();
1550 unsigned int c2 = m2.
getCols();
1553 vpERROR_TRACE(
"Kronecker prodect bad dimension of output vpMatrix");
1557 for (
unsigned int r = 0; r < r1; r++) {
1558 for (
unsigned int c = 0; c < c1; c++) {
1559 double alpha = m1[r][c];
1560 double *m2ptr = m2[0];
1561 unsigned int roffset = r * r2;
1562 unsigned int coffset = c * c2;
1563 for (
unsigned int rr = 0; rr < r2; rr++) {
1564 for (
unsigned int cc = 0; cc < c2; cc++) {
1565 out[roffset + rr][coffset + cc] = alpha * *(m2ptr++);
1588 unsigned int r1 = m1.
getRows();
1589 unsigned int c1 = m1.
getCols();
1590 unsigned int r2 = m2.
getRows();
1591 unsigned int c2 = m2.
getCols();
1595 for (
unsigned int r = 0; r < r1; r++) {
1596 for (
unsigned int c = 0; c < c1; c++) {
1597 double alpha = m1[r][c];
1598 double *m2ptr = m2[0];
1599 unsigned int roffset = r * r2;
1600 unsigned int coffset = c * c2;
1601 for (
unsigned int rr = 0; rr < r2; rr++) {
1602 for (
unsigned int cc = 0; cc < c2; cc++) {
1603 out[roffset + rr][coffset + cc] = alpha * *(m2ptr++);
1793 #if defined(VISP_HAVE_LAPACK) 1795 #elif defined(VISP_HAVE_EIGEN3) 1797 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 1799 #elif defined(VISP_HAVE_GSL) 1864 #if defined(VISP_HAVE_LAPACK) 1865 return pseudoInverseLapack(Ap, svThreshold);
1866 #elif defined(VISP_HAVE_EIGEN3) 1867 return pseudoInverseEigen3(Ap, svThreshold);
1868 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 1869 return pseudoInverseOpenCV(Ap, svThreshold);
1870 #elif defined(VISP_HAVE_GSL) 1871 return pseudoInverseGsl(Ap, svThreshold);
1876 "Install Lapack, Eigen3, OpenCV " 1877 "or GSL 3rd party"));
1933 #if defined(VISP_HAVE_LAPACK) 1934 return pseudoInverseLapack(svThreshold);
1935 #elif defined(VISP_HAVE_EIGEN3) 1936 return pseudoInverseEigen3(svThreshold);
1937 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 1938 return pseudoInverseOpenCV(svThreshold);
1939 #elif defined(VISP_HAVE_GSL) 1940 return pseudoInverseGsl(svThreshold);
1945 "Install Lapack, Eigen3, OpenCV " 1946 "or GSL 3rd party"));
1950 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1951 #if defined(VISP_HAVE_LAPACK) 1988 vpMatrix vpMatrix::pseudoInverseLapack(
double svThreshold)
const 1990 unsigned int nrows, ncols;
1991 unsigned int nrows_orig =
getRows();
1992 unsigned int ncols_orig =
getCols();
1994 vpMatrix Ap(ncols_orig, nrows_orig);
1996 if (nrows_orig >= ncols_orig) {
2008 if (nrows_orig >= ncols_orig)
2016 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
2061 unsigned int vpMatrix::pseudoInverseLapack(
vpMatrix &Ap,
double svThreshold)
const 2063 unsigned int nrows, ncols;
2064 unsigned int nrows_orig =
getRows();
2065 unsigned int ncols_orig =
getCols();
2068 Ap.
resize(ncols_orig, nrows_orig);
2070 if (nrows_orig >= ncols_orig) {
2082 if (nrows_orig >= ncols_orig)
2089 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
2140 unsigned int vpMatrix::pseudoInverseLapack(
vpMatrix &Ap,
vpColVector &sv,
double svThreshold)
const 2142 unsigned int nrows, ncols;
2143 unsigned int nrows_orig =
getRows();
2144 unsigned int ncols_orig =
getCols();
2147 Ap.
resize(ncols_orig, nrows_orig);
2149 if (nrows_orig >= ncols_orig) {
2161 if (nrows_orig >= ncols_orig)
2168 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
2283 unsigned int nrows =
getRows();
2284 unsigned int ncols =
getCols();
2289 if (nrows < ncols) {
2298 U.svdLapack(sv_, V);
2300 compute_pseudo_inverse(U, sv_, V, nrows, ncols, svThreshold, Ap, rank, imA, imAt, kerA);
2304 for (
unsigned int i = 0; i < sv.
size(); i++)
2310 #if defined(VISP_HAVE_EIGEN3) 2347 vpMatrix vpMatrix::pseudoInverseEigen3(
double svThreshold)
const 2349 unsigned int nrows, ncols;
2350 unsigned int nrows_orig =
getRows();
2351 unsigned int ncols_orig =
getCols();
2353 vpMatrix Ap(ncols_orig, nrows_orig);
2355 if (nrows_orig >= ncols_orig) {
2367 if (nrows_orig >= ncols_orig)
2375 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
2420 unsigned int vpMatrix::pseudoInverseEigen3(
vpMatrix &Ap,
double svThreshold)
const 2422 unsigned int nrows, ncols;
2423 unsigned int nrows_orig =
getRows();
2424 unsigned int ncols_orig =
getCols();
2427 Ap.
resize(ncols_orig, nrows_orig);
2429 if (nrows_orig >= ncols_orig) {
2441 if (nrows_orig >= ncols_orig)
2448 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
2499 unsigned int vpMatrix::pseudoInverseEigen3(
vpMatrix &Ap,
vpColVector &sv,
double svThreshold)
const 2501 unsigned int nrows, ncols;
2502 unsigned int nrows_orig =
getRows();
2503 unsigned int ncols_orig =
getCols();
2506 Ap.
resize(ncols_orig, nrows_orig);
2508 if (nrows_orig >= ncols_orig) {
2520 if (nrows_orig >= ncols_orig)
2527 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
2642 unsigned int nrows =
getRows();
2643 unsigned int ncols =
getCols();
2648 if (nrows < ncols) {
2657 U.svdEigen3(sv_, V);
2659 compute_pseudo_inverse(U, sv_, V, nrows, ncols, svThreshold, Ap, rank, imA, imAt, kerA);
2663 for (
unsigned int i = 0; i < sv.
size(); i++)
2669 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) 2706 vpMatrix vpMatrix::pseudoInverseOpenCV(
double svThreshold)
const 2708 unsigned int nrows, ncols;
2709 unsigned int nrows_orig =
getRows();
2710 unsigned int ncols_orig =
getCols();
2712 vpMatrix Ap(ncols_orig, nrows_orig);
2714 if (nrows_orig >= ncols_orig) {
2726 if (nrows_orig >= ncols_orig)
2734 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
2779 unsigned int vpMatrix::pseudoInverseOpenCV(
vpMatrix &Ap,
double svThreshold)
const 2781 unsigned int nrows, ncols;
2782 unsigned int nrows_orig =
getRows();
2783 unsigned int ncols_orig =
getCols();
2786 Ap.
resize(ncols_orig, nrows_orig);
2788 if (nrows_orig >= ncols_orig) {
2800 if (nrows_orig >= ncols_orig)
2807 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
2858 unsigned int vpMatrix::pseudoInverseOpenCV(
vpMatrix &Ap,
vpColVector &sv,
double svThreshold)
const 2860 unsigned int nrows, ncols;
2861 unsigned int nrows_orig =
getRows();
2862 unsigned int ncols_orig =
getCols();
2865 Ap.
resize(ncols_orig, nrows_orig);
2867 if (nrows_orig >= ncols_orig) {
2879 if (nrows_orig >= ncols_orig)
2886 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
3001 unsigned int nrows =
getRows();
3002 unsigned int ncols =
getCols();
3007 if (nrows < ncols) {
3016 U.svdOpenCV(sv_, V);
3018 compute_pseudo_inverse(U, sv_, V, nrows, ncols, svThreshold, Ap, rank, imA, imAt, kerA);
3022 for (
unsigned int i = 0; i < sv.
size(); i++)
3028 #if defined(VISP_HAVE_GSL) 3065 vpMatrix vpMatrix::pseudoInverseGsl(
double svThreshold)
const 3067 unsigned int nrows, ncols;
3068 unsigned int nrows_orig =
getRows();
3069 unsigned int ncols_orig =
getCols();
3071 vpMatrix Ap(ncols_orig, nrows_orig);
3073 if (nrows_orig >= ncols_orig) {
3085 if (nrows_orig >= ncols_orig)
3093 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
3138 unsigned int vpMatrix::pseudoInverseGsl(
vpMatrix &Ap,
double svThreshold)
const 3140 unsigned int nrows, ncols;
3141 unsigned int nrows_orig =
getRows();
3142 unsigned int ncols_orig =
getCols();
3145 Ap.
resize(ncols_orig, nrows_orig);
3147 if (nrows_orig >= ncols_orig) {
3159 if (nrows_orig >= ncols_orig)
3166 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
3217 unsigned int vpMatrix::pseudoInverseGsl(
vpMatrix &Ap,
vpColVector &sv,
double svThreshold)
const 3219 unsigned int nrows, ncols;
3220 unsigned int nrows_orig =
getRows();
3221 unsigned int ncols_orig =
getCols();
3224 Ap.
resize(ncols_orig, nrows_orig);
3226 if (nrows_orig >= ncols_orig) {
3238 if (nrows_orig >= ncols_orig)
3245 compute_pseudo_inverse(U, sv, V, nrows, ncols, nrows_orig, ncols_orig, svThreshold, Ap, rank);
3359 unsigned int nrows =
getRows();
3360 unsigned int ncols =
getCols();
3365 if (nrows < ncols) {
3376 compute_pseudo_inverse(U, sv_, V, nrows, ncols, svThreshold, Ap, rank, imA, imAt, kerA);
3380 for (
unsigned int i = 0; i < sv.
size(); i++)
3386 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS 3451 #if defined(VISP_HAVE_LAPACK) 3452 return pseudoInverseLapack(Ap, sv, svThreshold);
3453 #elif defined(VISP_HAVE_EIGEN3) 3454 return pseudoInverseEigen3(Ap, sv, svThreshold);
3455 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 3456 return pseudoInverseOpenCV(Ap, sv, svThreshold);
3457 #elif defined(VISP_HAVE_GSL) 3458 return pseudoInverseGsl(Ap, sv, svThreshold);
3464 "Install Lapack, Eigen3, OpenCV " 3465 "or GSL 3rd party"));
3547 return pseudoInverse(Ap, sv, svThreshold, imA, imAt, kerAt);
3687 #if defined(VISP_HAVE_LAPACK) 3688 return pseudoInverseLapack(Ap, sv, svThreshold, imA, imAt, kerAt);
3689 #elif defined(VISP_HAVE_EIGEN3) 3690 return pseudoInverseEigen3(Ap, sv, svThreshold, imA, imAt, kerAt);
3691 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 3692 return pseudoInverseOpenCV(Ap, sv, svThreshold, imA, imAt, kerAt);
3693 #elif defined(VISP_HAVE_GSL) 3694 return pseudoInverseGsl(Ap, sv, svThreshold, imA, imAt, kerAt);
3703 "Install Lapack, Eigen3, OpenCV " 3704 "or GSL 3rd party"));
3753 for (
unsigned int i = 0; i < column_size; i++)
3754 c[i] = (*
this)[i_begin + i][j];
3801 unsigned int nb_rows =
getRows();
3803 for (
unsigned int i = 0; i < nb_rows; i++)
3804 c[i] = (*
this)[i][j];
3900 for (
unsigned int j = 0; j < row_size; j++)
3901 r[j] = (*
this)[i][j_begin + i];
3956 unsigned int nra = A.
getRows();
3957 unsigned int nrb = B.
getRows();
3967 std::cerr <<
"A and C must be two different objects!" << std::endl;
3972 std::cerr <<
"B and C must be two different objects!" << std::endl;
3978 if (C.
data != NULL && A.
data != NULL && A.
size() > 0) {
3983 if (C.
data != NULL && B.
data != NULL && B.
size() > 0) {
4002 unsigned int nra = A.
getRows();
4012 std::cerr <<
"A and C must be two different objects!" << std::endl;
4016 if (r.
size() == 0) {
4023 if (C.
data != NULL && A.
data != NULL && A.
size() > 0) {
4028 if (C.
data != NULL && r.
data != NULL && r.
size() > 0) {
4073 for (
unsigned int i = 0; i < A.
getRows(); i++) {
4074 for (
unsigned int j = 0; j < A.
getCols(); j++) {
4075 if (i >= r && i < (r + B.
getRows()) && j >= c && j < (c + B.
getCols())) {
4076 C[i][j] = B[i - r][j - c];
4122 unsigned int nca = A.
getCols();
4123 unsigned int ncb = B.
getCols();
4132 if (B.
getRows() == 0 || nca + ncb == 0) {
4133 std::cerr <<
"B.getRows() == 0 || nca+ncb == 0" << std::endl;
4168 typedef std::string::size_type size_type;
4173 std::vector<std::string> values(m * n);
4174 std::ostringstream oss;
4175 std::ostringstream ossFixed;
4176 std::ios_base::fmtflags original_flags = oss.flags();
4179 ossFixed.setf(std::ios::fixed, std::ios::floatfield);
4181 size_type maxBefore = 0;
4182 size_type maxAfter = 0;
4184 for (
unsigned int i = 0; i < m; ++i) {
4185 for (
unsigned int j = 0; j < n; ++j) {
4187 oss << (*this)[i][j];
4188 if (oss.str().find(
"e") != std::string::npos) {
4190 ossFixed << (*this)[i][j];
4191 oss.str(ossFixed.str());
4194 values[i * n + j] = oss.str();
4195 size_type thislen = values[i * n + j].size();
4196 size_type p = values[i * n + j].find(
'.');
4198 if (p == std::string::npos) {
4208 size_type totalLength = length;
4212 maxAfter = (std::min)(maxAfter, totalLength - maxBefore);
4221 s <<
"[" << m <<
"," << n <<
"]=\n";
4223 for (
unsigned int i = 0; i < m; i++) {
4225 for (
unsigned int j = 0; j < n; j++) {
4226 size_type p = values[i * n + j].find(
'.');
4227 s.setf(std::ios::right, std::ios::adjustfield);
4228 s.width((std::streamsize)maxBefore);
4229 s << values[i * n + j].substr(0, p).c_str();
4232 s.setf(std::ios::left, std::ios::adjustfield);
4233 if (p != std::string::npos) {
4234 s.width((std::streamsize)maxAfter);
4235 s << values[i * n + j].substr(p, maxAfter).c_str();
4237 assert(maxAfter > 1);
4238 s.width((std::streamsize)maxAfter);
4248 s.flags(original_flags);
4250 return (
int)(maxBefore + maxAfter);
4292 for (
unsigned int i = 0; i < this->
getRows(); ++i) {
4293 for (
unsigned int j = 0; j < this->
getCols(); ++j) {
4294 os << (*this)[i][j] <<
", ";
4296 if (this->
getRows() != i + 1) {
4297 os <<
";" << std::endl;
4299 os <<
"]" << std::endl;
4335 os <<
"([ " << std::endl;
4336 for (
unsigned int i = 0; i < this->
getRows(); ++i) {
4338 for (
unsigned int j = 0; j < this->
getCols(); ++j) {
4339 os << (*this)[i][j] <<
", ";
4341 os <<
"]," << std::endl;
4343 os <<
"])" << std::endl;
4376 for (
unsigned int i = 0; i < this->
getRows(); ++i) {
4377 for (
unsigned int j = 0; j < this->
getCols(); ++j) {
4378 os << (*this)[i][j];
4379 if (!(j == (this->
getCols() - 1)))
4425 os <<
"vpMatrix " << matrixName <<
" (" << this->
getRows() <<
", " << this->
getCols() <<
"); " << std::endl;
4427 for (
unsigned int i = 0; i < this->
getRows(); ++i) {
4428 for (
unsigned int j = 0; j < this->
getCols(); ++j) {
4430 os << matrixName <<
"[" << i <<
"][" << j <<
"] = " << (*this)[i][j] <<
"; " << std::endl;
4432 for (
unsigned int k = 0; k <
sizeof(double); ++k) {
4433 os <<
"((unsigned char*)&(" << matrixName <<
"[" << i <<
"][" << j <<
"]) )[" << k <<
"] = 0x" << std::hex
4434 << (
unsigned int)((
unsigned char *)&((*this)[i][j]))[k] <<
"; " << std::endl;
4457 unsigned int rowNumOld =
rowNum;
4488 if (r.
size() == 0) {
4492 unsigned int oldSize =
size();
4497 memcpy(
data + oldSize, r.
data,
sizeof(
double) * r.
size());
4518 for (
unsigned int i = r; i < (r + A.
getRows()); i++) {
4573 #ifdef VISP_HAVE_GSL 4577 for (
unsigned int i = 0; i <
rowNum; i++) {
4578 for (
unsigned int j = 0; j <
rowNum; j++) {
4580 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
4588 gsl_vector *eval = gsl_vector_alloc(rowNum);
4589 gsl_matrix *evec = gsl_matrix_alloc(rowNum,
colNum);
4591 gsl_eigen_symmv_workspace *w = gsl_eigen_symmv_alloc(rowNum);
4592 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
4594 unsigned int Atda = (
unsigned int)m->tda;
4595 for (
unsigned int i = 0; i <
rowNum; i++) {
4596 unsigned int k = i * Atda;
4597 for (
unsigned int j = 0; j <
colNum; j++)
4598 m->data[k + j] = (*
this)[i][j];
4600 gsl_eigen_symmv(m, eval, evec, w);
4602 gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_ASC);
4604 for (
unsigned int i = 0; i <
rowNum; i++) {
4605 evalue[i] = gsl_vector_get(eval, i);
4608 gsl_eigen_symmv_free(w);
4609 gsl_vector_free(eval);
4611 gsl_matrix_free(evec);
4618 "should install GSL rd party"));
4676 #ifdef VISP_HAVE_GSL 4687 #ifdef VISP_HAVE_GSL 4691 for (
unsigned int i = 0; i <
rowNum; i++) {
4692 for (
unsigned int j = 0; j <
rowNum; j++) {
4694 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
4702 evector.resize(rowNum,
colNum);
4704 gsl_vector *eval = gsl_vector_alloc(rowNum);
4705 gsl_matrix *evec = gsl_matrix_alloc(rowNum,
colNum);
4707 gsl_eigen_symmv_workspace *w = gsl_eigen_symmv_alloc(rowNum);
4708 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
4710 unsigned int Atda = (
unsigned int)m->tda;
4711 for (
unsigned int i = 0; i <
rowNum; i++) {
4712 unsigned int k = i * Atda;
4713 for (
unsigned int j = 0; j <
colNum; j++)
4714 m->data[k + j] = (*
this)[i][j];
4716 gsl_eigen_symmv(m, eval, evec, w);
4718 gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_ASC);
4720 for (
unsigned int i = 0; i <
rowNum; i++) {
4721 evalue[i] = gsl_vector_get(eval, i);
4723 Atda = (
unsigned int)evec->tda;
4724 for (
unsigned int i = 0; i <
rowNum; i++) {
4725 unsigned int k = i * Atda;
4726 for (
unsigned int j = 0; j <
rowNum; j++) {
4727 evector[i][j] = evec->data[k + j];
4731 gsl_eigen_symmv_free(w);
4732 gsl_vector_free(eval);
4734 gsl_matrix_free(evec);
4739 "should install GSL rd party"));
4765 unsigned int nbline =
getRows();
4766 unsigned int nbcol =
getCols();
4787 for (
unsigned int i = 0; i < nbcol; i++) {
4788 if (fabs(sv[i]) > maxsv) {
4789 maxsv = fabs(sv[i]);
4793 unsigned int rank = 0;
4794 for (
unsigned int i = 0; i < nbcol; i++) {
4795 if (fabs(sv[i]) > maxsv * svThreshold) {
4800 kerAt.
resize(nbcol - rank, nbcol);
4801 if (rank != nbcol) {
4802 for (
unsigned int j = 0, k = 0; j < nbcol; j++) {
4804 if ((fabs(sv[j]) <= maxsv * svThreshold) &&
4805 (std::fabs(V.
getCol(j).
sumSquare()) > std::numeric_limits<double>::epsilon())) {
4806 for (
unsigned int i = 0; i < V.
getRows(); i++) {
4807 kerAt[k][i] = V[i][j];
4872 #ifdef VISP_HAVE_GSL 4874 double *b =
new double[size_];
4875 for (
size_t i = 0; i < size_; i++)
4877 gsl_matrix_view m = gsl_matrix_view_array(this->
data,
rowNum, colNum);
4878 gsl_matrix_view em = gsl_matrix_view_array(b,
rowNum, colNum);
4879 gsl_linalg_exponential_ss(&m.matrix, &em.matrix, 0);
4882 memcpy(expA.
data, b, size_ *
sizeof(
double));
4903 for (
unsigned int i = 0; i <
rowNum; i++) {
4905 for (
unsigned int j = 0; j <
colNum; j++) {
4906 sum += fabs((*
this)[i][j]);
4908 if (sum > nA || i == 0) {
4917 double sca = 1.0 / pow(2.0, s);
4920 _expE = c * exp + _eye;
4921 _expD = -c * exp + _eye;
4922 for (
int k = 2; k <= q; k++) {
4923 c = c * ((double)(q - k + 1)) / ((
double)(k * (2 * q - k + 1)));
4924 _expcX = exp * _expX;
4927 _expE = _expE + _expcX;
4929 _expD = _expD + _expcX;
4931 _expD = _expD - _expcX;
4935 exp = _expX * _expE;
4936 for (
int k = 1; k <= s; k++) {
4968 for (
unsigned int i = 0; i < col; i++) {
4969 for (
unsigned int j = 0; j <
row; j++)
4970 M_comp[i][j] = M[i][j];
4971 for (
unsigned int j = row + 1; j < M.
getRows(); j++)
4972 M_comp[i][j - 1] = M[i][j];
4974 for (
unsigned int i = col + 1; i < M.
getCols(); i++) {
4975 for (
unsigned int j = 0; j <
row; j++)
4976 M_comp[i - 1][j] = M[i][j];
4977 for (
unsigned int j = row + 1; j < M.
getRows(); j++)
4978 M_comp[i - 1][j - 1] = M[i][j];
4998 for (
unsigned int i = 0; i < M.
getCols(); i++) {
5021 for (
unsigned int i = 0; i < H.
getCols(); i++) {
5022 for (
unsigned int j = 0; j < H.
getCols(); j++) {
5023 HLM[i][j] = H[i][j];
5025 HLM[i][j] += alpha * H[i][j];
5041 for (
unsigned int i = 0; i <
dsize; i++) {
5042 double x = *(
data + i);
5062 for (
unsigned int i = 0; i <
rowNum; i++) {
5064 for (
unsigned int j = 0; j <
colNum; j++) {
5065 x += fabs(*(*(
rowPtrs + i) + j));
5082 double sum_square = 0.0;
5085 for (
unsigned int i = 0; i <
rowNum; i++) {
5086 for (
unsigned int j = 0; j <
colNum; j++) {
5088 sum_square += x * x;
5095 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS) 5120 for (
unsigned int j = 0; j <
getCols(); j++)
5121 c[j] = (*
this)[i - 1][j];
5136 for (
unsigned int i = 0; i <
getRows(); i++)
5137 c[i] = (*
this)[i][j - 1];
5149 for (
unsigned int i = 0; i <
rowNum; i++)
5150 for (
unsigned int j = 0; j <
colNum; j++)
5152 (*this)[i][j] = val;
5157 #endif //#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS) void solveBySVD(const vpColVector &B, vpColVector &x) const
void svd(vpColVector &w, vpMatrix &V)
vpMatrix operator*(const double &x, const vpMatrix &B)
vpMatrix extract(unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols) const
Implementation of a matrix and operations on matrices.
double euclideanNorm() const
vpMatrix pseudoInverse(double svThreshold=1e-6) const
static vpMatrix juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B)
vpRowVector getRow(const unsigned int i) const
vp_deprecated vpRowVector row(const unsigned int i)
double det(vpDetMethod method=LU_DECOMPOSITION) const
void stack(const double &d)
void kron(const vpMatrix &m1, vpMatrix &out) const
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpMatrix operator-() const
Implementation of row vector and the associated operations.
std::ostream & csvPrint(std::ostream &os) const
static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
vpMatrix & operator*=(const double x)
Multiply all the element of the matrix by x : Aij = Aij * x.
void stack(const vpMatrix &A)
vpMatrix inverseByLU() const
vpMatrix & operator/=(double x)
Divide all the element of the matrix by x : Aij = Aij / x.
error that can be emited by ViSP classes.
unsigned int getRows() const
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 size() const
Return the number of elements of the 2D array.
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
vp_deprecated vpColVector column(const unsigned int j)
double infinityNorm() const
vpMatrix & operator-=(const vpMatrix &B)
Operation A = A - B.
vpMatrix operator+(const vpMatrix &B) const
static Type maximum(const Type &a, const Type &b)
Implementation of a rotation matrix and operations on such kind of matrices.
unsigned int getCols() const
std::ostream & maplePrint(std::ostream &os) const
unsigned int rowNum
Number of rows in the array.
vpMatrix hadamard(const vpMatrix &m) const
void insert(const vpMatrix &A, const unsigned int r, const unsigned int c)
vp_deprecated void setIdentity(const double &val=1.0)
std::ostream & matlabPrint(std::ostream &os) const
unsigned int kernel(vpMatrix &kerAt, double svThreshold=1e-6) const
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)
VISP_EXPORT bool checkSSE2()
static void multMatrixVector(const vpMatrix &A, const vpColVector &v, vpColVector &w)
static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
vpColVector eigenValues() const
vpMatrix transpose() const
unsigned int colNum
Number of columns in the array.
vpMatrix operator/(const double x) const
Cij = Aij / x (A is unchanged)
void resize(const unsigned int i, const bool flagNullify=true)
vpMatrix operator*(const vpMatrix &B) const
int print(std::ostream &s, unsigned int length, char const *intro=0) const
Implementation of column vector and the associated operations.
static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA)
vpColVector stackColumns()
unsigned int dsize
Current array size (rowNum * colNum)
static void negateMatrix(const vpMatrix &A, vpMatrix &C)
vpColVector getCol(const unsigned int j) const
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 *)