44 num_rows(nrows), num_cols(ncols) { }
50 matrix=SG_MALLOC(T, ((int64_t) nrows)*ncols);
67 REQUIRE(nrows>0,
"Number of rows (%d) has to be a positive integer!\n", nrows);
68 REQUIRE(ncols>0,
"Number of cols (%d) has to be a positive integer!\n", ncols);
69 REQUIRE(vec.
vlen==nrows*ncols,
"Number of elements in the matrix (%d) must "
70 "be the same as the number of elements in the vector (%d)!\n",
71 nrows*ncols, vec.
vlen);
87 num_rows(mat.rows()), num_cols(mat.cols())
123 for (int64_t i=0; i<int64_t(num_rows)*num_cols; ++i)
125 if (matrix[i]!=other.
matrix[i])
135 for (int64_t i=0; i<int64_t(num_rows)*num_cols; i++)
136 matrix[i]=const_elem ;
142 if (matrix && (int64_t(num_rows)*num_cols))
149 if (matrix && (int64_t(num_rows)*num_cols))
156 if (num_rows!=num_cols)
158 for (
int i=0; i<num_rows; ++i)
160 for (
int j=i+1; j<num_cols; ++j)
162 if (matrix[j*num_rows+i]!=matrix[i*num_rows+j])
172 if (num_rows!=num_cols)
174 for (
int i=0; i<num_rows; ++i)
176 for (
int j=i+1; j<num_cols; ++j)
178 if (!CMath::fequals<float32_t>(matrix[j*num_rows+i],
179 matrix[i*num_rows+j], FLT_EPSILON))
189 if (num_rows!=num_cols)
191 for (
int i=0; i<num_rows; ++i)
193 for (
int j=i+1; j<num_cols; ++j)
195 if (!CMath::fequals<float64_t>(matrix[j*num_rows+i],
196 matrix[i*num_rows+j], DBL_EPSILON))
206 if (num_rows!=num_cols)
208 for (
int i=0; i<num_rows; ++i)
210 for (
int j=i+1; j<num_cols; ++j)
212 if (!CMath::fequals<floatmax_t>(matrix[j*num_rows+i],
213 matrix[i*num_rows+j], LDBL_EPSILON))
223 if (num_rows!=num_cols)
225 for (
int i=0; i<num_rows; ++i)
227 for (
int j=i+1; j<num_cols; ++j)
229 if (!(CMath::fequals<float64_t>(matrix[j*num_rows+i].real(),
230 matrix[i*num_rows+j].real(), DBL_EPSILON) &&
231 CMath::fequals<float64_t>(matrix[j*num_rows+i].imag(),
232 matrix[i*num_rows+j].imag(), DBL_EPSILON)))
243 for (int64_t i=1; i<int64_t(num_rows)*num_cols; ++i)
255 SG_SERROR(
"SGMatrix::max_single():: Not supported for complex128_t\n");
262 return SGMatrix<T>(clone_matrix(matrix, num_rows, num_cols),
269 T* result = SG_MALLOC(T, int64_t(nrows)*ncols);
270 for (int64_t i=0; i<int64_t(nrows)*ncols; i++)
278 T*& matrix, int32_t& num_feat, int32_t& num_vec)
281 T* transposed=SG_MALLOC(T, int64_t(num_vec)*num_feat);
282 for (int64_t i=0; i<num_vec; i++)
284 for (int64_t j=0; j<num_feat; j++)
285 transposed[i+j*num_vec]=matrix[i*num_feat+j];
291 CMath::swap(num_feat, num_vec);
297 for(int64_t i=0;i<size;i++)
299 for(int64_t j=0;j<size;j++)
302 matrix[j*size+i]=v[i];
311 float64_t* mat, int32_t cols, int32_t rows)
314 for (int64_t i=0; i<rows; i++)
315 trace+=mat[i*cols+i];
322 T* rowsums=SG_CALLOC(T, n);
324 for (int64_t i=0; i<n; i++)
326 for (int64_t j=0; j<m; j++)
327 rowsums[i]+=matrix[j+i*m];
335 T* colsums=SG_CALLOC(T, m);
337 for (int64_t i=0; i<n; i++)
339 for (int64_t j=0; j<m; j++)
340 colsums[j]+=matrix[j+i*m];
348 center_matrix(matrix, num_rows, num_cols);
356 T* colsums=get_column_sum(matrix, m,n);
357 T* rowsums=get_row_sum(matrix, m,n);
359 for (int32_t i=0; i<m; i++)
360 colsums[i]/=num_data;
361 for (int32_t j=0; j<n; j++)
362 rowsums[j]/=num_data;
366 for (int64_t i=0; i<n; i++)
368 for (int64_t j=0; j<m; j++)
369 matrix[i*m+j]+=s-colsums[j]-rowsums[i];
381 T* means=get_row_sum(matrix, num_rows, num_cols);
384 for (int64_t i=0; i<num_cols; ++i)
387 for (int64_t j=0; j<num_rows; ++j)
388 matrix[i*num_rows+j]-=means[i];
396 display_matrix(matrix, num_rows, num_cols, name);
409 const bool* matrix, int32_t rows, int32_t cols,
const char* name,
412 ASSERT(rows>=0 && cols>=0)
414 for (int64_t i=0; i<rows; i++)
417 for (int64_t j=0; j<cols; j++)
418 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i] ? 1 : 0,
419 j==cols-1?
"" :
",");
420 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
427 const char* matrix, int32_t rows, int32_t cols,
const char* name,
430 ASSERT(rows>=0 && cols>=0)
432 for (int64_t i=0; i<rows; i++)
435 for (int64_t j=0; j<cols; j++)
436 SG_SPRINT(
"%s\t%c%s", prefix, matrix[j*rows+i],
437 j==cols-1?
"" :
",");
438 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
445 const int8_t* matrix, int32_t rows, int32_t cols,
const char* name,
448 ASSERT(rows>=0 && cols>=0)
450 for (int64_t i=0; i<rows; i++)
453 for (int64_t j=0; j<cols; j++)
454 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
455 j==cols-1?
"" :
",");
456 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
463 const uint8_t* matrix, int32_t rows, int32_t cols,
const char* name,
466 ASSERT(rows>=0 && cols>=0)
468 for (int64_t i=0; i<rows; i++)
471 for (int64_t j=0; j<cols; j++)
472 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
473 j==cols-1?
"" :
",");
474 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
481 const int16_t* matrix, int32_t rows, int32_t cols,
const char* name,
484 ASSERT(rows>=0 && cols>=0)
486 for (int64_t i=0; i<rows; i++)
489 for (int64_t j=0; j<cols; j++)
490 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
491 j==cols-1?
"" :
",");
492 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
499 const uint16_t* matrix, int32_t rows, int32_t cols,
const char* name,
502 ASSERT(rows>=0 && cols>=0)
504 for (int64_t i=0; i<rows; i++)
507 for (int64_t j=0; j<cols; j++)
508 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
509 j==cols-1?
"" :
",");
510 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
518 const int32_t* matrix, int32_t rows, int32_t cols,
const char* name,
521 ASSERT(rows>=0 && cols>=0)
523 for (int64_t i=0; i<rows; i++)
526 for (int64_t j=0; j<cols; j++)
527 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
528 j==cols-1?
"" :
",");
529 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
536 const uint32_t* matrix, int32_t rows, int32_t cols,
const char* name,
539 ASSERT(rows>=0 && cols>=0)
541 for (int64_t i=0; i<rows; i++)
544 for (int64_t j=0; j<cols; j++)
545 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
546 j==cols-1?
"" :
",");
547 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
553 const int64_t* matrix, int32_t rows, int32_t cols,
const char* name,
556 ASSERT(rows>=0 && cols>=0)
558 for (int64_t i=0; i<rows; i++)
561 for (int64_t j=0; j<cols; j++)
562 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
563 j==cols-1?
"" :
",");
564 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
571 const uint64_t* matrix, int32_t rows, int32_t cols,
const char* name,
574 ASSERT(rows>=0 && cols>=0)
576 for (int64_t i=0; i<rows; i++)
579 for (int64_t j=0; j<cols; j++)
580 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
581 j==cols-1?
"" :
",");
582 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
589 const float32_t* matrix, int32_t rows, int32_t cols,
const char* name,
592 ASSERT(rows>=0 && cols>=0)
594 for (int64_t i=0; i<rows; i++)
597 for (int64_t j=0; j<cols; j++)
598 SG_SPRINT(
"%s\t%.18g%s", prefix, (
float) matrix[j*rows+i],
599 j==cols-1?
"" :
",");
600 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
607 const float64_t* matrix, int32_t rows, int32_t cols,
const char* name,
610 ASSERT(rows>=0 && cols>=0)
612 for (int64_t i=0; i<rows; i++)
615 for (int64_t j=0; j<cols; j++)
616 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
617 j==cols-1?
"" :
",");
618 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
625 const floatmax_t* matrix, int32_t rows, int32_t cols,
const char* name,
628 ASSERT(rows>=0 && cols>=0)
630 for (int64_t i=0; i<rows; i++)
633 for (int64_t j=0; j<cols; j++)
634 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
635 j==cols-1?
"" :
",");
636 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
643 const complex128_t* matrix, int32_t rows, int32_t cols,
const char* name,
646 ASSERT(rows>=0 && cols>=0)
648 for (int64_t i=0; i<rows; i++)
651 for (int64_t j=0; j<cols; j++)
652 SG_SPRINT(
"%s\t(%.18g+i%.18g)%s", prefix, matrix[j*rows+i].real(),
653 matrix[j*rows+i].imag(), j==cols-1?
"" :
",");
654 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
673 I(i,j)=i==j ? scale : 0.0;
686 I(i,j)=i==j ? scale : 0.0;
699 I(i,j)=i==j ? scale : (!
scale);
712 I(i,j)=i==j ? scale : 0.0;
725 I(i,j)=i==j ? scale : 0.0;
738 I(i,j)=i==j ? scale : 0.0;
751 I(i,j)=i==j ? scale : 0.0;
764 I(i,j)=i==j ? scale : 0.0;
777 I(i,j)=i==j ? scale : 0.0;
790 I(i,j)=i==j ? scale : 0.0;
803 I(i,j)=i==j ? scale : 0.0;
816 I(i,j)=i==j ? scale : 0.0;
860 int32_t lsize=CMath::min((int32_t) m, (int32_t) n);
861 double* s=SG_MALLOC(
double, lsize);
862 double* u=SG_MALLOC(
double, m*m);
863 double* vt=SG_MALLOC(
double, n*n);
865 wrap_dgesvd(jobu, jobvt, m, n, matrix, lda, s, u, ldu, vt, ldvt, &info);
868 for (int64_t i=0; i<n; i++)
870 for (int64_t j=0; j<lsize; j++)
871 vt[i*n+j]=vt[i*n+j]/s[j];
874 cblas_dgemm(CblasColMajor, CblasTrans, CblasTrans, m, n, m, 1.0, vt, ldvt, u, ldu, 0, target, m);
888 int32_t* ipiv = SG_MALLOC(int32_t, matrix.
num_cols);
899 SG_SERROR(
"SGMatrix::compute_eigenvectors(SGMatrix<float64_t>): matrix"
900 " rows and columns are not equal!\n");
921 double* eigenvalues=SG_CALLOC(
float64_t, n+1);
928 SG_SERROR(
"DSYEV failed with code %d\n", info)
935 int n,
int il,
int iu)
937 eigenvalues = SG_MALLOC(
double, n);
938 eigenvectors = SG_MALLOC(
double, (iu-il+1)*n);
940 wrap_dsyevr(
'V',
'U',n,matrix_,n,il,iu,eigenvalues,eigenvectors,&status);
960 SG_SERROR(
"SGMatrix::matrix_multiply(): Dimension mismatch: "
961 "A(%dx%d)*B(%dx%D)\n", rows_A, cols_A, rows_B, cols_B);
969 cblas_dgemm(CblasColMajor,
970 transpose_A ? CblasTrans : CblasNoTrans,
971 transpose_B ? CblasTrans : CblasNoTrans,
972 rows_A, cols_B, cols_A, scale,
977 for (int32_t i=0; i<rows_A; i++)
979 for (int32_t j=0; j<cols_B; j++)
981 for (int32_t k=0; k<cols_A; k++)
983 float64_t x1=transpose_A ? A(k,i):A(i,k);
984 float64_t x2=transpose_B ? B(j,k):B(k,j);
1003 if (pre_allocated.
matrix)
1005 result=pre_allocated;
1008 if (pre_allocated.
num_rows!=num_rows ||
1011 SG_SERROR(
"SGMatrix<T>::get_allocated_matrix(). Provided target"
1012 "matrix dimensions (%dx%d) do not match passed data "
1013 "dimensions (%dx%d)!\n", pre_allocated.
num_rows,
1014 pre_allocated.
num_cols, num_rows, num_cols);
1029 matrix=((
SGMatrix*)(&orig))->matrix;
1030 num_rows=((
SGMatrix*)(&orig))->num_rows;
1031 num_cols=((
SGMatrix*)(&orig))->num_cols;
1069 SG_SERROR(
"SGMatrix::load():: Not supported for complex128_t\n");
1077 writer->
set_matrix(matrix, num_rows, num_cols);
1084 SG_SERROR(
"SGMatrix::save():: Not supported for complex128_t\n");
1091 for (int64_t i = 0; i < num_cols; i++)
1093 rowv[i] = matrix[i*num_rows+row];
1101 index_t diag_vlen=CMath::min(num_cols, num_rows);
1104 for (int64_t i=0; i<diag_vlen; i++)
1106 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)