-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
175 lines (142 loc) · 2.82 KB
/
Matrix.cpp
File metadata and controls
175 lines (142 loc) · 2.82 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
#include <vector>
#include <iostream>
#include <stdexcept>
#include <cmath>
#include <utility>
using namespace std;
class Point {
public:
int row;
int col;
Point(int row, int col) : row(row), col(col) {}
};
Point& operator+=(Point& a, const Point& b) {
a.row += b.row;
a.col += b.col;
return a;
}
Point operator+(Point a, const Point& b) {
return a += b;
}
Point& operator-=(Point& a, const Point& b) {
a.row -= b.row;
a.col -= b.col;
return a;
}
Point operator-(Point a, const Point& b) {
return a -= b;
}
using Size = Point;
class Matrix {
public:
virtual Size size() const = 0;
virtual float operator[](Point p) const = 0;
bool is_inside(Point p) const {
const auto s = size();
return
p.row >= 0 && p.row < s.row
&&
p.col >= 0 && p.col < s.col;
}
Matrix(const Matrix&) = delete;
Matrix& operator=(const Matrix&) = delete;
virtual ~Matrix() = default;
protected:
Matrix() = default;
};
class BufferMatrix : public Matrix {
vector <float> _data;
Size _size;
public:
BufferMatrix(Size size) :
_data(size.row* size.col),
_size(size) {}
virtual Size size() const override
{
return _size;
}
virtual float operator[](Point p) const override
{
return _data[p.row * _size.col + p.col];
};
float& operator[](Point p)
{
return _data[p.row * _size.col + p.col];
};
};
class SubMatrix : public Matrix {
const Matrix& _m;
Point _p;
public:
SubMatrix(const Matrix& m, Point p) : _m(m), _p(p) {}
virtual Size size() const override
{
return _m.size() - Size(1, 1);
}
virtual float operator[](Point p) const override
{
if (p.row >= _p.row)
++p.row;
if (p.col >= _p.col)
++p.col;
return _m[p];
}
};
pair <Point, Point> my_find(const Matrix& m) {
Point start(0, 0);
Point step(0, 1);
int zeros = 0;
const auto s = m.size();
for (int i = 0; i < s.row; ++i) {
int count = 0;
for (int j = 0; j < s.col; ++j) {
if (m[{i, j}] == 0)
++count;
}
if (count > zeros) {
start = Point(i, 0);
zeros = count;
}
}
for (int j = 0; j < s.col; ++j) {
int count = 0;
for (int i = 0; i < s.row; ++i) {
if (m[{i, j}] == 0)
++count;
}
if (count > zeros) {
start = Point(0, j);
step = Point(1, 0);
zeros = count;
}
}
return { start,step };
}
float det(const Matrix& m) {
const auto s = m.size();
if (s.row != s.col)
throw exception("row!=col");
if (s.row == 2)
return m[{0, 0}] * m[{1, 1}] - m[{0, 1}] * m[{1, 0}];
float sum = 0;
const auto start_step = my_find(m);
const auto start = start_step.first;
const auto step = start_step.second;
for (auto p = start; m.is_inside(p); p += step) {
if (m[p] != 0) {
SubMatrix sm(m, p);
sum += pow(-1, p.row + p.col) * m[p] * det(sm);
}
}
return sum;
}
int main() {
BufferMatrix m({ 2,2 });
// 2 1
// 5 4
m[{0, 0}] = 2;
m[{0, 1}] = 1;
m[{1, 0}] = 5;
m[{1, 1}] = 4;
cout << det(m) << endl;
}