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;
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>
138 template<
typename Vector>
145 vec(std::initializer_list<Type> l) {
155 std::copy(l.begin(), l.end(), &data[0]);
178 return *
this * (Type) -1;
195 for (
unsigned int i = 0; i < N; ++i)
196 result.data[i] = scalar * data[i];
206 for (
unsigned int i = 0; i < N; ++i)
207 result.data[i] = data[i] / scalar;
214 template<
typename Vector>
215 inline Type
dot(
const Vector& other)
const {
221 template<
typename Vector>
229 static_assert(N == 3,
"The vector must be three dimensional");
235 template<
typename Vector>
238 if(other.size() != 3) {
248 template<
typename Vector>
251 for (
unsigned int i = 0; i < N; ++i)
252 data[i] += other.data[i];
259 template<
typename Vector>
262 for (
unsigned int i = 0; i < N; ++i)
263 data[i] -= other.data[i];
272 for (
unsigned int i = 0; i < N; ++i)
288 for (
unsigned int i = 0; i < N; ++i)
320 inline Type&
at(
unsigned int i) {
326 inline Type
get(
unsigned int i)
const {
332 inline void set(
unsigned int i, Type x) {
384 template<
typename Vector>
387 if(
size() != other.size())
390 for (
unsigned int i = 0; i < N; ++i)
391 if(data[i] != other[i])
399 template<
typename Vector>
401 return !(*
this == other);
427 unsigned int i,
unsigned int n = N) {
449 #ifndef THEORETICA_NO_PRINT
453 const std::string& separator =
", ",
454 bool parenthesis =
true)
const {
456 std::stringstream res;
461 for (
unsigned int i = 0; i < N; ++i) {
475 inline operator std::string() {
496 template<
typename Type>
501 using Container = std::vector<T>;
504 Container<Type> data;
509 static constexpr
size_t size_argument = 0;
525 vec(
unsigned int n, Type a) {
526 data = std::vector<Type>(n, a);
531 template<
unsigned int M>
538 template<
typename Vector>
545 vec(std::initializer_list<Type> l) {
548 std::copy(l.begin(), l.end(), &data[0]);
561 template<
typename Vector>
573 return *
this * (Type) -1;
578 template<
typename Vector>
594 for (
unsigned int i = 0; i <
size(); ++i)
595 result.data[i] = scalar * data[i];
607 for (
unsigned int i = 0; i <
size(); ++i)
608 result.data[i] = data[i] / scalar;
615 template<
typename Vector>
616 inline Type
dot(
const Vector& other)
const {
622 template<
typename Vector>
629 template<
typename Vector>
632 if(other.size() != 3) {
642 template<
typename Vector>
651 if(
size() != other.size()) {
656 for (
unsigned int i = 0; i <
size(); ++i)
657 data[i] += other.data[i];
664 template<
typename Vector>
667 if(
size() != other.size()) {
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)
730 inline Type&
at(
unsigned int i) {
736 inline Type
get(
unsigned int i)
const {
742 inline void set(
unsigned int i, Type x) {
747 using iterator =
typename Container<Type>::iterator;
760 return data.cbegin();
791 template<
typename Vector>
794 if(
size() != other.size())
797 for (
unsigned int i = 0; i <
size(); ++i)
798 if(data[i] != other[i])
806 template<
typename Vector>
808 return !(*
this == other);
826 inline void push(
const Type& x) {
841 unsigned int i,
unsigned int n) {
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() {
910 template<
typename ElementType,
typename Type,
typename ...Args>
924 template<
typename ElementType,
typename Type,
typename ...Args>
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...);
vec(std::initializer_list< Type > l)
Initialize from a list, e.g. {1, 2, 3}.
Definition: vec.h:545
std::string to_string(const std::string &separator=", ", bool parenthesis=true) const
Convert the vector to string representation.
Definition: vec.h:866
vec< Type > & operator-=(const Vector &other)
Subtract a vector from the vector itself.
Definition: vec.h:665
void push(const Type &x)
Add a value at the end of the vector (only for dynamically allocated vectors).
Definition: vec.h:826
vec< Type > & operator+=(const Vector &other)
Sum a vector to the vector itself.
Definition: vec.h:643
void resize(size_t n)
Change the size of the vector.
Definition: vec.h:819
vec< Type > operator+(const Vector &other) const
Vector sum (v + w = (v.x + w.x, ...))
Definition: vec.h:562
friend std::ostream & operator<<(std::ostream &out, const vec< Type > &obj)
Stream the vector in string representation to an output stream (std::ostream)
Definition: vec.h:895
vec< Type > normalized() const
Return the normalized vector (v / |v|)
Definition: vec.h:785
void push(Type &&x)
Add a value at the end of the vector (only for dynamically allocated vectors).
Definition: vec.h:833
vec< Type > operator-(const Vector &other) const
Vector subtraction.
Definition: vec.h:579
const Type & operator[](unsigned int i) const
Get the i-th component.
Definition: vec.h:724
vec(unsigned int n, Type a)
Construct a vector with the given size and all elements equal to the given value.
Definition: vec.h:525
void normalize()
Vector normalization (v / |v|)
Definition: vec.h:779
Type & operator[](unsigned int i)
Access i-th component.
Definition: vec.h:718
vec< Type > cross(const Vector &other) const
Cross product between vectors.
Definition: vec.h:630
Type dot(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition: vec.h:616
Type get(unsigned int i) const
Getters and setters.
Definition: vec.h:736
auto begin() const
Get a const iterator to the first element of the vector.
Definition: vec.h:759
vec< Type > operator-() const
Opposite vector.
Definition: vec.h:572
void set(unsigned int i, Type x)
Set the i-th element.
Definition: vec.h:742
bool operator!=(const Vector &other) const
Check whether all elements of both vectors are unequal.
Definition: vec.h:807
vec(unsigned int n)
Construct a vector with the given size and all elements equal to zero.
Definition: vec.h:517
vec< Type > & operator/=(Type scalar)
Divide the vector itself by a scalar.
Definition: vec.h:690
vec()
Construct an empty vector.
Definition: vec.h:512
Type & at(unsigned int i)
Access i-th element.
Definition: vec.h:730
static vec< Type > euclidean_base(unsigned int i, unsigned int n)
Returns an euclidean base unit vector with the i-th element set to 1 and size n.
Definition: vec.h:840
Type sqr_norm() const
Compute the square norm of the vector (v * v)
Definition: vec.h:712
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector.
Definition: vec.h:813
auto end()
Get an iterator to one plus the last element of the vector.
Definition: vec.h:766
vec(const vec< Type, M > &other)
Copy constructor.
Definition: vec.h:532
Type operator*(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition: vec.h:623
bool operator==(const Vector &other) const
Check whether all elements of both vectors are equal.
Definition: vec.h:792
auto end() const
Get a const iterator to one plus the last element of the vector.
Definition: vec.h:773
friend vec< Type > operator*(Type a, const vec< Type > &v)
Friend operator to enable equations of the form (Type) * (vec)
Definition: vec.h:858
auto begin()
Get an iterator to the first element of the vector.
Definition: vec.h:752
vec< Type > operator*(Type scalar) const
Scalar multiplication (av = (v.x * a, ...))
Definition: vec.h:589
vec< Type > & operator*=(Type scalar)
Multiply the vector itself by a scalar.
Definition: vec.h:680
vec< Type > operator/(Type scalar) const
Scalar division (v / a = (v.x / a, ...))
Definition: vec.h:602
Type norm() const
Compute the norm of the vector (sqrt(v * v))
Definition: vec.h:706
vec< Type > & operator=(const Vector &other)
Copy from other.
Definition: vec.h:539
vec< Type > operator+() const
Identity.
Definition: vec.h:555
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
vec< Type, N > & operator=(const Vector &other)
Copy from other.
Definition: vec.h:139
bool operator!=(const Vector &other) const
Check whether all elements of both vectors are unequal.
Definition: vec.h:400
Type operator*(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition: vec.h:222
void set(unsigned int i, Type x)
Set the i-th element.
Definition: vec.h:332
auto begin() const
Get a const iterator to the first element of the vector.
Definition: vec.h:359
vec_iterator< vec< Type, N >, Type & > iterator
Sequential iterator for statically allocated vectors.
Definition: vec.h:338
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:426
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition: vec.h:416
vec()
Construct a vector with all elements equal to zero.
Definition: vec.h:104
Type get(unsigned int i) const
Getters and setters.
Definition: vec.h:326
vec< Type, N > operator-(const vec< Type, N > &other) const
Vector subtraction.
Definition: vec.h:183
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:444
auto end()
Get an iterator to one plus the last element of the vector.
Definition: vec.h:353
vec< Type, N > operator/(Type scalar) const
Scalar division (v / a = (v.x / a, ...))
Definition: vec.h:203
const Type & operator[](unsigned int i) const
Get the i-th component.
Definition: vec.h:314
vec< Type, N > cross(const vec< Type, N > &other) const
Cross product between vectors.
Definition: vec.h:228
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:341
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+(const vec< Type, N > &other) const
Vector sum (v + w = (v.x + w.x, ...))
Definition: vec.h:168
Type sqr_norm() const
Compute the square norm of the vector (v * v)
Definition: vec.h:302
std::string to_string(const std::string &separator=", ", bool parenthesis=true) const
Convert the vector to string representation.
Definition: vec.h:452
vec< Type, N > operator-() const
Opposite vector.
Definition: vec.h:177
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition: vec.h:406
vec< Type, N > operator*(Type scalar) const
Scalar multiplication (av = (v.x * a, ...))
Definition: vec.h:192
vec< Type, N > & operator-=(const Vector &other)
Subtract a vector the the vector itself.
Definition: vec.h:260
vec< Type, N > & operator+=(const Vector &other)
Sum a vector the the vector itself.
Definition: vec.h:249
Type & operator[](unsigned int i)
Access i-th component.
Definition: vec.h:308
vec< Type, N > operator+() const
Identity.
Definition: vec.h:162
vec< Type, N > & operator/=(Type scalar)
Divide the vector itself by a scalar.
Definition: vec.h:280
vec< Type, N > normalized() const
Return the normalized vector (v / |v|)
Definition: vec.h:378
Type norm() const
Compute the norm of the vector (sqrt(v * v))
Definition: vec.h:296
void normalize()
Vector normalization (v / |v|)
Definition: vec.h:372
vec(std::initializer_list< Type > l)
Initialize from a list, e.g. {1, 2, 3}.
Definition: vec.h:145
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:481
Type dot(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition: vec.h:215
auto begin()
Get an iterator to the first element of the vector.
Definition: vec.h:346
vec< Type, N > cross(const Vector &other) const
Cross product between vectors.
Definition: vec.h:236
Type & at(unsigned int i)
Access i-th element.
Definition: vec.h:320
vec< Type, N > & operator*=(Type scalar)
Multiply the vector itself by a scalar.
Definition: vec.h:270
bool operator==(const Vector &other) const
Check whether all elements of both vectors are equal.
Definition: vec.h:385
auto end() const
Get a const iterator to one plus the last element of the vector.
Definition: vec.h:366
vec(Type val)
Construct a vector with all elements equal to the given value.
Definition: vec.h:111
#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:219
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
Vector2 & vec_sum(Vector1 &v1, const Vector2 &v2)
Sum two vectors and store the result in the first vector.
Definition: algebra.h:1135
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition: algebra.h:62
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
Vector1 & vec_copy(Vector1 &dest, const Vector2 &src)
Copy a vector by overwriting another.
Definition: algebra.h:143
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
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
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition: core_traits.h:134
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition: dataset.h:330
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
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54