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 
28  template<typename Vector, typename ReturnType = vector_element_t<Vector>&>
29  class vec_iterator {
30 
31  private:
32 
34  Vector& vector;
35 
37  size_t i;
38 
39  public:
40 
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&;
45 
48  vec_iterator(Vector& vector, size_t index) : vector(vector), i(index) {}
49 
52  ReturnType operator*() {
53  return vector[i];
54  }
55 
58  ++i;
59  return *this;
60  }
61 
63  // vec_iterator& operator--() {
64  // --i;
65  // return *this;
66  // }
67 
68 
70  size_t index() {
71  return i;
72  }
73 
74 
76  bool operator==(const vec_iterator& other) const {
77  return i == other.i;
78  }
79 
80  bool operator!=(const vec_iterator& other) const {
81  return !(*this == other);
82  }
83  };
84 
85 
91  template<typename Type = real, unsigned int N = 0>
92  class vec {
93 
94  private:
95  Type data[N];
96 
97  public:
98 
99  // Vector size template argument
100  static constexpr size_t size_argument = N;
101 
104  vec() {
105  algebra::vec_zeroes(*this);
106  }
107 
108 
111  vec(Type val) {
112 
113  for (unsigned int i = 0; i < N; ++i)
114  data[i] = val;
115  }
116 
117 
121  vec(unsigned int size, Type val) {
122 
123  resize(size);
124 
125  for (unsigned int i = 0; i < N; ++i)
126  data[i] = val;
127  }
128 
129 
131  template<unsigned int M>
132  vec(const vec<Type, M>& other) {
133  algebra::vec_copy(*this, other);
134  }
135 
136 
138  template<typename Vector>
139  vec<Type, N>& operator=(const Vector& other) {
140  return algebra::vec_copy(*this, other);
141  }
142 
143 
145  vec(std::initializer_list<Type> l) {
146 
147  if(l.size() != N) {
148  TH_MATH_ERROR("vec::vec(initializer_list<Type>)", l.size(),
149  INVALID_ARGUMENT);
150  // Set all elements to NaN
151  *this = vec<Type, N>(Type(nan()));
152  return;
153  }
154 
155  std::copy(l.begin(), l.end(), &data[0]);
156  }
157 
158  ~vec() = default;
159 
160 
162  inline vec<Type, N> operator+() const {
163  return *this;
164  }
165 
166 
168  inline vec<Type, N> operator+(const vec<Type, N>& other) const {
169 
170  vec<Type, N> result;
171  algebra::vec_sum(result, *this, other);
172  return result;
173  }
174 
175 
177  inline vec<Type, N> operator-() const {
178  return *this * (Type) -1;
179  }
180 
181 
183  inline vec<Type, N> operator-(const vec<Type, N>& other) const {
184 
185  vec<Type, N> result;
186  algebra::vec_diff(result, *this, other);
187  return result;
188  }
189 
190 
192  inline vec<Type, N> operator*(Type scalar) const {
193  vec<Type, N> result;
194 
195  for (unsigned int i = 0; i < N; ++i)
196  result.data[i] = scalar * data[i];
197 
198  return result;
199  }
200 
201 
203  inline vec<Type, N> operator/(Type scalar) const {
204  vec<Type, N> result;
205 
206  for (unsigned int i = 0; i < N; ++i)
207  result.data[i] = data[i] / scalar;
208 
209  return result;
210  }
211 
212 
214  template<typename Vector>
215  inline Type dot(const Vector& other) const {
216  return algebra::dot(*this, other);
217  }
218 
219 
221  template<typename Vector>
222  inline Type operator*(const Vector& other) const {
223  return dot(other);
224  }
225 
226 
228  inline vec<Type, N> cross(const vec<Type, N>& other) const {
229  static_assert(N == 3, "The vector must be three dimensional");
230  return algebra::cross(*this, other);
231  }
232 
233 
235  template<typename Vector>
236  inline vec<Type, N> cross(const Vector& other) const {
237 
238  if(other.size() != 3) {
239  TH_MATH_ERROR("vec::cross", other.size(), INVALID_ARGUMENT);
240  return vec<Type, N>(Type(nan()));
241  }
242 
243  return algebra::cross(*this, other);
244  }
245 
246 
248  template<typename Vector>
249  inline vec<Type, N>& operator+=(const Vector& other) {
250 
251  for (unsigned int i = 0; i < N; ++i)
252  data[i] += other.data[i];
253 
254  return *this;
255  }
256 
257 
259  template<typename Vector>
260  inline vec<Type, N>& operator-=(const Vector& other) {
261 
262  for (unsigned int i = 0; i < N; ++i)
263  data[i] -= other.data[i];
264 
265  return *this;
266  }
267 
268 
270  inline vec<Type, N>& operator*=(Type scalar) {
271 
272  for (unsigned int i = 0; i < N; ++i)
273  data[i] *= scalar;
274 
275  return *this;
276  }
277 
278 
280  inline vec<Type, N>& operator/=(Type scalar) {
281 
282  if(abs(scalar) < MACH_EPSILON) {
283  TH_MATH_ERROR("vec::operator/=", scalar, DIV_BY_ZERO);
284  vec_error(*this);
285  return *this;
286  }
287 
288  for (unsigned int i = 0; i < N; ++i)
289  data[i] /= scalar;
290 
291  return *this;
292  }
293 
294 
296  inline Type norm() const {
297  return algebra::norm(*this);
298  }
299 
300 
302  inline Type sqr_norm() const {
303  return algebra::sqr_norm(*this);
304  }
305 
306 
308  inline Type& operator[](unsigned int i) {
309  return data[i];
310  }
311 
312 
314  inline const Type& operator[](unsigned int i) const {
315  return data[i];
316  }
317 
318 
320  inline Type& at(unsigned int i) {
321  return data[i];
322  }
323 
324 
326  inline Type get(unsigned int i) const {
327  return data[i];
328  }
329 
330 
332  inline void set(unsigned int i, Type x) {
333  data[i] = x;
334  }
335 
336 
339 
342 
343 
346  inline auto begin() {
347  return iterator(*this, 0);
348  }
349 
350 
353  inline auto end() {
354  return iterator(*this, size());
355  }
356 
357 
359  inline auto begin() const {
360  return const_iterator(*this, 0);
361  }
362 
363 
366  inline auto end() const {
367  return const_iterator(*this, size());
368  }
369 
370 
372  inline void normalize() {
374  }
375 
376 
378  inline vec<Type, N> normalized() const {
379  return algebra::normalize(*this);
380  }
381 
382 
384  template<typename Vector>
385  inline bool operator==(const Vector& other) const {
386 
387  if(size() != other.size())
388  return false;
389 
390  for (unsigned int i = 0; i < N; ++i)
391  if(data[i] != other[i])
392  return false;
393 
394  return true;
395  }
396 
397 
399  template<typename Vector>
400  inline bool operator!=(const Vector& other) const {
401  return !(*this == other);
402  }
403 
404 
406  inline TH_CONSTEXPR unsigned int size() const {
407  return N;
408  }
409 
410 
416  inline void resize(size_t n) const {
417 
418  if(N != n) {
419  TH_MATH_ERROR("vec::resize", N, INVALID_ARGUMENT);
420  }
421  }
422 
423 
427  unsigned int i, unsigned int n = N) {
428 
429  if(i >= n) {
430  TH_MATH_ERROR("vec::euclidean_base", i, INVALID_ARGUMENT);
431  return vec<Type, N>(Type(nan()));
432  }
433 
434  vec<Type, N> e_i = vec<Type, N>(n, Type(0.0));
435  e_i.resize(n);
436  e_i[i] = 1;
437 
438  return e_i;
439  }
440 
441 
444  inline friend vec<Type, N> operator*(Type a, const vec<Type, N>& v) {
445  return v * a;
446  }
447 
448 
449 #ifndef THEORETICA_NO_PRINT
450 
452  inline std::string to_string(
453  const std::string& separator = ", ",
454  bool parenthesis = true) const {
455 
456  std::stringstream res;
457 
458  if(parenthesis)
459  res << "(";
460 
461  for (unsigned int i = 0; i < N; ++i) {
462  res << data[i];
463  if(i != N - 1)
464  res << separator;
465  }
466 
467  if(parenthesis)
468  res << ")";
469 
470  return res.str();
471  }
472 
473 
475  inline operator std::string() {
476  return to_string();
477  }
478 
479 
481  inline friend std::ostream& operator<<(
482  std::ostream& out, const vec<Type, N>& obj) {
483  return out << obj.to_string();
484  }
485 
486 #endif
487 
488  };
489 
490 
496  template<typename Type>
497  class vec<Type, 0> {
498 
499  // Container type for storage (alias for std::vector)
500  template<typename T>
501  using Container = std::vector<T>;
502 
503  private:
504  Container<Type> data;
505 
506  public:
507 
508  // Vector size template argument
509  static constexpr size_t size_argument = 0;
510 
512  vec() {}
513 
514 
517  vec(unsigned int n) {
518  resize(n);
519  algebra::vec_zeroes(*this);
520  }
521 
522 
525  vec(unsigned int n, Type a) {
526  data = std::vector<Type>(n, a);
527  }
528 
529 
531  template<unsigned int M>
532  vec(const vec<Type, M>& other) {
533  algebra::vec_copy(*this, other);
534  }
535 
536 
538  template<typename Vector>
539  vec<Type>& operator=(const Vector& other) {
540  return algebra::vec_copy(*this, other);
541  }
542 
543 
545  vec(std::initializer_list<Type> l) {
546 
547  resize(l.size());
548  std::copy(l.begin(), l.end(), &data[0]);
549  }
550 
551  ~vec() = default;
552 
553 
555  inline vec<Type> operator+() const {
556  return *this;
557  }
558 
559 
561  template<typename Vector>
562  inline vec<Type> operator+(const Vector& other) const {
563 
564  vec<Type> result;
565  result.resize(size());
566  algebra::vec_sum(result, *this, other);
567  return result;
568  }
569 
570 
572  inline vec<Type> operator-() const {
573  return *this * (Type) -1;
574  }
575 
576 
578  template<typename Vector>
579  inline vec<Type> operator-(const Vector& other) const {
580 
581  vec<Type> result;
582  result.resize(size());
583  algebra::vec_diff(result, *this, other);
584  return result;
585  }
586 
587 
589  inline vec<Type> operator*(Type scalar) const {
590 
591  vec<Type> result;
592  result.resize(size());
593 
594  for (unsigned int i = 0; i < size(); ++i)
595  result.data[i] = scalar * data[i];
596 
597  return result;
598  }
599 
600 
602  inline vec<Type> operator/(Type scalar) const {
603 
604  vec<Type> result;
605  result.resize(size());
606 
607  for (unsigned int i = 0; i < size(); ++i)
608  result.data[i] = data[i] / scalar;
609 
610  return result;
611  }
612 
613 
615  template<typename Vector>
616  inline Type dot(const Vector& other) const {
617  return algebra::dot(*this, other);
618  }
619 
620 
622  template<typename Vector>
623  inline Type operator*(const Vector& other) const {
624  return dot(other);
625  }
626 
627 
629  template<typename Vector>
630  inline vec<Type> cross(const Vector& other) const {
631 
632  if(other.size() != 3) {
633  TH_MATH_ERROR("vec::cross", other.size(), INVALID_ARGUMENT);
634  return vec<Type>(3, nan());
635  }
636 
637  return algebra::cross(*this, other);
638  }
639 
640 
642  template<typename Vector>
643  inline vec<Type>& operator+=(const Vector& other) {
644 
645  // If the vector is uninitialized,
646  // initialize it to be a zero vector
647  // with the target size
648  if(size() == 0)
649  resize(other.size());
650 
651  if(size() != other.size()) {
652  TH_MATH_ERROR("vec::operator+=", size(), INVALID_ARGUMENT);
653  return (*this = vec<Type>(max(size(), 1), nan()));
654  }
655 
656  for (unsigned int i = 0; i < size(); ++i)
657  data[i] += other.data[i];
658 
659  return *this;
660  }
661 
662 
664  template<typename Vector>
665  inline vec<Type>& operator-=(const Vector& other) {
666 
667  if(size() != other.size()) {
668  TH_MATH_ERROR("vec::operator-=", size(), INVALID_ARGUMENT);
669  return (*this = vec<Type>(max(size(), 1), nan()));
670  }
671 
672  for (unsigned int i = 0; i < size(); ++i)
673  data[i] -= other.data[i];
674 
675  return *this;
676  }
677 
678 
680  inline vec<Type>& operator*=(Type scalar) {
681 
682  for (unsigned int i = 0; i < size(); ++i)
683  data[i] *= scalar;
684 
685  return *this;
686  }
687 
688 
690  inline vec<Type>& operator/=(Type scalar) {
691 
692  if(abs(scalar) < MACH_EPSILON) {
693  TH_MATH_ERROR("vec::operator/=", scalar, DIV_BY_ZERO);
694  *this = vec<Type>(max(size(), 1), nan());
695  return *this;
696  }
697 
698  for (unsigned int i = 0; i < size(); ++i)
699  data[i] /= scalar;
700 
701  return *this;
702  }
703 
704 
706  inline Type norm() const {
707  return algebra::norm(*this);
708  }
709 
710 
712  inline Type sqr_norm() const {
713  return algebra::sqr_norm(*this);
714  }
715 
716 
718  inline Type& operator[](unsigned int i) {
719  return data[i];
720  }
721 
722 
724  inline const Type& operator[](unsigned int i) const {
725  return data[i];
726  }
727 
728 
730  inline Type& at(unsigned int i) {
731  return data[i];
732  }
733 
734 
736  inline Type get(unsigned int i) const {
737  return data[i];
738  }
739 
740 
742  inline void set(unsigned int i, Type x) {
743  data[i] = x;
744  }
745 
746 
747  using iterator = typename Container<Type>::iterator;
748 
749 
752  inline auto begin() {
753  return data.begin();
754  }
755 
756 
759  inline auto begin() const {
760  return data.cbegin();
761  }
762 
763 
766  inline auto end() {
767  return data.end();
768  }
769 
770 
773  inline auto end() const {
774  return data.cend();
775  }
776 
777 
779  inline void normalize() {
781  }
782 
783 
785  inline vec<Type> normalized() const {
786  return algebra::normalize(*this);
787  }
788 
789 
791  template<typename Vector>
792  inline bool operator==(const Vector& other) const {
793 
794  if(size() != other.size())
795  return false;
796 
797  for (unsigned int i = 0; i < size(); ++i)
798  if(data[i] != other[i])
799  return false;
800 
801  return true;
802  }
803 
804 
806  template<typename Vector>
807  inline bool operator!=(const Vector& other) const {
808  return !(*this == other);
809  }
810 
811 
813  inline TH_CONSTEXPR unsigned int size() const {
814  return data.size();
815  }
816 
817 
819  inline void resize(size_t n) {
820  data.resize(n);
821  }
822 
823 
826  inline void push(const Type& x) {
827  data.push_back(x);
828  }
829 
830 
833  inline void push(Type&& x) {
834  data.push_back(x);
835  }
836 
837 
840  inline static vec<Type> euclidean_base(
841  unsigned int i, unsigned int n) {
842 
843  if(i >= n) {
844  TH_MATH_ERROR("vec::euclidean_base", i, INVALID_ARGUMENT);
845  return vec<Type>(n, nan());
846  }
847 
848  vec<Type> e_i = vec<Type>(n, Type(0.0));
849  e_i.resize(n);
850  e_i[i] = 1;
851 
852  return e_i;
853  }
854 
855 
858  inline friend vec<Type> operator*(Type a, const vec<Type>& v) {
859  return v * a;
860  }
861 
862 
863 #ifndef THEORETICA_NO_PRINT
864 
866  inline std::string to_string(
867  const std::string& separator = ", ",
868  bool parenthesis = true) const {
869 
870  std::stringstream res;
871 
872  if(parenthesis)
873  res << "(";
874 
875  for (unsigned int i = 0; i < size(); ++i) {
876  res << data[i];
877  if(i != size() - 1)
878  res << separator;
879  }
880 
881  if(parenthesis)
882  res << ")";
883 
884  return res.str();
885  }
886 
887 
889  inline operator std::string() {
890  return to_string();
891  }
892 
893 
895  inline friend std::ostream& operator<<(std::ostream& out, const vec<Type>& obj) {
896  return out << obj.to_string();
897  }
898 
899 #endif
900 
901  };
902 
903 
910  template<typename ElementType, typename Type, typename ...Args>
911  void make_vec(vec<ElementType>& v, size_t index, Type last) {
912  v[index] = last;
913  }
914 
915 
924  template<typename ElementType, typename Type, typename ...Args>
925  void make_vec(vec<ElementType>& v, size_t index, Type first, Args... elements) {
926 
927  v[index] = first;
928  make_vec<ElementType>(v, index + 1, elements...);
929  }
930 
931 
934  template<typename Type, typename ...Args>
935  vec<Type> make_vec(Type first, Args... elements) {
936 
937  vec<Type> v;
938  v.resize(sizeof...(elements) + 1);
939 
940  v[0] = first;
941  make_vec<Type>(v, 1, elements...);
942 
943  return v;
944  }
945 
946 }
947 
948 
949 #endif
Linear algebra routines.
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