-
Notifications
You must be signed in to change notification settings - Fork 2
/
matrix.hpp
99 lines (87 loc) · 2.26 KB
/
matrix.hpp
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
#include <algorithm>
#include <valarray>
#include <vector>
template <class element_type>
struct MatrixVA {
MatrixVA(const size_t n):m_size(n) {
m_storage.resize(n*n);
}
MatrixVA(const size_t n, const element_type val):m_size(n) {
m_storage.resize(n*n);
m_storage = val;
}
// Accurate copy constructor
MatrixVA(const size_t n, const MatrixVA& from):m_size(n) {
m_storage.resize(n*n);
const size_t from_size = from.m_size;
if (n == from_size) {
m_storage = from.m_storage;
}
else {
const int to = std::min(n, from_size);
for (int i = 0; i < to; i++)
for (int j = 0; j < to; j++)
m_storage[i*n + j] = from.m_storage[i*from_size + j];
}
}
MatrixVA<element_type>& operator+=(const MatrixVA<element_type> &rhs)
{
this->m_storage += rhs.m_storage;
return *this;
}
MatrixVA<element_type> operator+(const MatrixVA<element_type> &other)
{
return MatrixVA(*this) += other;
}
MatrixVA<element_type>& operator-=(const MatrixVA<element_type> &rhs)
{
this->m_storage -= rhs.m_storage;
return *this;
}
MatrixVA<element_type> operator-(const MatrixVA<element_type> &other)
{
return MatrixVA(*this) -= other;
}
MatrixVA<element_type> operator*(const MatrixVA<element_type> &other)
{
int i, k, j;
const int n = this->m_size;
MatrixVA res(n);
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
const element_type aink = m_storage[i*n + k];
for (j = 0; j < n; j++)
res(i,j) += aink * other(k,j);
}
}
return res;
}
element_type& operator()(const int row, const int col)
{
return m_storage[row * m_size + col];
}
const element_type& operator()(const int row, const int col) const
{
return m_storage[row * m_size + col];
}
std::valarray<element_type> m_storage;
size_t m_size;
};
template <class element_type>
class MatrixVect {
public:
MatrixVect(const size_t n):m_size(n) {
m_storage.resize(n*n);
}
MatrixVect(const size_t n, const element_type val):m_size(n) {
m_storage.resize(n*n);
std::fill(m_storage.begin(), m_storage.end(), val);
}
element_type& operator()(const int row, const int col)
{
return m_storage[row * m_size + col];
}
private:
std::vector<element_type> m_storage;
size_t m_size;
};