44 num_rows(nrows), num_cols(ncols) { }
50 matrix=SG_MALLOC(T, ((int64_t) nrows)*ncols);
63 num_rows(mat.rows()), num_cols(mat.cols())
99 for (int64_t i=0; i<int64_t(num_rows)*num_cols; ++i)
101 if (matrix[i]!=other.
matrix[i])
111 for (int64_t i=0; i<int64_t(num_rows)*num_cols; i++)
112 matrix[i]=const_elem ;
118 if (matrix && (int64_t(num_rows)*num_cols))
125 if (matrix && (int64_t(num_rows)*num_cols))
132 if (num_rows!=num_cols)
134 for (
int i=0; i<num_rows; ++i)
136 for (
int j=i+1; j<num_cols; ++j)
138 if (matrix[j*num_rows+i]!=matrix[i*num_rows+j])
148 if (num_rows!=num_cols)
150 for (
int i=0; i<num_rows; ++i)
152 for (
int j=i+1; j<num_cols; ++j)
154 if (!CMath::fequals<float32_t>(matrix[j*num_rows+i],
155 matrix[i*num_rows+j], FLT_EPSILON))
165 if (num_rows!=num_cols)
167 for (
int i=0; i<num_rows; ++i)
169 for (
int j=i+1; j<num_cols; ++j)
171 if (!CMath::fequals<float64_t>(matrix[j*num_rows+i],
172 matrix[i*num_rows+j], DBL_EPSILON))
182 if (num_rows!=num_cols)
184 for (
int i=0; i<num_rows; ++i)
186 for (
int j=i+1; j<num_cols; ++j)
188 if (!CMath::fequals<floatmax_t>(matrix[j*num_rows+i],
189 matrix[i*num_rows+j], LDBL_EPSILON))
199 if (num_rows!=num_cols)
201 for (
int i=0; i<num_rows; ++i)
203 for (
int j=i+1; j<num_cols; ++j)
205 if (!(CMath::fequals<float64_t>(matrix[j*num_rows+i].real(),
206 matrix[i*num_rows+j].real(), DBL_EPSILON) &&
207 CMath::fequals<float64_t>(matrix[j*num_rows+i].imag(),
208 matrix[i*num_rows+j].imag(), DBL_EPSILON)))
219 for (int64_t i=1; i<int64_t(num_rows)*num_cols; ++i)
231 SG_SERROR(
"SGMatrix::max_single():: Not supported for complex128_t\n");
238 return SGMatrix<T>(clone_matrix(matrix, num_rows, num_cols),
245 T* result = SG_MALLOC(T, int64_t(nrows)*ncols);
246 for (int64_t i=0; i<int64_t(nrows)*ncols; i++)
254 T*& matrix, int32_t& num_feat, int32_t& num_vec)
257 T* transposed=SG_MALLOC(T, int64_t(num_vec)*num_feat);
258 for (int64_t i=0; i<num_vec; i++)
260 for (int64_t j=0; j<num_feat; j++)
261 transposed[i+j*num_vec]=matrix[i*num_feat+j];
267 CMath::swap(num_feat, num_vec);
273 for(int64_t i=0;i<size;i++)
275 for(int64_t j=0;j<size;j++)
278 matrix[j*size+i]=v[i];
287 float64_t* mat, int32_t cols, int32_t rows)
290 for (int64_t i=0; i<rows; i++)
291 trace+=mat[i*cols+i];
298 T* rowsums=SG_CALLOC(T, n);
300 for (int64_t i=0; i<n; i++)
302 for (int64_t j=0; j<m; j++)
303 rowsums[i]+=matrix[j+i*m];
311 T* colsums=SG_CALLOC(T, m);
313 for (int64_t i=0; i<n; i++)
315 for (int64_t j=0; j<m; j++)
316 colsums[j]+=matrix[j+i*m];
324 center_matrix(matrix, num_rows, num_cols);
332 T* colsums=get_column_sum(matrix, m,n);
333 T* rowsums=get_row_sum(matrix, m,n);
335 for (int32_t i=0; i<m; i++)
336 colsums[i]/=num_data;
337 for (int32_t j=0; j<n; j++)
338 rowsums[j]/=num_data;
342 for (int64_t i=0; i<n; i++)
344 for (int64_t j=0; j<m; j++)
345 matrix[i*m+j]+=s-colsums[j]-rowsums[i];
357 T* means=get_row_sum(matrix, num_rows, num_cols);
360 for (int64_t i=0; i<num_cols; ++i)
363 for (int64_t j=0; j<num_rows; ++j)
364 matrix[i*num_rows+j]-=means[i];
372 display_matrix(matrix, num_rows, num_cols, name);
385 const bool* matrix, int32_t rows, int32_t cols,
const char* name,
388 ASSERT(rows>=0 && cols>=0)
390 for (int64_t i=0; i<rows; i++)
393 for (int64_t j=0; j<cols; j++)
394 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i] ? 1 : 0,
395 j==cols-1?
"" :
",");
396 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
403 const char* matrix, int32_t rows, int32_t cols,
const char* name,
406 ASSERT(rows>=0 && cols>=0)
408 for (int64_t i=0; i<rows; i++)
411 for (int64_t j=0; j<cols; j++)
412 SG_SPRINT(
"%s\t%c%s", prefix, matrix[j*rows+i],
413 j==cols-1?
"" :
",");
414 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
421 const int8_t* matrix, int32_t rows, int32_t cols,
const char* name,
424 ASSERT(rows>=0 && cols>=0)
426 for (int64_t i=0; i<rows; i++)
429 for (int64_t j=0; j<cols; j++)
430 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
431 j==cols-1?
"" :
",");
432 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
439 const uint8_t* matrix, int32_t rows, int32_t cols,
const char* name,
442 ASSERT(rows>=0 && cols>=0)
444 for (int64_t i=0; i<rows; i++)
447 for (int64_t j=0; j<cols; j++)
448 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
449 j==cols-1?
"" :
",");
450 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
457 const int16_t* matrix, int32_t rows, int32_t cols,
const char* name,
460 ASSERT(rows>=0 && cols>=0)
462 for (int64_t i=0; i<rows; i++)
465 for (int64_t j=0; j<cols; j++)
466 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
467 j==cols-1?
"" :
",");
468 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
475 const uint16_t* matrix, int32_t rows, int32_t cols,
const char* name,
478 ASSERT(rows>=0 && cols>=0)
480 for (int64_t i=0; i<rows; i++)
483 for (int64_t j=0; j<cols; j++)
484 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
485 j==cols-1?
"" :
",");
486 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
494 const int32_t* matrix, int32_t rows, int32_t cols,
const char* name,
497 ASSERT(rows>=0 && cols>=0)
499 for (int64_t i=0; i<rows; i++)
502 for (int64_t j=0; j<cols; j++)
503 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
504 j==cols-1?
"" :
",");
505 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
512 const uint32_t* matrix, int32_t rows, int32_t cols,
const char* name,
515 ASSERT(rows>=0 && cols>=0)
517 for (int64_t i=0; i<rows; i++)
520 for (int64_t j=0; j<cols; j++)
521 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
522 j==cols-1?
"" :
",");
523 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
529 const int64_t* matrix, int32_t rows, int32_t cols,
const char* name,
532 ASSERT(rows>=0 && cols>=0)
534 for (int64_t i=0; i<rows; i++)
537 for (int64_t j=0; j<cols; j++)
538 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
539 j==cols-1?
"" :
",");
540 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
547 const uint64_t* matrix, int32_t rows, int32_t cols,
const char* name,
550 ASSERT(rows>=0 && cols>=0)
552 for (int64_t i=0; i<rows; i++)
555 for (int64_t j=0; j<cols; j++)
556 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
557 j==cols-1?
"" :
",");
558 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
565 const float32_t* matrix, int32_t rows, int32_t cols,
const char* name,
568 ASSERT(rows>=0 && cols>=0)
570 for (int64_t i=0; i<rows; i++)
573 for (int64_t j=0; j<cols; j++)
574 SG_SPRINT(
"%s\t%.18g%s", prefix, (
float) matrix[j*rows+i],
575 j==cols-1?
"" :
",");
576 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
583 const float64_t* matrix, int32_t rows, int32_t cols,
const char* name,
586 ASSERT(rows>=0 && cols>=0)
588 for (int64_t i=0; i<rows; i++)
591 for (int64_t j=0; j<cols; j++)
592 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
593 j==cols-1?
"" :
",");
594 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
601 const floatmax_t* matrix, int32_t rows, int32_t cols,
const char* name,
604 ASSERT(rows>=0 && cols>=0)
606 for (int64_t i=0; i<rows; i++)
609 for (int64_t j=0; j<cols; j++)
610 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
611 j==cols-1?
"" :
",");
612 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
619 const complex128_t* matrix, int32_t rows, int32_t cols,
const char* name,
622 ASSERT(rows>=0 && cols>=0)
624 for (int64_t i=0; i<rows; i++)
627 for (int64_t j=0; j<cols; j++)
628 SG_SPRINT(
"%s\t(%.18g+i%.18g)%s", prefix, matrix[j*rows+i].real(),
629 matrix[j*rows+i].imag(), j==cols-1?
"" :
",");
630 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
649 I(i,j)=i==j ? scale : 0.0;
662 I(i,j)=i==j ? scale : 0.0;
675 I(i,j)=i==j ? scale : (!
scale);
688 I(i,j)=i==j ? scale : 0.0;
701 I(i,j)=i==j ? scale : 0.0;
714 I(i,j)=i==j ? scale : 0.0;
727 I(i,j)=i==j ? scale : 0.0;
740 I(i,j)=i==j ? scale : 0.0;
753 I(i,j)=i==j ? scale : 0.0;
766 I(i,j)=i==j ? scale : 0.0;
779 I(i,j)=i==j ? scale : 0.0;
792 I(i,j)=i==j ? scale : 0.0;
836 int32_t lsize=CMath::min((int32_t) m, (int32_t) n);
837 double* s=SG_MALLOC(
double, lsize);
838 double* u=SG_MALLOC(
double, m*m);
839 double* vt=SG_MALLOC(
double, n*n);
841 wrap_dgesvd(jobu, jobvt, m, n, matrix, lda, s, u, ldu, vt, ldvt, &info);
844 for (int64_t i=0; i<n; i++)
846 for (int64_t j=0; j<lsize; j++)
847 vt[i*n+j]=vt[i*n+j]/s[j];
850 cblas_dgemm(CblasColMajor, CblasTrans, CblasTrans, m, n, m, 1.0, vt, ldvt, u, ldu, 0, target, m);
864 int32_t* ipiv = SG_MALLOC(int32_t, matrix.
num_cols);
875 SG_SERROR(
"SGMatrix::compute_eigenvectors(SGMatrix<float64_t>): matrix"
876 " rows and columns are not equal!\n");
897 double* eigenvalues=SG_CALLOC(
float64_t, n+1);
904 SG_SERROR(
"DSYEV failed with code %d\n", info)
911 int n,
int il,
int iu)
913 eigenvalues = SG_MALLOC(
double, n);
914 eigenvectors = SG_MALLOC(
double, (iu-il+1)*n);
916 wrap_dsyevr(
'V',
'U',n,matrix_,n,il,iu,eigenvalues,eigenvectors,&status);
936 SG_SERROR(
"SGMatrix::matrix_multiply(): Dimension mismatch: "
937 "A(%dx%d)*B(%dx%D)\n", rows_A, cols_A, rows_B, cols_B);
945 cblas_dgemm(CblasColMajor,
946 transpose_A ? CblasTrans : CblasNoTrans,
947 transpose_B ? CblasTrans : CblasNoTrans,
948 rows_A, cols_B, cols_A, scale,
953 for (int32_t i=0; i<rows_A; i++)
955 for (int32_t j=0; j<cols_B; j++)
957 for (int32_t k=0; k<cols_A; k++)
959 float64_t x1=transpose_A ? A(k,i):A(i,k);
960 float64_t x2=transpose_B ? B(j,k):B(k,j);
981 result=pre_allocated;
984 if (pre_allocated.
num_rows!=num_rows ||
987 SG_SERROR(
"SGMatrix<T>::get_allocated_matrix(). Provided target"
988 "matrix dimensions (%dx%d) do not match passed data "
989 "dimensions (%dx%d)!\n", pre_allocated.
num_rows,
990 pre_allocated.
num_cols, num_rows, num_cols);
1005 matrix=((
SGMatrix*)(&orig))->matrix;
1006 num_rows=((
SGMatrix*)(&orig))->num_rows;
1007 num_cols=((
SGMatrix*)(&orig))->num_cols;
1045 SG_SERROR(
"SGMatrix::load():: Not supported for complex128_t\n");
1053 writer->
set_matrix(matrix, num_rows, num_cols);
1060 SG_SERROR(
"SGMatrix::save():: Not supported for complex128_t\n");
1067 for (int64_t i = 0; i < num_cols; i++)
1069 rowv[i] = matrix[i*num_rows+row];
1077 index_t diag_vlen=CMath::min(num_cols, num_rows);
1080 for (int64_t i=0; i<diag_vlen; i++)
1082 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
Matrix::Scalar sum(Matrix m, bool no_diag=false)
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)