40 num_rows(nrows), num_cols(ncols) { }
46 matrix=SG_MALLOC(T, ((int64_t) nrows)*ncols);
79 for (int64_t i=0; i<int64_t(num_rows)*num_cols; ++i)
81 if (matrix[i]!=other.
matrix[i])
91 for (int64_t i=0; i<int64_t(num_rows)*num_cols; i++)
92 matrix[i]=const_elem ;
98 if (matrix && (int64_t(num_rows)*num_cols))
105 if (matrix && (int64_t(num_rows)*num_cols))
112 if (num_rows!=num_cols)
114 for (
int i=0; i<num_rows; ++i)
116 for (
int j=i+1; j<num_cols; ++j)
118 if (matrix[j*num_rows+i]!=matrix[i*num_rows+j])
128 if (num_rows!=num_cols)
130 for (
int i=0; i<num_rows; ++i)
132 for (
int j=i+1; j<num_cols; ++j)
134 if (!CMath::fequals<float32_t>(matrix[j*num_rows+i],
135 matrix[i*num_rows+j], FLT_EPSILON))
145 if (num_rows!=num_cols)
147 for (
int i=0; i<num_rows; ++i)
149 for (
int j=i+1; j<num_cols; ++j)
151 if (!CMath::fequals<float64_t>(matrix[j*num_rows+i],
152 matrix[i*num_rows+j], DBL_EPSILON))
162 if (num_rows!=num_cols)
164 for (
int i=0; i<num_rows; ++i)
166 for (
int j=i+1; j<num_cols; ++j)
168 if (!CMath::fequals<floatmax_t>(matrix[j*num_rows+i],
169 matrix[i*num_rows+j], LDBL_EPSILON))
179 if (num_rows!=num_cols)
181 for (
int i=0; i<num_rows; ++i)
183 for (
int j=i+1; j<num_cols; ++j)
185 if (!(CMath::fequals<float64_t>(matrix[j*num_rows+i].real(),
186 matrix[i*num_rows+j].real(), DBL_EPSILON) &&
187 CMath::fequals<float64_t>(matrix[j*num_rows+i].imag(),
188 matrix[i*num_rows+j].imag(), DBL_EPSILON)))
199 for (int64_t i=1; i<int64_t(num_rows)*num_cols; ++i)
211 SG_SERROR(
"SGMatrix::max_single():: Not supported for complex128_t\n");
218 return SGMatrix<T>(clone_matrix(matrix, num_rows, num_cols),
225 T* result = SG_MALLOC(T, int64_t(nrows)*ncols);
226 for (int64_t i=0; i<int64_t(nrows)*ncols; i++)
234 T*& matrix, int32_t& num_feat, int32_t& num_vec)
237 T* transposed=SG_MALLOC(T, int64_t(num_vec)*num_feat);
238 for (int64_t i=0; i<num_vec; i++)
240 for (int64_t j=0; j<num_feat; j++)
241 transposed[i+j*num_vec]=matrix[i*num_feat+j];
247 CMath::swap(num_feat, num_vec);
253 for(int64_t i=0;i<size;i++)
255 for(int64_t j=0;j<size;j++)
258 matrix[j*size+i]=v[i];
267 float64_t* mat, int32_t cols, int32_t rows)
270 for (int64_t i=0; i<rows; i++)
271 trace+=mat[i*cols+i];
278 T* rowsums=SG_CALLOC(T, n);
280 for (int64_t i=0; i<n; i++)
282 for (int64_t j=0; j<m; j++)
283 rowsums[i]+=matrix[j+i*m];
291 T* colsums=SG_CALLOC(T, m);
293 for (int64_t i=0; i<n; i++)
295 for (int64_t j=0; j<m; j++)
296 colsums[j]+=matrix[j+i*m];
304 center_matrix(matrix, num_rows, num_cols);
312 T* colsums=get_column_sum(matrix, m,n);
313 T* rowsums=get_row_sum(matrix, m,n);
315 for (int32_t i=0; i<m; i++)
316 colsums[i]/=num_data;
317 for (int32_t j=0; j<n; j++)
318 rowsums[j]/=num_data;
322 for (int64_t i=0; i<n; i++)
324 for (int64_t j=0; j<m; j++)
325 matrix[i*m+j]+=s-colsums[j]-rowsums[i];
337 T* means=get_row_sum(matrix, num_rows, num_cols);
340 for (int64_t i=0; i<num_cols; ++i)
343 for (int64_t j=0; j<num_rows; ++j)
344 matrix[i*num_rows+j]-=means[i];
352 display_matrix(matrix, num_rows, num_cols, name);
365 const bool* matrix, int32_t rows, int32_t cols,
const char* name,
368 ASSERT(rows>=0 && cols>=0)
370 for (int64_t i=0; i<rows; i++)
373 for (int64_t j=0; j<cols; j++)
374 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i] ? 1 : 0,
375 j==cols-1?
"" :
",");
376 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
383 const char* matrix, int32_t rows, int32_t cols,
const char* name,
386 ASSERT(rows>=0 && cols>=0)
388 for (int64_t i=0; i<rows; i++)
391 for (int64_t j=0; j<cols; j++)
392 SG_SPRINT(
"%s\t%c%s", prefix, matrix[j*rows+i],
393 j==cols-1?
"" :
",");
394 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
401 const int8_t* matrix, int32_t rows, int32_t cols,
const char* name,
404 ASSERT(rows>=0 && cols>=0)
406 for (int64_t i=0; i<rows; i++)
409 for (int64_t j=0; j<cols; j++)
410 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
411 j==cols-1?
"" :
",");
412 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
419 const uint8_t* matrix, int32_t rows, int32_t cols,
const char* name,
422 ASSERT(rows>=0 && cols>=0)
424 for (int64_t i=0; i<rows; i++)
427 for (int64_t j=0; j<cols; j++)
428 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
429 j==cols-1?
"" :
",");
430 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
437 const int16_t* matrix, int32_t rows, int32_t cols,
const char* name,
440 ASSERT(rows>=0 && cols>=0)
442 for (int64_t i=0; i<rows; i++)
445 for (int64_t j=0; j<cols; j++)
446 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
447 j==cols-1?
"" :
",");
448 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
455 const uint16_t* matrix, int32_t rows, int32_t cols,
const char* name,
458 ASSERT(rows>=0 && cols>=0)
460 for (int64_t i=0; i<rows; i++)
463 for (int64_t j=0; j<cols; j++)
464 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
465 j==cols-1?
"" :
",");
466 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
474 const int32_t* matrix, int32_t rows, int32_t cols,
const char* name,
477 ASSERT(rows>=0 && cols>=0)
479 for (int64_t i=0; i<rows; i++)
482 for (int64_t j=0; j<cols; j++)
483 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
484 j==cols-1?
"" :
",");
485 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
492 const uint32_t* matrix, int32_t rows, int32_t cols,
const char* name,
495 ASSERT(rows>=0 && cols>=0)
497 for (int64_t i=0; i<rows; i++)
500 for (int64_t j=0; j<cols; j++)
501 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
502 j==cols-1?
"" :
",");
503 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
509 const int64_t* matrix, int32_t rows, int32_t cols,
const char* name,
512 ASSERT(rows>=0 && cols>=0)
514 for (int64_t i=0; i<rows; i++)
517 for (int64_t j=0; j<cols; j++)
518 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
519 j==cols-1?
"" :
",");
520 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
527 const uint64_t* matrix, int32_t rows, int32_t cols,
const char* name,
530 ASSERT(rows>=0 && cols>=0)
532 for (int64_t i=0; i<rows; i++)
535 for (int64_t j=0; j<cols; j++)
536 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
537 j==cols-1?
"" :
",");
538 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
545 const float32_t* matrix, int32_t rows, int32_t cols,
const char* name,
548 ASSERT(rows>=0 && cols>=0)
550 for (int64_t i=0; i<rows; i++)
553 for (int64_t j=0; j<cols; j++)
554 SG_SPRINT(
"%s\t%.18g%s", prefix, (
float) matrix[j*rows+i],
555 j==cols-1?
"" :
",");
556 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
563 const float64_t* matrix, int32_t rows, int32_t cols,
const char* name,
566 ASSERT(rows>=0 && cols>=0)
568 for (int64_t i=0; i<rows; i++)
571 for (int64_t j=0; j<cols; j++)
572 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
573 j==cols-1?
"" :
",");
574 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
581 const floatmax_t* matrix, int32_t rows, int32_t cols,
const char* name,
584 ASSERT(rows>=0 && cols>=0)
586 for (int64_t i=0; i<rows; i++)
589 for (int64_t j=0; j<cols; j++)
590 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
591 j==cols-1?
"" :
",");
592 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
599 const complex128_t* matrix, int32_t rows, int32_t cols,
const char* name,
602 ASSERT(rows>=0 && cols>=0)
604 for (int64_t i=0; i<rows; i++)
607 for (int64_t j=0; j<cols; j++)
608 SG_SPRINT(
"%s\t(%.18g+i%.18g)%s", prefix, matrix[j*rows+i].real(),
609 matrix[j*rows+i].imag(), j==cols-1?
"" :
",");
610 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
629 I(i,j)=i==j ? scale : 0.0;
642 I(i,j)=i==j ? scale : 0.0;
655 I(i,j)=i==j ? scale : (!scale);
668 I(i,j)=i==j ? scale : 0.0;
681 I(i,j)=i==j ? scale : 0.0;
694 I(i,j)=i==j ? scale : 0.0;
707 I(i,j)=i==j ? scale : 0.0;
720 I(i,j)=i==j ? scale : 0.0;
733 I(i,j)=i==j ? scale : 0.0;
746 I(i,j)=i==j ? scale : 0.0;
759 I(i,j)=i==j ? scale : 0.0;
772 I(i,j)=i==j ? scale : 0.0;
816 int32_t lsize=CMath::min((int32_t) m, (int32_t) n);
817 double* s=SG_MALLOC(
double, lsize);
818 double* u=SG_MALLOC(
double, m*m);
819 double* vt=SG_MALLOC(
double, n*n);
821 wrap_dgesvd(jobu, jobvt, m, n, matrix, lda, s, u, ldu, vt, ldvt, &info);
824 for (int64_t i=0; i<n; i++)
826 for (int64_t j=0; j<lsize; j++)
827 vt[i*n+j]=vt[i*n+j]/s[j];
830 cblas_dgemm(CblasColMajor, CblasTrans, CblasTrans, m, n, m, 1.0, vt, ldvt, u, ldu, 0, target, m);
844 int32_t* ipiv = SG_MALLOC(int32_t, matrix.
num_cols);
855 SG_SERROR(
"SGMatrix::compute_eigenvectors(SGMatrix<float64_t>): matrix"
856 " rows and columns are not equal!\n");
877 double* eigenvalues=SG_CALLOC(
float64_t, n+1);
884 SG_SERROR(
"DSYEV failed with code %d\n", info)
891 int n,
int il,
int iu)
893 eigenvalues = SG_MALLOC(
double, n);
894 eigenvectors = SG_MALLOC(
double, (iu-il+1)*n);
896 wrap_dsyevr(
'V',
'U',n,matrix_,n,il,iu,eigenvalues,eigenvectors,&status);
905 bool transpose_A,
bool transpose_B,
float64_t scale)
916 SG_SERROR(
"SGMatrix::matrix_multiply(): Dimension mismatch: "
917 "A(%dx%d)*B(%dx%D)\n", rows_A, cols_A, rows_B, cols_B);
925 cblas_dgemm(CblasColMajor,
926 transpose_A ? CblasTrans : CblasNoTrans,
927 transpose_B ? CblasTrans : CblasNoTrans,
928 rows_A, cols_B, cols_A, scale,
933 for (int32_t i=0; i<rows_A; i++)
935 for (int32_t j=0; j<cols_B; j++)
937 for (int32_t k=0; k<cols_A; k++)
939 float64_t x1=transpose_A ? A(k,i):A(i,k);
940 float64_t x2=transpose_B ? B(j,k):B(k,j);
961 result=pre_allocated;
964 if (pre_allocated.
num_rows!=num_rows ||
967 SG_SERROR(
"SGMatrix<T>::get_allocated_matrix(). Provided target"
968 "matrix dimensions (%dx%d) do not match passed data "
969 "dimensions (%dx%d)!\n", pre_allocated.
num_rows,
970 pre_allocated.
num_cols, num_rows, num_cols);
985 matrix=((
SGMatrix*)(&orig))->matrix;
986 num_rows=((
SGMatrix*)(&orig))->num_rows;
987 num_cols=((
SGMatrix*)(&orig))->num_cols;
1025 SG_SERROR(
"SGMatrix::load():: Not supported for complex128_t\n");
1033 writer->
set_matrix(matrix, num_rows, num_cols);
1040 SG_SERROR(
"SGMatrix::save():: Not supported for complex128_t\n");
1047 for (int64_t i = 0; i < num_cols; i++)
1049 rowv[i] = matrix[i*num_rows+row];
1057 index_t diag_vlen=CMath::min(num_cols, num_rows);
1060 for (int64_t i=0; i<diag_vlen; i++)
1062 diag[i]=matrix[i*num_rows+i];
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)
bool equals(SGMatrix< T > &other)
void display_matrix(const char *name="matrix") const
#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)
virtual void get_matrix(bool *&matrix, int32_t &num_feat, int32_t &num_vec)
shogun reference count managed data
A File access base class.
bool operator==(SGMatrix< T > &other)
void wrap_dsyev(char jobz, char uplo, int n, double *a, int lda, double *w, int *info)
void set_const(T const_elem)
virtual void copy_data(const SGReferencedData &orig)