59 template <
typename T,
int N>
class Vector {
63 using iter =
typename std::array<T, N>::iterator;
64 using const_iter =
typename std::array<T, N>::const_iterator;
65 using reverse_iter =
typename std::array<T, N>::reverse_iterator;
66 using const_reverse_iter =
typename std::array<T, N>::const_reverse_iterator;
68 const static int size = N;
77 explicit Vector(T arg1) { std::fill(begin(), end(), arg1); }
84 static_assert(N == 2,
"Vector must have 2 elements");
95 static_assert(N == 3,
"Vector must have 3 elements");
108 static_assert(N == 4,
"Vector must have 4 elements");
122 Vector(T arg1, T arg2, T arg3, T arg4, T arg5) {
123 static_assert(N == 5,
"Vector must have 5 elements");
137 std::copy(arg.begin(), arg.end(), begin());
141 iter begin() noexcept {
return iter(mem.begin()); }
142 const_iter begin()
const noexcept {
return const_iter(mem.begin()); }
143 iter end() noexcept {
return iter(mem.end()); }
144 const_iter end()
const noexcept {
return const_iter(mem.end()); }
145 reverse_iter rbegin() noexcept {
return reverse_iter(end()); }
146 const_reverse_iter rbegin()
const noexcept {
147 return const_reverse_iter(end());
149 reverse_iter rend() noexcept {
return reverse_iter(begin()); }
150 const_reverse_iter rend()
const noexcept {
151 return const_reverse_iter(begin());
153 const_iter cbegin()
const noexcept {
return const_iter(mem.begin()); }
154 const_iter cend()
const noexcept {
return const_iter(mem.end()); }
155 const_reverse_iter crbegin()
const noexcept {
156 return const_reverse_iter(end());
158 const_reverse_iter crend()
const noexcept {
159 return const_reverse_iter(begin());
167 std::fill(ret.mem.begin(), ret.mem.end(), 0);
176 std::fill(ret.mem.begin(), ret.mem.end(), c);
185 const T &
operator[](
const int n)
const noexcept {
return mem[n]; }
198 return std::inner_product(begin(), end(), arg.begin(),
static_cast<T
>(0));
214 std::transform(begin(), end(), ret.begin(),
215 [](
const T &a) {
return static_cast<T2
>(a); });
221 T
squaredNorm() const noexcept {
return (*this).inner_product(*
this); }
230 T max = *std::max_element(begin(), end(), [](
const T &a,
const T &b) {
231 return std::fabs(a) < std::fabs(b);
233 return max >=
static_cast<T
>(0) ? max : -max;
239 template <
typename EXP_T>
242 std::transform(begin(), end(), ret.begin(), [&exponent](
const T &a) {
243 return static_cast<T
>(std::pow(a, exponent));
251 const double n =
norm();
252 std::transform(begin(), end(), begin(),
253 [&n](
const T &a) {
return static_cast<T
>(a / n); });
258 bool all() const noexcept {
259 return std::all_of(begin(), end(),
260 [](
const T &a) {
return static_cast<bool>(a); });
265 bool any() const noexcept {
266 return std::any_of(begin(), end(),
267 [](
const T &a) {
return static_cast<bool>(a); });
273 return std::none_of(begin(), end(),
274 [](
const T &a) {
return static_cast<bool>(a); });
279 T
minCoeff() const noexcept {
return *std::min_element(begin(), end()); }
283 T
maxCoeff() const noexcept {
return *std::max_element(begin(), end()); }
287 return std::accumulate(begin(), end(), static_cast<T>(1),
288 std::multiplies<T>());
293 return std::accumulate(begin(), end(), static_cast<T>(0));
297 T *
data() noexcept {
return mem.data(); }
298 const T *
data()
const noexcept {
return mem.data(); }
310 std::transform(begin(), end(), a.begin(), begin(), std::plus<T>());
316 std::transform(begin(), end(), a.begin(), begin(), std::minus<T>());
322 std::transform(begin(), end(), a.begin(), begin(), std::multiplies<T>());
328 std::transform(begin(), end(), a.begin(), begin(), std::divides<T>());
334 std::transform(begin(), end(), begin(),
335 std::bind(std::plus<T>(), std::placeholders::_1, k));
341 std::transform(begin(), end(), begin(),
342 std::bind(std::minus<T>(), std::placeholders::_1, k));
348 std::transform(begin(), end(), begin(),
349 std::bind(std::multiplies<T>(), std::placeholders::_1, k));
355 std::transform(begin(), end(), begin(),
356 std::bind(std::divides<T>(), std::placeholders::_1, k));
361 std::array<T, N> mem;
365 template <
typename T,
int N,
typename EXP_T>
367 return arg.pow(exponent);
371 template <
typename T,
int N>
374 std::transform(a.begin(), a.end(), ret.begin(), std::negate<T>());
387 template <
typename T,
int N>
390 std::transform(a.begin(), a.end(), b.begin(), ret.begin(), std::plus<T>());
395 template <
typename T,
int N>
398 std::transform(a.begin(), a.end(), b.begin(), ret.begin(), std::minus<T>());
403 template <
typename T,
int N>
406 std::transform(a.begin(), a.end(), b.begin(), ret.begin(),
407 std::multiplies<T>());
412 template <
typename T,
int N>
415 std::transform(a.begin(), a.end(), b.begin(), ret.begin(), std::divides<T>());
420 template <
typename T,
int N>
423 std::transform(b.begin(), b.end(), ret.begin(),
424 std::bind(std::plus<T>(), a, std::placeholders::_1));
429 template <
typename T,
int N>
432 std::transform(b.begin(), b.end(), ret.begin(),
433 std::bind(std::minus<T>(), a, std::placeholders::_1));
438 template <
typename T,
int N>
441 std::transform(b.begin(), b.end(), ret.begin(),
442 std::bind(std::multiplies<T>(), a, std::placeholders::_1));
447 template <
typename T,
int N>
450 std::transform(b.begin(), b.end(), ret.begin(),
451 std::bind(std::divides<T>(), a, std::placeholders::_1));
456 template <
typename T,
int N>
459 std::transform(b.begin(), b.end(), ret.begin(),
460 std::bind(std::plus<T>(), std::placeholders::_1, a));
465 template <
typename T,
int N>
468 std::transform(b.begin(), b.end(), ret.begin(),
469 std::bind(std::minus<T>(), std::placeholders::_1, a));
474 template <
typename T,
int N>
477 std::transform(b.begin(), b.end(), ret.begin(),
478 std::bind(std::multiplies<T>(), std::placeholders::_1, a));
483 template <
typename T,
int N>
486 std::transform(b.begin(), b.end(), ret.begin(),
487 std::bind(std::divides<T>(), std::placeholders::_1, a));
493 template <
typename T,
int N>
497 std::transform(a.begin(), a.end(), b.begin(), ret.begin(),
502 template <
typename T,
int N>
506 std::transform(a.begin(), a.end(), b.begin(), ret.begin(),
507 std::not_equal_to<T>());
511 template <
typename T,
int N>
515 std::transform(a.begin(), a.end(), b.begin(), ret.begin(), std::less<T>());
519 template <
typename T,
int N>
523 std::transform(a.begin(), a.end(), b.begin(), ret.begin(), std::greater<T>());
527 template <
typename T,
int N>
531 std::transform(a.begin(), a.end(), b.begin(), ret.begin(),
532 std::less_equal<T>());
536 template <
typename T,
int N>
540 std::transform(a.begin(), a.end(), b.begin(), ret.begin(),
541 std::greater_equal<T>());
546 template <
typename T,
int N>
549 std::transform(a.begin(), a.end(), ret.begin(),
550 [](
const T &a) {
return static_cast<T
>(std::floor(a)); });
557 std::transform(a.begin(), a.end(), ret.begin(),
558 [](
const T &a) {
return static_cast<T
>(std::ceil(a)); });
563 template <
typename T,
int N>
566 std::transform(a.begin(), a.end(), ret.begin(),
567 [](
const T &a) {
return static_cast<T
>(std::round(a)); });
574 std::transform(a.begin(), a.end(), ret.begin(),
575 [](
const T &a) {
return static_cast<T
>(std::fabs(a)); });
582 std::transform(a.begin(), a.end(), ret.begin(),
583 [](
const T &a) {
return a * a; });
588 template <
typename T>
591 ret[0] = arg1[1] * arg2[2] - arg1[2] * arg2[1];
592 ret[1] = -arg1[0] * arg2[2] + arg1[2] * arg2[0];
593 ret[2] = arg1[0] * arg2[1] - arg1[1] * arg2[0];
598 template <
typename T,
int N>
601 std::transform(arg.begin(), arg.end(), ret.begin(), [&n](
const T &a) {
603 const int num_digits = 1 +
static_cast<int>(std::log10(a));
605 const auto d =
static_cast<T
>(std::pow(10.0, n - num_digits));
606 return static_cast<T
>(std::floor(a * d + static_cast<T>(0.5)) / d);
611 template <
typename T,
int N>
618 template <
typename T,
int N>
619 std::ostream &operator<<(std::ostream &out, const Vector<T, N> &v) {
621 for (
auto it = std::next(v.cbegin()); it != v.cend(); ++it) {
628 template <
typename T,
int N>
643 std::size_t operator()(
const vint2_t &s)
const noexcept {
645 return ((s[0] + s[1]) * (s[0] + s[1] + 1) / 2) + s[1];
651 bool operator()(
const vint2_t &a,
const vint2_t &b)
const noexcept {
652 return a[0] == b[0] && a[1] == b[1];
bool any() const noexcept
collapse vector to bool using std::any_of
Definition: Vector.hpp:265
T prod() const noexcept
returns the product of every element in the vector
Definition: Vector.hpp:286
Vector< T, N > & operator/=(const Vector< T, N > &a)
this-Vector compound divides operator
Definition: Vector.hpp:327
T maxCoeff() const noexcept
find the maximum element of the vector
Definition: Vector.hpp:283
static Vector Zero()
Zero Vector.
Definition: Vector.hpp:165
const T & operator[](const int n) const noexcept
const Index operator
Definition: Vector.hpp:185
Vector(const Vector< T2, N > &arg)
Constructs a vector with another vector of different type.
Definition: Vector.hpp:136
Vector(T arg1, T arg2, T arg3)
Constructs an vector with initial values.
Definition: Vector.hpp:94
T * data() noexcept
returns the raw memory array containing the data for the vector
Definition: Vector.hpp:297
Vector< T, N > & operator-=(const T &k)
this-scalar compound minus operator
Definition: Vector.hpp:340
Vector< T, N > round(const Vector< T, N > &a) noexcept
element-wise round rounding function for Vector class
Definition: Vector.hpp:564
Vector< T, N > operator-(const Vector< T, N > &a) noexcept
unary - (minus) operator for Vector class
Definition: Vector.hpp:372
Vector< T, N > operator*(const Vector< T, N > &a, const Vector< T, N > &b) noexcept
binary * (multiples) operator for two Vectors
Definition: Vector.hpp:404
Vector< T, N > & operator*=(const Vector< T, N > &a)
this-Vector compound multiples operator
Definition: Vector.hpp:321
bool all() const noexcept
collapse vector to bool using std::all_of
Definition: Vector.hpp:258
Vector< T, N > round_off(const Vector< T, N > &arg, int n)
round off to n significant digits
Definition: Vector.hpp:599
Vector< T, N > & operator/=(const T &k)
this-scalar compound divides operator
Definition: Vector.hpp:354
An N-dimensional vector class.
Definition: Vector.hpp:59
static Vector Constant(const T &c)
Constant Vector.
Definition: Vector.hpp:174
Vector< T, N > & operator+=(const Vector< T, N > &a)
this-Vector compound plus operator
Definition: Vector.hpp:309
Vector< T, N > & operator-=(const Vector< T, N > &a)
this-Vector compound minus operator
Definition: Vector.hpp:315
T squaredNorm() const noexcept
squared norm
Definition: Vector.hpp:221
Vector< T, N > ceil(const Vector< T, N > &a) noexcept
element-wise ceil rounding function for Vector class
Definition: Vector.hpp:555
double norm() const noexcept
2-norm
Definition: Vector.hpp:225
T minCoeff() const noexcept
find the minimum element of the vector
Definition: Vector.hpp:279
T & operator[](const int n) noexcept
Index operator.
Definition: Vector.hpp:192
Vector< T, N > abs2(const Vector< T, N > &a) noexcept
element-wise e_i*e_i function for Vector class
Definition: Vector.hpp:580
Vector(T arg1)
Constructs an vector with initial values.
Definition: Vector.hpp:77
bool none() const noexcept
collapse vector to bool using std::none_of
Definition: Vector.hpp:272
Vector< T, N > abs(const Vector< T, N > &a) noexcept
element-wise absolute value function for Vector class
Definition: Vector.hpp:572
void normalize() noexcept
normalise vector so that its length (2-norm) equals one.
Definition: Vector.hpp:250
T inf_norm() const noexcept
inf-norm
Definition: Vector.hpp:229
Vector< T, N > pow(const EXP_T exponent) const noexcept
element-wise pow function
Definition: Vector.hpp:240
std::istream & operator>>(std::istream &out, Vector< T, N > &v)
stream input operator for Vector class
Definition: Vector.hpp:629
Vector< T, N > & operator+=(const T &k)
this-scalar compound plus operator
Definition: Vector.hpp:333
Vector< T, N > operator+(const Vector< T, N > &a, const Vector< T, N > &b) noexcept
binary + (plus) operator for two Vectors
Definition: Vector.hpp:388
Vector< T, 3 > cross(const Vector< T, 3 > &arg1, const Vector< T, 3 > &arg2)
cross-product function for 3D vectors
Definition: Vector.hpp:589
T dot(const Vector< T, N > &arg) const noexcept
dot product
Definition: Vector.hpp:206
Vector< T2, N > cast() const noexcept
change vector type
Definition: Vector.hpp:212
T sum() const noexcept
returns the sum of every element in the vector
Definition: Vector.hpp:292
T inner_product(const Vector< T, N > &arg) const noexcept
inner product
Definition: Vector.hpp:197
Vector< T, N > & operator*=(const T &k)
this-scalar compound multiples operator
Definition: Vector.hpp:347
Vector< T, N > floor(const Vector< T, N > &a) noexcept
element-wise floor rounding function for Vector class
Definition: Vector.hpp:547
Vector< T, N > operator/(const Vector< T, N > &a, const Vector< T, N > &b) noexcept
binary / (divides) operator for two Vectors
Definition: Vector.hpp:413
hash function for an STL map of 2d int vectors
Definition: Vector.hpp:642
Definition: Backend.cpp:39
Vector(T arg1, T arg2)
Constructs a vector with initial values.
Definition: Vector.hpp:83
Vector(T arg1, T arg2, T arg3, T arg4, T arg5)
Constructs an vector with initial values.
Definition: Vector.hpp:122
equality function for an STL map of 2d int vectors
Definition: Vector.hpp:650
Vector(T arg1, T arg2, T arg3, T arg4)
Constructs an vector with initial values.
Definition: Vector.hpp:107
Vector()=default
Constructs an vector and allocates memory.