-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmymatrix.h
245 lines (198 loc) · 5.45 KB
/
mymatrix.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
/////////////////////////////////////////////////////////////////////////
// This file "mymatrix.h" includes the definition of class Matrix.
// It aims to provide users with the most convenient approach to handle
// matrix computation. We are also exploring ways to fasten all these
// operations with C++.
////////////////////////////////////////////////////////////////////////
#pragma once
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <istream>
#include <ppl.h>
#include <utility>
#include "exception.h"
class Matrix
{
public:
/*
Constructors
*/
Matrix();
// Initialize with row and column
Matrix(int, int);
// Initialize with a vector
Matrix(const std::vector<double>&);
// Initialize with vector of vector
Matrix(const std::vector<std::vector<double> >&);
// Copy constructor
Matrix(const Matrix&);
/*
Destructor
*/
~Matrix();
//////////////////// Common Methods ////////////////
/*
The size of the matrix.
Input: 0 or 1
Output: the number of rows or columns
*/
int size(int)const;
/*
Fetch a certain element in the matrix
Input: row and column
Output: an element in the matrix
*/
double get(int row, int col)const;
/*
Set a element to a certain value
Input: row, colume, value
Output: 0 or 1 (success or failure).
*/
int set(int row, int col, double value);
/*
Print the matrix on the console window.
*/
void print();
/*
Read from input stream.
*/
void read(std::istream&);
/*
Convert matrix to std::vector
*/
std::vector<std::vector<double> > toVector();
/////////////////// Scientific Computation /////////////////
// row and column selecting
Matrix getRow(int)const;
Matrix getRow(const std::vector<int>&);
Matrix getColumn(int)const;
Matrix getColumn(const std::vector<int>&);
void setRow(int, const Matrix&);
void setCol(int, const Matrix&);
void setRow(int, const std::vector<double>&);
void setCol(int, const std::vector<double>&);
/*
special slicing
*/
Matrix getDiagonal()const;
Matrix getUpper()const;
Matrix getLower()const;
/*
Add a new row.
Input: A vector of the same length as other rows in the matrix.
*/
void appendRow(const std::vector<double>&);
void appendCol(const std::vector<double>&);
// general version of append
void appendRow(const Matrix&);
void appendCol(const Matrix&);
// swap two row
void swapRow(int, int);
/*
Remove a row/column
Input: The row/column index of the row/column to remove.
Danger: This operation disarranges the matrix order.
: fix it to do stable removal
*/
void removeRow(int);
void removeCol(int);
// Splitting
// horizontal split
std::vector<Matrix> hsplit(const std::vector<int>&);
std::vector<Matrix> hsplit();
// vertical split
std::vector<Matrix> vsplit(const std::vector<int>&);
std::vector<Matrix> vsplit();
// sum along rows or columns (0 or 1)
Matrix sum(int)const;
// sum up all elements
double sum()const;
// Similar for product
Matrix product(int);
double product();
// max/min element
Matrix max(int)const;
Matrix min(int)const;
Matrix mean(int)const;
Matrix variance(int)const;
// sorting the matrix by row or column
void sort(int);
Matrix sort(int)const;
// transpose to make a new matrix
Matrix transpose()const;
// cofactor matrix
Matrix cofactor();
// adjoint/adjugate matrix
Matrix adjoint();
// reverse to make a new matrix
Matrix inverse();
// elementary row operation
Matrix elemRowOp();
// turn into diagonal matrix by elementary row operation
Matrix diagonalize();
// matrix rank
int rank();
// det
double determinant();
// trace
double trace();
/////////////// Overloaded Operators ////////////////////////
// Equal
bool operator==(const Matrix&);
// Not equal
bool operator!=(const Matrix&);
// multiply with a double
Matrix operator*(const double&);
friend Matrix operator*(const double&, Matrix&);
// multiply with a matrix
Matrix operator*(const Matrix&)const;
//friend Matrix operator*(const Matrix&, const Matrix&);
// plus
Matrix operator+(const Matrix&);
friend Matrix operator+(const double&, const Matrix&);
friend Matrix operator+(const Matrix&, const double&);
// minus
Matrix operator-(const Matrix&)const;
friend Matrix operator-(const double&, const Matrix&);
friend Matrix operator-(const Matrix&, const double&);
Matrix operator/(double);
// Assignment Operator
Matrix& operator=(const Matrix&);
///////////////////////// Itreator /////////////////////////////
class iterator: public std::iterator<std::forward_iterator_tag, double>{
public:
iterator();
// Attention: the 3-th param is the size of the row.
iterator(std::vector<std::vector<double> >::iterator, std::vector<double>::iterator, int, int);
iterator& operator=(const iterator&);
iterator operator++(int);
iterator operator++();
double& operator*();
friend bool operator==(const iterator&, const iterator&);
friend bool operator!=(const iterator&, const iterator&);
private:
std::vector<std::vector<double> >::iterator iter1;
std::vector<double>::iterator iter2;
int _width;
int _height;
int _step;
int _line;
};
typedef const iterator& const_iterator;
iterator begin();
iterator end();
const_iterator begin()const;
const_iterator end()const;
protected:
// protected class for advanced construction
Matrix(int, int, double);
//Matrix(Matrix::const_iterator, Matrix::const_iterator);
private:
int _row;
int _col;
std::vector<std::vector<double> > _mat;
iterator _begin;
iterator _end;
};