-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.hpp
121 lines (99 loc) · 2.2 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "coordinate_maps.hpp"
#include <vector>
#include <algorithm>
#include <cstddef>
using std::size_t;
using std::vector;
using std::copy;
template<typename coord_map, typename val_t=double>
struct matrix{
unsigned int x_size, y_size;
coord_map cmap;
vector<val_t> data;
matrix(unsigned int x, unsigned int y): x_size(x), y_size(y), cmap(x,y){
data.resize(cmap.max_index());
}
//template<typename ot>
matrix(vector<vector<val_t>> contents): x_size(contents[0].size()), y_size(contents.size()), cmap(x_size,y_size){
data.resize(cmap.max_index());
for(int y=0;y<y_size;y++){
for(int x=0;x<x_size;x++){
data[cmap(x,y)]=contents[y][x];
}
}
}
val_t& operator()(size_t x, size_t y){
unsigned int i=cmap(x,y);
return data[i];
}
size_t size(){
return data.size();
}
size_t get_x_size(){
return x_size;
}
size_t get_y_size(){
return y_size;
}
template<typename other_t>
//should this be pass by reference for rhs?
matrix& operator=(other_t rhs){
vector<val_t> tdata(rhs.size());
size_t tx_size=rhs.get_x_size();
size_t ty_size=rhs.get_y_size();
coord_map tmap(tx_size,ty_size);
for(int x=0;x<x_size;x++){
for(int y=0;y<y_size;y++){
tdata[tmap(x,y)]=rhs(x,y);
}
}
data=std::move(tdata);
cmap=std::move(tmap);
x_size=tx_size;
y_size=ty_size;
return *this;
}
};
template<typename left_t, typename right_t>
struct mat_add{
left_t lhs;
right_t rhs;
mat_add(left_t lhs, right_t rhs): lhs(lhs), rhs(rhs) {}
double operator()(size_t x, size_t y){
return lhs(x,y)+rhs(x,y);
}
size_t size(){
return lhs.size();
}
size_t get_x_size(){
return lhs.get_x_size();
}
size_t get_y_size(){
return lhs.get_y_size();
}
};
template<typename lhs_t, typename rhs_t>
mat_add<lhs_t, rhs_t> operator+(lhs_t l, rhs_t r){
return mat_add<lhs_t, rhs_t>(l, r);
}
template<typename mat_t>
struct mat_transpose{
mat_t mat;
mat_transpose(mat_t m): mat(m) {}
double operator()(size_t x, size_t y){
return mat(y,x);
}
size_t size(){
return mat.size();
}
size_t get_x_size(){
return mat.get_x_size();
}
size_t get_y_size(){
return mat.get_y_size();
}
};
template<typename mat_t>
mat_transpose<mat_t> transpose(mat_t m){
return mat_transpose<mat_t>(m);
}