-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
199 lines (183 loc) · 5.16 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
#include "Matrix.hpp"
template <typename T>
Matrix<T>::Matrix(string str)
{
string out;
for (int i = 0; i < str.size(); i++) {
int tmp = (int) str[i];
if (tmp < 48 || tmp > 57) {
continue;
} else {
out.append(str, i, 1);
}
}
int size = stoi(out);
this->rows = size;
this->cols = size;
this->mat = new T[rows * cols];
printf("The matrix of size %d rows & %d colsumns has been built successfully with equal rows and cols!\n", rows, cols);
//input the matrix
ifstream myfile(str);
for (int i = 0; i < size * size; i++)
{
myfile >> mat[i];
}
}
template <typename T>
Matrix<T>::Matrix(int rows, int cols) : rows(rows), cols(cols)
{
this->mat = new T[rows * cols];
for(int i = 0;i<rows * cols;i++){
this->mat[i] = 0;
}
printf("The matrix of size %d rows & %d columns has been built successfully without data!\n", rows, cols);
}
// template <typename T>
// Matrix<T>::Matrix(const Matrix<T> &m)
// {
// this->rows = m.rows;
// this->cols = m.cols;
// this->mat = m.mat;
// printf("Using copy constructor\n");
// }
template <typename T>
Matrix<T>::Matrix(int rows, int cols, string matrixSource) : rows(rows), cols(cols)
{
//input the matrix
this->mat = new T[rows * cols];
ifstream myfile(matrixSource);
for (int i = 0; i < rows * cols; i++)
{
myfile >> mat[i];
}
printf("The matrix of size %d rows & %d columns has been built from source with special rows and cols!\n", rows, cols);
}
template <typename T>
int Matrix<T>::getRows() const
{
return this->rows;
}
template <typename T>
int Matrix<T>::getCols() const
{
return this->cols;
}
template <typename T>
T *Matrix<T>::getMat() const
{
return this->mat;
}
template <typename T> // overload operator "+"
Matrix<T> Matrix<T>::operator+(const Matrix<T> &m)
{
if (this->cols != m.getRows() || this->cols != m.getCols())
{
cout << "The size of two input matrices is not proper!!" << endl;
Matrix<T> Wrong(0, 0);
return Wrong;
}
else
{
Matrix<T> result(this->rows, this->cols);
for (int i = 0; i < this->rows * this->cols; i++)
{
result.getMat()[i] = this->mat[i] + m.getMat()[i];
// cout<<result.getMat()[i]<<"="<<this->mat[i]<<"+"<<m.getMat()[i]<<endl;
}
return result;
}
}
template <typename T> // overload operator "+"
Matrix<T> Matrix<T>::operator*(const Matrix<T> &m)
{
omp_set_num_threads(8);
if (this->cols != m.getRows())
{
cout << "The size of two input matrices is not proper!!" << endl;
Matrix<T> Wrong(0, 0);
return Wrong;
}
else
{
Matrix<T> result(this->rows, m.getCols());
#pragma omp parallel for
for (int i = 0; i < this->rows; i++) {
for (int k = 0; k < this->cols; k++) {
float s = this->mat[i*this->rows+k];
// float s = *(A->matrix + (i * A->n + k));
for (int j = 0; j < m.cols; j++) {
result.getMat()[i*this->rows+j]+=s*m.getMat()[k*this->cols+j];
printf("C(%d,%d) += A(%d,%d) * B(%d,%d); %.2f * %.2f\n", i*this->rows, j, i*this->rows, k, k*this->rows, j, s, m.getMat()[k*this->rows+j]);
}
}
}
return result;
}
}
template <typename T> // overload operator "-"
Matrix<T> Matrix<T>::operator-(const Matrix<T> &m)
{
if (this->rows != m.getRows() || this->cols != m.getCols())
{
cout << "The size of two input matrices is not proper!!" << endl;
Matrix<T> Wrong(0, 0);
return Wrong;
}
else
{
Matrix<T> result(this->rows, this->cols);
for (int i = 0; i < this->rows * this->cols; i++)
{
result.getMat()[i] = this->mat[i] - m.getMat()[i];
// cout<<result.getMat()[i]<<"="<<this->mat[i]<<"+"<<m.getMat()[i]<<endl;
}
return result;
}
}
template <typename T> // overload operator "="
Matrix<T>& Matrix<T>::operator=(const Matrix<T> &m)
{
this->rows = m.getRows();
this->cols = m.getCols();
if (this->mat != NULL)
{
this->mat = m.mat;
}
return *this;
}
template <typename T> // print the matrix
ostream &operator<<(ostream &os, Matrix<T> &m)
{
for (int i = 0; i < m.getRows() * m.getCols(); i++)
{
os <<m.getMat()[i] <<" ";
if ((i + 1) % m.getCols() == 0)
{
os << endl;
}
}
return os;
}
template <typename T> // overload operator "=="
bool Matrix<T>::operator==(const Matrix<T> &m)
{
if (this->row != m.getRow() || this->col != m.getCol())
{
return 0;
}
else
{
for (int i = 0; i < this->row * this->col; i++)
{
if (this->mat[i] != m.getMat()[i])
{
return 0;
}
}
return 1;
}
}
// template <typename T>
// Matrix<T>::~Matrix(){
// delete []this->mat;
// }