-
Notifications
You must be signed in to change notification settings - Fork 42
/
MathMatrix.h
289 lines (243 loc) · 7.68 KB
/
MathMatrix.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
//////////////////////////////////////////////////////////////////////
// libsrc/MathMatrix.h
// (c) 2000-2010 Goncalo Abecasis
//
// This file is distributed as part of the Goncalo source code package
// and may not be redistributed in any form, without prior written
// permission from the author. Permission is granted for you to
// modify this file for your own personal use, but modified versions
// must retain this copyright notice and must not be distributed.
//
// Permission is granted for you to use this file to compile Goncalo.
//
// All computer programs have bugs. Use this file at your own risk.
//
// Sunday May 02, 2010
//
#ifndef __MATHMATRIX_H__
#define __MATHMATRIX_H__
#include <set>
#include <string>
#include "MathVector.h"
#define DECLARE_EIGEN_MATRIX(matRef, varName) \
Eigen::Map<Eigen::MatrixXd> varName((matRef).data.data(), (matRef).rows, \
(matRef).cols);
#define DECLARE_EIGEN_CONST_MATRIX(matRef, varName) \
Eigen::Map<const Eigen::MatrixXd> varName((matRef).data.data(), \
(matRef).rows, (matRef).cols);
// column-major
class Matrix {
public:
Matrix();
explicit Matrix(int nr, int nc);
Matrix(const Matrix& m);
Matrix& operator=(const Matrix& m);
double& operator()(int i, int j) { return data[i + j * rows]; }
double operator()(int i, int j) const { return data[i + j * rows]; }
/**
* Resize matrix to @param nr by @param nc
* Existing contents is preserved
*/
void Dimension(int nr, int nc);
/**
* Set all matrix elements to @param val
*/
void Dimension(int nr, int nc, double val);
/**
* change matrix dimension with keeping contents
*/
void DimensionQuick(int nr, int nc);
void Reserve(int nr, int nc);
void Fill(double val) { std::fill(data.begin(), data.end(), val); }
void Ones(int nr, int nc) {
rows = nr;
cols = nc;
data.resize(nr * nc);
colLabel.resize(nc);
Fill(1.0);
}
void Zero() { Fill(0.); }
const std::string& GetColumnLabel(int idx) const { return colLabel[idx]; }
void SetColumnLabel(int idx, const std::string& label) {
colLabel[idx] = label;
}
double Min() const;
double Max() const;
void Product(const Matrix& in1, const Matrix& in2);
void Transpose(const Matrix& old);
/**
* Each element is multiplied by @param s
*/
Matrix& Multiply(double s);
/**
* @param rowIndexToRemove each index should be in [0, ... , rows-1]
* @return number of row deleted
*/
int RemoveByRowIndex(const std::vector<int>& rowIndexToRemove);
/**
* Stack @param m to the right
*/
Matrix& StackRight(const Matrix& m);
Matrix& MakeColumnMatrix(const Vector& v) {
rows = v.Length();
cols = 1;
data = v.data;
colLabel.resize(1);
return *this;
}
#if 0
operator Eigen::Map<Eigen::MatrixXd>() {
Eigen::Map<Eigen::MatrixXd> ret(data.data(), rows, cols);
return ret;
}
operator Eigen::Map<const Eigen::MatrixXd>() const {
Eigen::Map<const Eigen::MatrixXd> ret(data.data(), rows, cols);
return ret;
}
#endif
public:
int rows;
int cols;
std::vector<double> data;
std::vector<std::string> colLabel;
};
#if 0
#include "Error.h"
#include "MathVector.h"
#include <stdio.h>
class ColumnExtras
{
private:
bool dirty;
int precision, width;
void Init();
void Copy(ColumnExtras & c);
public:
String label;
ColumnExtras()
{ Init(); }
ColumnExtras(ColumnExtras & original)
{ Init(); Copy(original); }
~ColumnExtras();
void SetLabel(const char * name);
void SetPrecision(int p)
{
precision = p;
dirty = true;
}
void SetWidth(int w)
{
width = w;
dirty = true;
}
int GetWidth();
int GetPrecision()
{ return precision; }
ColumnExtras & operator = (ColumnExtras & rhs)
{ Copy(rhs); return (*this); }
void Swap(ColumnExtras & rhs);
};
class Matrix
{
public:
String label;
ColumnExtras * extras;
int rows, cols, size, extraSize;
Vector ** data;
Matrix()
{ Init(); }
Matrix(Matrix & m)
{ Init(); Copy(m); }
Matrix(Matrix & m, const char * name)
{ Init(); Copy(m); SetLabel(name); }
Matrix(int n, int m)
{ Init(); Dimension(n, m); }
Matrix(const char * name)
{ Init(); SetLabel(name); }
Matrix(const char * name, int n, int m)
{ Init(); Dimension(n, m); SetLabel(name); }
~Matrix();
void Dimension(int m, int n);
void Dimension(int m, int n, double value);
void GrowTo(int m, int n)
{ Dimension(m > rows ? m : rows, n > cols ? n : cols); }
void GrowTo(int m, int n, double value)
{ Dimension(m > rows ? m : rows, n > cols ? n : cols, value); }
void SetLabel(const char * name);
void SetColumnLabel(int n, const char * name)
{ extras[n].SetLabel(name); }
const char * GetColumnLabel(int n)
{ return extras[n].label; }
void SetColWidth(int n, int w)
{ extras[n].SetWidth(w); }
void SetColPrecision(int n, int p)
{ extras[n].SetPrecision(p); }
void CopyLabels(Matrix & m);
void Negate();
void Identity();
void Zero();
void Set(double k);
void Copy(const Matrix & m);
void Transpose(const Matrix & m);
void Add(const Matrix & m);
void AddMultiple(double k, const Matrix & m);
void Product(const Matrix & left, const Matrix & right);
void Add(double k);
void Multiply(double k);
// Reduces a matrix to row echelon form, assuming
// values smaller than tol are zero
void Reduce(double tol = 0.0);
Vector & operator [] (int i)
{ assert(i < rows); return *(data[i]); }
const Vector & operator [] (int i) const
{ assert(i < rows); return *(data[i]); }
void DeleteRow(int r);
void DeleteColumn(int c);
void SwapRows(int r1, int r2)
{ Vector * temp = data[r1];
data[r1] = data[r2];
data[r2] = temp;
};
void SwapColumns(int c1, int c2);
void MultiplyRow(int r1, double k);
void AddRows(int r1, int r2);
void AddRows(double k, int r1, int r2);
// Sort according to numeric values in the first column
void Sort();
void Print(FILE * f, int maxRows = -1, int maxCols = -1);
void PrintUpper(FILE * f, int maxRows = -1, int maxCols = -1, bool print_diag = false);
void PrintLower(FILE * f, int maxRows = -1, int maxCols = -1, bool print_diag = false);
void SetupPrint(FILE *f, int r, int c, int & column_zero, int * precision, int * width);
void Read(FILE * f);
Matrix & operator = (const Matrix & rhs)
{ Copy(rhs); return *this; }
bool operator == (const Matrix & rhs) const;
bool operator != (const Matrix & rhs) const { return !(*this == rhs); }
Matrix & operator *= (double rhs)
{ Multiply(rhs); return *this; }
Matrix & operator /= (double rhs)
{ Multiply(1.0/rhs); return *this; }
// Stack a matrix to the bottom of the current matrix
void StackBottom(const Matrix & m);
// Stack a matrix to the left of the current matrix
void StackLeft(const Matrix & m);
// Swap dynamic allocation for two matrices
void Swap(Matrix & m);
// Functions that calculate basic summary statistics
double Min() const;
double Max() const;
double Mean() const;
// Functions that calculate summary statistics in the presence of missing data
double SafeMin() const;
double SafeMax() const;
double SafeMean() const;
int SafeCount() const;
// Return the last row in matrix
Vector & Last() { return *(data[rows - 1]); }
private:
static int alloc;
static int CompareRows(Vector ** row1, Vector ** row2);
void Init();
};
#endif
#endif