Theoretica
A C++ numerical and automatic mathematical library
vec.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_VECTOR_H
7 #define THEORETICA_VECTOR_H
8 
9 #ifndef THEORETICA_NO_PRINT
10 #include <sstream>
11 #include <ostream>
12 #endif
13 
14 #include "../core/error.h"
15 #include "../core/real_analysis.h"
16 #include "./algebra.h"
17 #include <vector>
18 
19 
20 namespace theoretica {
21 
22 
24  template<typename Vector, typename ReturnType = vector_element_t<Vector>&>
25  class vec_iterator {
26 
27  private:
28 
30  Vector& vector;
31 
33  size_t i;
34 
35  public:
36 
37  using iterator_category = std::forward_iterator_tag;
38  using value_type = vector_element_t<Vector>;
39  using pointer = value_type*;
40  using reference = value_type&;
41 
44  vec_iterator(Vector& vector, size_t index) : vector(vector), i(index) {}
45 
48  ReturnType operator*() {
49  return vector[i];
50  }
51 
54  ++i;
55  return *this;
56  }
57 
59  // vec_iterator& operator--() {
60  // --i;
61  // return *this;
62  // }
63 
64 
66  size_t index() {
67  return i;
68  }
69 
70 
72  bool operator==(const vec_iterator& other) const {
73  return i == other.i;
74  }
75 
76  bool operator!=(const vec_iterator& other) const {
77  return !(*this == other);
78  }
79  };
80 
81 
87  template<typename Type = real, unsigned int N = 0>
88  class vec {
89 
90  private:
91  Type data[N];
92 
93  public:
94 
95  // Vector size template argument
96  static constexpr size_t size_argument = N;
97 
100  vec() {
101  algebra::vec_zeroes(*this);
102  }
103 
104 
107  vec(Type val) {
108 
109  for (unsigned int i = 0; i < N; ++i)
110  data[i] = val;
111  }
112 
113 
117  vec(unsigned int size, Type val) {
118 
119  resize(size);
120 
121  for (unsigned int i = 0; i < N; ++i)
122  data[i] = val;
123  }
124 
125 
127  template<unsigned int M>
128  vec(const vec<Type, M>& other) {
129  algebra::vec_copy(*this, other);
130  }
131 
132 
134  template<typename Vector>
135  vec<Type, N>& operator=(const Vector& other) {
136  return algebra::vec_copy(*this, other);
137  }
138 
139 
141  vec(std::initializer_list<Type> l) {
142 
143  if(l.size() != N) {
144  TH_MATH_ERROR("vec::vec(initializer_list<Type>)", l.size(),
145  INVALID_ARGUMENT);
146  // Set all elements to NaN
147  *this = vec<Type, N>(Type(nan()));
148  return;
149  }
150 
151  std::copy(l.begin(), l.end(), &data[0]);
152  }
153 
154  ~vec() = default;
155 
156 
158  inline vec<Type, N> operator+() const {
159  return *this;
160  }
161 
162 
164  inline vec<Type, N> operator+(const vec<Type, N>& other) const {
165 
166  vec<Type, N> result;
167  algebra::vec_sum(result, *this, other);
168  return result;
169  }
170 
171 
173  inline vec<Type, N> operator-() const {
174  return *this * (Type) -1;
175  }
176 
177 
179  inline vec<Type, N> operator-(const vec<Type, N>& other) const {
180 
181  vec<Type, N> result;
182  algebra::vec_diff(result, *this, other);
183  return result;
184  }
185 
186 
188  inline vec<Type, N> operator*(Type scalar) const {
189  vec<Type, N> result;
190 
191  for (unsigned int i = 0; i < N; ++i)
192  result.data[i] = scalar * data[i];
193 
194  return result;
195  }
196 
197 
199  inline vec<Type, N> operator/(Type scalar) const {
200  vec<Type, N> result;
201 
202  for (unsigned int i = 0; i < N; ++i)
203  result.data[i] = data[i] / scalar;
204 
205  return result;
206  }
207 
208 
210  template<typename Vector>
211  inline Type dot(const Vector& other) const {
212  return algebra::dot(*this, other);
213  }
214 
215 
217  template<typename Vector>
218  inline Type operator*(const Vector& other) const {
219  return dot(other);
220  }
221 
222 
224  inline vec<Type, N> cross(const vec<Type, N>& other) const {
225  static_assert(N == 3, "The vector must be three dimensional");
226  return algebra::cross(*this, other);
227  }
228 
229 
231  template<typename Vector>
232  inline vec<Type, N> cross(const Vector& other) const {
233 
234  if(other.size() != 3) {
235  TH_MATH_ERROR("vec::cross", other.size(), INVALID_ARGUMENT);
236  return vec<Type, N>(Type(nan()));
237  }
238 
239  return algebra::cross(*this, other);
240  }
241 
242 
244  template<typename Vector>
245  inline vec<Type, N>& operator+=(const Vector& other) {
246 
247  for (unsigned int i = 0; i < N; ++i)
248  data[i] += other.data[i];
249 
250  return *this;
251  }
252 
253 
255  template<typename Vector>
256  inline vec<Type, N>& operator-=(const Vector& other) {
257 
258  for (unsigned int i = 0; i < N; ++i)
259  data[i] -= other.data[i];
260 
261  return *this;
262  }
263 
264 
266  inline vec<Type, N>& operator*=(Type scalar) {
267 
268  for (unsigned int i = 0; i < N; ++i)
269  data[i] *= scalar;
270 
271  return *this;
272  }
273 
274 
276  inline vec<Type, N>& operator/=(Type scalar) {
277 
278  if(abs(scalar) < MACH_EPSILON) {
279  TH_MATH_ERROR("vec::operator/=", scalar, DIV_BY_ZERO);
280  vec_error(*this);
281  return *this;
282  }
283 
284  for (unsigned int i = 0; i < N; ++i)
285  data[i] /= scalar;
286 
287  return *this;
288  }
289 
290 
292  inline Type norm() const {
293  return algebra::norm(*this);
294  }
295 
296 
298  inline Type sqr_norm() const {
299  return algebra::sqr_norm(*this);
300  }
301 
302 
304  inline Type& operator[](unsigned int i) {
305  return data[i];
306  }
307 
308 
310  inline const Type& operator[](unsigned int i) const {
311  return data[i];
312  }
313 
314 
316  inline Type& at(unsigned int i) {
317  return data[i];
318  }
319 
320 
322  inline Type get(unsigned int i) const {
323  return data[i];
324  }
325 
326 
328  inline void set(unsigned int i, Type x) {
329  data[i] = x;
330  }
331 
332 
335 
338 
339 
342  inline auto begin() {
343  return iterator(*this, 0);
344  }
345 
346 
349  inline auto end() {
350  return iterator(*this, size());
351  }
352 
353 
355  inline auto begin() const {
356  return const_iterator(*this, 0);
357  }
358 
359 
362  inline auto end() const {
363  return const_iterator(*this, size());
364  }
365 
366 
368  inline void normalize() {
370  }
371 
372 
374  inline vec<Type, N> normalized() const {
375  return algebra::normalize(*this);
376  }
377 
378 
380  template<typename Vector>
381  inline bool operator==(const Vector& other) const {
382 
383  if(size() != other.size())
384  return false;
385 
386  for (unsigned int i = 0; i < N; ++i)
387  if(data[i] != other[i])
388  return false;
389 
390  return true;
391  }
392 
393 
395  template<typename Vector>
396  inline bool operator!=(const Vector& other) const {
397  return !(*this == other);
398  }
399 
400 
402  inline TH_CONSTEXPR unsigned int size() const {
403  return N;
404  }
405 
406 
412  inline void resize(size_t n) const {
413 
414  if(N != n) {
415  TH_MATH_ERROR("vec::resize", N, INVALID_ARGUMENT);
416  }
417  }
418 
419 
423  unsigned int i, unsigned int n = N) {
424 
425  if(i >= n) {
426  TH_MATH_ERROR("vec::euclidean_base", i, INVALID_ARGUMENT);
427  return vec<Type, N>(Type(nan()));
428  }
429 
430  vec<Type, N> e_i = vec<Type, N>(n, Type(0.0));
431  e_i.resize(n);
432  e_i[i] = 1;
433 
434  return e_i;
435  }
436 
437 
440  inline friend vec<Type, N> operator*(Type a, const vec<Type, N>& v) {
441  return v * a;
442  }
443 
444 
445 #ifndef THEORETICA_NO_PRINT
446 
449  const std::string& separator = ", ",
450  bool parenthesis = true) const {
451 
452  std::stringstream res;
453 
454  if(parenthesis)
455  res << "(";
456 
457  for (unsigned int i = 0; i < N; ++i) {
458  res << data[i];
459  if(i != N - 1)
460  res << separator;
461  }
462 
463  if(parenthesis)
464  res << ")";
465 
466  return res.str();
467  }
468 
469 
471  inline operator std::string() {
472  return to_string();
473  }
474 
475 
477  inline friend std::ostream& operator<<(
478  std::ostream& out, const vec<Type, N>& obj) {
479  return out << obj.to_string();
480  }
481 
482 #endif
483 
484  };
485 
486 
492  template<typename Type>
493  class vec<Type, 0> {
494 
495  // Container type for storage (alias for std::vector)
496  template<typename T>
497  using Container = std::vector<T>;
498 
499  private:
500  Container<Type> data;
501 
502  public:
503 
504  // Vector size template argument
505  static constexpr size_t size_argument = 0;
506 
508  vec() {}
509 
510 
513  vec(unsigned int n) {
514  resize(n);
515  algebra::vec_zeroes(*this);
516  }
517 
518 
521  vec(unsigned int n, Type a) {
522  data = std::vector<Type>(n, a);
523  }
524 
525 
527  template<unsigned int M>
528  vec(const vec<Type, M>& other) {
529  algebra::vec_copy(*this, other);
530  }
531 
532 
534  template<typename Vector>
535  vec<Type>& operator=(const Vector& other) {
536  return algebra::vec_copy(*this, other);
537  }
538 
539 
541  vec(std::initializer_list<Type> l) {
542 
543  resize(l.size());
544  std::copy(l.begin(), l.end(), &data[0]);
545  }
546 
547  ~vec() = default;
548 
549 
551  inline vec<Type> operator+() const {
552  return *this;
553  }
554 
555 
557  template<typename Vector>
558  inline vec<Type> operator+(const Vector& other) const {
559 
560  vec<Type> result;
561  result.resize(size());
562  algebra::vec_sum(result, *this, other);
563  return result;
564  }
565 
566 
568  inline vec<Type> operator-() const {
569  return *this * (Type) -1;
570  }
571 
572 
574  template<typename Vector>
575  inline vec<Type> operator-(const Vector& other) const {
576 
577  vec<Type> result;
578  result.resize(size());
579  algebra::vec_diff(result, *this, other);
580  return result;
581  }
582 
583 
585  inline vec<Type> operator*(Type scalar) const {
586 
587  vec<Type> result;
588  result.resize(size());
589 
590  for (unsigned int i = 0; i < size(); ++i)
591  result.data[i] = scalar * data[i];
592 
593  return result;
594  }
595 
596 
598  inline vec<Type> operator/(Type scalar) const {
599 
600  vec<Type> result;
601  result.resize(size());
602 
603  for (unsigned int i = 0; i < size(); ++i)
604  result.data[i] = data[i] / scalar;
605 
606  return result;
607  }
608 
609 
611  template<typename Vector>
612  inline Type dot(const Vector& other) const {
613  return algebra::dot(*this, other);
614  }
615 
616 
618  template<typename Vector>
619  inline Type operator*(const Vector& other) const {
620  return dot(other);
621  }
622 
623 
625  template<typename Vector>
626  inline vec<Type> cross(const Vector& other) const {
627 
628  if(other.size() != 3) {
629  TH_MATH_ERROR("vec::cross", other.size(), INVALID_ARGUMENT);
630  return vec<Type>(3, nan());
631  }
632 
633  return algebra::cross(*this, other);
634  }
635 
636 
638  template<typename Vector>
639  inline vec<Type>& operator+=(const Vector& other) {
640 
641  // If the vector is uninitialized,
642  // initialize it to be a zero vector
643  // with the target size
644  if(size() == 0)
645  resize(other.size());
646 
647  if(size() != other.size()) {
648  TH_MATH_ERROR("vec::operator+=", size(), INVALID_ARGUMENT);
649  return (*this = vec<Type>(max(size(), 1), nan()));
650  }
651 
652  for (unsigned int i = 0; i < size(); ++i)
653  data[i] += other.data[i];
654 
655  return *this;
656  }
657 
658 
660  template<typename Vector>
661  inline vec<Type>& operator-=(const Vector& other) {
662 
663  if(size() != other.size()) {
664  TH_MATH_ERROR("vec::operator-=", size(), INVALID_ARGUMENT);
665  return (*this = vec<Type>(max(size(), 1), nan()));
666  }
667 
668  for (unsigned int i = 0; i < size(); ++i)
669  data[i] -= other.data[i];
670 
671  return *this;
672  }
673 
674 
676  inline vec<Type>& operator*=(Type scalar) {
677 
678  for (unsigned int i = 0; i < size(); ++i)
679  data[i] *= scalar;
680 
681  return *this;
682  }
683 
684 
686  inline vec<Type>& operator/=(Type scalar) {
687 
688  if(abs(scalar) < MACH_EPSILON) {
689  TH_MATH_ERROR("vec::operator/=", scalar, DIV_BY_ZERO);
690  *this = vec<Type>(max(size(), 1), nan());
691  return *this;
692  }
693 
694  for (unsigned int i = 0; i < size(); ++i)
695  data[i] /= scalar;
696 
697  return *this;
698  }
699 
700 
702  inline Type norm() const {
703  return algebra::norm(*this);
704  }
705 
706 
708  inline Type sqr_norm() const {
709  return algebra::sqr_norm(*this);
710  }
711 
712 
714  inline Type& operator[](unsigned int i) {
715  return data[i];
716  }
717 
718 
720  inline const Type& operator[](unsigned int i) const {
721  return data[i];
722  }
723 
724 
726  inline Type& at(unsigned int i) {
727  return data[i];
728  }
729 
730 
732  inline Type get(unsigned int i) const {
733  return data[i];
734  }
735 
736 
738  inline void set(unsigned int i, Type x) {
739  data[i] = x;
740  }
741 
742 
743  using iterator = typename Container<Type>::iterator;
744 
745 
748  inline auto begin() {
749  return data.begin();
750  }
751 
752 
755  inline auto begin() const {
756  return data.cbegin();
757  }
758 
759 
762  inline auto end() {
763  return data.end();
764  }
765 
766 
769  inline auto end() const {
770  return data.cend();
771  }
772 
773 
775  inline void normalize() {
777  }
778 
779 
781  inline vec<Type> normalized() const {
782  return algebra::normalize(*this);
783  }
784 
785 
787  template<typename Vector>
788  inline bool operator==(const Vector& other) const {
789 
790  if(size() != other.size())
791  return false;
792 
793  for (unsigned int i = 0; i < size(); ++i)
794  if(data[i] != other[i])
795  return false;
796 
797  return true;
798  }
799 
800 
802  template<typename Vector>
803  inline bool operator!=(const Vector& other) const {
804  return !(*this == other);
805  }
806 
807 
809  inline TH_CONSTEXPR unsigned int size() const {
810  return data.size();
811  }
812 
813 
815  inline void resize(size_t n) {
816  data.resize(n);
817  }
818 
819 
822  inline void push(const Type& x) {
823  data.push_back(x);
824  }
825 
826 
829  inline void push(Type&& x) {
830  data.push_back(x);
831  }
832 
833 
836  inline static vec<Type> euclidean_base(
837  unsigned int i, unsigned int n) {
838 
839  if(i >= n) {
840  TH_MATH_ERROR("vec::euclidean_base", i, INVALID_ARGUMENT);
841  return vec<Type>(n, nan());
842  }
843 
844  vec<Type> e_i = vec<Type>(n, Type(0.0));
845  e_i.resize(n);
846  e_i[i] = 1;
847 
848  return e_i;
849  }
850 
851 
854  inline friend vec<Type> operator*(Type a, const vec<Type>& v) {
855  return v * a;
856  }
857 
858 
859 #ifndef THEORETICA_NO_PRINT
860 
863  const std::string& separator = ", ",
864  bool parenthesis = true) const {
865 
866  std::stringstream res;
867 
868  if(parenthesis)
869  res << "(";
870 
871  for (unsigned int i = 0; i < size(); ++i) {
872  res << data[i];
873  if(i != size() - 1)
874  res << separator;
875  }
876 
877  if(parenthesis)
878  res << ")";
879 
880  return res.str();
881  }
882 
883 
885  inline operator std::string() {
886  return to_string();
887  }
888 
889 
891  inline friend std::ostream& operator<<(std::ostream& out, const vec<Type>& obj) {
892  return out << obj.to_string();
893  }
894 
895 #endif
896 
897  };
898 
899 
900  template<typename ElementType, typename Type, typename ...Args>
901  void make_vec(vec<ElementType>& v, size_t index, Type last) {
902  v[index] = last;
903  }
904 
905  template<typename ElementType, typename Type, typename ...Args>
906  void make_vec(vec<ElementType>& v, size_t index, Type first, Args... elements) {
907 
908  v[index] = first;
909  make_vec<ElementType>(v, index + 1, elements...);
910  }
911 
912 
915  template<typename Type, typename ...Args>
916  vec<Type> make_vec(Type first, Args... elements) {
917 
918  vec<Type> v;
919  v.resize(sizeof...(elements) + 1);
920 
921  v[0] = first;
922  make_vec<Type>(v, 1, elements...);
923 
924  return v;
925  }
926 
927 }
928 
929 
930 #endif
Linear algebra routines.
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