42 num_rows(nrows), num_cols(ncols) { }
48 matrix=SG_MALLOC(T, ((int64_t) nrows)*ncols);
65 REQUIRE(nrows>0,
"Number of rows (%d) has to be a positive integer!\n", nrows);
66 REQUIRE(ncols>0,
"Number of cols (%d) has to be a positive integer!\n", ncols);
67 REQUIRE(vec.
vlen==nrows*ncols,
"Number of elements in the matrix (%d) must "
68 "be the same as the number of elements in the vector (%d)!\n",
69 nrows*ncols, vec.
vlen);
84 num_rows(mat.rows()), num_cols(mat.cols())
119 for (int64_t i=0; i<int64_t(num_rows)*num_cols; ++i)
121 if (matrix[i]!=other.
matrix[i])
131 for (int64_t i=0; i<int64_t(num_rows)*num_cols; i++)
132 matrix[i]=const_elem ;
138 if (matrix && (int64_t(num_rows)*num_cols))
145 if (matrix && (int64_t(num_rows)*num_cols))
152 if (num_rows!=num_cols)
154 for (
int i=0; i<num_rows; ++i)
156 for (
int j=i+1; j<num_cols; ++j)
158 if (matrix[j*num_rows+i]!=matrix[i*num_rows+j])
168 if (num_rows!=num_cols)
170 for (
int i=0; i<num_rows; ++i)
172 for (
int j=i+1; j<num_cols; ++j)
174 if (!CMath::fequals<float32_t>(matrix[j*num_rows+i],
175 matrix[i*num_rows+j], FLT_EPSILON))
185 if (num_rows!=num_cols)
187 for (
int i=0; i<num_rows; ++i)
189 for (
int j=i+1; j<num_cols; ++j)
191 if (!CMath::fequals<float64_t>(matrix[j*num_rows+i],
192 matrix[i*num_rows+j], DBL_EPSILON))
202 if (num_rows!=num_cols)
204 for (
int i=0; i<num_rows; ++i)
206 for (
int j=i+1; j<num_cols; ++j)
208 if (!CMath::fequals<floatmax_t>(matrix[j*num_rows+i],
209 matrix[i*num_rows+j], LDBL_EPSILON))
219 if (num_rows!=num_cols)
221 for (
int i=0; i<num_rows; ++i)
223 for (
int j=i+1; j<num_cols; ++j)
225 if (!(CMath::fequals<float64_t>(matrix[j*num_rows+i].real(),
226 matrix[i*num_rows+j].real(), DBL_EPSILON) &&
227 CMath::fequals<float64_t>(matrix[j*num_rows+i].imag(),
228 matrix[i*num_rows+j].imag(), DBL_EPSILON)))
239 for (int64_t i=1; i<int64_t(num_rows)*num_cols; ++i)
251 SG_SERROR(
"SGMatrix::max_single():: Not supported for complex128_t\n");
258 return SGMatrix<T>(clone_matrix(matrix, num_rows, num_cols),
265 T* result = SG_MALLOC(T, int64_t(nrows)*ncols);
266 for (int64_t i=0; i<int64_t(nrows)*ncols; i++)
274 T*& matrix, int32_t& num_feat, int32_t& num_vec)
277 T* transposed=SG_MALLOC(T, int64_t(num_vec)*num_feat);
278 for (int64_t i=0; i<num_vec; i++)
280 for (int64_t j=0; j<num_feat; j++)
281 transposed[i+j*num_vec]=matrix[i*num_feat+j];
287 CMath::swap(num_feat, num_vec);
293 for(int64_t i=0;i<size;i++)
295 for(int64_t j=0;j<size;j++)
298 matrix[j*size+i]=v[i];
307 float64_t* mat, int32_t cols, int32_t rows)
310 for (int64_t i=0; i<rows; i++)
311 trace+=mat[i*cols+i];
318 T* rowsums=SG_CALLOC(T, n);
320 for (int64_t i=0; i<n; i++)
322 for (int64_t j=0; j<m; j++)
323 rowsums[i]+=matrix[j+i*m];
331 T* colsums=SG_CALLOC(T, m);
333 for (int64_t i=0; i<n; i++)
335 for (int64_t j=0; j<m; j++)
336 colsums[j]+=matrix[j+i*m];
344 center_matrix(matrix, num_rows, num_cols);
352 T* colsums=get_column_sum(matrix, m,n);
353 T* rowsums=get_row_sum(matrix, m,n);
355 for (int32_t i=0; i<m; i++)
356 colsums[i]/=num_data;
357 for (int32_t j=0; j<n; j++)
358 rowsums[j]/=num_data;
362 for (int64_t i=0; i<n; i++)
364 for (int64_t j=0; j<m; j++)
365 matrix[i*m+j]+=s-colsums[j]-rowsums[i];
377 T* means=get_row_sum(matrix, num_rows, num_cols);
380 for (int64_t i=0; i<num_cols; ++i)
383 for (int64_t j=0; j<num_rows; ++j)
384 matrix[i*num_rows+j]-=means[i];
392 display_matrix(matrix, num_rows, num_cols, name);
405 const bool* matrix, int32_t rows, int32_t cols,
const char* name,
408 ASSERT(rows>=0 && cols>=0)
410 for (int64_t i=0; i<rows; i++)
413 for (int64_t j=0; j<cols; j++)
414 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i] ? 1 : 0,
415 j==cols-1?
"" :
",");
416 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
423 const char* matrix, int32_t rows, int32_t cols,
const char* name,
426 ASSERT(rows>=0 && cols>=0)
428 for (int64_t i=0; i<rows; i++)
431 for (int64_t j=0; j<cols; j++)
432 SG_SPRINT(
"%s\t%c%s", prefix, matrix[j*rows+i],
433 j==cols-1?
"" :
",");
434 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
441 const int8_t* matrix, int32_t rows, int32_t cols,
const char* name,
444 ASSERT(rows>=0 && cols>=0)
446 for (int64_t i=0; i<rows; i++)
449 for (int64_t j=0; j<cols; j++)
450 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
451 j==cols-1?
"" :
",");
452 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
459 const uint8_t* matrix, int32_t rows, int32_t cols,
const char* name,
462 ASSERT(rows>=0 && cols>=0)
464 for (int64_t i=0; i<rows; i++)
467 for (int64_t j=0; j<cols; j++)
468 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
469 j==cols-1?
"" :
",");
470 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
477 const int16_t* matrix, int32_t rows, int32_t cols,
const char* name,
480 ASSERT(rows>=0 && cols>=0)
482 for (int64_t i=0; i<rows; i++)
485 for (int64_t j=0; j<cols; j++)
486 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
487 j==cols-1?
"" :
",");
488 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
495 const uint16_t* matrix, int32_t rows, int32_t cols,
const char* name,
498 ASSERT(rows>=0 && cols>=0)
500 for (int64_t i=0; i<rows; i++)
503 for (int64_t j=0; j<cols; j++)
504 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
505 j==cols-1?
"" :
",");
506 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
514 const int32_t* matrix, int32_t rows, int32_t cols,
const char* name,
517 ASSERT(rows>=0 && cols>=0)
519 for (int64_t i=0; i<rows; i++)
522 for (int64_t j=0; j<cols; j++)
523 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
524 j==cols-1?
"" :
",");
525 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
532 const uint32_t* matrix, int32_t rows, int32_t cols,
const char* name,
535 ASSERT(rows>=0 && cols>=0)
537 for (int64_t i=0; i<rows; i++)
540 for (int64_t j=0; j<cols; j++)
541 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
542 j==cols-1?
"" :
",");
543 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
549 const int64_t* matrix, int32_t rows, int32_t cols,
const char* name,
552 ASSERT(rows>=0 && cols>=0)
554 for (int64_t i=0; i<rows; i++)
557 for (int64_t j=0; j<cols; j++)
558 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
559 j==cols-1?
"" :
",");
560 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
567 const uint64_t* matrix, int32_t rows, int32_t cols,
const char* name,
570 ASSERT(rows>=0 && cols>=0)
572 for (int64_t i=0; i<rows; i++)
575 for (int64_t j=0; j<cols; j++)
576 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
577 j==cols-1?
"" :
",");
578 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
585 const float32_t* matrix, int32_t rows, int32_t cols,
const char* name,
588 ASSERT(rows>=0 && cols>=0)
590 for (int64_t i=0; i<rows; i++)
593 for (int64_t j=0; j<cols; j++)
594 SG_SPRINT(
"%s\t%.18g%s", prefix, (
float) matrix[j*rows+i],
595 j==cols-1?
"" :
",");
596 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
603 const float64_t* matrix, int32_t rows, int32_t cols,
const char* name,
606 ASSERT(rows>=0 && cols>=0)
608 for (int64_t i=0; i<rows; i++)
611 for (int64_t j=0; j<cols; j++)
612 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
613 j==cols-1?
"" :
",");
614 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
621 const floatmax_t* matrix, int32_t rows, int32_t cols,
const char* name,
624 ASSERT(rows>=0 && cols>=0)
626 for (int64_t i=0; i<rows; i++)
629 for (int64_t j=0; j<cols; j++)
630 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
631 j==cols-1?
"" :
",");
632 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
639 const complex128_t* matrix, int32_t rows, int32_t cols,
const char* name,
642 ASSERT(rows>=0 && cols>=0)
644 for (int64_t i=0; i<rows; i++)
647 for (int64_t j=0; j<cols; j++)
648 SG_SPRINT(
"%s\t(%.18g+i%.18g)%s", prefix, matrix[j*rows+i].real(),
649 matrix[j*rows+i].imag(), j==cols-1?
"" :
",");
650 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
669 I(i,j)=i==j ? scale : 0.0;
682 I(i,j)=i==j ? scale : 0.0;
695 I(i,j)=i==j ? scale : (!
scale);
708 I(i,j)=i==j ? scale : 0.0;
721 I(i,j)=i==j ? scale : 0.0;
734 I(i,j)=i==j ? scale : 0.0;
747 I(i,j)=i==j ? scale : 0.0;
760 I(i,j)=i==j ? scale : 0.0;
773 I(i,j)=i==j ? scale : 0.0;
786 I(i,j)=i==j ? scale : 0.0;
799 I(i,j)=i==j ? scale : 0.0;
812 I(i,j)=i==j ? scale : 0.0;
856 int32_t lsize=CMath::min((int32_t) m, (int32_t) n);
857 double* s=SG_MALLOC(
double, lsize);
858 double* u=SG_MALLOC(
double, m*m);
859 double* vt=SG_MALLOC(
double, n*n);
861 wrap_dgesvd(jobu, jobvt, m, n, matrix, lda, s, u, ldu, vt, ldvt, &info);
864 for (int64_t i=0; i<n; i++)
866 for (int64_t j=0; j<lsize; j++)
867 vt[i*n+j]=vt[i*n+j]/s[j];
870 cblas_dgemm(CblasColMajor, CblasTrans, CblasTrans, m, n, m, 1.0, vt, ldvt, u, ldu, 0, target, m);
884 int32_t* ipiv = SG_MALLOC(int32_t, matrix.
num_cols);
895 SG_SERROR(
"SGMatrix::compute_eigenvectors(SGMatrix<float64_t>): matrix"
896 " rows and columns are not equal!\n");
917 double* eigenvalues=SG_CALLOC(
float64_t, n+1);
924 SG_SERROR(
"DSYEV failed with code %d\n", info)
931 int n,
int il,
int iu)
933 eigenvalues = SG_MALLOC(
double, n);
934 eigenvectors = SG_MALLOC(
double, (iu-il+1)*n);
936 wrap_dsyevr(
'V',
'U',n,matrix_,n,il,iu,eigenvalues,eigenvectors,&status);
956 SG_SERROR(
"SGMatrix::matrix_multiply(): Dimension mismatch: "
957 "A(%dx%d)*B(%dx%D)\n", rows_A, cols_A, rows_B, cols_B);
965 cblas_dgemm(CblasColMajor,
966 transpose_A ? CblasTrans : CblasNoTrans,
967 transpose_B ? CblasTrans : CblasNoTrans,
968 rows_A, cols_B, cols_A, scale,
973 for (int32_t i=0; i<rows_A; i++)
975 for (int32_t j=0; j<cols_B; j++)
977 for (int32_t k=0; k<cols_A; k++)
979 float64_t x1=transpose_A ? A(k,i):A(i,k);
980 float64_t x2=transpose_B ? B(j,k):B(k,j);
1001 result=pre_allocated;
1004 if (pre_allocated.
num_rows!=num_rows ||
1007 SG_SERROR(
"SGMatrix<T>::get_allocated_matrix(). Provided target"
1008 "matrix dimensions (%dx%d) do not match passed data "
1009 "dimensions (%dx%d)!\n", pre_allocated.
num_rows,
1010 pre_allocated.
num_cols, num_rows, num_cols);
1025 matrix=((
SGMatrix*)(&orig))->matrix;
1026 num_rows=((
SGMatrix*)(&orig))->num_rows;
1027 num_cols=((
SGMatrix*)(&orig))->num_cols;
1065 SG_SERROR(
"SGMatrix::load():: Not supported for complex128_t\n");
1073 writer->
set_matrix(matrix, num_rows, num_cols);
1080 SG_SERROR(
"SGMatrix::save():: Not supported for complex128_t\n");
1087 for (int64_t i = 0; i < num_cols; i++)
1089 rowv[i] = matrix[i*num_rows+row];
1097 index_t diag_vlen=CMath::min(num_cols, num_rows);
1100 for (int64_t i=0; i<diag_vlen; i++)
1102 diag[i]=matrix[i*num_rows+i];
void display_matrix(const char *name="matrix") const
void wrap_dsyevr(char jobz, char uplo, int n, double *a, int lda, int il, int iu, double *eigenvalues, double *eigenvectors, int *info)
std::complex< float64_t > complex128_t
virtual void set_matrix(const bool *matrix, int32_t num_feat, int32_t num_vec)
#define SG_SNOTIMPLEMENTED
bool operator==(SGMatrix< T > &other)
void wrap_dgesvd(char jobu, char jobvt, int m, int n, double *a, int lda, double *sing, double *u, int ldu, double *vt, int ldvt, int *info)
virtual void get_matrix(bool *&matrix, int32_t &num_feat, int32_t &num_vec)
shogun reference count managed data
A File access base class.
void wrap_dsyev(char jobz, char uplo, int n, double *a, int lda, double *w, int *info)
virtual void copy_data(const SGReferencedData &orig)
all of classes and functions are contained in the shogun namespace
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
Matrix::Scalar max(Matrix m)
bool equals(SGMatrix< T > &other)
void set_const(T const_elem)