-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
335 lines (300 loc) · 9.84 KB
/
matrix.cpp
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
330
331
332
333
334
335
// ver. 1.1
#include<iostream>
#include "matrix.hpp"
#include<stdlib.h>
#define TEST_MODE 0
#ifdef _LOCAL_MKL_
void Double_SquareMatrix::Inverse(){
int m=row, n=column, lda=row;
int info;
std::vector<int> ipiv(n);
int lwork = n;
std::vector<double> work(n);
dgetrf_( &m, &n, AccessArray(), &lda, &ipiv.front(), &info);
dgetri_( &n, AccessArray(), &lda, &ipiv.front(), &work.front(), &lwork, &info);
}
void Double_Matrix::Product(Double_Matrix& A, Double_Matrix& B){
char transa='N',transb='N';
int m=A.row ,n=B.column, k=A.column;
int lda=m, ldb=k, ldc=m;
double alpha=1.0,beta=0.0;
dgemm_(&transa,&transb,&m,&n,&k,&alpha,A.AccessArray(),&lda,B.AccessArray(),&ldb,&beta,AccessArray(),&ldc);
}
void Double_SquareMatrix::Diagonalize(std::vector<double> &EV, std::vector<double> &ES){
char jobz='V',uplo='U';
int n=row;
int lda=n, lwork=3*n-1, info;
std::vector<double> work(3*n);
ES = matrix;
dsyev_(&jobz,&uplo,&n, &ES.front(), &lda, &EV.front(), &work.front(), &lwork, &info);
if (info!=0){
std::cout << "Error at Double_SquareMatrix::Diagonalize in matrix.cpp";
exit(1);
}
}
void Complex_SquareMatrix::Inverse(){
int m=row, n=column, lda=row;
int info;
std::vector<int> ipiv(n);
int lwork = n;
std::vector<std::complex<double> > work(n);
zgetrf_( &m, &n, AccessArray(), &lda, &ipiv.front(), &info);
zgetri_( &n, AccessArray(), &lda, &ipiv.front(), &work.front(), &lwork, &info);
}
void Complex_Matrix::Product(Complex_Matrix& A, Complex_Matrix& B){
char transa='N',transb='N';
int m=A.row ,n=B.column, k=A.column;
int lda=m, ldb=k, ldc=m;
std::complex<double> alpha=1.0,beta=0.0;
zgemm_(&transa,&transb,&m,&n,&k,&alpha,A.AccessArray(),&lda,B.AccessArray(),&ldb,&beta,AccessArray(),&ldc);
}
void Complex_SquareMatrix::Diagonalize(std::vector<double> &EV, std::vector<std::complex<double> > &ES){
char jobz='V',uplo='U';
int n=row;
int lda=n, lwork=2*n-1, info;
std::vector<std::complex<double> > work(2*n);
std::vector<double> rwork(3*n-2);
ES = matrix;
zheev_(&jobz,&uplo,&n, &ES.front(), &lda, &EV.front(), &work.front(), &lwork, &rwork.front(), &info);
if (info!=0){
std::cout << "Error at Complex_SquareMatrix::Diagonalize in matrix.cpp";
exit(1);
}
}
#else
void Double_SquareMatrix::Inverse(){
int m=row, n=column, lda=row;
int info;
std::vector<int> ipiv(n);
int lwork = n;
std::vector<double> work(n);
dgetrf( &m, &n, AccessArray(), &lda, &ipiv.front(), &info);
dgetri( &n, AccessArray(), &lda, &ipiv.front(), &work.front(), &lwork, &info);
}
void Double_Matrix::Product(Double_Matrix& A, Double_Matrix& B){
char transa='N',transb='N';
int m=A.row ,n=B.column, k=A.column;
int lda=m, ldb=k, ldc=m;
double alpha=1.0,beta=0.0;
dgemm(&transa,&transb,&m,&n,&k,&alpha,A.AccessArray(),&lda,B.AccessArray(),&ldb,&beta,AccessArray(),&ldc);
}
void Double_SquareMatrix::Diagonalize(std::vector<double> &EV, std::vector<double> &ES){
char jobz='V',uplo='U';
int n=row;
int lda=n, lwork=3*n-1, info;
std::vector<double> work(3*n);
ES = matrix;
dsyev(&jobz,&uplo,&n, &ES.front(), &lda, &EV.front(), &work.front(), &lwork, &info);
if (info!=0){
std::cout << "Error at Double_SquareMatrix::Diagonalize in matrix.cpp";
exit(1);
}
}
void Complex_SquareMatrix::Inverse(){
int m=row, n=column, lda=row;
int info;
std::vector<int> ipiv(n);
int lwork = n;
std::vector<std::complex<double> > work(n);
zgetrf( &m, &n, AccessArray(), &lda, &ipiv.front(), &info);
zgetri( &n, AccessArray(), &lda, &ipiv.front(), &work.front(), &lwork, &info);
}
void Complex_Matrix::Product(Complex_Matrix& A, Complex_Matrix& B){
char transa='N',transb='N';
int m=A.row ,n=B.column, k=A.column;
int lda=m, ldb=k, ldc=m;
std::complex<double> alpha=1.0,beta=0.0;
zgemm(&transa,&transb,&m,&n,&k,&alpha,A.AccessArray(),&lda,B.AccessArray(),&ldb,&beta,AccessArray(),&ldc);
}
void Complex_SquareMatrix::Diagonalize(std::vector<double> &EV, std::vector<std::complex<double> > &ES){
char jobz='V',uplo='U';
int n=row;
int lda=n, lwork=2*n-1, info;
std::vector<std::complex<double> > work(2*n);
std::vector<double> rwork(3*n-2);
ES = matrix;
zheev(&jobz,&uplo,&n, &ES.front(), &lda, &EV.front(), &work.front(), &lwork, &rwork.front(), &info);
if (info!=0){
std::cout << "Error at Complex_SquareMatrix::Diagonalize in matrix.cpp";
exit(1);
}
}
#endif
// ------DOUBLE------
// General Matrix
// Constructor
Double_Matrix::Double_Matrix(){ row = column = 0; }
Double_Matrix::Double_Matrix(int n){
row = column = n;
matrix.resize(n*n);
}
Double_Matrix::Double_Matrix(int n, int m){
row = n;
column = m;
matrix.resize(n*m);
}
Double_Matrix::Double_Matrix(const Double_Matrix& A){
row = A.row;
column = A.column;
matrix.resize(row*column);
for (int i=0; i<row*column; i++) matrix[i]=A.matrix[i];
}
// Operator
double Double_Matrix::operator () (int i){ return matrix[i]; }
double Double_Matrix::operator () (int i, int j){ return matrix[i+j*row]; }
// Interface
void Double_Matrix::SetMatrix(int n){
row = column = n;
matrix.resize(n*n);
}
void Double_Matrix::SetMatrix(int n, int m){
row = n;
column = m;
matrix.resize(n*m);
}
double* Double_Matrix::AccessArray(){ return &matrix.front(); }
double& Double_Matrix::Element(int i){ return matrix[i]; }
double& Double_Matrix::Element(int i, int j){ return matrix[i + j*row]; }
int Double_Matrix::Row(){ return row; }
int Double_Matrix::Column(){ return column; }
// Functions
void Double_Matrix::PrintMatrix(){
for(int i=0; i<row; i++){
for(int j=0; j<column; j++)
std::cout << matrix[i + j*row] << ",";
std::cout << std::endl;
}
}
// Square Matrix
Double_SquareMatrix::Double_SquareMatrix(){
Double_Matrix();
}
Double_SquareMatrix::Double_SquareMatrix(int n){
row = column = n;
matrix.resize(n*n);
}
Double_SquareMatrix::Double_SquareMatrix(const Double_SquareMatrix& A){
matrix = A.matrix;
column = A.column;
row = A.row;
}
int Double_SquareMatrix::Dim(){ return column; }
double Double_SquareMatrix::Trace(){
double sum=0;
for(int i=0; i<row; i++)sum += matrix[i + row*i];
return sum;
}
void Double_SquareMatrix::DiagonalShift(double x){ for(int i=0; i<row; i++)matrix[i + row*i] += x; }
// ---------COMPLEX----------
Complex_Matrix::Complex_Matrix(){ row = column = 0; }
Complex_Matrix::Complex_Matrix(int n){
row = column = n;
matrix.resize(n*n);
}
Complex_Matrix::Complex_Matrix(int n, int m){
row = n;
column = m;
matrix.resize(n*m);
}
Complex_Matrix::Complex_Matrix(const Complex_Matrix& A){
row = A.row;
column = A.column;
matrix.resize(row*column);
for (int i=0; i<row*column; i++) matrix[i]=A.matrix[i];
}
//~ // Operator
//~ inline std::complex<double>& Complex_Matrix::operator [] (int i){ return matrix[i]; }
//~ inline std::complex<double>& Complex_Matrix::operator () (int i, int j){ return matrix[i+j*row]; }
//~ inline Complex_Matrix& Complex_Matrix::operator=(const Complex_Matrix& A){
//~ row = A.row;
//~ column = A.column;
//~ matrix = A.matrix;
//~ return *this;
//~ }
//~ inline Complex_Matrix& Complex_Matrix::operator+=(const Complex_Matrix& A){
//~ if ((column != A.column) || (row != A.row)) throw;
//~ else {
//~ matrix += A.matrix;
//~ return *this;
//~ }
//~ }
//~ inline Complex_Matrix& Complex_Matrix::operator-=(const Complex_Matrix& A){
//~ if ((column != A.column) || (row != A.row)) throw;
//~ else {
//~ matrix -= A.matrix;
//~ return *this;
//~ }
//~ }
// Interface
void Complex_Matrix::SetMatrix(int n){
row = column = n;
matrix.resize(n*n);
}
void Complex_Matrix::SetMatrix(int n, int m){
row = n;
column = m;
matrix.resize(n*m);
}
std::complex<double>* Complex_Matrix::AccessArray(){ return &matrix.front(); }
std::complex<double>& Complex_Matrix::Element(int i){ return matrix[i]; }
std::complex<double>& Complex_Matrix::Element(int i, int j){ return matrix[i + j*row]; }
int Complex_Matrix::Row(){ return row; }
int Complex_Matrix::Column(){ return column; }
// Functions
void Complex_Matrix::PrintMatrix(){
for(int i=0; i<row; i++){
for(int j=0; j<column; j++)
std::cout << matrix[i + j*row] << ",";
std::cout << std::endl;
}
}
void Complex_Matrix::Sub(Complex_Matrix& A){
for(int i=0; i<row; i++)
for(int j=0; j<column; j++)
matrix[i+j*row] -= A.matrix[i+j*row];
}
void Complex_Matrix::Sub(Complex_Matrix& A, Complex_Matrix& B){
for(int i=0; i<row; i++)
for(int j=0; j<column; j++)
matrix[i+j*row] = A.matrix[i+j*row] - B.matrix[i+j*row];
}
// Square Matrix
Complex_SquareMatrix::Complex_SquareMatrix(){
Complex_Matrix();
}
Complex_SquareMatrix::Complex_SquareMatrix(int n){
row = column = n;
matrix.resize(n*n);
}
Complex_SquareMatrix::Complex_SquareMatrix(const Complex_SquareMatrix& A){
matrix = A.matrix;
column = A.column;
row = A.row;
}
int Complex_SquareMatrix::Dim(){ return column; }
std::complex<double> Complex_SquareMatrix::Trace(){
std::complex<double> sum=0;
for(int i=0; i<row; i++)sum += matrix[i + row*i];
return sum;
}
void Complex_SquareMatrix::DiagonalShift(std::complex<double> x){ for(int i=0; i<row; i++)matrix[i + row*i] += x; }
#if TEST_MODE == 1
#define DIM 2
using namespace std;
const std::complex<double> I(0.0,1.0);
int main(void){
Complex_SquareMatrix a(DIM),b(DIM),c(DIM);
for(int i=0;i<DIM*DIM;i++){
a.Element(i)=i;
b.Element(i)=(double)i-I*(double)i;
}
a.Element(1,0) = complex<double>(0.0,-2.0);
cout << "a:" << endl;
a.PrintMatrix();
cout << "b:" << endl;
b.PrintMatrix();
cout << "c=a*b:" << endl;
c.Product(a,b);
c.PrintMatrix();
}
#endif