13 T*& matrix, int32_t& num_feat, int32_t& num_vec)
16 T* transposed=
SG_MALLOC(T, num_vec*num_feat);
17 for (int32_t i=0; i<num_vec; i++)
19 for (int32_t j=0; j<num_feat; j++)
20 transposed[i+j*num_vec]=matrix[i*num_feat+j];
34 T* colsums=get_column_sum(matrix, m,n);
35 T* rowsums=get_row_sum(matrix, m,n);
37 for (int32_t i=0; i<m; i++)
39 for (int32_t j=0; j<n; j++)
44 for (int32_t i=0; i<n; i++)
46 for (int32_t j=0; j<m; j++)
47 matrix[int64_t(i)*m+j]+=s-colsums[j]-rowsums[i];
59 T* means=get_row_sum(matrix, num_rows, num_cols);
62 for (
index_t i=0; i<num_cols; ++i)
65 for (
index_t j=0; j<num_rows; ++j)
66 matrix[i*num_rows+j]-=means[i];
74 display_matrix(matrix, num_rows, num_cols, name);
87 const bool* matrix, int32_t rows, int32_t cols,
const char* name,
90 ASSERT(rows>=0 && cols>=0);
92 for (int32_t i=0; i<rows; i++)
95 for (int32_t j=0; j<cols; j++)
96 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i] ? 1 : 0,
98 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
105 const char* matrix, int32_t rows, int32_t cols,
const char* name,
108 ASSERT(rows>=0 && cols>=0);
110 for (int32_t i=0; i<rows; i++)
113 for (int32_t j=0; j<cols; j++)
114 SG_SPRINT(
"%s\t%c%s", prefix, matrix[j*rows+i],
115 j==cols-1?
"" :
",");
116 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
123 const int8_t* matrix, int32_t rows, int32_t cols,
const char* name,
126 ASSERT(rows>=0 && cols>=0);
128 for (int32_t i=0; i<rows; i++)
131 for (int32_t j=0; j<cols; j++)
132 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
133 j==cols-1?
"" :
",");
134 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
141 const uint8_t* matrix, int32_t rows, int32_t cols,
const char* name,
144 ASSERT(rows>=0 && cols>=0);
146 for (int32_t i=0; i<rows; i++)
149 for (int32_t j=0; j<cols; j++)
150 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
151 j==cols-1?
"" :
",");
152 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
159 const int16_t* matrix, int32_t rows, int32_t cols,
const char* name,
162 ASSERT(rows>=0 && cols>=0);
164 for (int32_t i=0; i<rows; i++)
167 for (int32_t j=0; j<cols; j++)
168 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
169 j==cols-1?
"" :
",");
170 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
177 const uint16_t* matrix, int32_t rows, int32_t cols,
const char* name,
180 ASSERT(rows>=0 && cols>=0);
182 for (int32_t i=0; i<rows; i++)
185 for (int32_t j=0; j<cols; j++)
186 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
187 j==cols-1?
"" :
",");
188 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
196 const int32_t* matrix, int32_t rows, int32_t cols,
const char* name,
199 ASSERT(rows>=0 && cols>=0);
201 for (int32_t i=0; i<rows; i++)
204 for (int32_t j=0; j<cols; j++)
205 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
206 j==cols-1?
"" :
",");
207 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
214 const uint32_t* matrix, int32_t rows, int32_t cols,
const char* name,
217 ASSERT(rows>=0 && cols>=0);
219 for (int32_t i=0; i<rows; i++)
222 for (int32_t j=0; j<cols; j++)
223 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
224 j==cols-1?
"" :
",");
225 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
231 const int64_t* matrix, int32_t rows, int32_t cols,
const char* name,
234 ASSERT(rows>=0 && cols>=0);
236 for (int32_t i=0; i<rows; i++)
239 for (int32_t j=0; j<cols; j++)
240 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
241 j==cols-1?
"" :
",");
242 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
249 const uint64_t* matrix, int32_t rows, int32_t cols,
const char* name,
252 ASSERT(rows>=0 && cols>=0);
254 for (int32_t i=0; i<rows; i++)
257 for (int32_t j=0; j<cols; j++)
258 SG_SPRINT(
"%s\t%d%s", prefix, matrix[j*rows+i],
259 j==cols-1?
"" :
",");
260 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
267 const float32_t* matrix, int32_t rows, int32_t cols,
const char* name,
270 ASSERT(rows>=0 && cols>=0);
272 for (int32_t i=0; i<rows; i++)
275 for (int32_t j=0; j<cols; j++)
276 SG_SPRINT(
"%s\t%.18g%s", prefix, (
float) matrix[j*rows+i],
277 j==cols-1?
"" :
",");
278 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
285 const float64_t* matrix, int32_t rows, int32_t cols,
const char* name,
288 ASSERT(rows>=0 && cols>=0);
290 for (int32_t i=0; i<rows; i++)
293 for (int32_t j=0; j<cols; j++)
294 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
295 j==cols-1?
"" :
",");
296 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
303 const floatmax_t* matrix, int32_t rows, int32_t cols,
const char* name,
306 ASSERT(rows>=0 && cols>=0);
308 for (int32_t i=0; i<rows; i++)
311 for (int32_t j=0; j<cols; j++)
312 SG_SPRINT(
"%s\t%.18g%s", prefix, (
double) matrix[j*rows+i],
313 j==cols-1?
"" :
",");
314 SG_SPRINT(
"%s]%s\n", prefix, i==rows-1?
"" :
",");
333 I(i,j)=i==j ? scale : 0.0;
346 I(i,j)=i==j ? scale : 0.0;
359 I(i,j)=i==j ? scale : (!scale);
372 I(i,j)=i==j ? scale : 0.0;
385 I(i,j)=i==j ? scale : 0.0;
398 I(i,j)=i==j ? scale : 0.0;
411 I(i,j)=i==j ? scale : 0.0;
424 I(i,j)=i==j ? scale : 0.0;
437 I(i,j)=i==j ? scale : 0.0;
450 I(i,j)=i==j ? scale : 0.0;
463 I(i,j)=i==j ? scale : 0.0;
476 I(i,j)=i==j ? scale : 0.0;
523 int32_t lsize=CMath::min((int32_t) m, (int32_t) n);
528 wrap_dgesvd(jobu, jobvt, m, n, matrix, lda, s, u, ldu, vt, ldvt, &info);
531 for (int32_t i=0; i<n; i++)
533 for (int32_t j=0; j<lsize; j++)
534 vt[i*n+j]=vt[i*n+j]/s[j];
537 cblas_dgemm(CblasColMajor, CblasTrans, CblasTrans, m, n, m, 1.0, vt, ldvt, u, ldu, 0, target, m);
562 SG_SERROR(
"SGMatrix::compute_eigenvectors(SGMatrix<float64_t>): matrix"
563 " rows and columns are not equal!\n");
591 SG_SERROR(
"DSYEV failed with code %d\n", info);
598 int n,
int il,
int iu)
601 eigenvectors =
SG_MALLOC(
double, (iu-il+1)*n);
603 wrap_dsyevr(
'V',
'U',n,matrix_,n,il,iu,eigenvalues,eigenvectors,&status);
612 bool transpose_A,
bool transpose_B,
float64_t scale)
623 SG_SERROR(
"SGMatrix::matrix_multiply(): Dimension mismatch: "
624 "A(%dx%d)*B(%dx%D)\n", rows_A, cols_A, rows_B, cols_B);
632 cblas_dgemm(CblasColMajor,
633 transpose_A ? CblasTrans : CblasNoTrans,
634 transpose_B ? CblasTrans : CblasNoTrans,
635 rows_A, cols_B, cols_A, scale,
639 for (int32_t i=0; i<rows_A; i++)
641 for (int32_t j=0; j<cols_B; j++)
643 for (int32_t k=0; k<cols_A; k++)
644 C(i,j) += A(i,k)*B(k,j);
661 result=pre_allocated;
664 if (pre_allocated.
num_rows!=num_rows ||
667 SG_SERROR(
"SGMatrix<T>::get_allocated_matrix(). Provided target"
668 "matrix dimensions (%dx%d) do not match passed data "
669 "dimensions (%dx%d)!\n", pre_allocated.
num_rows,
670 pre_allocated.
num_cols, num_rows, num_cols);