This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
153 lines (133 loc) · 3.77 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
#include "Matrix.h"
Matrix::Matrix(int row, int col, int num) {
this->row = row;
this->col = col;
this->data_ptr = new int *[row];
for (int i = 0; i < this->row; i++) {
data_ptr[i] = new int[this->col];
}
for (int i = 0; i < this->row; i++) {
for (int j = 0; j < this->row; j++) {
this->data_ptr[i][j] = num;
}
}
}
Matrix::~Matrix() {
for (int i = 0; i < this->row; i++) {
if (data_ptr[i] != nullptr) {
delete[] data_ptr[i];
data_ptr[i] = nullptr;
}
}
if (data_ptr != nullptr) {
delete[] data_ptr;
data_ptr = nullptr;
}
}
Matrix::Matrix(const Matrix &rhs) {
row = rhs.row;
col = rhs.col;
data_ptr = new int *[row];
for (int i = 0; i < row; i++) {
data_ptr[i] = new int[col];
memcpy(data_ptr[i], rhs.data_ptr[i], sizeof(int) * col);
}
}
Matrix Matrix::operator*(const Matrix &rhs) const {
if (this->col != rhs.row) {
std::cerr << "shape error" << std::endl;
exit(0);
}
Matrix m(this->row, rhs.col);
for (int i = 0; i < this->row; i++) {
for (int j = 0; j < rhs.col; j++) {
int sum = 0;
for (int k = 0; k < this->col; k++) {
sum += this->data_ptr[i][k] * rhs.data_ptr[k][j];
}
m.data_ptr[i][j] = sum;
}
}
return m;
}
Matrix Matrix::operator+(const Matrix &rhs) const {
if (this->col != rhs.col || this->row != rhs.row) {
std::cerr << "shape error" << std::endl;
}
Matrix m(this->row, this->col);
for (int i = 0; i < this->row; i++) {
for (int j = 0; j < this->col; j++) {
m.data_ptr[i][j] = this->data_ptr[i][j] + rhs.data_ptr[i][j];
}
}
return m;
}
Matrix Matrix::operator-(const Matrix &rhs) const {
if (this->col != rhs.col or this->row != rhs.row) {
std::cout << "shape error" << std::endl;
}
Matrix m(this->row, this->col);
for (int i = 0; i < this->row; i++) {
for (int j = 0; j < this->col; j++) {
m.data_ptr[i][j] = this->data_ptr[i][j] - rhs.data_ptr[i][j];
}
}
return m;
}
std::string Matrix::shape() const {
std::string s = "(" + std::to_string(this->row) + "," + std::to_string(this->col) + ")";
return s;
}
void Matrix::print_all(std::ostream& os) const {
for (int i = 0; i < this->row; i++) {
for (int j = 0; j < this->col; j++) {
os << this->data_ptr[i][j] << " ";
}
os << std::endl;
}
}
Matrix::Matrix(int row, int col, std::initializer_list<int> il) {
this->row = row;
this->col = col;
if (row * col != il.size()) {
std::cerr << "shape error" << std::endl;
}
this->data_ptr = new int *[row];
for (int i = 0; i < this->row; i++) {
data_ptr[i] = new int[this->col];
}
int i = 0;
for (auto it = il.begin(); it != il.end(); it++, i++) {
this->data_ptr[i / this->col][i % this->col] = *it;
}
}
Matrix &Matrix::operator+=(const Matrix &rhs) {
*this = this->operator+(rhs);
return *this;
}
Matrix &Matrix::operator=(const Matrix &rhs) {
if (this == &rhs) {
return *this;
}
this->row = rhs.row;
this->col = rhs.col;
this->data_ptr = new int *[row];
for (int i = 0; i < row; i++) {
data_ptr[i] = new int[col];
memcpy(data_ptr[i], rhs.data_ptr[i], sizeof(int) * col);
}
return *this;
}
Matrix &Matrix::operator-=(const Matrix &rhs) {
*this = this->operator-(rhs);
return *this;
}
Matrix Matrix::transpose() const {
Matrix m(this->col, this->row);
for (int i = 0; i < this->row; i++) {
for (int j = 0; j < this->col; j++) {
std::swap(m.data_ptr[j][i],this->data_ptr[i][j]) ;
}
}
return m;
}