回答編集履歴
1
追記
test
CHANGED
@@ -1 +1,105 @@
|
|
1
1
|
`std::valarray<double>` を sliceする方が万倍楽ちゃいますか?
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
[追記]
|
6
|
+
|
7
|
+
columnを返すのやってみた:
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
```C++
|
12
|
+
|
13
|
+
#include <iostream>
|
14
|
+
|
15
|
+
#include <algorithm>
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
template<typename T>
|
20
|
+
|
21
|
+
class column {
|
22
|
+
|
23
|
+
public:
|
24
|
+
|
25
|
+
column(T* data, size_t cols) : data_(data), cols_(cols) {}
|
26
|
+
|
27
|
+
T& operator[](size_t row) { return data_[cols_*row]; }
|
28
|
+
|
29
|
+
private:
|
30
|
+
|
31
|
+
T* data_;
|
32
|
+
|
33
|
+
size_t cols_;
|
34
|
+
|
35
|
+
};
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
template<typename T>
|
40
|
+
|
41
|
+
class matrix {
|
42
|
+
|
43
|
+
public:
|
44
|
+
|
45
|
+
matrix(size_t rows, size_t cols) : cols_(cols), rows_(rows) {
|
46
|
+
|
47
|
+
data_ = new T[rows*cols];
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
~matrix() { delete[] data_; }
|
52
|
+
|
53
|
+
void fill(const T& val) { std::fill_n(data_, rows_*cols_, val); }
|
54
|
+
|
55
|
+
column<T> operator()(size_t col) { return column<T>(data_+col, cols_); }
|
56
|
+
|
57
|
+
T* operator[](size_t row) { return data_+cols_*row; }
|
58
|
+
|
59
|
+
private:
|
60
|
+
|
61
|
+
T* data_;
|
62
|
+
|
63
|
+
size_t cols_;
|
64
|
+
|
65
|
+
size_t rows_;
|
66
|
+
|
67
|
+
};
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
// おためし
|
72
|
+
|
73
|
+
int main() {
|
74
|
+
|
75
|
+
using namespace std;
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
matrix<double> mtx(2,3);
|
80
|
+
|
81
|
+
mtx.fill(0.0);
|
82
|
+
|
83
|
+
column<double> clm = mtx(1);
|
84
|
+
|
85
|
+
clm[0] = 1.23;
|
86
|
+
|
87
|
+
clm[1] = 2.34;
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
for ( size_t r = 0; r < 2; ++r ) {
|
92
|
+
|
93
|
+
for ( size_t c = 0; c < 3; ++c ) {
|
94
|
+
|
95
|
+
cout << mtx[r][c] << ' ';
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
cout << endl;
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
```
|