14#include <initializer_list>
31 explicit Arr(
size_t n) : _data(n, 0.0), _rows(n) {}
33 Arr(
size_t r,
size_t c) : _data(r * c, 0.0), _rows(r), _cols(c) {}
35 Arr(
size_t r,
size_t c,
double val) : _data(r * c, val), _rows(r), _cols(c) {}
39 Arr(std::initializer_list<double> il) : _data(il), _rows(il.
size()) {}
41 explicit Arr(std::vector<double> v) : _data(std::move(v)), _rows(_data.
size()) {}
46 const double&
operator()(
size_t i)
const {
return _data[i]; }
48 double&
operator()(
size_t i,
size_t j) {
return _data[i * _cols + j]; }
50 const double&
operator()(
size_t i,
size_t j)
const {
return _data[i * _cols + j]; }
54 const double&
operator[](
size_t i)
const {
return _data[i]; }
57 size_t size()
const {
return _data.size(); }
59 size_t rows()
const {
return _rows; }
61 size_t cols()
const {
return _cols; }
63 bool is_2d()
const {
return _cols > 0; }
65 double*
data() {
return _data.data(); }
67 const double*
data()
const {
return _data.data(); }
69 auto begin() {
return _data.begin(); }
71 auto begin()
const {
return _data.begin(); }
73 auto end() {
return _data.end(); }
75 auto end()
const {
return _data.end(); }
79 for (
auto& v : _data) v += s;
84 for (
auto& v : _data) v -= s;
89 for (
auto& v : _data) v *= s;
94 assert(_data.size() == other._data.size());
95 for (
size_t i = 0; i < _data.size(); ++i) _data[i] += other._data[i];
100 assert(_data.size() == other._data.size());
101 for (
size_t i = 0; i < _data.size(); ++i) _data[i] -= other._data[i];
106 std::vector<double> _data;
129 static constexpr size_t ALL = SIZE_MAX;
145 auto rs = rows.
start;
147 auto cs = cols.
start;
149 auto rstep = rows.
step;
150 auto cstep = cols.
step;
151 size_t out_r = (re - rs + rstep - 1) / rstep;
152 size_t out_c = (ce - cs + cstep - 1) / cstep;
153 Arr out(out_r, out_c);
154 for (
size_t i = 0; i < out_r; ++i)
155 for (
size_t j = 0; j < out_c; ++j) out(i, j) = a(rs + i * rstep, cs + j * cstep);
167 auto step = rows.
step;
168 size_t n = (e - s + step - 1) / step;
170 for (
size_t i = 0; i < n; ++i) out(i) = a(s + i * step);
187inline Arr ones(
size_t r,
size_t c) {
return Arr(r, c, 1.0); }
201 if (n == 0)
return out;
206 double step = (end - start) /
static_cast<double>(n - 1);
207 for (
size_t i = 0; i < n; ++i) out(i) = start + step * static_cast<double>(i);
214 size_t n = (end > start) ?
static_cast<size_t>(end - start) : 0;
216 for (
size_t i = 0; i < n; ++i) out(i) = start + static_cast<double>(i);
231 for (
size_t i = 0; i < a.
size(); ++i) o(i) = std::cos(a(i));
237 for (
size_t i = 0; i < a.
size(); ++i) o(i) = std::log(a(i));
243 for (
size_t i = 0; i < a.
size(); ++i) o(i) = std::abs(a(i));
249 for (
size_t i = 0; i < a.
size(); ++i) o(i) = std::exp(a(i));
255 for (
size_t i = 0; i < a.
size(); ++i) o(i) = std::sqrt(a(i));
260inline double sum(
const Arr& a) {
return std::accumulate(a.
begin(), a.
end(), 0.0); }
268inline std::vector<Arr>
where(
const Arr& condition) {
269 assert(!condition.
is_2d());
270 std::vector<size_t> idx;
271 for (
size_t i = 0; i < condition.
size(); ++i)
272 if (condition(i) != 0.0) idx.push_back(i);
273 Arr indices(idx.size());
274 for (
size_t i = 0; i < idx.size(); ++i) indices(i) =
static_cast<double>(idx[i]);
292 for (
size_t i = 0; i < A.
rows(); ++i) {
294 for (
size_t j = 0; j < A.
cols(); ++j) s += A(i, j) * x(j);
311 for (
size_t i = 0; i < u.
size(); ++i)
312 for (
size_t j = 0; j < v.
size(); ++j) out(i, j) = u(i) * v(j);
326 size_t ca = a.
cols();
327 size_t cb = b.
cols();
329 for (
size_t i = 0; i < m; ++i) {
330 for (
size_t j = 0; j < ca; ++j) out(i, j) = a(i, j);
331 for (
size_t j = 0; j < cb; ++j) out(i, ca + j) = b(i, j);
342 for (
size_t i = 0; i < a.
size(); ++i) o(i) = -a(i);
349 for (
size_t i = 0; i < a.
size(); ++i) o(i) = a(i) + b(i);
356 for (
size_t i = 0; i < a.
size(); ++i) o(i) = a(i) - b(i);
363 for (
size_t i = 0; i < a.
size(); ++i) o(i) = a(i) * b(i);
377 for (
size_t i = 0; i < a.
size(); ++i) o(i) = (a(i) <= s) ? 1.0 : 0.0;
384 for (
size_t i = 0; i < a.
size(); ++i) o(i) = (a(i) >= s) ? 1.0 : 0.0;
391 for (
size_t i = 0; i < a.
size(); ++i) o(i) = (a(i) < s) ? 1.0 : 0.0;
398 for (
size_t i = 0; i < a.
size(); ++i) o(i) = (a(i) > s) ? 1.0 : 0.0;
Arr ones(size_t r, size_t c)
Create 2D array of ones with r rows, c columns.
Definition arr.hpp:187
Arr dot(const Arr &A, const Arr &x)
Matrix-vector multiplication.
Definition arr.hpp:289
Arr cos(const Arr &a)
Element-wise cosine.
Definition arr.hpp:229
Arr zeros(size_t n)
Create zero-initialized 1D array of size n.
Definition arr.hpp:180
Arr operator<=(const Arr &a, double s)
Element-wise less-than-or-equal-to comparison with scalar.
Definition arr.hpp:375
Arr abs(const Arr &a)
Element-wise absolute value.
Definition arr.hpp:241
Arr operator>(const Arr &a, double s)
Element-wise greater-than comparison with scalar.
Definition arr.hpp:396
std::vector< Arr > where(const Arr &condition)
Find indices of non-zero elements.
Definition arr.hpp:268
constexpr size_t ALL
Convenience alias for Range::ALL.
Definition arr.hpp:133
double sum(const Arr &a)
Sum of all elements.
Definition arr.hpp:260
Arr arange(double start, double end)
Values from start to end-1 with step 1.
Definition arr.hpp:213
Arr operator-(const Arr &a)
Unary negation (element-wise)
Definition arr.hpp:340
Arr linspace(double start, double end, size_t n)
Linearly spaced values from start to end, inclusive.
Definition arr.hpp:199
Arr concatenate(const Arr &a, const Arr &b, int=1)
Concatenate two 2D arrays along columns (axis=1)
Definition arr.hpp:323
Arr operator*(const Arr &a, const Arr &b)
Element-wise multiplication of two arrays.
Definition arr.hpp:360
Arr make_same_shape(const Arr &a)
Create a zero-initialized Arr with the same shape as input.
Definition arr.hpp:221
Arr outer(const Arr &u, const Arr &v)
Outer product of two 1D vectors.
Definition arr.hpp:308
Arr exp(const Arr &a)
Element-wise exponential.
Definition arr.hpp:247
Arr eval(Arr a)
Identity function for Arr (for API compatibility)
Definition arr.hpp:403
Arr operator/(const Arr &a, double s)
Scalar division (array / scalar)
Definition arr.hpp:371
Arr operator<(const Arr &a, double s)
Element-wise less-than comparison with scalar.
Definition arr.hpp:389
Arr view(const Arr &a, const Range &rows, const Range &cols)
Extract a submatrix view from a 2D array using Range for rows and cols.
Definition arr.hpp:143
Arr operator+(const Arr &a, const Arr &b)
Element-wise addition of two arrays.
Definition arr.hpp:346
Arr sqrt(const Arr &a)
Element-wise square root.
Definition arr.hpp:253
Arr log(const Arr &a)
Element-wise natural logarithm.
Definition arr.hpp:235
Arr operator>=(const Arr &a, double s)
Element-wise greater-than-or-equal-to comparison with scalar.
Definition arr.hpp:382
1D or 2D array backed by std::vector<double> for small optimization problems
Definition arr.hpp:23
double * data()
Raw pointer to underlying data (mutable)
Definition arr.hpp:65
Arr(size_t n)
Construct 1D array of size n, zero-initialized.
Definition arr.hpp:31
Arr(std::initializer_list< double > il)
Construct from initializer list (1D)
Definition arr.hpp:39
bool is_2d() const
Whether this is a 2D array (cols > 0)
Definition arr.hpp:63
size_t cols() const
Number of columns (0 for 1D arrays)
Definition arr.hpp:61
const double & operator[](size_t i) const
1D element access (const)
Definition arr.hpp:54
Arr(size_t r, size_t c)
Construct 2D array with r rows, c columns, zero-initialized.
Definition arr.hpp:33
auto end() const
Iterator past the end (const)
Definition arr.hpp:75
const double & operator()(size_t i, size_t j) const
2D element access (const), row-major
Definition arr.hpp:50
auto end()
Iterator past the end (mutable)
Definition arr.hpp:73
Arr & operator-=(double s)
Subtract scalar from all elements.
Definition arr.hpp:83
Arr()=default
Default constructor (empty array)
const double * data() const
Raw pointer to underlying data (const)
Definition arr.hpp:67
double & operator()(size_t i)
1D element access (mutable)
Definition arr.hpp:44
size_t rows() const
Number of rows (1 for 1D arrays)
Definition arr.hpp:59
double & operator[](size_t i)
1D element access (mutable)
Definition arr.hpp:52
Arr(std::vector< double > v)
Construct from vector (1D)
Definition arr.hpp:41
auto begin()
Iterator to beginning (mutable)
Definition arr.hpp:69
const double & operator()(size_t i) const
1D element access (const)
Definition arr.hpp:46
double value_type
Definition arr.hpp:25
auto begin() const
Iterator to beginning (const)
Definition arr.hpp:71
Arr & operator*=(double s)
Multiply all elements by scalar.
Definition arr.hpp:88
Arr & operator-=(const Arr &other)
Element-wise subtraction with another Arr.
Definition arr.hpp:99
Arr(size_t r, size_t c, double val)
Construct 2D array with r rows, c columns, fill value val.
Definition arr.hpp:35
double & operator()(size_t i, size_t j)
2D element access (mutable), row-major
Definition arr.hpp:48
Arr & operator+=(double s)
Add scalar to all elements.
Definition arr.hpp:78
size_t size() const
Total number of elements.
Definition arr.hpp:57
Arr & operator+=(const Arr &other)
Element-wise addition with another Arr.
Definition arr.hpp:93
Index range [start, end) with step for slicing views.
Definition arr.hpp:115
size_t end
End index (exclusive)
Definition arr.hpp:117
size_t step
Step size.
Definition arr.hpp:118
Range()=default
Default range (empty)
static constexpr size_t ALL
Sentinel value meaning "all elements".
Definition arr.hpp:129
size_t start
Start index (inclusive)
Definition arr.hpp:116
Range(size_t start, size_t end, size_t step)
Range [start, end) with given step.
Definition arr.hpp:127
Range(size_t start, size_t end)
Range [start, end)
Definition arr.hpp:125
Range(size_t end)
Range from 0 to end.
Definition arr.hpp:123