-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
128 lines (105 loc) · 2.4 KB
/
Matrix.cpp
File metadata and controls
128 lines (105 loc) · 2.4 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
#include "Matrix.h"
Matrix::Matrix() : N(10), M(10)
{
setSize();
}
Matrix::Matrix(size_t rows, size_t cols) : N(rows), M(cols)
{
setSize();
}
size_t Matrix::getN() const
{
return N;
}
size_t Matrix::getM() const
{
return M;
}
void Matrix::setSize()
{
field.resize(M);
for (size_t i = 0; i < M; i++)
field[i].resize(N);
}
void Matrix::setValue(size_t x, size_t y, double value)
{
field[x][y] = value;
}
double Matrix::getValue(size_t x, size_t y) const
{
return field[x][y];
}
double Matrix::max()
{
double c = 0.0;
for (size_t i = 0; i < N; i++)
for (size_t j = 0; j < M; j++)
if (abs(this->getValue(i,j)) > c)
c = abs(this->getValue(i, j));
return c;
}
void Matrix::swapRows(size_t k, size_t l)
{
double temp;
for (size_t j = 0; j < M; j++) {
temp = field[k][j];
field[k][j] = field[l][j];
field[l][j] = temp;
}
}
std::ostream & operator<<(std::ostream & os, const Matrix & m)
{
for (size_t i = 0; i < m.N; i++) {
for (size_t j = 0; j < m.M; j++)
os << m.field[i][j] << " ";
os << std::endl;
}
return os;
}
void Matrix::fillNonZeroElementsPosition()
{
nonZeroElemntsPosition.resize(N);
for(size_t i = 0; i < N; i++) {
for(size_t j = 0; j < M; j++)
if(fabs(getValue(i,j)) > eps)
nonZeroElemntsPosition[i].push_back(j);
}
}
void Matrix::saveToFile(std::string filename) const
{
std::ofstream fout(filename);
for (size_t i = 0; i < N; i++) {
for (size_t j = 0; j < M; j++)
fout << field[i][j] << ' ';
fout << std::endl;
}
fout.close();
}
void Matrix::copyFromData(const Matrix &m)
{
if (this->getM() != m.getM()) {
std::cout << "Function copyFromData: sizes aren't equale" << std::endl;
return;
}
for (size_t i = 0; i < N; i++)
for (size_t j = 0; j < M; j++)
setValue(i, j, m.getValue(i,j));
}
void trans(Matrix &D1, Matrix &D2)
{
for (size_t i = 0; i < D1.getN(); i++) {
for (size_t j = 0; j < D1.getM(); j++) {
D2.setValue(i, j, D1.getValue(j, i));
}
}
}
void sqr(Matrix &D1)
{
double c = 0.0;
for(size_t i = 0; i < D1.getN(); i++)
for (size_t j = 0; j < D1.getM(); j++)
{
c = D1.getValue(i, j) * D1.getValue(i, j);
D1.setValue(i, j, c);
}
}