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));
560 #ifdef VISP_HAVE_CPP11_COMPATIBILITY 574 if (
this != &other) {
586 other.rowPtrs = NULL;
609 for (
unsigned int i = 0; i <
rowNum; i++) {
610 for (
unsigned int j = 0; j <
colNum; j++) {
655 unsigned int rows = A.
getRows();
659 for (
unsigned int i = 0; i < rows; i++)
660 (*
this)[i][i] = A[i];
697 for (
unsigned int i = 0; i < min_; i++)
714 unsigned int rows = A.
getRows();
717 for (
unsigned int i = 0; i < rows; i++)
734 for (
unsigned int j = 0; j < 3; j++)
737 for (
unsigned int j = 0; j < 3; j++) {
739 for (
unsigned int i = 0; i < 3; i++) {
740 t_out[i] +=
rowPtrs[i][j] * tj;
775 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) 781 vpMatrix::blas_dgemv(trans, A.
colNum, A.
rowNum, alpha, A.
data, A.
colNum, v.
data, incr, beta, w.
data, incr);
784 for (
unsigned int j = 0; j < A.
colNum; j++) {
786 for (
unsigned int i = 0; i < A.
rowNum; i++) {
816 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) 821 vpMatrix::blas_dgemm(trans, trans, B.
colNum, A.
rowNum, A.
colNum, alpha, B.
data, B.
colNum, A.
data, A.
colNum, beta,
825 unsigned int BcolNum = B.
colNum;
826 unsigned int BrowNum = B.
rowNum;
827 unsigned int i, j, k;
829 for (i = 0; i < A.
rowNum; i++) {
830 double *rowptri = A.
rowPtrs[i];
832 for (j = 0; j < BcolNum; j++) {
834 for (k = 0; k < BrowNum; k++)
835 s += rowptri[k] * BrowPtrs[k][j];
859 "Cannot multiply (%dx%d) matrix by (%dx%d) matrix as a " 865 unsigned int BcolNum = B.
colNum;
866 unsigned int BrowNum = B.
rowNum;
867 unsigned int i, j, k;
869 for (i = 0; i < A.
rowNum; i++) {
870 double *rowptri = A.
rowPtrs[i];
872 for (j = 0; j < BcolNum; j++) {
874 for (k = 0; k < BrowNum; k++)
875 s += rowptri[k] * BrowPtrs[k][j];
898 "Cannot multiply (%dx%d) matrix by (%dx%d) matrix as a " 904 unsigned int BcolNum = B.
colNum;
905 unsigned int BrowNum = B.
rowNum;
906 unsigned int i, j, k;
908 for (i = 0; i < A.
rowNum; i++) {
909 double *rowptri = A.
rowPtrs[i];
911 for (j = 0; j < BcolNum; j++) {
913 for (k = 0; k < BrowNum; k++)
914 s += rowptri[k] * BrowPtrs[k][j];
962 unsigned int RcolNum = R.
getCols();
963 unsigned int RrowNum = R.
getRows();
964 for (
unsigned int i = 0; i <
rowNum; i++) {
967 for (
unsigned int j = 0; j < RcolNum; j++) {
969 for (
unsigned int k = 0; k < RrowNum; k++)
970 s += rowptri[k] * R[k][j];
990 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) 995 vpMatrix::blas_dgemm(trans, trans, V.
colNum,
rowNum,
colNum, alpha, V.
data, V.
colNum,
data,
colNum, beta, M.
data,
1006 for (
unsigned int i = 0; i < 6; i++) {
1007 for (
unsigned int j = 0; j < 6; j++) {
1008 V_trans[i][j] = V[j][i];
1012 for (
unsigned int i = 0; i <
rowNum; i++) {
1016 for (
int j = 0; j < 6; j++) {
1017 __m128d v_mul = _mm_setzero_pd();
1018 for (
int k = 0; k < 6; k += 2) {
1019 v_mul = _mm_add_pd(v_mul, _mm_mul_pd(_mm_loadu_pd(&rowptri[k]), _mm_loadu_pd(&V_trans[j][k])));
1023 _mm_storeu_pd(v_tmp, v_mul);
1024 ci[j] = v_tmp[0] + v_tmp[1];
1029 unsigned int VcolNum = V.
getCols();
1030 unsigned int VrowNum = V.
getRows();
1031 for (
unsigned int i = 0; i <
rowNum; i++) {
1034 for (
unsigned int j = 0; j < VcolNum; j++) {
1036 for (
unsigned int k = 0; k < VrowNum; k++)
1037 s += rowptri[k] * V[k][j];
1058 unsigned int VcolNum = V.
getCols();
1059 unsigned int VrowNum = V.
getRows();
1060 for (
unsigned int i = 0; i <
rowNum; i++) {
1063 for (
unsigned int j = 0; j < VcolNum; j++) {
1065 for (
unsigned int k = 0; k < VrowNum; k++)
1066 s += rowptri[k] * V[k][j];
1095 double **ArowPtrs = A.
rowPtrs;
1096 double **BrowPtrs = B.
rowPtrs;
1097 double **CrowPtrs = C.
rowPtrs;
1099 for (
unsigned int i = 0; i < A.
rowNum; i++)
1100 for (
unsigned int j = 0; j < A.
colNum; j++)
1101 CrowPtrs[i][j] = wB * BrowPtrs[i][j] + wA * ArowPtrs[i][j];
1123 double **ArowPtrs = A.
rowPtrs;
1124 double **BrowPtrs = B.
rowPtrs;
1125 double **CrowPtrs = C.
rowPtrs;
1127 for (
unsigned int i = 0; i < A.
rowNum; i++) {
1128 for (
unsigned int j = 0; j < A.
colNum; j++) {
1129 CrowPtrs[i][j] = BrowPtrs[i][j] + ArowPtrs[i][j];
1156 double **ArowPtrs = A.
rowPtrs;
1157 double **BrowPtrs = B.
rowPtrs;
1158 double **CrowPtrs = C.
rowPtrs;
1160 for (
unsigned int i = 0; i < A.
rowNum; i++) {
1161 for (
unsigned int j = 0; j < A.
colNum; j++) {
1162 CrowPtrs[i][j] = BrowPtrs[i][j] + ArowPtrs[i][j];
1203 double **ArowPtrs = A.
rowPtrs;
1204 double **BrowPtrs = B.
rowPtrs;
1205 double **CrowPtrs = C.
rowPtrs;
1207 for (
unsigned int i = 0; i < A.
rowNum; i++) {
1208 for (
unsigned int j = 0; j < A.
colNum; j++) {
1209 CrowPtrs[i][j] = ArowPtrs[i][j] - BrowPtrs[i][j];
1236 double **ArowPtrs = A.
rowPtrs;
1237 double **BrowPtrs = B.
rowPtrs;
1238 double **CrowPtrs = C.
rowPtrs;
1240 for (
unsigned int i = 0; i < A.
rowNum; i++) {
1241 for (
unsigned int j = 0; j < A.
colNum; j++) {
1242 CrowPtrs[i][j] = ArowPtrs[i][j] - BrowPtrs[i][j];
1267 double **BrowPtrs = B.
rowPtrs;
1269 for (
unsigned int i = 0; i <
rowNum; i++)
1270 for (
unsigned int j = 0; j <
colNum; j++)
1271 rowPtrs[i][j] += BrowPtrs[i][j];
1284 double **BrowPtrs = B.
rowPtrs;
1285 for (
unsigned int i = 0; i <
rowNum; i++)
1286 for (
unsigned int j = 0; j <
colNum; j++)
1287 rowPtrs[i][j] -= BrowPtrs[i][j];
1306 double **ArowPtrs = A.
rowPtrs;
1307 double **CrowPtrs = C.
rowPtrs;
1310 for (
unsigned int i = 0; i < A.
rowNum; i++)
1311 for (
unsigned int j = 0; j < A.
colNum; j++)
1312 CrowPtrs[i][j] = -ArowPtrs[i][j];
1329 for (
unsigned int i = 0; i <
rowNum; i++) {
1330 for (
unsigned int j = 0; j <
colNum; j++) {
1354 unsigned int Brow = B.
getRows();
1355 unsigned int Bcol = B.
getCols();
1357 for (
unsigned int i = 0; i < Brow; i++)
1358 for (
unsigned int j = 0; j < Bcol; j++)
1359 C[i][j] = B[i][j] * x;
1372 for (
unsigned int i = 0; i <
rowNum; i++)
1373 for (
unsigned int j = 0; j <
colNum; j++)
1387 if (std::fabs(x) <= std::numeric_limits<double>::epsilon()) {
1391 double xinv = 1 / x;
1393 for (
unsigned int i = 0; i <
rowNum; i++)
1394 for (
unsigned int j = 0; j <
colNum; j++)
1395 C[i][j] =
rowPtrs[i][j] * xinv;
1403 for (
unsigned int i = 0; i <
rowNum; i++)
1404 for (
unsigned int j = 0; j <
colNum; j++)
1413 for (
unsigned int i = 0; i <
rowNum; i++)
1414 for (
unsigned int j = 0; j <
colNum; j++)
1426 for (
unsigned int i = 0; i <
rowNum; i++)
1427 for (
unsigned int j = 0; j <
colNum; j++)
1437 if (std::fabs(x) <= std::numeric_limits<double>::epsilon())
1440 double xinv = 1 / x;
1442 for (
unsigned int i = 0; i <
rowNum; i++)
1443 for (
unsigned int j = 0; j <
colNum; j++)
1462 double *optr = out.
data;
1463 for (
unsigned int j = 0; j <
colNum; j++) {
1464 for (
unsigned int i = 0; i <
rowNum; i++) {
1490 double *mdata =
data;
1491 double *optr = out.
data;
1492 for (
unsigned int i = 0; i <
dsize; i++) {
1493 *(optr++) = *(mdata++);
1526 for (; i <=
dsize - 2; i += 2) {
1527 __m128d vout = _mm_mul_pd(_mm_loadu_pd(
data + i), _mm_loadu_pd(m.
data + i));
1528 _mm_storeu_pd(out.
data + i, vout);
1533 for (; i <
dsize; i++) {
1548 unsigned int r1 = m1.
getRows();
1549 unsigned int c1 = m1.
getCols();
1550 unsigned int r2 = m2.
getRows();
1551 unsigned int c2 = m2.
getCols();
1554 vpERROR_TRACE(
"Kronecker prodect bad dimension of output vpMatrix");
1558 for (
unsigned int r = 0; r < r1; r++) {
1559 for (
unsigned int c = 0; c < c1; c++) {
1560 double alpha = m1[r][c];
1561 double *m2ptr = m2[0];
1562 unsigned int roffset = r * r2;
1563 unsigned int coffset = c * c2;
1564 for (
unsigned int rr = 0; rr < r2; rr++) {
1565 for (
unsigned int cc = 0; cc < c2; cc++) {
1566 out[roffset + rr][coffset + cc] = alpha * *(m2ptr++);
1589 unsigned int r1 = m1.
getRows();
1590 unsigned int c1 = m1.
getCols();
1591 unsigned int r2 = m2.
getRows();
1592 unsigned int c2 = m2.
getCols();
1596 for (
unsigned int r = 0; r < r1; r++) {
1597 for (
unsigned int c = 0; c < c1; c++) {
1598 double alpha = m1[r][c];
1599 double *m2ptr = m2[0];
1600 unsigned int roffset = r * r2;
1601 unsigned int coffset = c * c2;
1602 for (
unsigned int rr = 0; rr < r2; rr++) {
1603 for (
unsigned int cc = 0; cc < c2; cc++) {
1604 out[roffset + rr][coffset + cc] = alpha * *(m2ptr++);
1794 #if defined(VISP_HAVE_LAPACK) 1796 #elif defined(VISP_HAVE_EIGEN3) 1798 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 1800 #elif defined(VISP_HAVE_GSL) 1865 #if defined(VISP_HAVE_LAPACK) 1867 #elif defined(VISP_HAVE_EIGEN3) 1869 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 1871 #elif defined(VISP_HAVE_GSL) 1877 "Install Lapack, Eigen3, OpenCV " 1878 "or GSL 3rd party"));
1934 #if defined(VISP_HAVE_LAPACK) 1936 #elif defined(VISP_HAVE_EIGEN3) 1938 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 1940 #elif defined(VISP_HAVE_GSL) 1945 "Install Lapack, Eigen3, OpenCV " 1946 "or GSL 3rd party"));
1950 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1951 #if defined(VISP_HAVE_LAPACK) 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);
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);
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) {
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) 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);
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);
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) {
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) 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);
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);
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) {
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) 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);
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);
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) 3453 #elif defined(VISP_HAVE_EIGEN3) 3455 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 3457 #elif defined(VISP_HAVE_GSL) 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) 3689 #elif defined(VISP_HAVE_EIGEN3) 3691 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1 3693 #elif defined(VISP_HAVE_GSL) 3703 "Install Lapack, Eigen3, OpenCV " 3704 "or GSL 3rd party"));
3754 for (
unsigned int i = 0; i < column_size; i++)
3755 c[i] = (*
this)[i_begin + i][j];
3802 unsigned int nb_rows =
getRows();
3804 for (
unsigned int i = 0; i < nb_rows; i++)
3805 c[i] = (*
this)[i][j];
3901 for (
unsigned int j = 0; j < row_size; j++)
3902 r[j] = (*
this)[i][j_begin + i];
3938 unsigned int nra = A.
getRows();
3939 unsigned int nrb = B.
getRows();
3949 std::cerr <<
"A and C must be two different objects!" << std::endl;
3954 std::cerr <<
"B and C must be two different objects!" << std::endl;
3960 if (C.
data != NULL && A.
data != NULL && A.
size() > 0) {
3965 if (C.
data != NULL && B.
data != NULL && B.
size() > 0) {
4002 std::cerr <<
"A and C must be two different objects!" << std::endl;
4041 std::cerr <<
"A and C must be two different objects!" << std::endl;
4088 for (
unsigned int i = 0; i < A.
getRows(); i++) {
4089 for (
unsigned int j = 0; j < A.
getCols(); j++) {
4090 if (i >= r && i < (r + B.
getRows()) && j >= c && j < (c + B.
getCols())) {
4091 C[i][j] = B[i - r][j - c];
4137 unsigned int nca = A.
getCols();
4138 unsigned int ncb = B.
getCols();
4147 if (B.
getRows() == 0 || nca + ncb == 0) {
4148 std::cerr <<
"B.getRows() == 0 || nca+ncb == 0" << std::endl;
4183 typedef std::string::size_type size_type;
4188 std::vector<std::string> values(m * n);
4189 std::ostringstream oss;
4190 std::ostringstream ossFixed;
4191 std::ios_base::fmtflags original_flags = oss.flags();
4194 ossFixed.setf(std::ios::fixed, std::ios::floatfield);
4196 size_type maxBefore = 0;
4197 size_type maxAfter = 0;
4199 for (
unsigned int i = 0; i < m; ++i) {
4200 for (
unsigned int j = 0; j < n; ++j) {
4202 oss << (*this)[i][j];
4203 if (oss.str().find(
"e") != std::string::npos) {
4205 ossFixed << (*this)[i][j];
4206 oss.str(ossFixed.str());
4209 values[i * n + j] = oss.str();
4210 size_type thislen = values[i * n + j].size();
4211 size_type p = values[i * n + j].find(
'.');
4213 if (p == std::string::npos) {
4223 size_type totalLength = length;
4227 maxAfter = (std::min)(maxAfter, totalLength - maxBefore);
4236 s <<
"[" << m <<
"," << n <<
"]=\n";
4238 for (
unsigned int i = 0; i < m; i++) {
4240 for (
unsigned int j = 0; j < n; j++) {
4241 size_type p = values[i * n + j].find(
'.');
4242 s.setf(std::ios::right, std::ios::adjustfield);
4243 s.width((std::streamsize)maxBefore);
4244 s << values[i * n + j].substr(0, p).c_str();
4247 s.setf(std::ios::left, std::ios::adjustfield);
4248 if (p != std::string::npos) {
4249 s.width((std::streamsize)maxAfter);
4250 s << values[i * n + j].substr(p, maxAfter).c_str();
4252 assert(maxAfter > 1);
4253 s.width((std::streamsize)maxAfter);
4263 s.flags(original_flags);
4265 return (
int)(maxBefore + maxAfter);
4307 for (
unsigned int i = 0; i < this->
getRows(); ++i) {
4308 for (
unsigned int j = 0; j < this->
getCols(); ++j) {
4309 os << (*this)[i][j] <<
", ";
4311 if (this->
getRows() != i + 1) {
4312 os <<
";" << std::endl;
4314 os <<
"]" << std::endl;
4350 os <<
"([ " << std::endl;
4351 for (
unsigned int i = 0; i < this->
getRows(); ++i) {
4353 for (
unsigned int j = 0; j < this->
getCols(); ++j) {
4354 os << (*this)[i][j] <<
", ";
4356 os <<
"]," << std::endl;
4358 os <<
"])" << std::endl;
4391 for (
unsigned int i = 0; i < this->
getRows(); ++i) {
4392 for (
unsigned int j = 0; j < this->
getCols(); ++j) {
4393 os << (*this)[i][j];
4394 if (!(j == (this->
getCols() - 1)))
4440 os <<
"vpMatrix " << matrixName <<
" (" << this->
getRows() <<
", " << this->
getCols() <<
"); " << std::endl;
4442 for (
unsigned int i = 0; i < this->
getRows(); ++i) {
4443 for (
unsigned int j = 0; j < this->
getCols(); ++j) {
4445 os << matrixName <<
"[" << i <<
"][" << j <<
"] = " << (*this)[i][j] <<
"; " << std::endl;
4447 for (
unsigned int k = 0; k <
sizeof(double); ++k) {
4448 os <<
"((unsigned char*)&(" << matrixName <<
"[" << i <<
"][" << j <<
"]) )[" << k <<
"] = 0x" << std::hex
4449 << (
unsigned int)((
unsigned char *)&((*this)[i][j]))[k] <<
"; " << std::endl;
4472 unsigned int rowNumOld =
rowNum;
4503 if (r.
size() == 0) {
4507 unsigned int oldSize =
size();
4512 memcpy(
data + oldSize, r.
data,
sizeof(
double) * r.
size());
4542 if (c.
size() == 0) {
4547 unsigned int oldColNum =
colNum;
4552 for (
unsigned int i = 0; i <
rowNum; i++) {
4553 memcpy(
data + i*
colNum, tmp.
data + i*oldColNum,
sizeof(
double) * oldColNum);
4576 for (
unsigned int i = r; i < (r + A.
getRows()); i++) {
4631 #ifdef VISP_HAVE_GSL 4635 for (
unsigned int i = 0; i <
rowNum; i++) {
4636 for (
unsigned int j = 0; j <
rowNum; j++) {
4638 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
4646 gsl_vector *eval = gsl_vector_alloc(rowNum);
4647 gsl_matrix *evec = gsl_matrix_alloc(rowNum,
colNum);
4649 gsl_eigen_symmv_workspace *w = gsl_eigen_symmv_alloc(rowNum);
4650 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
4652 unsigned int Atda = (
unsigned int)m->tda;
4653 for (
unsigned int i = 0; i <
rowNum; i++) {
4654 unsigned int k = i * Atda;
4655 for (
unsigned int j = 0; j <
colNum; j++)
4656 m->data[k + j] = (*
this)[i][j];
4658 gsl_eigen_symmv(m, eval, evec, w);
4660 gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_ASC);
4662 for (
unsigned int i = 0; i <
rowNum; i++) {
4663 evalue[i] = gsl_vector_get(eval, i);
4666 gsl_eigen_symmv_free(w);
4667 gsl_vector_free(eval);
4669 gsl_matrix_free(evec);
4676 "should install GSL rd party"));
4734 #ifdef VISP_HAVE_GSL 4745 #ifdef VISP_HAVE_GSL 4749 for (
unsigned int i = 0; i <
rowNum; i++) {
4750 for (
unsigned int j = 0; j <
rowNum; j++) {
4752 if (std::fabs(At_A[i][j]) > std::numeric_limits<double>::epsilon()) {
4760 evector.resize(rowNum,
colNum);
4762 gsl_vector *eval = gsl_vector_alloc(rowNum);
4763 gsl_matrix *evec = gsl_matrix_alloc(rowNum,
colNum);
4765 gsl_eigen_symmv_workspace *w = gsl_eigen_symmv_alloc(rowNum);
4766 gsl_matrix *m = gsl_matrix_alloc(rowNum,
colNum);
4768 unsigned int Atda = (
unsigned int)m->tda;
4769 for (
unsigned int i = 0; i <
rowNum; i++) {
4770 unsigned int k = i * Atda;
4771 for (
unsigned int j = 0; j <
colNum; j++)
4772 m->data[k + j] = (*
this)[i][j];
4774 gsl_eigen_symmv(m, eval, evec, w);
4776 gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_ASC);
4778 for (
unsigned int i = 0; i <
rowNum; i++) {
4779 evalue[i] = gsl_vector_get(eval, i);
4781 Atda = (
unsigned int)evec->tda;
4782 for (
unsigned int i = 0; i <
rowNum; i++) {
4783 unsigned int k = i * Atda;
4784 for (
unsigned int j = 0; j <
rowNum; j++) {
4785 evector[i][j] = evec->data[k + j];
4789 gsl_eigen_symmv_free(w);
4790 gsl_vector_free(eval);
4792 gsl_matrix_free(evec);
4797 "should install GSL rd party"));
4823 unsigned int nbline =
getRows();
4824 unsigned int nbcol =
getCols();
4845 for (
unsigned int i = 0; i < nbcol; i++) {
4846 if (fabs(sv[i]) > maxsv) {
4847 maxsv = fabs(sv[i]);
4851 unsigned int rank = 0;
4852 for (
unsigned int i = 0; i < nbcol; i++) {
4853 if (fabs(sv[i]) > maxsv * svThreshold) {
4858 kerAt.
resize(nbcol - rank, nbcol);
4859 if (rank != nbcol) {
4860 for (
unsigned int j = 0, k = 0; j < nbcol; j++) {
4862 if ((fabs(sv[j]) <= maxsv * svThreshold) &&
4863 (std::fabs(V.
getCol(j).
sumSquare()) > std::numeric_limits<double>::epsilon())) {
4864 for (
unsigned int i = 0; i < V.
getRows(); i++) {
4865 kerAt[k][i] = V[i][j];
4930 #ifdef VISP_HAVE_GSL 4932 double *b =
new double[size_];
4933 for (
size_t i = 0; i < size_; i++)
4935 gsl_matrix_view m = gsl_matrix_view_array(this->
data,
rowNum, colNum);
4936 gsl_matrix_view em = gsl_matrix_view_array(b,
rowNum, colNum);
4937 gsl_linalg_exponential_ss(&m.matrix, &em.matrix, 0);
4940 memcpy(expA.
data, b, size_ *
sizeof(
double));
4961 for (
unsigned int i = 0; i <
rowNum; i++) {
4963 for (
unsigned int j = 0; j <
colNum; j++) {
4964 sum += fabs((*
this)[i][j]);
4966 if (sum > nA || i == 0) {
4975 double sca = 1.0 / pow(2.0, s);
4978 _expE = c * exp + _eye;
4979 _expD = -c * exp + _eye;
4980 for (
int k = 2; k <= q; k++) {
4981 c = c * ((double)(q - k + 1)) / ((
double)(k * (2 * q - k + 1)));
4982 _expcX = exp * _expX;
4985 _expE = _expE + _expcX;
4987 _expD = _expD + _expcX;
4989 _expD = _expD - _expcX;
4993 exp = _expX * _expE;
4994 for (
int k = 1; k <= s; k++) {
5026 for (
unsigned int i = 0; i < col; i++) {
5027 for (
unsigned int j = 0; j <
row; j++)
5028 M_comp[i][j] = M[i][j];
5029 for (
unsigned int j = row + 1; j < M.
getRows(); j++)
5030 M_comp[i][j - 1] = M[i][j];
5032 for (
unsigned int i = col + 1; i < M.
getCols(); i++) {
5033 for (
unsigned int j = 0; j <
row; j++)
5034 M_comp[i - 1][j] = M[i][j];
5035 for (
unsigned int j = row + 1; j < M.
getRows(); j++)
5036 M_comp[i - 1][j - 1] = M[i][j];
5056 for (
unsigned int i = 0; i < M.
getCols(); i++) {
5079 for (
unsigned int i = 0; i < H.
getCols(); i++) {
5080 for (
unsigned int j = 0; j < H.
getCols(); j++) {
5081 HLM[i][j] = H[i][j];
5083 HLM[i][j] += alpha * H[i][j];
5099 for (
unsigned int i = 0; i <
dsize; i++) {
5100 double x = *(
data + i);
5120 for (
unsigned int i = 0; i <
rowNum; i++) {
5122 for (
unsigned int j = 0; j <
colNum; j++) {
5123 x += fabs(*(*(
rowPtrs + i) + j));
5140 double sum_square = 0.0;
5143 for (
unsigned int i = 0; i <
rowNum; i++) {
5144 for (
unsigned int j = 0; j <
colNum; j++) {
5146 sum_square += x * x;
5167 conv2(M, kernel, res, mode);
5188 if (mode ==
"valid") {
5195 if (mode ==
"full" || mode ==
"same") {
5196 const unsigned int pad_x = kernel.
getCols()-1;
5197 const unsigned int pad_y = kernel.
getRows()-1;
5199 M_padded.
insert(M, pad_y, pad_x);
5201 if (mode ==
"same") {
5207 }
else if (mode ==
"valid") {
5214 if (mode ==
"same") {
5215 for (
unsigned int i = 0; i < res_same.
getRows(); i++) {
5216 for (
unsigned int j = 0; j < res_same.
getCols(); j++) {
5217 for (
unsigned int k = 0; k < kernel.
getRows(); k++) {
5218 for (
unsigned int l = 0; l < kernel.
getCols(); l++) {
5219 res_same[i][j] += M_padded[i+k][j+l] * kernel[kernel.
getRows()-k-1][kernel.
getCols()-l-1];
5225 const unsigned int start_i = kernel.
getRows()/2;
5226 const unsigned int start_j = kernel.
getCols()/2;
5227 for (
unsigned int i = 0; i < M.
getRows(); i++) {
5231 for (
unsigned int i = 0; i < res.
getRows(); i++) {
5232 for (
unsigned int j = 0; j < res.
getCols(); j++) {
5233 for (
unsigned int k = 0; k < kernel.
getRows(); k++) {
5234 for (
unsigned int l = 0; l < kernel.
getCols(); l++) {
5235 res[i][j] += M_padded[i+k][j+l] * kernel[kernel.
getRows()-k-1][kernel.
getCols()-l-1];
5243 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS) 5280 for (
unsigned int j = 0; j <
getCols(); j++)
5281 c[j] = (*
this)[i - 1][j];
5306 for (
unsigned int i = 0; i <
getRows(); i++)
5307 c[i] = (*
this)[i][j - 1];
5319 for (
unsigned int i = 0; i <
rowNum; i++)
5320 for (
unsigned int j = 0; j <
colNum; j++)
5322 (*this)[i][j] = val;
5327 #endif //#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS) void svd(vpColVector &w, vpMatrix &V)
vpMatrix operator*(const double &x, const vpMatrix &B)
vpMatrix pseudoInverseEigen3(double svThreshold=1e-6) const
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)
vpMatrix pseudoInverseOpenCV(double svThreshold=1e-6) const
vp_deprecated vpRowVector row(const unsigned int i)
double infinityNorm() const
static vpMatrix conv2(const vpMatrix &M, const vpMatrix &kernel, const std::string &mode="full")
vpMatrix pseudoInverseLapack(double svThreshold=1e-6) const
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of row vector and the associated operations.
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) 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 & 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 size() const
Return the number of elements of the 2D array.
unsigned int getCols() const
vpMatrix & operator+=(const vpMatrix &B)
Operation A = A + B.
vp_deprecated vpColVector column(const unsigned int j)
std::ostream & maplePrint(std::ostream &os) const
void svdLapack(vpColVector &w, vpMatrix &V)
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)
vpMatrix extract(unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols) 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)
vpColVector getCol(const unsigned int j) const
static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
vpMatrix operator+(const vpMatrix &B) const
vpMatrix transpose() const
void svdOpenCV(vpColVector &w, vpMatrix &V)
vpMatrix pseudoInverseGsl(double svThreshold=1e-6) const
unsigned int getRows() const
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 svdGsl(vpColVector &w, vpMatrix &V)
void resize(const unsigned int i, const bool flagNullify=true)
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
unsigned int kernel(vpMatrix &kerAt, double svThreshold=1e-6) const
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)
Function not implemented.
Class that consider the case of a translation vector.
double ** rowPtrs
Address of the first element of each rows.
void svdEigen3(vpColVector &w, vpMatrix &V)
vpMatrix & operator=(const vpArray2D< double > &A)
void resize(const unsigned int i, const bool flagNullify=true)
vpMatrix & operator<<(double *)
vpMatrix hadamard(const vpMatrix &m) const