-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixStructure.cpp
161 lines (141 loc) · 4.1 KB
/
matrixStructure.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
#include <iostream>
#include <stdexcept>
#include <cmath>
class matrix {
public:
int rows;
int columns;
double** data;
// constructor
matrix(int r, int c) : rows(r), columns(c) {
if(r <= 0 || c <= 0){
throw std::invalid_argument("\nMatrix dimensions must be positive!\n");
}
data = new double*[rows];
for (int i = 0; i < rows; i++) {
data[i] = new double[columns];
for (int j = 0; j < columns; j++) {
data[i][j] = 0.0;
}
}
}
// destructor
~matrix() {
for (int i = 0; i < rows; i++) {
delete[] data[i];
}
delete[] data;
}
// copy constructor
matrix(const matrix& other) : rows(other.rows), columns(other.columns) {
data = new double*[rows];
for (int i = 0; i < rows; i++) {
data[i] = new double[columns];
for (int j = 0; j < columns; j++) {
data[i][j] = other.data[i][j];
}
}
}
// copy assignment operator
matrix& operator=(const matrix& other) {
if (this != &other) {
for (int i = 0; i < rows; i++) {
delete[] data[i];
}
delete[] data;
rows = other.rows;
columns = other.columns;
data = new double*[rows];
for (int i = 0; i < rows; i++) {
data[i] = new double[columns];
for (int j = 0; j < columns; j++) {
data[i][j] = other.data[i][j];
}
}
}
return *this;
}
// move constructor
matrix(matrix&& other) noexcept : rows(other.rows), columns(other.columns), data(other.data) {
other.data = nullptr;
other.rows = 0;
other.columns = 0;
}
// move assignment operator
matrix& operator=(matrix&& other) noexcept {
if (this != &other) {
if (data != nullptr) {
for (int i = 0; i < rows; i++) {
delete[] data[i];
}
delete[] data;
}
rows = other.rows;
columns = other.columns;
data = other.data;
other.data = nullptr;
other.rows = 0;
other.columns = 0;
}
return *this;
}
// subscript operators
double* operator[](int index) { return data[index]; }
const double* operator[](int index) const { return data[index]; }
};
// print matrix
void print(const matrix& A) {
for (int i = 0; i < A.rows; i++) {
std::cout << "|";
for (int j = 0; j < A.columns; j++) {
std::cout << " " << A.data[i][j] << " ";
}
std::cout << "|\n";
}
}
// matrix multiplication
matrix multiply(const matrix& A, const matrix& B) {
if (A.columns != B.rows) {
throw std::invalid_argument("\nMatrix dimensions do not match for multiplication!\n");
}
matrix result(A.rows, B.columns);
for (int i = 0; i < A.rows; i++) {
for (int j = 0; j < B.columns; j++) {
for (int k = 0; k < A.columns; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
return result;
}
matrix multiplyScalar(const matrix& A, double scalar){
matrix result(A.rows, A.columns);
for (int i = 0; i < A.rows; i++){
for (int j = 0; j < A.columns; j++){
result[i][j] = A[i][j] * scalar;
}
}
return result;
}
// matrix addition
matrix add(const matrix& A, const matrix& B) {
if (A.columns != B.columns || A.rows != B.rows) {
throw std::invalid_argument("\nMatrix dimensions do not match for addition!\n");
}
matrix result(A.rows, B.columns);
for (int i = 0; i < A.rows; i++) {
for (int j = 0; j < B.columns; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
return result;
}
matrix transpose(const matrix& A) {
matrix result(A.columns, A.rows);
for (int i = 0; i < A.rows; i++) {
for (int j = 0; j < A.columns; j++) {
result[j][i] = A[i][j];
}
}
return result;
}