-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.h
193 lines (164 loc) · 4.62 KB
/
Matrix.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
//
// Matrix.h
// MatrixClass
//
// Created by Li Xinrui on 10/1/14.
// Copyright (c) 2014 Li Xinrui. All rights reserved.
//
#ifndef __MatrixClass__Matrix__
#define __MatrixClass__Matrix__
#define TYPE double
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <cassert>
using std::vector;
using std::cout;
using std::endl;
class Matrix{
// Members
protected:
int mNumRows = 0;
int mNumCols = 0;
TYPE **data;
public:
// Constructors
Matrix();
Matrix(int row, int col);
Matrix(int dim);
Matrix(const Matrix& m);
// Deconstructor
~Matrix(){
for (int i = 0; i != this -> mNumCols; i++) {
delete [] data[i];
}
delete [] data;
}
public:
// Operators
TYPE* operator[](int index);
const TYPE* operator[](int index) const;
TYPE& operator()(int rowIndex, int colIndex);
const TYPE& operator() (int rowIndex, int colIndex) const;
// Members
int GetNumRows(){
return mNumRows;
}
int GetNumRows() const{
return mNumRows;
}
int GetNumCols() const{
return mNumCols;
}
int GetNumCols(){
return mNumCols;
}
void ResizeRow(int row){
mNumRows = row;
}
void ResizeCol(int col) {
mNumCols = col;
}
// Operations
Matrix& operator+=(const Matrix& rhs);
Matrix& operator-=(const Matrix& rhs);
Matrix& operator*=(const TYPE& rhs);
Matrix& operator/=(const TYPE& rhs);
Matrix& operator=(const TYPE* rhs);
Matrix& operator=(const vector<TYPE>& rhs);
Matrix& operator=(const Matrix& rhs);
public:
Matrix& rowSwap(int i, int j);
Matrix& colSwap(int i, int j);
Matrix& transpose();
double Norm2Vec();
int transpose(Matrix& res);
bool QSquare();
void SetIdentity(){
if( !QSquare() ){
return;
}
for (int i = 0; i != this -> mNumCols; i++) {
data[i][i] = 1;
}
}
public:
friend std::ostream& operator<<(std::ostream& out, const Matrix& m);
friend std::ofstream& operator<<(std::ofstream& out, Matrix& m);
friend std::ostream& latex(std::ostream& out, const Matrix& m, int line);
};
// Operations
Matrix operator+(const Matrix& m1, const Matrix& m2);
Matrix operator-(const Matrix& m1, const Matrix& m2);
Matrix operator*(const Matrix& m1, const Matrix& m2);
Matrix operator*(const Matrix& m1, const TYPE& num);
Matrix operator*(const TYPE& num, const Matrix& m1);
Matrix operator/(const Matrix& m1, const TYPE& num);
// Algorithms
int LUDecomp(const Matrix& m, Matrix& l, Matrix& u);
int LUPivotDecomp(const Matrix& m, Matrix& p, Matrix& l, Matrix& u);
int CholeskyDecomp(const Matrix& m, Matrix& l);
int LDLDecomp(const Matrix& m, Matrix &l, Matrix& d);
int LUSolve(const Matrix& m, const Matrix& rhs, Matrix& res);
int LUPivotSolve(const Matrix& m, const Matrix& rhs, Matrix& res);
int CholeskySolve(const Matrix&m, const Matrix& rhs, Matrix& res);
int LDLSolve(const Matrix& m, const Matrix& rhs, Matrix& res);
int lowerSolve(const Matrix& l,const Matrix& rhs, Matrix& res);
int upperSolve(const Matrix& u,const Matrix& rhs, Matrix& res);
class mVector : public Matrix {
public:
using Matrix::operator=;
using Matrix::operator/=;
using Matrix::operator*=;
using Matrix::operator+=;
using Matrix::operator-=;
public:
int dim(){
return this -> mNumRows;
}
int dim() const{
return this -> mNumRows;
}
public:
TYPE& operator[](int index){
return data[0][index];
}
const TYPE& operator[](int index) const{
return data[0][index];
}
public:
mVector() {}
mVector(int dim){
mNumRows = dim;
mNumCols = 1;
data = new TYPE*[1];
data[0] = new TYPE[dim];
for (int i = 0; i != dim; i++) {
data[0][i] = 0;
}
}
mVector(const mVector& rhs){
mNumRows = rhs.dim();
mNumCols = 1;
data = new TYPE*[1];
data[0] = new TYPE[rhs.dim()];
for (int i = 0; i != rhs.dim(); i++) {
data[0][i] = rhs[i];
}
}
double NormInf();
double NormInf(int& position);
double Norm_2();
double Norm_1();
};
mVector operator+(const mVector& m1, const mVector& m2);
mVector operator-(const mVector& m1, const mVector& m2);
mVector operator*(const Matrix& m1, const mVector& m2);
mVector operator*(const mVector& m1, const TYPE& num);
mVector operator*(const TYPE& num, const mVector& m1);
mVector operator/(const mVector& m1, const TYPE& num);
double InnerProduct(mVector& v1, mVector& v2);
#endif /* defined(__MatrixClass__Matrix__) */