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"
24 template<
typename Vector,
typename ReturnType = vector_element_t<Vector>&>
37 using iterator_category = std::forward_iterator_tag;
39 using pointer = value_type*;
40 using reference = value_type&;
77 return !(*
this == other);
87 template<
typename Type = real,
unsigned int N = 0>
96 static constexpr
size_t size_argument = N;
109 for (
unsigned int i = 0; i < N; ++i)
121 for (
unsigned int i = 0; i < N; ++i)
127 template<
unsigned int M>
134 template<
typename Vector>
141 vec(std::initializer_list<Type> l) {
151 std::copy(l.begin(), l.end(), &data[0]);
174 return *
this * (Type) -1;
191 for (
unsigned int i = 0; i < N; ++i)
192 result.data[i] = scalar * data[i];
202 for (
unsigned int i = 0; i < N; ++i)
203 result.data[i] = data[i] / scalar;
210 template<
typename Vector>
211 inline Type
dot(
const Vector& other)
const {
217 template<
typename Vector>
225 static_assert(N == 3,
"The vector must be three dimensional");
231 template<
typename Vector>
234 if(other.size() != 3) {
244 template<
typename Vector>
247 for (
unsigned int i = 0; i < N; ++i)
248 data[i] += other.data[i];
255 template<
typename Vector>
258 for (
unsigned int i = 0; i < N; ++i)
259 data[i] -= other.data[i];
268 for (
unsigned int i = 0; i < N; ++i)
284 for (
unsigned int i = 0; i < N; ++i)
316 inline Type&
at(
unsigned int i) {
322 inline Type
get(
unsigned int i)
const {
328 inline void set(
unsigned int i, Type x) {
380 template<
typename Vector>
383 if(
size() != other.size())
386 for (
unsigned int i = 0; i < N; ++i)
387 if(data[i] != other[i])
395 template<
typename Vector>
397 return !(*
this == other);
423 unsigned int i,
unsigned int n = N) {
445 #ifndef THEORETICA_NO_PRINT
450 bool parenthesis =
true)
const {
452 std::stringstream res;
457 for (
unsigned int i = 0; i < N; ++i) {
492 template<
typename Type>
497 using Container = std::vector<T>;
500 Container<Type> data;
505 static constexpr
size_t size_argument = 0;
521 vec(
unsigned int n, Type a) {
522 data = std::vector<Type>(n, a);
527 template<
unsigned int M>
534 template<
typename Vector>
541 vec(std::initializer_list<Type> l) {
544 std::copy(l.begin(), l.end(), &data[0]);
557 template<
typename Vector>
569 return *
this * (Type) -1;
574 template<
typename Vector>
590 for (
unsigned int i = 0; i <
size(); ++i)
591 result.data[i] = scalar * data[i];
603 for (
unsigned int i = 0; i <
size(); ++i)
604 result.data[i] = data[i] / scalar;
611 template<
typename Vector>
612 inline Type
dot(
const Vector& other)
const {
618 template<
typename Vector>
625 template<
typename Vector>
628 if(other.size() != 3) {
638 template<
typename Vector>
647 if(
size() != other.size()) {
652 for (
unsigned int i = 0; i <
size(); ++i)
653 data[i] += other.data[i];
660 template<
typename Vector>
663 if(
size() != other.size()) {
668 for (
unsigned int i = 0; i <
size(); ++i)
669 data[i] -= other.data[i];
678 for (
unsigned int i = 0; i <
size(); ++i)
694 for (
unsigned int i = 0; i <
size(); ++i)
726 inline Type&
at(
unsigned int i) {
732 inline Type
get(
unsigned int i)
const {
738 inline void set(
unsigned int i, Type x) {
743 using iterator =
typename Container<Type>::iterator;
756 return data.cbegin();
787 template<
typename Vector>
790 if(
size() != other.size())
793 for (
unsigned int i = 0; i <
size(); ++i)
794 if(data[i] != other[i])
802 template<
typename Vector>
804 return !(*
this == other);
822 inline void push(
const Type& x) {
837 unsigned int i,
unsigned int n) {
859 #ifndef THEORETICA_NO_PRINT
864 bool parenthesis =
true)
const {
866 std::stringstream res;
871 for (
unsigned int i = 0; i <
size(); ++i) {
900 template<
typename ElementType,
typename Type,
typename ...Args>
901 void make_vec(vec<ElementType>& v,
size_t index, Type last) {
905 template<
typename ElementType,
typename Type,
typename ...Args>
906 void make_vec(vec<ElementType>& v,
size_t index, Type first, Args... elements) {
909 make_vec<ElementType>(v, index + 1, elements...);
915 template<
typename Type,
typename ...Args>
919 v.
resize(
sizeof...(elements) + 1);
922 make_vec<Type>(v, 1, elements...);
vec(std::initializer_list< Type > l)
Initialize from a list, e.g. {1, 2, 3}.
Definition: vec.h:541
std::string to_string(const std::string &separator=", ", bool parenthesis=true) const
Convert the vector to string representation.
Definition: vec.h:862
vec< Type > & operator-=(const Vector &other)
Subtract a vector from the vector itself.
Definition: vec.h:661
void push(const Type &x)
Add a value at the end of the vector (only for dynamically allocated vectors).
Definition: vec.h:822
vec< Type > & operator+=(const Vector &other)
Sum a vector to the vector itself.
Definition: vec.h:639
void resize(size_t n)
Change the size of the vector.
Definition: vec.h:815
vec< Type > operator+(const Vector &other) const
Vector sum (v + w = (v.x + w.x, ...))
Definition: vec.h:558
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:891
vec< Type > normalized() const
Return the normalized vector (v / |v|)
Definition: vec.h:781
void push(Type &&x)
Add a value at the end of the vector (only for dynamically allocated vectors).
Definition: vec.h:829
vec< Type > operator-(const Vector &other) const
Vector subtraction.
Definition: vec.h:575
const Type & operator[](unsigned int i) const
Get the i-th component.
Definition: vec.h:720
vec(unsigned int n, Type a)
Construct a vector with the given size and all elements equal to the given value.
Definition: vec.h:521
void normalize()
Vector normalization (v / |v|)
Definition: vec.h:775
Type & operator[](unsigned int i)
Access i-th component.
Definition: vec.h:714
vec< Type > cross(const Vector &other) const
Cross product between vectors.
Definition: vec.h:626
Type dot(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition: vec.h:612
Type get(unsigned int i) const
Getters and setters.
Definition: vec.h:732
auto begin() const
Get a const iterator to the first element of the vector.
Definition: vec.h:755
vec< Type > operator-() const
Opposite vector.
Definition: vec.h:568
void set(unsigned int i, Type x)
Set the i-th element.
Definition: vec.h:738
bool operator!=(const Vector &other) const
Check whether all elements of both vectors are unequal.
Definition: vec.h:803
vec(unsigned int n)
Construct a vector with the given size and all elements equal to zero.
Definition: vec.h:513
vec< Type > & operator/=(Type scalar)
Divide the vector itself by a scalar.
Definition: vec.h:686
vec()
Construct an empty vector.
Definition: vec.h:508
Type & at(unsigned int i)
Access i-th element.
Definition: vec.h:726
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:836
Type sqr_norm() const
Compute the square norm of the vector (v * v)
Definition: vec.h:708
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector.
Definition: vec.h:809
auto end()
Get an iterator to one plus the last element of the vector.
Definition: vec.h:762
vec(const vec< Type, M > &other)
Copy constructor.
Definition: vec.h:528
Type operator*(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition: vec.h:619
bool operator==(const Vector &other) const
Check whether all elements of both vectors are equal.
Definition: vec.h:788
auto end() const
Get a const iterator to one plus the last element of the vector.
Definition: vec.h:769
friend vec< Type > operator*(Type a, const vec< Type > &v)
Friend operator to enable equations of the form (Type) * (vec)
Definition: vec.h:854
auto begin()
Get an iterator to the first element of the vector.
Definition: vec.h:748
vec< Type > operator*(Type scalar) const
Scalar multiplication (av = (v.x * a, ...))
Definition: vec.h:585
vec< Type > & operator*=(Type scalar)
Multiply the vector itself by a scalar.
Definition: vec.h:676
vec< Type > operator/(Type scalar) const
Scalar division (v / a = (v.x / a, ...))
Definition: vec.h:598
Type norm() const
Compute the norm of the vector (sqrt(v * v))
Definition: vec.h:702
vec< Type > & operator=(const Vector &other)
Copy from other.
Definition: vec.h:535
vec< Type > operator+() const
Identity.
Definition: vec.h:551
for vectors.
Definition: vec.h:25
ReturnType operator*()
Dereference the iterator to get the current element.
Definition: vec.h:48
vec_iterator(Vector &vector, size_t index)
Construct the iterator from a pointer to the elements and a starting index.
Definition: vec.h:44
size_t index()
Move to the previous element in the vector.
Definition: vec.h:66
vec_iterator & operator++()
Move to the next element in the vector.
Definition: vec.h:53
bool operator==(const vec_iterator &other) const
Comparison operators.
Definition: vec.h:72
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
vec< Type, N > & operator=(const Vector &other)
Copy from other.
Definition: vec.h:135
bool operator!=(const Vector &other) const
Check whether all elements of both vectors are unequal.
Definition: vec.h:396
Type operator*(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition: vec.h:218
void set(unsigned int i, Type x)
Set the i-th element.
Definition: vec.h:328
auto begin() const
Get a const iterator to the first element of the vector.
Definition: vec.h:355
vec_iterator< vec< Type, N >, Type & > iterator
Sequential iterator for statically allocated vectors.
Definition: vec.h:334
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:422
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition: vec.h:412
vec()
Construct a vector with all elements equal to zero.
Definition: vec.h:100
Type get(unsigned int i) const
Getters and setters.
Definition: vec.h:322
vec< Type, N > operator-(const vec< Type, N > &other) const
Vector subtraction.
Definition: vec.h:179
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:440
auto end()
Get an iterator to one plus the last element of the vector.
Definition: vec.h:349
vec< Type, N > operator/(Type scalar) const
Scalar division (v / a = (v.x / a, ...))
Definition: vec.h:199
const Type & operator[](unsigned int i) const
Get the i-th component.
Definition: vec.h:310
vec< Type, N > cross(const vec< Type, N > &other) const
Cross product between vectors.
Definition: vec.h:224
vec(const vec< Type, M > &other)
Copy constructor.
Definition: vec.h:128
vec_iterator< const vec< Type, N >, const Type & > const_iterator
Const sequential iterator for statically allocated vectors.
Definition: vec.h:337
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:117
vec< Type, N > operator+(const vec< Type, N > &other) const
Vector sum (v + w = (v.x + w.x, ...))
Definition: vec.h:164
Type sqr_norm() const
Compute the square norm of the vector (v * v)
Definition: vec.h:298
std::string to_string(const std::string &separator=", ", bool parenthesis=true) const
Convert the vector to string representation.
Definition: vec.h:448
vec< Type, N > operator-() const
Opposite vector.
Definition: vec.h:173
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition: vec.h:402
vec< Type, N > operator*(Type scalar) const
Scalar multiplication (av = (v.x * a, ...))
Definition: vec.h:188
vec< Type, N > & operator-=(const Vector &other)
Subtract a vector the the vector itself.
Definition: vec.h:256
vec< Type, N > & operator+=(const Vector &other)
Sum a vector the the vector itself.
Definition: vec.h:245
Type & operator[](unsigned int i)
Access i-th component.
Definition: vec.h:304
vec< Type, N > operator+() const
Identity.
Definition: vec.h:158
vec< Type, N > & operator/=(Type scalar)
Divide the vector itself by a scalar.
Definition: vec.h:276
vec< Type, N > normalized() const
Return the normalized vector (v / |v|)
Definition: vec.h:374
Type norm() const
Compute the norm of the vector (sqrt(v * v))
Definition: vec.h:292
void normalize()
Vector normalization (v / |v|)
Definition: vec.h:368
vec(std::initializer_list< Type > l)
Initialize from a list, e.g. {1, 2, 3}.
Definition: vec.h:141
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:477
Type dot(const Vector &other) const
Dot product between vectors (v * w = v.x * w.x + ...)
Definition: vec.h:211
auto begin()
Get an iterator to the first element of the vector.
Definition: vec.h:342
vec< Type, N > cross(const Vector &other) const
Cross product between vectors.
Definition: vec.h:232
Type & at(unsigned int i)
Access i-th element.
Definition: vec.h:316
vec< Type, N > & operator*=(Type scalar)
Multiply the vector itself by a scalar.
Definition: vec.h:266
bool operator==(const Vector &other) const
Check whether all elements of both vectors are equal.
Definition: vec.h:381
auto end() const
Get a const iterator to one plus the last element of the vector.
Definition: vec.h:362
vec(Type val)
Construct a vector with all elements equal to the given value.
Definition: vec.h:107
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition: constants.h:151
#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
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:102
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:183
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
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54