-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.h
More file actions
179 lines (139 loc) · 4.16 KB
/
Matrix.h
File metadata and controls
179 lines (139 loc) · 4.16 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
#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
#include <vector>
#include <ostream>
#include <assert.h>
#include "Fraction.h"
namespace L_Algebra
{
class Matrix
{
private:
std::size_t rows_num;
std::size_t cols_num;
std::vector<Fraction> data;
Fraction& at(std::size_t r, std::size_t c)
{
return data.at( r * cols_num + c );
}
const Fraction& at(std::size_t r, std::size_t c) const
{
return data.at(r * cols_num + c);
}
public:
Matrix () = default;
Matrix(std::size_t r, std::size_t c, Fraction n = 0 ) : rows_num(r), cols_num(c), data(r * c, n)
{
assert(r > 0 && c > 0);
}
Matrix(std::size_t r, std::size_t c, std::initializer_list<Fraction> values ) : rows_num(r), cols_num(c), data(values)
{
assert(r > 0 && c > 0);
assert(values.size() == size());
}
Matrix(std::initializer_list<std::initializer_list<Fraction>> values );
friend std::ostream& operator<<(std::ostream& out, const Matrix& mx);
//friend std::vector<Fraction> operator<<(std::ostream& os, std::vector<Fraction> diag);
explicit operator bool() const
{
return ! is_zero();
}
bool operator== (const Matrix& mx) const
{
return data == mx.data;
}
bool operator!= (const Matrix& mx) const
{
return !(*this == mx);
}
Matrix operator-()
{
return ( (*this) * (-1) );
}
Matrix operator+()
{
return (*this);
}
Matrix operator+(const Matrix& mx) const;
Matrix operator-(const Matrix& mx) const;
Matrix operator*(const Matrix& mx) const;
Matrix& operator+=(const Matrix& mx);
Matrix& operator-=(const Matrix& mx);
Matrix& operator*=(const Matrix& mx);
Matrix& operator*=(const Fraction& n);
friend Matrix operator*(const Matrix& mx, Fraction n);
friend Matrix operator*(Fraction n, const Matrix& mx);
Matrix operator/(const Fraction& n) const;
Fraction& operator()(std::size_t r, std::size_t c)
{
return at(r,c);
}
const Fraction& operator()(std::size_t r, std::size_t c) const
{
return at(r,c);
}
constexpr std::size_t size() const
{
return rows_num * cols_num;
}
void clear()
{
data.clear();
}
void resize(int r, int c, long long n = 0)
{
data.clear();
data.resize( r * c, n );
rows_num = r;
cols_num = c;
}
size_t rows() const
{
return rows_num;
}
size_t cols() const
{
return cols_num;
}
static Matrix Identity(int n);
static Matrix Constant(int r, int c, long long n);
bool is_square() const
{
return rows_num == cols_num;
}
bool is_identity() const;
bool is_symmetric() const;
bool is_skewSymmetric() const;
bool is_diagonal() const;
bool is_zero() const;
bool is_constant() const;
bool is_orthogonal() const;
bool is_invertible() const;
bool is_linearly_dependent() const;
bool is_linearly_independent() const;
bool is_upperTriangular() const;
bool is_lowerTriangular() const;
bool is_consistent() const;
Matrix transpose() const;
Fraction determinant() const ;
Matrix inverse() const throw();
Matrix adjoint() const;
Matrix gaussElimination() const;
Matrix gaussJordanElimination() const;
Fraction trace() const;
std::size_t rank() const;
std::vector<Fraction> main_diagonal();
std::vector<Fraction> secondary_diagonal();
friend Matrix transitionMatrix(Matrix from, Matrix to);
private:
void swapRows(int row1, int row2);
bool pivotEqualTo_one_Found(int pivot_row, int pivot_col, std::size_t& alternative_pivot_row );
bool pivotNot_zero_Found(int pivot_row, int pivot_col, std::size_t& col_dif_zero );
bool firstNumberNot_zero(int row_num, std::size_t& num_coluna_num_dif_zero);
void changePivotTo_one(int row_num, Fraction constant);
void zeroOutTheColumn(int row_num, int num_pivot_row, Fraction constant);
bool has_one_row_zero() const;
};
extern std::ostream& operator << (std::ostream& os, const std::vector<Fraction>& v);
} // L_Algebra namespace
#endif // MATRIX_H_INCLUDED