-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathndarray.h
More file actions
157 lines (136 loc) · 4.02 KB
/
ndarray.h
File metadata and controls
157 lines (136 loc) · 4.02 KB
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
#pragma once
#include <numeric>
#include <vector>
template<typename T>
T product(const std::vector<T>& numbers) {
return std::accumulate(numbers.cbegin(), numbers.cend(), (T) 1, std::multiplies<T>());
}
using index_item_t = int64_t;
using index_t = std::vector<index_item_t>;
/**
* multidimensional array
* @tparam T element type
*/
template<typename T>
class NDArray {
public:
// constructors
explicit NDArray(const std::vector<size_t>& shape);
NDArray(const std::vector<size_t>& shape, const index_t& offsets);
NDArray(const std::vector<size_t>& shape, const index_t& offsets, const std::vector<T>& data);
// access data
[[nodiscard]]
auto& at(const index_t& index) {
if (!isValidIndex(index)) {
throw std::runtime_error("Invalid index.");
}
size_t internal = 0;
for (size_t i = 0; i < _shape.size(); i++) {
internal += size_t((index[i] - _offsets[i]) * _strides[i]);
}
return _data.at(internal);
}
[[nodiscard]]
const auto& at(const index_t& index) const {
if (!isValidIndex(index)) {
throw std::runtime_error("Invalid index.");
}
size_t internal = 0;
for (size_t i = 0; i < _shape.size(); i++) {
internal += size_t((index[i] - _offsets[i]) * _strides[i]);
}
return _data.at(internal);
}
// set data
void set(const index_t& index, const T& value) {
if (!isValidIndex(index)) {
throw std::runtime_error("Invalid index.");
}
size_t internal = 0;
for (size_t i = 0; i < _shape.size(); i++) {
internal += size_t((index[i] - _offsets[i]) * _strides[i]);
}
_data[internal] = value;
}
[[nodiscard]]
bool isValidIndex(const index_t& index) const {
for (size_t i = 0; i < _shape.size(); i++) {
if ((index[i] < _offsets[i]) || (index[i] >= _shape[i] + _offsets[i])) {
return false;
}
}
return true;
}
// getters
[[nodiscard]]
const std::vector<size_t>& shape() const {
return _shape;
}
[[nodiscard]]
const index_t& offsets() const {
return _offsets;
}
// total amount of elements in array
[[nodiscard]]
size_t size() const {
return product(_shape);
}
// number of dimensions
[[nodiscard]]
size_t ndim() const {
return _shape.size();
}
// sum of all elements
[[nodiscard]]
T sum() const {
return std::accumulate(_data.cbegin(), _data.cend(), 0, std::plus<T>());
}
NDArray<T>& operator=(const T& val) {
_data = std::vector(size(), val);
}
private:
std::vector<T> _data;
std::vector<size_t> _shape;
std::vector<size_t> _strides;
index_t _offsets;
void _calculateStrides();
};
template<typename T>
NDArray<T>::NDArray(const std::vector<size_t>& shape): _shape(shape),
_data(product(shape)), _offsets(shape.size(), 0) {
_calculateStrides();
}
template<typename T>
NDArray<T>::NDArray(const std::vector<size_t>& shape, const index_t& offsets) {
if (offsets.size() != shape.size()) {
throw std::runtime_error("Invalid offset size.");
}
_shape = shape;
_offsets = offsets;
_data = std::vector<T>(product(shape));
_calculateStrides();
}
template<typename T>
NDArray<T>::NDArray(const std::vector<size_t>& shape, const index_t& offsets, const std::vector<T>& data) {
if (data.size() != product(shape)) {
throw std::runtime_error("Invalid shape.");
}
if (shape.size() != offsets.size()) {
throw std::runtime_error("Invalid offsets.");
}
_shape = shape;
_data = data;
_offsets = offsets;
_calculateStrides();
}
template<typename T>
void NDArray<T>::_calculateStrides() {
_strides = std::vector<size_t>(ndim());
(*_strides.rbegin()) = 1;
if (ndim() == 1) {
return;
}
for (int i = _strides.size() - 1; i >= 0; i--) {
_strides[i] = _strides[i + 1] * _shape[i + 1];
}
}