34 #include <visp3/core/vpConfig.h>
35 #include <visp3/core/vpMatrix.h>
37 #if defined(VISP_HAVE_SIMDLIB)
38 #include <Simd/SimdLib.h>
69 const unsigned int val_16 = 16;
71 for (
unsigned int i = 0; i <
rowNum; ++i) {
72 for (
unsigned int j = 0; j <
colNum; ++j) {
73 At[j][i] = (*this)[i][j];
78 #if defined(VISP_HAVE_SIMDLIB)
82 const int tileSize = 32;
83 for (
unsigned int i = 0; i <
rowNum; i += tileSize) {
84 for (
unsigned int j = 0; j <
colNum; ++j) {
85 for (
unsigned int b = 0; ((b < static_cast<unsigned int>(tileSize)) && ((i + b) <
rowNum)); ++b) {
86 At[j][i + b] = (*this)[i + b][j];
114 bool useLapack = ((A.
rowNum > vpMatrix::m_lapack_min_size) || (A.
colNum > vpMatrix::m_lapack_min_size));
115 #if !(defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL))
120 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL)
126 vpMatrix::blas_dgemv(trans, A.
colNum, A.
rowNum, alpha, A.
data, A.
colNum, v.
data, incr, beta, w.
data, incr);
131 for (
unsigned int j = 0; j < A.
colNum; ++j) {
133 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
165 bool useLapack = ((A.
rowNum > vpMatrix::m_lapack_min_size) || (A.
colNum > vpMatrix::m_lapack_min_size) ||
166 (B.
colNum > vpMatrix::m_lapack_min_size));
167 #if !(defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL))
172 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL)
173 const double alpha = 1.0;
174 const double beta = 0.0;
175 const char trans =
'n';
176 vpMatrix::blas_dgemm(trans, trans, B.
colNum, A.
rowNum, A.
colNum, alpha, B.
data, B.
colNum, A.
data, A.
colNum, beta,
182 const unsigned int BcolNum = B.
colNum;
183 const unsigned int BrowNum = B.
rowNum;
185 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
186 const double *rowptri = A.
rowPtrs[i];
188 for (
unsigned int j = 0; j < BcolNum; ++j) {
190 for (
unsigned int k = 0; k < BrowNum; ++k) {
191 s += rowptri[k] * BrowPtrs[k][j];
213 const unsigned int val_3 = 3;
216 "Cannot multiply (%dx%d) matrix by (%dx%d) matrix as a "
221 const unsigned int BcolNum = B.
colNum;
222 const unsigned int BrowNum = B.
rowNum;
224 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
225 const double *rowptri = A.
rowPtrs[i];
227 for (
unsigned int j = 0; j < BcolNum; ++j) {
229 for (
unsigned int k = 0; k < BrowNum; ++k) {
230 s += rowptri[k] * BrowPtrs[k][j];
251 const unsigned int val_4 = 4;
254 "Cannot multiply (%dx%d) matrix by (%dx%d) matrix as a "
263 bool useLapack = ((A.
rowNum > vpMatrix::m_lapack_min_size) || (A.
colNum > vpMatrix::m_lapack_min_size) ||
264 (B.
colNum > vpMatrix::m_lapack_min_size));
265 #if !(defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL))
270 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL)
271 const double alpha = 1.0;
272 const double beta = 0.0;
273 const char trans =
'n';
274 vpMatrix::blas_dgemm(trans, trans, B.
colNum, A.
rowNum, A.
colNum, alpha, B.
data, B.
colNum, A.
data, A.
colNum, beta,
280 const unsigned int BcolNum = B.
colNum;
281 const unsigned int BrowNum = B.
rowNum;
283 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
284 const double *rowptri = A.
rowPtrs[i];
286 for (
unsigned int j = 0; j < BcolNum; ++j) {
288 for (
unsigned int k = 0; k < BrowNum; ++k) {
289 s += rowptri[k] * BrowPtrs[k][j];
340 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
341 for (
unsigned int j = 0; j < A.
colNum; ++j) {
342 CrowPtrs[i][j] = (wB * BrowPtrs[i][j]) + (wA * ArowPtrs[i][j]);
371 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
372 for (
unsigned int j = 0; j < A.
colNum; ++j) {
373 CrowPtrs[i][j] = BrowPtrs[i][j] + ArowPtrs[i][j];
405 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
406 for (
unsigned int j = 0; j < A.
colNum; ++j) {
407 CrowPtrs[i][j] = BrowPtrs[i][j] + ArowPtrs[i][j];
442 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
443 for (
unsigned int j = 0; j < A.
colNum; ++j) {
444 CrowPtrs[i][j] = ArowPtrs[i][j] - BrowPtrs[i][j];
476 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
477 for (
unsigned int j = 0; j < A.
colNum; ++j) {
478 CrowPtrs[i][j] = ArowPtrs[i][j] - BrowPtrs[i][j];
501 for (
unsigned int i = 0; i < A.
rowNum; ++i) {
502 for (
unsigned int j = 0; j < A.
colNum; ++j) {
503 CrowPtrs[i][j] = -ArowPtrs[i][j];
540 bool useLapack = ((
rowNum > vpMatrix::m_lapack_min_size) || (
colNum > vpMatrix::m_lapack_min_size));
541 #if !(defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL))
546 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL)
547 const double alpha = 1.0;
548 const double beta = 0.0;
549 const char transa =
't';
550 const char transb =
'n';
552 vpMatrix::blas_dgemm(transa, transb,
rowNum,
rowNum,
colNum, alpha,
data,
colNum,
data,
colNum, beta, B.
data,
558 for (
unsigned int i = 0; i <
rowNum; ++i) {
559 for (
unsigned int j = i; j <
rowNum; ++j) {
565 for (
unsigned int k = 0; k <
colNum; ++k) {
566 ssum += (*pi) * (*pj);
598 bool useLapack = ((
rowNum > vpMatrix::m_lapack_min_size) || (
colNum > vpMatrix::m_lapack_min_size));
599 #if !(defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL))
604 #if defined(VISP_HAVE_LAPACK) && !defined(VISP_HAVE_LAPACK_BUILT_IN) && !defined(VISP_HAVE_GSL)
605 const double alpha = 1.0;
606 const double beta = 0.0;
607 const char transa =
'n';
608 const char transb =
't';
610 vpMatrix::blas_dgemm(transa, transb,
colNum,
colNum,
rowNum, alpha,
data,
colNum,
data,
colNum, beta, B.
data,
615 for (
unsigned int i = 0; i <
colNum; ++i) {
617 for (
unsigned int j = 0; j < i; ++j) {
620 for (
unsigned int k = 0; k <
rowNum; ++k) {
621 s += (*(ptr + i)) * (*(ptr + j));
629 for (
unsigned int k = 0; k <
rowNum; ++k) {
630 s += (*(ptr + i)) * (*(ptr + i));
693 unsigned int rows = A.
getRows();
697 for (
unsigned int i = 0; i < rows; ++i) {
698 (*this)[i][i] = A[i];
740 for (
unsigned int i = 0; i < min_; ++i) {
757 unsigned int rows = A.
getRows();
758 DA.
resize(rows, rows,
true);
760 for (
unsigned int i = 0; i < rows; ++i) {
788 for (
unsigned int i = 0; i <
rowNum; ++i) {
789 for (
unsigned int j = 0; j <
colNum; ++j) {
815 #if defined(VISP_HAVE_SIMDLIB)
818 for (
unsigned int i = 0; i <
dsize; ++i) {
834 unsigned int r1 = m1.
getRows();
835 unsigned int c1 = m1.
getCols();
836 unsigned int r2 = m2.
getRows();
837 unsigned int c2 = m2.
getCols();
839 out.
resize(r1 * r2, c1 * c2,
false,
false);
841 for (
unsigned int r = 0; r < r1; ++r) {
842 for (
unsigned int c = 0; c < c1; ++c) {
843 double alpha = m1[r][c];
844 double *m2ptr = m2[0];
845 unsigned int roffset = r * r2;
846 unsigned int coffset = c * c2;
847 for (
unsigned int rr = 0; rr < r2; ++rr) {
848 for (
unsigned int cc = 0; cc < c2; ++cc) {
849 out[roffset + rr][coffset + cc] = alpha * (*m2ptr);
873 unsigned int r1 = m1.
getRows();
874 unsigned int c1 = m1.
getCols();
875 unsigned int r2 = m2.
getRows();
876 unsigned int c2 = m2.
getCols();
879 out.
resize(r1 * r2, c1 * c2,
false,
false);
881 for (
unsigned int r = 0; r < r1; ++r) {
882 for (
unsigned int c = 0; c < c1; ++c) {
883 double alpha = m1[r][c];
884 double *m2ptr = m2[0];
885 unsigned int roffset = r * r2;
886 unsigned int coffset = c * c2;
887 for (
unsigned int rr = 0; rr < r2; ++rr) {
888 for (
unsigned int cc = 0; cc < c2; ++cc) {
889 out[roffset + rr][coffset + cc] = alpha * (*m2ptr);
918 if (mode ==
"valid") {
926 if ((mode ==
"full") || (mode ==
"same")) {
927 const unsigned int pad_x =
kernel.getCols() - 1;
928 const unsigned int pad_y =
kernel.getRows() - 1;
929 const unsigned int pad = 2;
931 M_padded.
insert(M, pad_y, pad_x);
933 if (mode ==
"same") {
941 else if (mode ==
"valid") {
949 if (mode ==
"same") {
950 unsigned int res_same_rows = res_same.
getRows();
951 unsigned int res_same_cols = res_same.
getCols();
952 unsigned int kernel_rows =
kernel.getRows();
953 unsigned int kernel_cols =
kernel.getCols();
954 for (
unsigned int i = 0; i < res_same_rows; ++i) {
955 for (
unsigned int j = 0; j < res_same_cols; ++j) {
956 for (
unsigned int k = 0; k < kernel_rows; ++k) {
957 for (
unsigned int l = 0; l < kernel_cols; ++l) {
958 res_same[i][j] += M_padded[i + k][j + l] *
kernel[
kernel.getRows() - k - 1][
kernel.getCols() - l - 1];
964 const unsigned int start_i =
kernel.getRows() / 2;
965 const unsigned int start_j =
kernel.getCols() / 2;
966 unsigned int m_rows = M.
getRows();
967 for (
unsigned int i = 0; i < m_rows; ++i) {
973 unsigned int res_rows = res.
getRows();
974 unsigned int res_cols = res.
getCols();
975 unsigned int kernel_rows =
kernel.getRows();
976 unsigned int kernel_cols =
kernel.getCols();
977 for (
unsigned int i = 0; i < res_rows; ++i) {
978 for (
unsigned int j = 0; j < res_cols; ++j) {
979 for (
unsigned int k = 0; k < kernel_rows; ++k) {
980 for (
unsigned int l = 0; l < kernel_cols; ++l) {
981 res[i][j] += M_padded[i + k][j + l] *
kernel[
kernel.getRows() - k - 1][
kernel.getCols() - l - 1];
unsigned int getCols() const
double * data
Address of the first element of the data array.
Type ** rowPtrs
Address of the first element of each rows.
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 dsize
Current array size (rowNum * colNum)
unsigned int getRows() const
unsigned int colNum
Number of columns in the array.
Implementation of column vector and the associated operations.
void resize(unsigned int i, bool flagNullify=true)
error that can be emitted by ViSP classes.
@ dimensionError
Bad dimension.
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a matrix and operations on matrices.
vpMatrix hadamard(const vpMatrix &m) const
unsigned int kernel(vpMatrix &kerAt, double svThreshold=1e-6) const
static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
void diag(const double &val=1.0)
static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B, const double &wB, vpMatrix &C)
static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA)
static void multMatrixVector(const vpMatrix &A, const vpColVector &v, vpColVector &w)
static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
void insert(const vpMatrix &A, unsigned int r, unsigned int c)
void kron(const vpMatrix &m1, vpMatrix &out) const
static vpMatrix conv2(const vpMatrix &M, const vpMatrix &kernel, const std::string &mode)
vpMatrix transpose() const
static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
static void negateMatrix(const vpMatrix &A, vpMatrix &C)
Implementation of a rotation matrix and operations on such kind of matrices.