-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrices.cpp
92 lines (82 loc) · 1.98 KB
/
matrices.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
#include "matrices.hpp"
//возвращает ближайшую справа степень двойки
unsigned int getClosestOrder(unsigned int number)
{
while(true)
{
unsigned int tmp = number;
while(true)
{
if(tmp == 1 && number >= 2)
{
return number;
}
if(tmp % 2 != 0)
{
++number;
break;
}
tmp /= 2;
}
}
}
Matrix::Matrix(int type, unsigned int columnIndex, unsigned int rowIndex)
{
_type = type;
_columnIndex = columnIndex;
_rowIndex = rowIndex;
std::vector<Complex> column;
column.resize(_rowIndex);
//первый коэф - столбец, второй - строка
for(unsigned int iter = 0; iter < _columnIndex; ++iter)
{
_coefs.push_back(column);
}
}
std::pair<unsigned int, unsigned int> Matrix::getOrder() const
{
auto tmp = std::make_pair(_columnIndex, _rowIndex);
return tmp;
}
void Matrix::setCoef(unsigned int columnPos, unsigned int rowPos, Complex newCoef)
{
if(columnPos >= _columnIndex || rowPos >= _rowIndex)
{
throw std::runtime_error("Error: position doesn't exist");
}
_coefs[columnPos][rowPos] = newCoef;
}
void Matrix::setType(int newType)
{
_type = newType;
}
void Matrix::setCorrectSize()
{
//вычисление скорректированных размеров
unsigned int correctColumn, correctRow;
correctColumn = getClosestOrder(_columnIndex);
correctRow = getClosestOrder(_rowIndex);
//внесение правок в матрицу
_columnIndex = correctColumn;
_rowIndex = correctRow;
_coefs.resize(_columnIndex);
for(unsigned int columnIter = 0; columnIter < _columnIndex; ++columnIter)
{
_coefs[columnIter].resize(_rowIndex);
}
}
Complex Matrix::getCoef(unsigned int columnPos, unsigned int rowPos) const
{
return _coefs[columnPos][rowPos];
}
void Matrix::print() const
{
for(unsigned int rowIter = 0; rowIter < _rowIndex; ++rowIter)
{
for(unsigned int columnIter = 0; columnIter < _columnIndex; ++columnIter)
{
_coefs[columnIter][rowIter].print();
}
std::cout << std::endl;
}
}