-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_openblas.h
319 lines (284 loc) · 6.71 KB
/
Matrix_openblas.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
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <math.h>
#include <iomanip>
#include <time.h>
#include <fstream>
#include <map>
#include "cblas.h"
#include "lapack.h"
#include "udf.h"
using namespace std;
class Matrix
{
private:
int row, col;
public:
float* index;
Matrix(int x = 1, int y = 1) {
row = x;
col = y;
index = (float *)malloc( row*col*sizeof( float ));
}
Matrix(const Matrix& obj) {
row = obj.row;
col = obj.col;
index = (float *)malloc( row*col*sizeof( float ));
cblas_scopy(row*col, obj.index, 1, index, 1);
}
Matrix(float **array, int x, int y) {
row = x;
col = y;
index = (float *)malloc( row*col*sizeof( float ));
for (int i = 0; i < row; ++i) {
cblas_scopy(row*col, array[i], 1, index + i*col, 1);
}
}
~Matrix() {
free(index);
}
void E() {
memset(index, 0, row*col*sizeof( float ));
for (int i = 0; i < row; ++i) {
index[i*col+i] = 1;
}
}
void zero() {
memset(index, 0, row*col*sizeof( float ));
}
friend istream& operator>>(istream& is, Matrix& obj) {
for (int i = 0; i < (obj.row*obj.col); ++i) {
is >> obj.index[i];
}
return is;
}
friend ostream& operator<<(ostream& os, const Matrix& obj) {
if (obj.row == 0 || obj.col == 0) {
os << "ERROR!"; //不符合矩阵运算时自动报错
}
else {
for (int i = 0; i < obj.row; ++i) {
for (int j = 0; j < obj.col; ++j) {
os << obj.index[i*obj.col + j] << ' ';
}
os << endl;
}
}
return os;
}
int Row() {
return row;
}
int Col() {
return col;
}
float& operator()(int x, int y) {
return index[x*col + y];
}
// *this = *this + obj
Matrix add(const Matrix obj) {
if (obj.row != row || obj.col != col) {
printf("相加矩阵维数不同!\n");
return *this;
}
else {
cblas_saxpy(row*col, 1, obj.index, 1, index, 1);
return *this;
}
}
// *this = *this - obj
Matrix minus(const Matrix& obj) {
if (obj.row != row || obj.col != col) {
printf("相加矩阵维数不同!\n");
return *this;
}
else {
cblas_saxpy(row*col, -1, obj.index, 1, index, 1);
return *this;
}
}
// *this = *this * obj
Matrix multp(const Matrix& obj) {
if (col != obj.row) {
cout << "维数不符合矩阵相乘原则!" << endl;
}
else {
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, row, obj.col, col, 1, index, col, obj.index, obj.col, 0, index, obj.col);
return *this;
}
}
Matrix multp(const float x) {
cblas_sscal(row*col, x, index, 1);
return *this;
}
Matrix div(const float x) {
cblas_sscal(row*col, 1/x, index, 1);
return *this;
}
Matrix operator+(const Matrix& obj) {
Matrix tmp(row, col);
if (obj.row != row || obj.col != col) {
printf("相加矩阵维数不同!\n");
return *this;
}
else {
tmp.zero();
cblas_saxpy(row*col, 1, obj.index, 1, tmp.index, 1);
cblas_saxpy(row*col, 1, index, 1, tmp.index, 1);
return tmp;
}
}
// *this = *this + x
Matrix operator+(const float x) {
Matrix tmp(*this);
for (int i = 0; i < row; ++i){
for (int j = 0; j < col; ++j){
tmp(i, j) += x;
}
}
return tmp;
}
// *this = *this - obj
Matrix operator-(const Matrix& obj) {
Matrix tmp(row, col);
if (obj.row != row || obj.col != col) {
printf("相加矩阵维数不同!\n");
return *this;
}
else {
tmp.zero();
cblas_saxpy(row*col, -1, obj.index, 1, tmp.index, 1);
cblas_saxpy(row*col, 1, index, 1, tmp.index, 1);
return tmp;
}
}
// *this = *this - x
Matrix operator-(const float x) {
Matrix tmp(*this);
for (int i = 0; i < row; ++i){
for (int j = 0; j < col; ++j){
tmp(i, j) -= x;
}
}
return tmp;
}
friend Matrix operator*(const Matrix& obj1, const Matrix& obj2) {
if (obj1.col != obj2.row) {
cout << "维数不符合矩阵相乘原则!" << endl;
Matrix tmp;
return tmp;
}
else {
Matrix tmp(obj1.row, obj2.col);
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, obj1.row, obj2.col, obj1.col, 1, obj1.index, obj1.col, obj2.index, obj2.col, 0, tmp.index, obj2.col);
return tmp;
}
}
friend Matrix operator*(const Matrix& obj, const float x) {
Matrix tmp(obj);
cblas_sscal(obj.row*obj.col, x, tmp.index, 1);
return tmp;
}
friend Matrix operator/(const Matrix& obj, const float x) {
Matrix tmp(obj);
cblas_sscal(obj.row*obj.col, 1/x, tmp.index, 1);
return tmp;
}
Matrix& operator=(const Matrix& obj) {
free(index);
row = obj.row;
col = obj.col;
index = (float *)malloc( row*col*sizeof( float ));
cblas_scopy(row*col, obj.index, 1, index, 1);
return *this;
}
//矩阵转置
Matrix trans() {
Matrix tmp(col, row);
for (int i = 0; i < col; ++i) {
cblas_scopy(row, index + i, col, tmp.index + i*row, 1);
}
return tmp;
}
//分块矩阵
Matrix block(int x, int y, int new_row, int new_col) {
Matrix tmp(new_row, new_col);
for (int i = 0; i < new_row; ++i) {
cblas_scopy(new_col, index + (i + x)*col, 1, tmp.index + i*new_col, 1);
}
return tmp;
}
//行交换
void swaprow(const int i, const int j) {
cblas_sswap(col, index + i*col, 1, index + j*col, 1);
}
//行变换
void rowmultik(int k, float x) {
cblas_sscal(col, x, index + k*col, 1);
}
//求解线性方程组
Matrix LUsolve(const Matrix b, bool& flag) {
Matrix A(*this);
Matrix x(b);
int info;
int inc = 1;
char trans = 'N';
int* ipiv = (int *)malloc( minInt(row, col)*sizeof( int ));
LAPACK_sgetrf(&row, &col, A.index, &row, ipiv, &info);
if (info < 0){
flag = false;
return b;
}
LAPACK_sgetrs(&trans, &row, &inc, A.index, &row, ipiv, x.index, &row, &info);
return x;
}
//求解线性方程组(对称)
Matrix solve(const Matrix b, bool& flag) {
Matrix A(*this);
Matrix x(b);
int info;
int inc = 1;
int* ipiv = (int *)malloc( row*sizeof( int ));
LAPACK_sgesv(&row, &inc, A.index, &row, ipiv, x.index, &row, &info);
return x;
}
//一维范数
float norm() {
return cblas_snrm2(row*col, index, 1);
}
//求取两向量夹角
friend float theta(Matrix obj1, Matrix obj2)
{
if (obj1.row != obj2.row || obj1.col != 1 || obj2.col != 1) {
cout << "向量维数不相等!" << endl;
return 0;
}
return cblas_sdot(obj1.row, obj1.index, 1, obj2.index, 1) / obj1.norm() / obj2.norm();
}
//数组乘法
friend Matrix dot(Matrix obj1, Matrix obj2)
{
if (obj1.row != obj2.row || obj1.col != obj2.col) {
cout << "矩阵维数不相等!" << endl;
return obj1;
}
else {
Matrix tmp(obj1.row, obj1.col);
for (int i = 0; i < obj1.row; ++i){
for (int j = 0; j < obj1.col; ++j){
tmp(i, j) = obj1(i, j)*obj2(i, j);
}
}
return tmp;
}
}
//矩阵赋值
void cpy(Matrix obj, int x, int y) {
for (int i = 0; i < obj.row; ++i) {
cblas_scopy(obj.col, obj.index + i*obj.col, 1, index + (x + i)*col + y, 1);
}
}
};