-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject2_matrix.h
More file actions
179 lines (168 loc) · 3.44 KB
/
project2_matrix.h
File metadata and controls
179 lines (168 loc) · 3.44 KB
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
#pragma once
#include <iostream>
#include <assert.h>
class Matrix {
private:
double** M;
public:
int rows;
int cols;
void init(int a1, int a2, int a3) {
for (int i = 0; i < rows - 2; i++) {
for (int j = 0; j < cols - 2; j++) {
if (i == j) {
M[i][j] = a1;
M[i + 1][j] = M[i][j + 1] = a2;
M[i + 2][j] = M[i][j + 2] = a3;
}
}
}
M[rows - 2][cols - 2] = M[rows - 1][cols - 1] = a1;
M[rows - 1][cols - 2] = M[rows - 2][cols - 1] = a2;
}
static double norm(Matrix m) {
assert(m.cols == 1);
double val = 0.0;
for (int i = 0; i < m.rows; i++) {
val += pow(m[i][0], 2);
}
return sqrt(val);
}
void allocateMemory() {
M = new double* [rows];
for (int i = 0; i < rows; i++)
M[i] = new double[cols];
}
Matrix() : rows(1), cols(1) {
allocateMemory();
M[0][0] = 0;
}
Matrix(int rows, int cols) {
this->rows = rows;
this->cols = cols;
allocateMemory();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
M[i][j] = 0;
}
}
}
Matrix(const Matrix& m) {
this->rows = m.rows;
this->cols = m.cols;
allocateMemory();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
M[i][j] = m[i][j];
}
}
}
~Matrix() {
for (int i = 0; i < rows; i++) {
delete[] M[i];
}
delete[] M;
}
// Operacje na macierzach
Matrix& operator=(const Matrix& m) {
if (this == &m) {
return *this;
}
this->~Matrix();
this->rows = m.rows;
this->cols = m.cols;
allocateMemory();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
M[i][j] = m[i][j];
}
}
return *this;
}
Matrix& operator+=(const Matrix& m)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
M[i][j] += m[i][j];
}
}
return *this;
}
Matrix& operator-=(const Matrix& m)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
M[i][j] -= m[i][j];
}
}
return *this;
}
Matrix& operator*=(const Matrix& m) {
Matrix temp(rows, m.cols);
for (int i = 0; i < temp.rows; ++i) {
for (int j = 0; j < temp.cols; ++j) {
for (int k = 0; k < cols; ++k) {
temp[i][j] += (M[i][k] * m[k][j]);
}
}
}
return (*this = temp);
}
Matrix& operator*=(double num)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
M[i][j] *= num;
}
}
return *this;
}
Matrix& operator-() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (M[i][j] != 0)
M[i][j] = -M[i][j];
}
}
return *this;
}
double* operator[](size_t elem) {
return M[elem];
}
double* operator[](size_t elem)const {
return M[elem];
}
friend std::ostream& operator <<(std::ostream& stream, const Matrix& m) {
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
stream << m[i][j] << " ";
}
stream << std::endl;
}
return stream;
}
};
Matrix operator+(const Matrix& m1, const Matrix& m2)
{
Matrix temp(m1);
return (temp += m2);
}
Matrix operator-(const Matrix& m1, const Matrix& m2)
{
Matrix temp(m1);
return (temp -= m2);
}
Matrix operator*(const Matrix& m1, const Matrix& m2)
{
Matrix temp(m1);
return (temp *= m2);
}
Matrix operator*(const Matrix& m, double num)
{
Matrix temp(m);
return (temp *= num);
}
Matrix operator*(double num, const Matrix& m)
{
return (m * num);
}