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);
178 if (!CMath::fequals<float32_t>(
matrix[j*num_rows+i],
179 matrix[i*num_rows+j], FLT_EPSILON))
195 if (!CMath::fequals<float64_t>(
matrix[j*num_rows+i],
196 matrix[i*num_rows+j], DBL_EPSILON))
212 if (!CMath::fequals<floatmax_t>(
matrix[j*num_rows+i],
213 matrix[i*num_rows+j], LDBL_EPSILON))
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)))
255 SG_SERROR(
"SGMatrix::max_single():: Not supported for complex128_t\n");
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];
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];
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];
388 matrix[i*num_rows+j]-=means[i];
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?
"" :
",")
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?
"" :
",")
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?
"" :
",")
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?
"" :
",")
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);
1069 SG_SERROR(
"SGMatrix::load():: Not supported for complex128_t\n");
1084 SG_SERROR(
"SGMatrix::save():: Not supported for complex128_t\n");
1091 for (int64_t i = 0; i <
num_cols; i++)
1104 for (int64_t i=0; i<diag_vlen; i++)
static SGMatrix< float64_t > matrix_multiply(SGMatrix< float64_t > A, SGMatrix< float64_t > B, bool transpose_A=false, bool transpose_B=false, float64_t scale=1.0)
void wrap_dsyevr(char jobz, char uplo, int n, double *a, int lda, int il, int iu, double *eigenvalues, double *eigenvectors, int *info)
SGVector< T > get_row_vector(index_t row) const
std::complex< float64_t > complex128_t
virtual void set_matrix(const bool *matrix, int32_t num_feat, int32_t num_vec)
bool equals(SGMatrix< T > &other)
static T * get_row_sum(T *matrix, int32_t m, int32_t n)
void display_matrix(const char *name="matrix") const
static T sum(T *vec, int32_t len)
Return sum(vec)
static void transpose_matrix(T *&matrix, int32_t &num_feat, int32_t &num_vec)
static float64_t * pinv(float64_t *matrix, int32_t rows, int32_t cols, float64_t *target=NULL)
static T * get_column_sum(T *matrix, int32_t m, int32_t n)
#define SG_SNOTIMPLEMENTED
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)
static SGVector< float64_t > compute_eigenvectors(SGMatrix< float64_t > matrix)
void copy_refcount(const SGReferencedData &orig)
virtual void get_matrix(bool *&matrix, int32_t &num_feat, int32_t &num_vec)
static T * clone_matrix(const T *matrix, int32_t nrows, int32_t ncols)
shogun reference count managed data
static SGMatrix< T > create_identity_matrix(index_t size, T scale)
A File access base class.
bool operator==(SGMatrix< T > &other)
static void create_diagonal_matrix(T *matrix, T *v, int32_t size)
void remove_column_mean()
static float64_t trace(float64_t *mat, int32_t cols, int32_t rows)
void wrap_dsyev(char jobz, char uplo, int n, double *a, int lda, double *w, int *info)
static void inverse(SGMatrix< float64_t > matrix)
inverses square matrix in-place
void compute_few_eigenvectors(double *matrix_, double *&eigenvalues, double *&eigenvectors, int n, int il, int iu)
all of classes and functions are contained in the shogun namespace
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
static void swap(T &a, T &b)
void set_const(T const_elem)
Matrix::Scalar max(Matrix m)
static SGMatrix< T > get_allocated_matrix(index_t num_rows, index_t num_cols, SGMatrix< T > pre_allocated=SGMatrix< T >())
SGVector< T > get_diagonal_vector() const
virtual void copy_data(const SGReferencedData &orig)
static void center_matrix(T *matrix, int32_t m, int32_t n)