6#ifndef THEORETICA_VECTOR_H
7#define THEORETICA_VECTOR_H
9#ifndef THEORETICA_NO_PRINT
14#include "../core/error.h"
15#include "../core/real_analysis.h"
28 template<
typename Vector,
typename ReturnType = vector_element_t<Vector>&>
41 using iterator_category = std::forward_iterator_tag;
42 using value_type = vector_element_t<Vector>;
43 using pointer = value_type*;
44 using reference = value_type&;
81 return !(*
this ==
other);
91 template<
typename Type = real,
unsigned int N = 0>
100 static constexpr size_t size_argument = N;
113 for (
unsigned int i = 0; i < N; ++i)
129 for (
unsigned int i = 0; i < N; ++i)
137 enable_vector<Vector> =
true
146 template<
typename...
Args>
150 2 +
sizeof...(args) == N,
151 "Number of arguments must match vector size"
159 template<
typename Vector>
162 if (N ==
other.size())
170 vec(std::initializer_list<Type>
l) {
178 std::copy(
l.begin(),
l.end(), &elements[0]);
201 return *
this * (
Type) -1;
218 for (
unsigned int i = 0; i < N; ++i)
229 for (
unsigned int i = 0; i < N; ++i)
237 template<
typename Vector, enable_vector<Vector> = true>
244 template<
typename Vector, enable_vector<Vector> = true>
252 static_assert(N == 3,
"The vector must be three dimensional");
258 template<
typename Vector>
265 template<
typename Vector>
268 for (
unsigned int i = 0; i < N; ++i)
269 elements[i] +=
other.elements[i];
276 template<
typename Vector>
279 for (
unsigned int i = 0; i < N; ++i)
280 elements[i] -=
other.elements[i];
289 for (
unsigned int i = 0; i < N; ++i)
305 for (
unsigned int i = 0; i < N; ++i)
343 throw std::out_of_range(
344 "The element index in vec::at() is out of bounds"
359 throw std::out_of_range(
360 "The element index in vec::at() is out of bounds"
427 template<
typename Vector>
433 for (
unsigned int i = 0; i < N; ++i)
434 if(elements[i] !=
other[i])
442 template<
typename Vector>
444 return !(*
this ==
other);
485 unsigned int i,
unsigned int n = N) {
507#ifndef THEORETICA_NO_PRINT
514 std::stringstream
res;
519 for (
unsigned int i = 0; i < N; ++i) {
533 inline operator std::string() {
541 return out <<
obj.to_string();
554 template<
typename Type>
559 using Container = std::vector<T>;
567 static constexpr size_t size_argument = 0;
574 vec(
unsigned int n) {
582 elements = std::vector<Type>(
n, a);
596 template<
typename...
Args>
604 vec(std::initializer_list<Type>
l) : elements(
l) {}
614 template<
typename Vector>
626 return *
this * (
Type) -1;
631 template<
typename Vector>
647 for (
unsigned int i = 0; i <
size(); ++i)
660 for (
unsigned int i = 0; i <
size(); ++i)
668 template<
typename Vector, enable_vector<Vector> = true>
675 template<
typename Vector, enable_vector<Vector> = true>
682 template<
typename Vector>
689 template<
typename Vector>
703 for (
unsigned int i = 0; i <
size(); ++i)
704 elements[i] +=
other.elements[i];
711 template<
typename Vector>
719 for (
unsigned int i = 0; i <
size(); ++i)
720 elements[i] -=
other.elements[i];
729 for (
unsigned int i = 0; i <
size(); ++i)
745 for (
unsigned int i = 0; i <
size(); ++i)
780 inline Type&
at(
unsigned int i) {
781 return elements.at(i);
789 inline Type at(
unsigned int i)
const {
790 return elements.at(i);
794 using iterator =
typename Container<Type>::iterator;
799 inline auto begin() {
800 return elements.begin();
806 inline auto begin()
const {
807 return elements.cbegin();
814 return elements.end();
820 inline auto end()
const {
821 return elements.cend();
827 return elements.data();
833 return elements.data();
850 template<
typename Vector>
856 for (
unsigned int i = 0; i <
size(); ++i)
857 if(elements[i] !=
other[i])
865 template<
typename Vector>
867 return !(*
this ==
other);
873 return elements.size();
885 elements.resize(
n, value);
891 inline void append(
const Type& x) {
892 elements.push_back(x);
898 inline void append(
Type&& x) {
899 elements.push_back(x);
906 unsigned int i,
unsigned int n) {
928#ifndef THEORETICA_NO_PRINT
935 std::stringstream
res;
940 for (
unsigned int i = 0; i <
size(); ++i) {
954 inline operator std::string() {
961 return out <<
obj.to_string();
A sequential iterator for traversing vector-like containers.
Definition vec.h:29
ReturnType operator*()
Dereference the iterator to get the current element.
Definition vec.h:52
vec_iterator(Vector &vector, size_t index)
Construct the iterator from a pointer to the elements and a starting index.
Definition vec.h:48
size_t index()
Move to the previous element in the vector.
Definition vec.h:70
vec_iterator & operator++()
Move to the next element in the vector.
Definition vec.h:57
bool operator==(const vec_iterator &other) const
Comparison operators.
Definition vec.h:76
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
const Type & operator[](unsigned int i) const
Get the i-th component by value.
Definition vec.h:331
vec< Type, N > cross(const vec< Type, N > &other) const
Cross product between vectors.
Definition vec.h:251
Type & operator[](unsigned int i)
Access i-th component by reference.
Definition vec.h:325
bool operator!=(const Vector &other) const
Check whether all elements of both vectors are unequal.
Definition vec.h:443
Type dot(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition vec.h:238
auto begin() const
Get a const iterator to the first element of the vector.
Definition vec.h:390
vec_iterator< vec< Type, N >, Type & > iterator
Sequential iterator for statically allocated vectors.
Definition vec.h:369
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition vec.h:459
vec()
Construct a vector with all elements equal to zero.
Definition vec.h:104
const Type * data() const
Get a raw pointer to the elements of the vector.
Definition vec.h:409
auto end()
Get an iterator to one plus the last element of the vector.
Definition vec.h:384
static vec< Type, N > euclidean_base(unsigned int i, unsigned int n=N)
Returns an N-dimensional euclidean base unit vector with the i-th element set to 1.
Definition vec.h:484
vec_iterator< const vec< Type, N >, const Type & > const_iterator
Const sequential iterator for statically allocated vectors.
Definition vec.h:372
vec(unsigned int size, Type val)
Construct a vector with all elements equal to the given value, checking that the given size matches t...
Definition vec.h:121
void resize(size_t n, const Type &value) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition vec.h:474
vec< Type, N > & operator*=(Type scalar)
Multiply the vector itself by a scalar.
Definition vec.h:287
Type sqr_norm() const
Compute the square norm of the vector (v * v)
Definition vec.h:319
std::string to_string(const std::string &separator=", ", bool parenthesis=true) const
Convert the vector to string representation.
Definition vec.h:510
vec< Type, N > & operator+=(const Vector &other)
Sum a vector the the vector itself.
Definition vec.h:266
vec< Type, N > operator-(const vec< Type, N > &other) const
Vector subtraction.
Definition vec.h:206
vec(Type x1, Type x2, Args... args)
Construct a vector from its elements, provided they are more than two (to avoid conflict with other c...
Definition vec.h:147
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition vec.h:449
vec(const Vector &other)
Copy constructor.
Definition vec.h:139
vec< Type, N > & operator-=(const Vector &other)
Subtract a vector the the vector itself.
Definition vec.h:277
vec< Type, N > cross(const Vector &other) const
Cross product between vectors.
Definition vec.h:259
vec< Type, N > & operator=(const Vector &other)
Copy from other.
Definition vec.h:160
Type norm() const
Compute the norm of the vector (sqrt(v * v))
Definition vec.h:313
friend vec< Type, N > operator*(Type a, const vec< Type, N > &v)
Friend operator to enable equations of the form (Type) * (vec)
Definition vec.h:502
void normalize()
Vector normalization (v / |v|)
Definition vec.h:415
vec(std::initializer_list< Type > l)
Initialize from a list, e.g. {1, 2, 3}.
Definition vec.h:170
Type & at(unsigned int i)
Access i-th element by reference, with bound checking.
Definition vec.h:340
vec< Type, N > normalized() const
Return the normalized vector (v / |v|)
Definition vec.h:421
auto begin()
Get an iterator to the first element of the vector.
Definition vec.h:377
vec< Type, N > operator+(const vec< Type, N > &other) const
Vector sum (v + w = (v.x + w.x, ...))
Definition vec.h:191
friend std::ostream & operator<<(std::ostream &out, const vec< Type, N > &obj)
Stream the vector in string representation to an output stream (std::ostream)
Definition vec.h:539
bool operator==(const Vector &other) const
Check whether all elements of both vectors are equal.
Definition vec.h:428
auto end() const
Get a const iterator to one plus the last element of the vector.
Definition vec.h:397
vec< Type, N > operator/(Type scalar) const
Scalar division (v / a = (v.x / a, ...))
Definition vec.h:226
Type at(unsigned int i) const
Get the i-th element by value, with bound checking.
Definition vec.h:356
vec(Type val)
Construct a vector with all elements equal to the given value.
Definition vec.h:111
vec< Type, N > & operator/=(Type scalar)
Divide the vector itself by a scalar.
Definition vec.h:297
vec< Type, N > operator+() const
Identity.
Definition vec.h:185
Type operator*(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition vec.h:245
vec< Type, N > operator*(Type scalar) const
Scalar multiplication (av = (v.x * a, ...))
Definition vec.h:215
Type * data()
Get a raw pointer to the elements of the vector.
Definition vec.h:403
vec< Type, N > operator-() const
Opposite vector.
Definition vec.h:200
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition constants.h:170
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:225
Vector1 cross(const Vector1 &v1, const Vector2 &v2)
Compute the cross product between two tridimensional vectors.
Definition algebra.h:476
Vector & make_normalized(Vector &v)
Normalize a given vector overwriting it.
Definition algebra.h:430
Vector1 & vec_copy(Vector1 &dest, const Vector2 &src)
Copy a vector by overwriting another.
Definition algebra.h:241
auto dot(const Vector1 &v, const Vector2 &w)
Computes the dot product between two vectors, using the Hermitian form if needed.
Definition algebra.h:454
auto sqr_norm(const Vector &v)
Returns the square of the Euclidean/Hermitian norm of the given vector.
Definition algebra.h:378
Vector2 & vec_sum(Vector1 &v1, const Vector2 &v2)
Sum two vectors and store the result in the first vector.
Definition algebra.h:1202
Vector2 & vec_diff(Vector1 &v1, const Vector2 &v2)
Subtract two vectors and store the result in the first vector.
Definition algebra.h:1247
Vector & vec_zeroes(Vector &v)
Overwrite a vector with all zeroes.
Definition algebra.h:197
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:58
auto norm(const Vector &v)
Returns the Euclidean/Hermitian norm of the given vector.
Definition algebra.h:395
Vector normalize(const Vector &v)
Returns the normalized vector.
Definition algebra.h:405
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition dataset.h:330
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:78
@ InvalidArgument
Invalid argument.
@ DivByZero
Division by zero.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216