-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
228 lines (196 loc) · 6.45 KB
/
Matrix.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "Matrix.h"
Matrix::Matrix(std::initializer_list<Vector> columns) : SizeCols(static_cast<int>(columns.size())), SizeRows(0) {
if (SizeCols > 0) {
SizeRows = columns.begin()->GetSize();
matrix.reserve(SizeCols);
for (const auto& col : columns) {
if (col.GetSize() != SizeRows) {
throw std::invalid_argument("All columns must have the same size");
}
matrix.push_back(col);
}
} else {
throw std::invalid_argument("Matrix initializer list cannot be empty");
}
}
Matrix::Matrix(int row, int col) : SizeRows(row), SizeCols(col) {
if (row <= 0 || col <= 0) {
throw std::invalid_argument("Row and column size must be positive integers");
}
matrix.resize(row, Vector(col));
}
Matrix::Matrix(const Matrix& other) : SizeRows(other.SizeRows), SizeCols(other.SizeCols) {}
int Matrix::GetRowSize() const {
return SizeRows;
}
int Matrix::GetColSize() const {
return SizeCols;
}
void Matrix::SetElement(int row, int col, Rational n) {
if (row <= 0 || row > GetRowSize() || col <= 0 || col > GetColSize()) {
throw std::out_of_range("Invalid row or column index");
}
matrix[col - 1].SetElement(row, n);
}
void Matrix::SetRowVector(int row, Vector v) {
if (row <= 0 || row > GetRowSize() || v.GetSize() != GetColSize()) {
throw std::invalid_argument("Invalid row or column size");
}
for (int i = 1; i <= GetColSize(); ++i) {
SetElement(row, i, v.GetElement(i));
}
}
void Matrix::SetColVector(int col, Vector v) {
if (col <= 0 || col > GetColSize() || v.GetSize() != GetRowSize()) {
throw std::invalid_argument("Invalid row or column size");
}
matrix[col - 1] = v;
}
Rational Matrix::GetElement(int row, int col) const{
if (row < 1 || row > GetRowSize() || col < 1 || col > GetColSize()) {
throw std::out_of_range("Invalid row or column index");
}
return GetRowVector(row).GetElement(col);
}
Vector Matrix::GetRowVector(int row) const{
if (row < 1 || row > GetRowSize()){
throw std::out_of_range("Invalid row number");
}
return matrix[row-1];
}
Vector Matrix::GetColVector(int col) const{
if (col < 1 || col > GetColSize()){
throw std::out_of_range("Invalid column number");
}
Vector v(GetRowSize());
for(int i=1;i<=GetRowSize();++i){
v.SetElement(i, GetElement(i, col));
}
return v;
}
Matrix Matrix::Transpose() const{
Matrix result(GetColSize(), GetRowSize());
for (int i=1;i<=GetColSize();++i){
for (int j=1;j<=GetRowSize();++j){
result.SetElement(i, j, GetElement(j, i));
}
}
return result;
}
bool Matrix::IsSquare() const{ return GetRowSize() == GetColSize();}
bool Matrix::IsSymmetric() const{
return *this == Transpose();
}
bool Matrix::IsSkewSymmetric() const{
return *this * -1 == Transpose();
}
bool Matrix::IsUpperTriangular() const{
for(int i=1;i<=GetRowSize();++i){
for(int j=1;j<=GetColSize();++j){
if (i > j && GetElement(i, j) != Rational()) return false;
}
}
return true;
}
bool Matrix::IsLowerTriangular() const{
for(int i=1;i<=GetRowSize();++i){
for(int j=1;j<=GetColSize();++j){
if (i < j && GetElement(i, j) != Rational()) return false;
}
}
return true;
}
bool Matrix::IsTriangular() const { return (IsUpperTriangular() || IsLowerTriangular()); }
bool Matrix::IsDiagonal() const{return (IsUpperTriangular() && IsLowerTriangular());}
Matrix Matrix::operator+(const Matrix& b) const {
int rows = GetRowSize();
int cols = GetColSize();
if (rows != b.GetRowSize() || cols != b.GetColSize()) {
throw std::invalid_argument("ERROR: Matrices must have the same dimensions for addition");
}
Matrix result(rows, cols);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
result.SetElement(i, j, GetElement(i, j) + b.GetElement(i, j));
}
}
return result;
}
Matrix Matrix::operator-(const Matrix& b) const {
int rows = GetRowSize();
int cols = GetColSize();
if (rows != b.GetRowSize() || cols != b.GetColSize()) {
throw std::invalid_argument("ERROR: Matrices must have the same dimensions for subtraction");
}
Matrix result(rows, cols);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
result.SetElement(i, j, GetElement(i, j) - b.GetElement(i, j));
}
}
return result;
}
Matrix Matrix::operator* (const Rational& b)const{
int rows = GetRowSize();
int cols = GetColSize();
Matrix result(rows,cols);
for (int i=1;i<=rows;i++){
for (int j=1;j<=cols;j++){
result.SetElement(i,j, GetElement(i, j) * b);
}
}
return result;
}
Matrix Matrix::operator* (const int& b)const{
int rows = GetRowSize();
int cols = GetColSize();
Matrix result(rows,cols);
for (int i=1;i<=rows;i++){
for (int j=1;j<=cols;j++){
result.SetElement(i,j, GetElement(i, j) * Rational(b));
}
}
return result;
}
Matrix Matrix::operator* (const Matrix& b)const{
int rowsA = GetRowSize();
int colsA = GetColSize();
int rowsB = b.GetRowSize();
int colsB = b.GetColSize();
if (colsA != rowsB) {
throw std::runtime_error("ERROR: UNCALCULATED MATRIX PAIR");
}
Matrix result(rowsA, colsB);
for (int i = 1; i <= rowsA; i++) {
for (int j = 1; j <= colsB; j++) {
for (int k = 1; k <= colsA; k++){
result.SetElement(i,j, result.GetElement(i,j) + (GetElement(i, k) * b.GetElement(k, j)));
}
}
}
return result;
}
bool Matrix::operator==(const Matrix& b)const{
if(GetRowSize() != b.GetRowSize() || GetColSize() != b.GetColSize()) return false;
for (int i=1;i<=GetRowSize();++i){
for (int j=1;j<=GetColSize();++j){
if (GetElement(i, j) != b.GetElement(i, j)) return false;
}
}
return true;
}
bool Matrix::operator!=(const Matrix& b)const{
if(GetRowSize() != b.GetRowSize() || GetColSize() != b.GetColSize()) return true;
for (int i=1;i<=GetRowSize();++i){
for (int j=1;j<=GetColSize();++j){
if (GetElement(i, j) != b.GetElement(i, j)) return true;
}
}
return false;
}
std::ostream& operator<<(std::ostream& os, const Matrix& b) {
for (int i = 1; i <= b.GetRowSize(); ++i) {
os << b.GetRowVector(i) << std::endl;
}
return os;
}