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)
125 for (
unsigned int i = 0; i < N; ++i)
131 template<
unsigned int M>
132 vec(
const vec<Type, M>& other) {
138 template<
typename Vector>
145 vec(std::initializer_list<Type> l) {
148 TH_MATH_ERROR(
"vec::vec(initializer_list<Type>)", l.size(), INVALID_ARGUMENT);
153 std::copy(l.begin(), l.end(), &data[0]);
166 inline vec<Type, N>
operator+(
const vec<Type, N>& other)
const {
176 return *
this * (Type) -1;
181 inline vec<Type, N>
operator-(
const vec<Type, N>& other)
const {
193 for (
unsigned int i = 0; i < N; ++i)
194 result.data[i] = scalar * data[i];
204 for (
unsigned int i = 0; i < N; ++i)
205 result.data[i] = data[i] / scalar;
212 template<
typename Vector>
213 inline Type
dot(
const Vector& other)
const {
219 template<
typename Vector>
226 inline vec<Type, N>
cross(
const vec<Type, N>& other)
const {
227 static_assert(N == 3,
"The vector must be three dimensional");
233 template<
typename Vector>
234 inline vec<Type, N>
cross(
const Vector& other)
const {
240 template<
typename Vector>
243 for (
unsigned int i = 0; i < N; ++i)
244 data[i] += other.data[i];
251 template<
typename Vector>
254 for (
unsigned int i = 0; i < N; ++i)
255 data[i] -= other.data[i];
264 for (
unsigned int i = 0; i < N; ++i)
280 for (
unsigned int i = 0; i < N; ++i)
315 inline Type&
at(
unsigned int i) {
318 throw std::out_of_range(
319 "The element index in vec::at() is out of bounds"
331 inline Type
at(
unsigned int i)
const {
334 throw std::out_of_range(
335 "The element index in vec::at() is out of bounds"
344 using iterator = vec_iterator<vec<Type, N>, Type&>;
390 template<
typename Vector>
393 if(
size() != other.size())
396 for (
unsigned int i = 0; i < N; ++i)
397 if(data[i] != other[i])
405 template<
typename Vector>
407 return !(*
this == other);
433 unsigned int i,
unsigned int n = N) {
437 return vec<Type, N>(Type(
nan()));
440 vec<Type, N> e_i = vec<Type, N>(n, Type(0.0));
450 inline friend vec<Type, N>
operator*(Type a,
const vec<Type, N>& v) {
455#ifndef THEORETICA_NO_PRINT
459 const std::string& separator =
", ",
460 bool parenthesis =
true)
const {
462 std::stringstream res;
467 for (
unsigned int i = 0; i < N; ++i) {
481 inline operator std::string() {
488 std::ostream& out,
const vec<Type, N>& obj) {
489 return out << obj.to_string();
502 template<
typename Type>
507 using Container = std::vector<T>;
510 Container<Type> data;
515 static constexpr size_t size_argument = 0;
523 vec(
unsigned int n) {
531 vec(
unsigned int n, Type a) {
532 data = std::vector<Type>(n, a);
537 template<
unsigned int M>
538 vec(
const vec<Type, M>& other) {
544 template<
typename Vector>
545 vec<Type>&
operator=(
const Vector& other) {
551 vec(std::initializer_list<Type> l) {
554 std::copy(l.begin(), l.end(), &data[0]);
567 template<
typename Vector>
568 inline vec<Type>
operator+(
const Vector& other)
const {
571 result.resize(
size());
579 return *
this * (Type) -1;
584 template<
typename Vector>
585 inline vec<Type>
operator-(
const Vector& other)
const {
588 result.resize(
size());
595 inline vec<Type>
operator*(Type scalar)
const {
598 result.resize(
size());
600 for (
unsigned int i = 0; i <
size(); ++i)
601 result.data[i] = scalar * data[i];
608 inline vec<Type>
operator/(Type scalar)
const {
611 result.resize(
size());
613 for (
unsigned int i = 0; i <
size(); ++i)
614 result.data[i] = data[i] / scalar;
621 template<
typename Vector>
622 inline Type
dot(
const Vector& other)
const {
628 template<
typename Vector>
629 inline Type
operator*(
const Vector& other)
const {
635 template<
typename Vector>
636 inline vec<Type>
cross(
const Vector& other)
const {
642 template<
typename Vector>
643 inline vec<Type>&
operator+=(
const Vector& other) {
651 if(
size() != other.size()) {
653 return (*
this = vec<Type>(
max(
size(), 1),
nan()));
656 for (
unsigned int i = 0; i <
size(); ++i)
657 data[i] += other.data[i];
664 template<
typename Vector>
665 inline vec<Type>&
operator-=(
const Vector& other) {
667 if(
size() != other.size()) {
669 return (*
this = vec<Type>(
max(
size(), 1),
nan()));
672 for (
unsigned int i = 0; i <
size(); ++i)
673 data[i] -= other.data[i];
682 for (
unsigned int i = 0; i <
size(); ++i)
698 for (
unsigned int i = 0; i <
size(); ++i)
706 inline Type
norm()
const {
724 inline const Type&
operator[](
unsigned int i)
const {
733 inline Type&
at(
unsigned int i) {
742 inline Type
at(
unsigned int i)
const {
747 using iterator =
typename Container<Type>::iterator;
752 inline auto begin() {
759 inline auto begin()
const {
760 return data.cbegin();
773 inline auto end()
const {
791 template<
typename Vector>
792 inline bool operator==(
const Vector& other)
const {
794 if(
size() != other.size())
797 for (
unsigned int i = 0; i <
size(); ++i)
798 if(data[i] != other[i])
806 template<
typename Vector>
807 inline bool operator!=(
const Vector& other)
const {
808 return !(*
this == other);
819 inline void resize(
size_t n) {
826 inline void push(
const Type& x) {
833 inline void push(Type&& x) {
841 unsigned int i,
unsigned int n) {
845 return vec<Type>(n,
nan());
848 vec<Type> e_i = vec<Type>(n, Type(0.0));
858 inline friend vec<Type>
operator*(Type a,
const vec<Type>& v) {
863#ifndef THEORETICA_NO_PRINT
867 const std::string& separator =
", ",
868 bool parenthesis =
true)
const {
870 std::stringstream res;
875 for (
unsigned int i = 0; i <
size(); ++i) {
889 inline operator std::string() {
895 inline friend std::ostream&
operator<<(std::ostream& out,
const vec<Type>& obj) {
896 return out << obj.to_string();
910 template<
typename ElementType,
typename Type,
typename ...Args>
911 void make_vec(vec<ElementType>& v,
size_t index, Type last) {
924 template<
typename ElementType,
typename Type,
typename ...Args>
925 void make_vec(vec<ElementType>& v,
size_t index, Type first, Args... elements) {
928 make_vec<ElementType>(v, index + 1, elements...);
934 template<
typename Type,
typename ...Args>
938 v.resize(
sizeof...(elements) + 1);
941 make_vec<Type>(v, 1, elements...);
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:306
vec< Type, N > cross(const vec< Type, N > &other) const
Cross product between vectors.
Definition vec.h:226
Type & operator[](unsigned int i)
Access i-th component by reference.
Definition vec.h:300
bool operator!=(const Vector &other) const
Check whether all elements of both vectors are unequal.
Definition vec.h:406
Type operator*(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition vec.h:220
auto begin() const
Get a const iterator to the first element of the vector.
Definition vec.h:365
vec_iterator< vec< Type, N >, Type & > iterator
Sequential iterator for statically allocated vectors.
Definition vec.h:344
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition vec.h:422
vec()
Construct a vector with all elements equal to zero.
Definition vec.h:104
auto end()
Get an iterator to one plus the last element of the vector.
Definition vec.h:359
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:432
vec(const vec< Type, M > &other)
Copy constructor.
Definition vec.h:132
vec_iterator< const vec< Type, N >, const Type & > const_iterator
Const sequential iterator for statically allocated vectors.
Definition vec.h:347
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
vec< Type, N > & operator*=(Type scalar)
Multiply the vector itself by a scalar.
Definition vec.h:262
Type sqr_norm() const
Compute the square norm of the vector (v * v)
Definition vec.h:294
std::string to_string(const std::string &separator=", ", bool parenthesis=true) const
Convert the vector to string representation.
Definition vec.h:458
vec< Type, N > & operator+=(const Vector &other)
Sum a vector the the vector itself.
Definition vec.h:241
vec< Type, N > operator-(const vec< Type, N > &other) const
Vector subtraction.
Definition vec.h:181
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition vec.h:412
vec< Type, N > & operator-=(const Vector &other)
Subtract a vector the the vector itself.
Definition vec.h:252
vec< Type, N > cross(const Vector &other) const
Cross product between vectors.
Definition vec.h:234
vec< Type, N > & operator=(const Vector &other)
Copy from other.
Definition vec.h:139
Type norm() const
Compute the norm of the vector (sqrt(v * v))
Definition vec.h:288
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:450
void normalize()
Vector normalization (v / |v|)
Definition vec.h:378
vec(std::initializer_list< Type > l)
Initialize from a list, e.g. {1, 2, 3}.
Definition vec.h:145
Type dot(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition vec.h:213
Type & at(unsigned int i)
Access i-th element by reference, with bound checking.
Definition vec.h:315
vec< Type, N > normalized() const
Return the normalized vector (v / |v|)
Definition vec.h:384
auto begin()
Get an iterator to the first element of the vector.
Definition vec.h:352
vec< Type, N > operator+(const vec< Type, N > &other) const
Vector sum (v + w = (v.x + w.x, ...))
Definition vec.h:166
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:487
bool operator==(const Vector &other) const
Check whether all elements of both vectors are equal.
Definition vec.h:391
auto end() const
Get a const iterator to one plus the last element of the vector.
Definition vec.h:372
vec< Type, N > operator/(Type scalar) const
Scalar division (v / a = (v.x / a, ...))
Definition vec.h:201
Type at(unsigned int i) const
Get the i-th element by value, with bound checking.
Definition vec.h:331
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:272
vec< Type, N > operator+() const
Identity.
Definition vec.h:160
vec< Type, N > operator*(Type scalar) const
Scalar multiplication (av = (v.x * a, ...))
Definition vec.h:190
vec< Type, N > operator-() const
Opposite vector.
Definition vec.h:175
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition constants.h:161
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition error.h:225
Vector1 cross(const Vector1 &v1, const Vector2 &v2)
Compute the cross product between two tridimensional vectors.
Definition algebra.h:373
Vector & make_normalized(Vector &v)
Normalize a given vector overwriting it.
Definition algebra.h:327
Vector1 & vec_copy(Vector1 &dest, const Vector2 &src)
Copy a vector by overwriting another.
Definition algebra.h:143
auto dot(const Vector1 &v, const Vector2 &w)
Computes the dot product between two vectors, using the Hermitian form if needed.
Definition algebra.h:351
auto sqr_norm(const Vector &v)
Returns the square of the Euclidean/Hermitian norm of the given vector.
Definition algebra.h:275
Vector2 & vec_sum(Vector1 &v1, const Vector2 &v2)
Sum two vectors and store the result in the first vector.
Definition algebra.h:1135
Vector2 & vec_diff(Vector1 &v1, const Vector2 &v2)
Subtract two vectors and store the result in the first vector.
Definition algebra.h:1183
Vector & vec_zeroes(Vector &v)
Overwrite a vector with all zeroes.
Definition algebra.h:109
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:62
auto norm(const Vector &v)
Returns the Euclidean/Hermitian norm of the given vector.
Definition algebra.h:292
Vector normalize(const Vector &v)
Returns the normalized vector.
Definition algebra.h:302
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:198
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:54
void make_vec(vec< ElementType > &v, size_t index, Type last)
Populates a vector with a single element at the specified index.
Definition vec.h:911
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:207