34 #include <visp3/core/vpConfig.h>
36 #include <visp3/core/vpColVector.h>
37 #include <visp3/core/vpMath.h>
38 #include <visp3/core/vpMatrix.h>
40 #ifdef VISP_HAVE_EIGEN3
44 #ifdef VISP_HAVE_LAPACK
46 #include <gsl/gsl_linalg.h>
47 #include <gsl/gsl_permutation.h>
51 typedef MKL_INT integer;
53 #ifdef VISP_HAVE_LAPACK_BUILT_IN
54 typedef long int integer;
58 extern "C" int dgetrf_(integer *m, integer *n,
double *a, integer *lda, integer *ipiv, integer *info);
59 extern "C" void dgetri_(integer *n,
double *a, integer *lda, integer *ipiv,
double *work, integer *lwork,
64 #if defined(VISP_HAVE_OPENCV)
65 #include <opencv2/core/core.hpp>
69 #include <visp3/core/vpException.h>
70 #include <visp3/core/vpMatrixException.h>
132 const unsigned int val_2 = 2;
133 const unsigned int val_3 = 3;
138 if (std::fabs(d) < std::numeric_limits<double>::epsilon()) {
147 inv.
resize(val_2, val_2,
false);
149 if (std::fabs(d) < std::numeric_limits<double>::epsilon()) {
154 inv[1][1] = (*this)[0][0] * d;
155 inv[0][0] = (*this)[1][1] * d;
156 inv[0][1] = -(*this)[0][1] * d;
157 inv[1][0] = -(*this)[1][0] * d;
162 inv.
resize(val_3, val_3,
false);
164 if (std::fabs(d) < std::numeric_limits<double>::epsilon()) {
169 const unsigned int index_0 = 0;
170 const unsigned int index_1 = 1;
171 const unsigned int index_2 = 2;
172 inv[index_0][index_0] = (((*this)[index_1][index_1] * (*this)[index_2][index_2]) - ((*
this)[index_1][index_2] * (*this)[index_2][index_1])) * d;
173 inv[index_0][index_1] = (((*this)[index_0][index_2] * (*this)[index_2][index_1]) - ((*
this)[index_0][index_1] * (*this)[index_2][index_2])) * d;
174 inv[index_0][index_2] = (((*this)[index_0][index_1] * (*this)[index_1][index_2]) - ((*
this)[index_0][index_2] * (*this)[index_1][index_1])) * d;
176 inv[index_1][index_0] = (((*this)[index_1][index_2] * (*this)[index_2][index_0]) - ((*
this)[index_1][index_0] * (*this)[index_2][index_2])) * d;
177 inv[index_1][index_1] = (((*this)[index_0][index_0] * (*this)[index_2][index_2]) - ((*
this)[index_0][index_2] * (*this)[index_2][index_0])) * d;
178 inv[index_1][index_2] = (((*this)[index_0][index_2] * (*this)[index_1][index_0]) - ((*
this)[index_0][index_0] * (*this)[index_1][index_2])) * d;
180 inv[index_2][index_0] = (((*this)[index_1][index_0] * (*this)[index_2][index_1]) - ((*
this)[index_1][index_1] * (*this)[index_2][index_0])) * d;
181 inv[index_2][index_1] = (((*this)[index_0][index_1] * (*this)[index_2][index_0]) - ((*
this)[index_0][index_0] * (*this)[index_2][index_1])) * d;
182 inv[index_2][index_2] = (((*this)[index_0][index_0] * (*this)[index_1][index_1]) - ((*
this)[index_0][index_1] * (*this)[index_1][index_0])) * d;
186 #if defined(VISP_HAVE_LAPACK)
188 #elif defined(VISP_HAVE_EIGEN3)
190 #elif defined(VISP_HAVE_OPENCV)
194 "Install Lapack, Eigen3 or OpenCV 3rd party"));
238 const unsigned int val_2 = 2;
239 const unsigned int val_3 = 3;
241 return (*
this)[0][0];
244 return (((*
this)[0][0] * (*
this)[1][1]) - ((*
this)[0][1] * (*
this)[1][0]));
247 const unsigned int index_0 = 0;
248 const unsigned int index_1 = 1;
249 const unsigned int index_2 = 2;
250 return ((((*
this)[index_0][index_0] * (((*
this)[index_1][index_1] * (*
this)[index_2][index_2]) - ((*
this)[index_1][index_2] * (*
this)[index_2][index_1]))) -
251 ((*
this)[index_0][index_1] * (((*
this)[index_1][index_0] * (*
this)[index_2][index_2]) - ((*
this)[index_1][index_2] * (*
this)[index_2][index_0])))) +
252 ((*
this)[index_0][index_2] * (((*
this)[index_1][index_0] * (*
this)[index_2][index_1]) - ((*
this)[index_1][index_1] * (*
this)[index_2][index_0]))));
255 #if defined(VISP_HAVE_LAPACK)
257 #elif defined(VISP_HAVE_EIGEN3)
259 #elif defined(VISP_HAVE_OPENCV)
263 "Install Lapack, Eigen3 or OpenCV 3rd party"));
268 #if defined(VISP_HAVE_LAPACK)
305 #if defined(VISP_HAVE_GSL)
314 unsigned int tda =
static_cast<unsigned int>(A->tda);
315 for (
unsigned int i = 0; i <
rowNum; ++i) {
316 unsigned int k = i * tda;
317 for (
unsigned int j = 0; j <
colNum; ++j)
318 A->data[k + j] = (*
this)[i][j];
326 inverse.tda = inverse.size2;
327 inverse.data = Ainv.
data;
331 gsl_permutation *p = gsl_permutation_alloc(
rowNum);
335 gsl_linalg_LU_decomp(A, p, &s);
336 gsl_linalg_LU_invert(A, p, &inverse);
338 gsl_permutation_free(p);
349 integer dim = (integer)
rowNum;
352 integer lwork = dim * dim;
353 integer *ipiv =
new integer[dim + 1];
354 double *work =
new double[lwork];
358 dgetrf_(&dim, &dim, A.
data, &lda, &ipiv[1], &info);
365 dgetri_(&dim, A.
data, &dim, &ipiv[1], work, &lwork, &info);
406 #if defined(VISP_HAVE_GSL)
418 unsigned int tda = (
unsigned int)A->tda;
419 for (
unsigned int i = 0; i <
rowNum; i++) {
420 unsigned int k = i * tda;
421 for (
unsigned int j = 0; j <
colNum; j++)
422 A->data[k + j] = (*
this)[i][j];
425 gsl_permutation *p = gsl_permutation_alloc(
rowNum);
429 gsl_linalg_LU_decomp(A, p, &s);
430 det = gsl_linalg_LU_det(A, s);
432 gsl_permutation_free(p);
444 integer dim = (integer)
rowNum;
447 integer *ipiv =
new integer[dim + 1];
451 dgetrf_(&dim, &dim, A.
data, &lda, &ipiv[1], &info);
457 double det = A[0][0];
458 for (
unsigned int i = 1; i <
rowNum; ++i) {
463 for (
int i = 1; i <= dim; ++i) {
478 #if defined(VISP_HAVE_OPENCV)
521 cv::Mat Minv = M.inv(cv::DECOMP_LU);
524 memcpy(A.
data, Minv.data, (
size_t)(8 * Minv.rows * Minv.cols));
568 det = cv::determinant(M);
574 #if defined(VISP_HAVE_EIGEN3)
617 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > M(this->
data, this->
getRows(),
619 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > A_(A.
data, this->getRows(),
663 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > M(this->
data, this->
getRows(),
666 return M.determinant();
unsigned int getCols() const
Type * data
Address of the first element of the data array.
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
unsigned int rowNum
Number of rows in the array.
unsigned int getRows() const
unsigned int colNum
Number of columns in the array.
error that can be emitted by ViSP classes.
Implementation of a matrix and operations on matrices.
vpMatrix inverseByLU() const
vpMatrix inverseByLUEigen3() const
vpMatrix inverseByLUOpenCV() const
double detByLUEigen3() const
double detByLUOpenCV() const
vpMatrix inverseByLULapack() const
double det(vpDetMethod method=LU_DECOMPOSITION) const
double detByLULapack() const