This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTMatrix.cpp
159 lines (132 loc) · 2.88 KB
/
TMatrix.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
#include "TMatrix.h"
TMatrix::TMatrix(const unsigned int n = 1) : TVector<TVector<MyType>>(n)
{
for (int i = 0; i < n; i++)
{
val[i].SetSize(n);
for (int j = 0; j < n; j++)
{
val[i][j] = 0;
}
}
}
TMatrix::TMatrix(const unsigned int n, const unsigned int m) : TVector<TVector<MyType>>(n)
{
for (int i = 0; i < n; i++)
{
val[i].SetSize(m);
for (int j = 0; j < m; j++)
{
val[i][j] = 0;
}
}
}
TMatrix::TMatrix(const TVector<TVector<MyType>>& vector) : TVector<TVector<MyType>>(vector.GetSize()) {}
const unsigned int& TMatrix::GetSizeLines() const
{
return size;
}
const unsigned int& TMatrix::GetSizeColumns() const
{
return val[0].GetSize();
}
TMatrix TMatrix::operator+(const TMatrix& matrix)
{
return TVector<TVector<MyType>>::operator+(matrix);
}
TMatrix TMatrix::operator-(const TMatrix& matrix)
{
return TVector<TVector<MyType>>::operator-(matrix);
}
TMatrix TMatrix::operator*(TMatrix& matrix)
{
TMatrix result(size, matrix.val[0].GetSize());
if (this->GetSizeColumns() != matrix.size)
{
throw std::exception("TMatrix operator*(const TMatrix& matrix) failure. Size mismatch");
}
try
{
for (int i = 0; i < result.GetSizeColumns(); i++)
{
for (int j = 0; j < result.size; j++)
{
for (int k = 0; k < this->GetSizeColumns(); k++)
{
result.val[i][j] += val[i][k] * matrix.val[k][j];
}
}
}
}
catch (...)
{
throw std::exception("TMatrix operator*(const TMatrix& matrix) failure.");
}
return result;
}
TMatrix TMatrix::operator*(const double scalar)
{
return TVector<TVector<MyType>>::operator*(scalar);
}
TMatrix TMatrix::operator/(const double scalar)
{
return TVector<TVector<MyType>>::operator/(scalar);
}
TMatrix& TMatrix::operator+=(const TMatrix& matrix)
{
TVector<TVector<MyType>>::operator+=(matrix);
return *this;
}
TMatrix& TMatrix::operator-=(const TMatrix& matrix)
{
TVector<TVector<MyType>>::operator-=(matrix);
return *this;
}
TVector<MyType>& TMatrix::operator[](int index)
{
return val[index];
}
TVector<MyType>& TMatrix::operator[](int index) const
{
return val[index];
}
bool TMatrix::operator==(const TMatrix& matrix) const
{
return TVector<TVector<MyType>>::operator==(matrix);
}
bool TMatrix::operator!=(const TMatrix& matrix) const
{
return TVector<TVector<MyType>>::operator!=(matrix);
}
std::ostream& operator<<(std::ostream& os, const TMatrix& matrix)
{
for (int i = 0; i < matrix.size; i++)
{
try
{
os << matrix.val[i] << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what();
throw std::exception("TMatrix std::ostream& operator<< failure.");
}
}
return os;
}
std::istream& operator>>(std::istream& os, TMatrix& matrix)
{
try
{
for (int i = 0; i < matrix.size && os.good(); i++)
{
os >> matrix.val[i];
}
}
catch (const std::exception& e)
{
std::cout << e.what();
throw std::exception("TMatrix std::istream& operator>> failure.");
}
return os;
}