-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
SGMatrix.h
329 lines (277 loc) · 9.58 KB
/
SGMatrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011-2013 Heiko Strathmann
* Written (W) 2012 Fernando Jose Iglesias Garcia
* Written (W) 2010,2012 Soeren Sonnenburg
* Copyright (C) 2010 Berlin Institute of Technology
* Copyright (C) 2012 Soeren Sonnenburg
*/
#ifndef __SGMATRIX_H__
#define __SGMATRIX_H__
#include <shogun/lib/config.h>
#include <shogun/lib/common.h>
#include <shogun/lib/SGReferencedData.h>
#ifdef HAVE_EIGEN3
#include <shogun/mathematics/eigen3.h>
#endif
namespace shogun
{
template<class T> class SGVector;
class CFile;
/** @brief shogun matrix */
template<class T> class SGMatrix : public SGReferencedData
{
public:
/** default constructor */
SGMatrix();
/** constructor for setting reference counting while not creating
* the matrix in memory (use this for static SGMatrix instances) */
SGMatrix(bool ref_counting);
/** constructor for setting params */
SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);
/** Wraps a matrix around an existing memory segment with an offset */
SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset)
: SGReferencedData(false), matrix(m+offset),
num_rows(nrows), num_cols(ncols) { }
/** constructor to create new matrix in memory */
SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
/** copy constructor */
SGMatrix(const SGMatrix &orig);
#ifndef SWIG // SWIG should skip this part
#ifdef HAVE_EIGEN3
/** Wraps a matrix around the data of an Eigen3 matrix */
template <class Derived>
SGMatrix(Eigen::PlainObjectBase<Derived>& mat)
: SGReferencedData(false), matrix(mat.data()),
num_rows(mat.rows()), num_cols(mat.cols()) { }
/** Wraps an Eigen3 matrix around the data of this matrix */
operator Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >() const
{
return Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >(
matrix, num_rows, num_cols);
}
#endif
#endif
/** empty destructor */
virtual ~SGMatrix();
/** get a column vector
* @param col column index
*/
T* get_column_vector(index_t col) const
{
const int64_t c = col;
return &matrix[c*num_rows];
}
/** get a row vector
*
* @param row row index
* @return row vector
*/
SGVector<T> get_row_vector(index_t row) const;
/** get a main diagonal vector. Matrix is not required to be square.
*
* @return main diagonal vector
*/
SGVector<T> get_diagonal_vector() const;
/** operator overload for matrix read only access
* @param i_row
* @param i_col
*/
inline const T& operator()(index_t i_row, index_t i_col) const
{
const int64_t c = i_col;
return matrix[c*num_rows + i_row];
}
/** operator overload for matrix read only access
* @param index to access
*/
inline const T& operator[](index_t index) const
{
return matrix[index];
}
/** operator overload for matrix r/w access
* @param i_row
* @param i_col
*/
inline T& operator()(index_t i_row, index_t i_col)
{
const int64_t c = i_col;
return matrix[c*num_rows + i_row];
}
/** operator overload for matrix r/w access
* @param index to access
*/
inline T& operator[](index_t index)
{
return matrix[index];
}
/**
* get the matrix (no copying is done here)
*
* @return the refcount increased matrix
*/
inline SGMatrix<T> get()
{
return *this;
}
/** check for pointer identity */
bool operator==(SGMatrix<T>& other);
/** operator overload for element-wise matrix comparison.
* Note that only numerical data is compared
*
* @param other matrix to compare with
* @return true iff all elements are equal
*/
bool equals(SGMatrix<T>& other);
/** set matrix to a constant */
void set_const(T const_elem);
/** fill matrix with zeros */
void zero();
/**
* Checks whether the matrix is symmetric or not. The equality check
* is performed using '==' operators for discrete types (int, char,
* bool) and using CMath::fequals method for floating types (float,
* double, long double, std::complex<double>) with default espilon
* values from std::numeric_limits
*
* @return whether the matrix is symmetric
*/
bool is_symmetric();
/** returns the maximum single element of the matrix */
T max_single();
/** clone matrix */
SGMatrix<T> clone();
/** clone matrix */
static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
/** transpose matrix */
static void transpose_matrix(
T*& matrix, int32_t& num_feat, int32_t& num_vec);
/** create diagonal matrix */
static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
/** returns the identity matrix, scaled by a factor
*
* @param size size of square identity matrix
* @param scale (optional) scaling factor
*/
static SGMatrix<T> create_identity_matrix(index_t size, T scale);
#ifdef HAVE_LAPACK
/** compute eigenvalues and eigenvectors of symmetric matrix using
* LAPACK
*
* @param matrix symmetric matrix to compute eigenproblem. Is
* overwritten and contains orthonormal eigenvectors afterwards
* @return eigenvalues vector with eigenvalues equal to number of rows
* in matrix
* */
static SGVector<float64_t> compute_eigenvectors(
SGMatrix<float64_t> matrix);
/** compute eigenvalues and eigenvectors of symmetric matrix
*
* @param matrix overwritten and contains n orthonormal eigenvectors
* @param n
* @param m
* @return eigenvalues (array of length n, to be deleted[])
* */
static double* compute_eigenvectors(double* matrix, int n, int m);
/** compute few eigenpairs of a symmetric matrix using LAPACK DSYEVR method
* (Relatively Robust Representations).
* Has at least O(n^3/3) complexity
* @param matrix_ symmetric matrix
* @param eigenvalues contains iu-il+1 eigenvalues in ascending order (to be free'd)
* @param eigenvectors contains iu-il+1 orthonormal eigenvectors of given matrix column-wise (to be free'd)
* @param n dimension of matrix
* @param il low index of requested eigenpairs (1<=il<=n)
* @param iu high index of requested eigenpairs (1<=il<=iu<=n)
*/
void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
int n, int il, int iu);
#endif
/** Computes scale* A*B, where A and B may be transposed.
* Asserts for matching inner dimensions.
* @param A matrix A
* @param transpose_A optional whether A should be transposed before
* @param B matrix B
* @param transpose_B optional whether B should be transposed before
* @param scale optional scaling factor for result
*/
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);
#ifdef HAVE_LAPACK
/** inverses square matrix in-place */
static void inverse(SGMatrix<float64_t> matrix);
/** return the pseudo inverse for matrix
* when matrix has shape (rows, cols) the pseudo inverse has (cols, rows)
*/
static float64_t* pinv(
float64_t* matrix, int32_t rows, int32_t cols,
float64_t* target=NULL);
#endif
/** compute trace */
static float64_t trace(
float64_t* mat, int32_t cols, int32_t rows);
/** sums up all rows of a matrix and returns the resulting rowvector */
static T* get_row_sum(T* matrix, int32_t m, int32_t n);
/** sums up all columns of a matrix and returns the resulting columnvector */
static T* get_column_sum(T* matrix, int32_t m, int32_t n);
/** Centers the matrix, i.e. removes column/row mean from columns/rows */
void center();
/** Centers matrix (e.g. kernel matrix in feature space INPLACE */
static void center_matrix(T* matrix, int32_t m, int32_t n);
/** remove column mean */
void remove_column_mean();
/** display matrix */
void display_matrix(const char* name="matrix") const;
/** display matrix (useful for debugging) */
static void display_matrix(
const T* matrix, int32_t rows, int32_t cols,
const char* name="matrix", const char* prefix="");
/** display matrix */
static void display_matrix(
const SGMatrix<T> matrix, const char* name="matrix",
const char* prefix="");
/** Simple helper method that returns a matrix with allocated memory
* for a given size. A pre_allocated one can optionally be specified
* in order to use that.
* Basically just for having dimension check encapsulated.
*
* @param num_rows rows of returned matrix
* @param num_cols columns of returned matrix
* @param pre_allocated optional matrix that is returned instead of new
* matrix. Make sure dimensions match
* @return matrix with allocated memory of specified size
*/
static SGMatrix<T> get_allocated_matrix(index_t num_rows,
index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
/** load matrix from file
*
* @param loader File object via which to load data
*/
void load(CFile* loader);
/** save matrix to file
*
* @param saver File object via which to save data
*/
void save(CFile* saver);
protected:
/** overridden to copy data */
virtual void copy_data(const SGReferencedData &orig);
/** overridden to initialize empty data */
virtual void init_data();
/** overridden to free data */
virtual void free_data();
public:
/** matrix */
T* matrix;
/** number of rows of matrix */
index_t num_rows;
/** number of columns of matrix */
index_t num_cols;
};
}
#endif // __SGMATRIX_H__