40 num_rows(nrows), num_cols(ncols) { }
46 matrix=SG_MALLOC(T, ((int64_t) nrows)*ncols);
79 for (
index_t i=0; i<num_rows*num_cols; ++i)
81 if (matrix[i]!=other.
matrix[i])
91 for (
index_t i=0; i<num_rows*num_cols; i++)
92 matrix[i]=const_elem ;
98 if (matrix && (num_rows*num_cols))
105 if (matrix && (num_rows*num_cols))
113 for (
index_t i=1; i<num_rows*num_cols; ++i)
125 SG_SERROR(
"SGMatrix::max_single():: Not supported for complex128_t\n");
132 return SGMatrix<T>(clone_matrix(matrix, num_rows, num_cols),
139 T* result = SG_MALLOC(T, int64_t(nrows)*ncols);
140 for (int64_t i=0; i<int64_t(nrows)*ncols; i++)
148 T*& matrix, int32_t& num_feat, int32_t& num_vec)
151 T* transposed=SG_MALLOC(T, num_vec*num_feat);
152 for (int32_t i=0; i<num_vec; i++)
154 for (int32_t j=0; j<num_feat; j++)
155 transposed[i+j*num_vec]=matrix[i*num_feat+j];
167 for(int32_t i=0;i<size;i++)
169 for(int32_t j=0;j<size;j++)
172 matrix[j*size+i]=v[i];
181 float64_t* mat, int32_t cols, int32_t rows)
184 for (int32_t i=0; i<rows; i++)
185 trace+=mat[i*cols+i];
192 T* rowsums=SG_CALLOC(T, n);
194 for (int32_t i=0; i<n; i++)
196 for (int32_t j=0; j<m; j++)
197 rowsums[i]+=matrix[j+int64_t(i)*m];
205 T* colsums=SG_CALLOC(T, m);
207 for (int32_t i=0; i<n; i++)
209 for (int32_t j=0; j<m; j++)
210 colsums[j]+=matrix[j+int64_t(i)*m];
218 center_matrix(matrix, num_rows, num_cols);
226 T* colsums=get_column_sum(matrix, m,n);
227 T* rowsums=get_row_sum(matrix, m,n);
229 for (int32_t i=0; i<m; i++)
230 colsums[i]/=num_data;
231 for (int32_t j=0; j<n; j++)
232 rowsums[j]/=num_data;
236 for (int32_t i=0; i<n; i++)
238 for (int32_t j=0; j<m; j++)
239 matrix[int64_t(i)*m+j]+=s-colsums[j]-rowsums[i];
251 T* means=get_row_sum(matrix, num_rows, num_cols);
254 for (
index_t i=0; i<num_cols; ++i)
257 for (
index_t j=0; j<num_rows; ++j)
258 matrix[i*num_rows+j]-=means[i];
266 display_matrix(matrix, num_rows, num_cols, name);
279 const bool* matrix, int32_t rows, int32_t cols,
const char* name,
282 ASSERT(rows>=0 && cols>=0)
284 for (int32_t i=0; i<rows; i++)
287 for (int32_t j=0; j<cols; j++)
288 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i] ? 1 : 0,
289 j==cols-1?
"" :
",");
290 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
297 const char* matrix, int32_t rows, int32_t cols,
const char* name,
300 ASSERT(rows>=0 && cols>=0)
302 for (int32_t i=0; i<rows; i++)
305 for (int32_t j=0; j<cols; j++)
306 SG_SPRINT(
"%s\t%c%s", prefix, matrix[j*rows+i],
307 j==cols-1?
"" :
",");
308 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
315 const int8_t* matrix, int32_t rows, int32_t cols,
const char* name,
318 ASSERT(rows>=0 && cols>=0)
320 for (int32_t i=0; i<rows; i++)
323 for (int32_t j=0; j<cols; j++)
324 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
325 j==cols-1?
"" :
",");
326 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
333 const uint8_t* matrix, int32_t rows, int32_t cols,
const char* name,
336 ASSERT(rows>=0 && cols>=0)
338 for (int32_t i=0; i<rows; i++)
341 for (int32_t j=0; j<cols; j++)
342 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
343 j==cols-1?
"" :
",");
344 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
351 const int16_t* matrix, int32_t rows, int32_t cols,
const char* name,
354 ASSERT(rows>=0 && cols>=0)
356 for (int32_t i=0; i<rows; i++)
359 for (int32_t j=0; j<cols; j++)
360 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
361 j==cols-1?
"" :
",");
362 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
369 const uint16_t* matrix, int32_t rows, int32_t cols,
const char* name,
372 ASSERT(rows>=0 && cols>=0)
374 for (int32_t i=0; i<rows; i++)
377 for (int32_t j=0; j<cols; j++)
378 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
379 j==cols-1?
"" :
",");
380 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
388 const int32_t* matrix, int32_t rows, int32_t cols,
const char* name,
391 ASSERT(rows>=0 && cols>=0)
393 for (int32_t i=0; i<rows; i++)
396 for (int32_t j=0; j<cols; j++)
397 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
398 j==cols-1?
"" :
",");
399 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
406 const uint32_t* matrix, int32_t rows, int32_t cols,
const char* name,
409 ASSERT(rows>=0 && cols>=0)
411 for (int32_t i=0; i<rows; i++)
414 for (int32_t j=0; j<cols; j++)
415 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
416 j==cols-1?
"" :
",");
417 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
423 const int64_t* matrix, int32_t rows, int32_t cols,
const char* name,
426 ASSERT(rows>=0 && cols>=0)
428 for (int32_t i=0; i<rows; i++)
431 for (int32_t j=0; j<cols; j++)
432 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
433 j==cols-1?
"" :
",");
434 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
441 const uint64_t* matrix, int32_t rows, int32_t cols,
const char* name,
444 ASSERT(rows>=0 && cols>=0)
446 for (int32_t i=0; i<rows; i++)
449 for (int32_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 float32_t* matrix, int32_t rows, int32_t cols,
const char* name,
462 ASSERT(rows>=0 && cols>=0)
464 for (int32_t i=0; i<rows; i++)
467 for (int32_t j=0; j<cols; j++)
468 SG_SPRINT(
"%s\t%.18g%s", prefix, (
float) matrix[j*rows+i],
469 j==cols-1?
"" :
",");
470 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
477 const float64_t* matrix, int32_t rows, int32_t cols,
const char* name,
480 ASSERT(rows>=0 && cols>=0)
482 for (int32_t i=0; i<rows; i++)
485 for (int32_t j=0; j<cols; j++)
486 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
487 j==cols-1?
"" :
",");
488 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
495 const floatmax_t* matrix, int32_t rows, int32_t cols,
const char* name,
498 ASSERT(rows>=0 && cols>=0)
500 for (int32_t i=0; i<rows; i++)
503 for (int32_t j=0; j<cols; j++)
504 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
505 j==cols-1?
"" :
",");
506 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
513 const complex128_t* matrix, int32_t rows, int32_t cols,
const char* name,
516 ASSERT(rows>=0 && cols>=0)
518 for (int32_t i=0; i<rows; i++)
521 for (int32_t j=0; j<cols; j++)
522 SG_SPRINT(
"%s\t(%.18g+i%.18g)%s", prefix, matrix[j*rows+i].real(),
523 matrix[j*rows+i].imag(), j==cols-1?
"" :
",");
524 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",")
543 I(i,j)=i==j ? scale : 0.0;
556 I(i,j)=i==j ? scale : 0.0;
569 I(i,j)=i==j ? scale : (!scale);
582 I(i,j)=i==j ? scale : 0.0;
595 I(i,j)=i==j ? scale : 0.0;
608 I(i,j)=i==j ? scale : 0.0;
621 I(i,j)=i==j ? scale : 0.0;
634 I(i,j)=i==j ? scale : 0.0;
647 I(i,j)=i==j ? scale : 0.0;
660 I(i,j)=i==j ? scale : 0.0;
673 I(i,j)=i==j ? scale : 0.0;
686 I(i,j)=i==j ? scale : 0.0;
746 int32_t lsize=CMath::min((int32_t) m, (int32_t) n);
747 double* s=SG_MALLOC(
double, lsize);
748 double* u=SG_MALLOC(
double, m*m);
749 double* vt=SG_MALLOC(
double, n*n);
751 wrap_dgesvd(jobu, jobvt, m, n, matrix, lda, s, u, ldu, vt, ldvt, &info);
754 for (int32_t i=0; i<n; i++)
756 for (int32_t j=0; j<lsize; j++)
757 vt[i*n+j]=vt[i*n+j]/s[j];
760 cblas_dgemm(CblasColMajor, CblasTrans, CblasTrans, m, n, m, 1.0, vt, ldvt, u, ldu, 0, target, m);
774 int32_t* ipiv = SG_MALLOC(int32_t, matrix.
num_cols);
785 SG_SERROR(
"SGMatrix::compute_eigenvectors(SGMatrix<float64_t>): matrix"
786 " rows and columns are not equal!\n");
807 double* eigenvalues=SG_CALLOC(
float64_t, n+1);
814 SG_SERROR(
"DSYEV failed with code %d\n", info)
821 int n,
int il,
int iu)
823 eigenvalues = SG_MALLOC(
double, n);
824 eigenvectors = SG_MALLOC(
double, (iu-il+1)*n);
826 wrap_dsyevr(
'V',
'U',n,matrix_,n,il,iu,eigenvalues,eigenvectors,&status);
835 bool transpose_A,
bool transpose_B,
float64_t scale)
846 SG_SERROR(
"SGMatrix::matrix_multiply(): Dimension mismatch: "
847 "A(%dx%d)*B(%dx%D)\n", rows_A, cols_A, rows_B, cols_B);
855 cblas_dgemm(CblasColMajor,
856 transpose_A ? CblasTrans : CblasNoTrans,
857 transpose_B ? CblasTrans : CblasNoTrans,
858 rows_A, cols_B, cols_A, scale,
862 for (int32_t i=0; i<rows_A; i++)
864 for (int32_t j=0; j<cols_B; j++)
866 for (int32_t k=0; k<cols_A; k++)
867 C(i,j) += A(i,k)*B(k,j);
884 result=pre_allocated;
887 if (pre_allocated.
num_rows!=num_rows ||
890 SG_SERROR(
"SGMatrix<T>::get_allocated_matrix(). Provided target"
891 "matrix dimensions (%dx%d) do not match passed data "
892 "dimensions (%dx%d)!\n", pre_allocated.
num_rows,
893 pre_allocated.
num_cols, num_rows, num_cols);
908 matrix=((
SGMatrix*)(&orig))->matrix;
909 num_rows=((
SGMatrix*)(&orig))->num_rows;
910 num_cols=((
SGMatrix*)(&orig))->num_cols;
948 SG_SERROR(
"SGMatrix::load():: Not supported for complex128_t\n");
956 writer->
set_matrix(matrix, num_rows, num_cols);
963 SG_SERROR(
"SGMatrix::save():: Not supported for complex128_t\n");
970 for (
index_t i = 0; i < num_cols; i++)
972 rowv[i] = matrix[i*num_rows+row];
980 index_t diag_vlen=CMath::min(num_cols, num_rows);
983 for (
index_t i=0; i<diag_vlen; i++)
985 diag[i]=matrix[i*num_rows+i];