-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.h
198 lines (175 loc) · 5.31 KB
/
matrix.h
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
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <vector>
// #include <functional>
#include <exception>
using namespace std;
template<typename T>
class Matrix
{
public:
Matrix();
Matrix(unsigned str, unsigned clmn); //создание матрицы по
Matrix(vector<vector<T>> m_); //создание матрицы по вектору векторов
Matrix(unsigned str, unsigned clmn, vector<T> b); //создание матрицы по вектору
// Matrix(unsigned str, unsigned clmn, function<T(unsigned, unsigned)> f);
template <typename Func>
Matrix(unsigned str, unsigned clmn, Func f);
Matrix(const Matrix &A); //copy constructor
unsigned getStrSize() const; //size of string
unsigned getClmnSize() const; //size of collumn
Matrix<T> operator=(const Matrix<T> &other); //copy assignment
vector<T> operator[](unsigned i) const; //return i-th string
vector<T>& operator[](unsigned i);
template<typename Y> friend Matrix<Y> operator+(const Matrix<Y> &A, const Matrix<Y> &B);
template<typename Y> friend Matrix<Y> operator-(const Matrix<Y> &A, const Matrix<Y> &B);
template<typename Y> friend Matrix<Y> operator*(const Matrix<Y> &A, const Matrix<Y> &B);
Matrix<T> operator-() const;
static Matrix<T> transpos(const Matrix &m);
template<typename Y> friend ostream& operator<<(ostream &os, const Matrix<Y> A); //output
private:
vector<vector<T>> m;
};
template<typename T>
Matrix<T>::Matrix() {T t(0); vector<T> tmp; tmp.push_back(t); m.push_back(tmp);}
template<typename T>
Matrix<T>::Matrix(unsigned str, unsigned clmn)
{
T t(0);
vector<T> tmp;
for(unsigned i=0; i<clmn; ++i) tmp.push_back(t);
for(unsigned i=0; i<str; ++i) m.push_back(tmp);
}
template<typename T>
Matrix<T>::Matrix(vector<vector<T>> m_):m(m_) {}
template<typename T>
Matrix<T>::Matrix(unsigned str, unsigned clmn, vector<T> b)
{
m.resize(str);
for(unsigned i=0; i<str; ++i) m[i].resize(clmn);
unsigned k = 0;
for (unsigned i = 0; i < str; i++)
{
for (unsigned j = 0; j < clmn; j++)
{
m[i][j] = b[k];
k++;
}
}
}
template <typename T> template <typename Func>
Matrix<T>::Matrix(unsigned str, unsigned clmn, Func f)
{
m.resize(str);
for(unsigned i=0; i<str; ++i) m[i].resize(clmn);
for (unsigned i = 0; i < str; i++)
{
for (unsigned j = 0; j < clmn; j++)
{
m[i][j] = f(i,j);
}
}
}
template<typename T>
Matrix<T>::Matrix(const Matrix<T> &A):m(A.m) {}
template<typename T>
unsigned Matrix<T>::getStrSize() const {return m.empty() ? 0 : m[0].size();}
template<typename T>
unsigned Matrix<T>::getClmnSize() const {return m.size();}
template<typename T>
Matrix<T> Matrix<T>::operator=(const Matrix<T> &other)
{
m=other.m;
return *this;
}
template<typename T>
vector<T> Matrix<T>::operator[](unsigned i) const {return m[i];}
template<typename T>
vector<T>& Matrix<T>::operator[](unsigned i) {return m[i];}
template<typename T>
Matrix<T> operator+(const Matrix<T> &A, const Matrix<T> &B)
{
unsigned ass(A.m.size()), acs(A.m[0].size()), bss(B.m.size()), bcs(B.m[0].size());
unsigned str=std::max(ass,bss), clmn=std::max(acs,bcs);
Matrix<T> res(str,clmn);
for(unsigned i=0; i<str; ++i)
{
for(unsigned j=0; j<clmn; ++j)
{
if(i<ass && j<acs) res.m[i][j]=res.m[i][j]+A.m[i][j];
if(i<bss && j<bcs) res.m[i][j]=res.m[i][j]+B.m[i][j];
}
}
return res;
}
template<typename T>
Matrix<T> operator-(const Matrix<T> &A, const Matrix<T> &B)
{
unsigned ass(A.m.size()), acs(A.m[0].size()), bss(B.m.size()), bcs(B.m[0].size());
unsigned str=std::max(ass,bss), clmn=std::max(acs,bcs);
Matrix<T> res(str,clmn);
for(unsigned i=0; i<str; ++i)
{
for(unsigned j=0; j<clmn; ++j)
{
if(i<ass && j<acs) res.m[i][j]=res.m[i][j]+A.m[i][j];
if(i<bss && j<bcs) res.m[i][j]=res.m[i][j]-B.m[i][j];
}
}
return res;
}
template<typename T>
Matrix<T> Matrix<T>::transpos(const Matrix &m)
{
unsigned str=m.getStrSize(), clmn=m.getClmnSize();
Matrix<T> res(str, clmn);
for(unsigned i=0; i<str; ++i)
{
for(unsigned j=0; j<clmn; ++j)
{
res[i][j]=m[j][i];
}
}
return res;
}
template<typename T>
Matrix<T> operator*(const Matrix<T> &A, const Matrix<T> &B)
{
unsigned ass(A.m.size()), acs(A.m[0].size()), bss(B.m.size()), bcs(B.m[0].size());
//ass - A string size, bss - B string size
if(acs!=bss) throw exception();
Matrix<T> res(ass,bcs);
for(unsigned i=0; i<ass; ++i)
{
for(unsigned j=0; j<bcs; ++j)
{
for(unsigned k=0; k<bss; ++k)
{
res.m[i][j]=res.m[i][j]+A.m[i][k]*B.m[k][j];
}
}
}
return res;
}
template<typename T>
Matrix<T> Matrix<T>::operator -() const
{
auto f = [this](unsigned i, unsigned j)->T{return -this->m[i][j];};
return Matrix<T>(getClmnSize(), getStrSize(), f);
}
template<typename T>
ostream& operator<<(ostream &os, const Matrix<T> &m)
{
for(unsigned i=0; i<m.getStrSize(); ++i)
{
for(unsigned j=0; j<m.getClmnSize(); ++j)
{
os<<m[i][j];
if(j!=m.getClmnSize()-1) os<<" ";
}
os<<"\n";
}
return os;
}
#endif // MATRIX_H