13 #include <initializer_list> 16 #include <type_traits> 19 #include "mu/typetraits.h" 20 #include "mu/utility.h" 31 template <std::
size_t X, std::
size_t Y,
typename Z>
47 template <std::
size_t N,
typename T>
49 static_assert(N != 0,
"Vector dimension cannot be zero");
50 static_assert(std::is_arithmetic<T>::value,
51 "Vector type T must be an arithmetic type");
55 using value_type =
typename std::array<T, N>::value_type;
56 using size_type =
typename std::array<T, N>::size_type;
58 using iterator =
typename std::array<T, N>::iterator;
59 using const_iterator =
typename std::array<T, N>::const_iterator;
67 constexpr
Vector() =
default;
80 template <
typename... TArgs,
82 sizeof...(TArgs) == N &&
83 (mu::conjunction<std::is_same<
84 T, std::remove_reference_t<TArgs>>::value...>::value),
87 Vector(TArgs &&... args) : data_{std::forward<TArgs>(args)...} {}
101 template <std::
size_t Nn,
class U>
104 static_assert(N == Nn,
"Vector size mismatch");
105 std::transform(v.
begin(), v.
end(),
begin(), [](U data) {
return data; });
116 Vector(
const std::array<T, N> &a) : data_{a} {}
128 template <
typename U = T,
129 std::enable_if_t<std::is_arithmetic<U>::value,
int> = 0>
132 std::transform(a.begin(), a.end(),
begin(), [](U data) {
return data; });
145 template <
typename U = T,
146 std::enable_if_t<std::is_arithmetic<U>::value,
int> = 0>
218 const T &
operator[](size_type idx)
const noexcept {
return data_[idx]; }
229 T &
at(size_type idx) {
return data_.at(idx); }
240 const T &
at(size_type idx)
const {
return data_.at(idx); }
249 constexpr size_type
size() const noexcept {
return N; }
258 iterator
begin() noexcept {
return data_.begin(); }
267 const_iterator
begin() const noexcept {
return data_.begin(); }
277 iterator
end() noexcept {
return data_.end(); }
287 const_iterator
end() const noexcept {
return data_.end(); }
298 for (std::size_t i = 1; i < N; i++) {
299 ret = mu::min(ret, data_[i]);
313 for (std::size_t i = 1; i < N; i++) {
314 ret = mu::max(ret, data_[i]);
328 for (
const auto &item : data_) {
348 template <
typename U = T>
379 template <
typename U =
void, std::
size_t N2,
typename T2>
380 std::conditional_t<std::is_same<U, void>::value, T, U>
dot(
382 static_assert(N == N2,
"Vector size mismatch");
383 using U_ = std::conditional_t<!std::is_same<T, T2>::value, U, T>;
384 static_assert(!std::is_same<U_, void>::value,
385 "Vector types are different. please specify the return " 386 "type. e.g. \"vec1.dot<float>(vec2);\"");
388 for (std::size_t i = 0; i < N; i++) {
389 ret += data_[i] * rhs[i];
426 template <
typename U =
void, std::
size_t N2, std::
size_t M2,
typename T2>
429 static_assert(N == N2,
430 "Vector-Matrix dimension mismatch. Vector size must be equal " 431 "to the first dimension of the matrix");
432 using U_ = std::conditional_t<!std::is_same<T, T2>::value, U, T>;
434 !std::is_same<U_, void>::value,
435 "Vector and Matrix types are different. please specify the return " 436 "type. e.g. \"vec.dot<float>(mat);\"");
438 for (std::size_t i = 0; i < M2; i++) {
440 for (std::size_t k = 0; k < N; k++) {
441 sum += (data_[k] * rhs[k][i]);
458 template <
class U = T>
462 for (
const auto &item : data_) {
463 sum += mu::pow(item - m, 2);
465 return U(mu::sqrt(
sum / N));
480 template <
class U = T>
482 return U(mu::sqrt(
dot(*
this)));
551 template <
typename Compare>
552 void sort(
const Compare &comp) {
578 template <
typename Compare>
598 template <std::
size_t Nn,
class U>
599 friend std::ostream &operator<<(std::ostream &os, const Vector<Nn, U> &v);
616 template <
typename U = T>
618 for (std::size_t i = 0; i < N; i++) {
619 if (!mu::TypeTraits<T>::equals(data_[i], rhs[i]) ||
620 !mu::TypeTraits<U>::equals(data_[i], rhs[i])) {
633 template <
typename U = T>
647 template <
typename U = T>
649 for (std::size_t i = 0; i < N; i++) {
664 template <
typename U = T>
666 for (std::size_t i = 0; i < N; i++) {
681 template <
typename U = T>
683 for (std::size_t i = 0; i < N; i++) {
700 template <
typename U = T>
702 for (std::size_t i = 0; i < N; i++) {
727 template <
class TScalar>
728 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
Vector<N, T> &>
730 for (
auto &item : data_) {
744 template <
class TScalar>
745 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
Vector<N, T> &>
747 for (
auto &item : data_) {
760 template <
class TScalar>
761 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
Vector<N, T> &>
763 for (
auto &item : data_) {
779 template <
class TScalar>
780 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
Vector<N, T> &>
785 if (std::is_integral<TScalar>::value) {
786 assert(scalar != static_cast<TScalar>(0));
788 for (
auto &item : data_) {
797 std::array<T, N> data_;
802 template <std::
size_t Nn,
class U>
803 std::ostream &operator<<(std::ostream &os, const Vector<Nn, U> &v) {
805 for (std::size_t i = 0; i < Nn; i++) {
829 template <std::
size_t N,
class T,
class U = T>
845 template <std::
size_t N,
class T,
class U = T>
861 template <std::
size_t N,
class T,
class U = T>
877 template <std::
size_t N,
class T,
class U = T>
897 template <std::
size_t N,
class T,
class TScalar>
898 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
916 template <std::
size_t N,
class T,
class TScalar>
917 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
933 template <std::
size_t N,
class T,
class TScalar>
934 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
952 template <std::
size_t N,
class T,
class TScalar>
953 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
971 template <std::
size_t N,
class T,
class TScalar>
972 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
988 template <std::
size_t N,
class T,
class TScalar>
989 typename std::enable_if_t<std::is_arithmetic<TScalar>::value,
997 template <std::
size_t N,
class T>
1002 template <std::
size_t N,
class T>
1007 template <std::
size_t N,
class T>
1012 template <
class U =
void, std::
size_t N,
typename T>
1013 inline std::conditional_t<std::is_same<U, void>::value, T, U>
mean(
1016 .template mean<std::conditional_t<std::is_same<U, void>::value, T, U>>();
1019 template <
class U =
void, std::
size_t N1,
class T1, std::
size_t N2,
class T2>
1020 inline std::conditional_t<std::is_same<U, void>::value, T1, U>
dot(
1022 return lhs.template dot<U>(rhs);
1025 template <
typename U = void, std::size_t N,
typename T, std::size_t N2,
1026 std::size_t M2,
typename T2>
1029 return lhs.template dot<U>(rhs);
1039 template <std::
size_t N,
class T>
1045 template <std::
size_t N,
class T>
1050 template <std::
size_t N,
class T>
1056 template <std::
size_t N,
class T,
typename Compare>
1062 template <std::
size_t N,
class T>
1067 template <std::
size_t N,
class T,
typename Compare>
1069 return v.
sorted(compare);
1072 template <std::
size_t N,
typename T =
int>
1077 template <std::
size_t N,
typename T =
int>
1083 #endif // MU_VECTOR_H_ Matrix< N, M, T > operator-(const Matrix< N, M, T > &lhs, const Matrix< N, M, U > &rhs)
minus operator
Definition: matrix.h:901
std::conditional_t< std::is_same< U, void >::value, Vector< M2, T >, Vector< M2, U > > dot(const Matrix< N2, M2, T2 > &rhs) const
dot product of a vector and a matrix
Definition: vector.h:428
void normalize()
normalizes this vector
Definition: vector.h:495
std::enable_if_t< std::is_arithmetic< TScalar >::value, Vector< N, T > & > operator/=(const TScalar &scalar)
divide every element of this vector by a scalar
Definition: vector.h:781
Matrix< N, M, T > operator/(const Matrix< N, M, T > &lhs, const Matrix< N, M, U > &rhs)
division operator
Definition: matrix.h:935
Vector & operator=(const Vector &other)=default
Copy assignment operator.
Vector(const Vector< Nn, U > &v)
Construct a new Vector from an existing Vector of a different type.
Definition: vector.h:103
Vector< N, T > & operator/=(const Vector< N, U > &rhs)
divison equal operator
Definition: vector.h:701
Matrix< N, M, T > operator*(const Matrix< N, M, T > &lhs, const Matrix< N, M, U > &rhs)
multiplication operator
Definition: matrix.h:918
bool operator==(const Vector< N, U > &rhs) const
equality operator
Definition: vector.h:617
const_iterator begin() const noexcept
returns a const iterator pointing to the first element
Definition: vector.h:267
Vector< N, T > sorted() const
returns a sorted vector
Definition: vector.h:563
A generic vector.
Definition: vector.h:48
constexpr Vector()=default
Construct a new Vector object.
Vector< N, T > normalized()
returns a normalized vector
Definition: vector.h:505
std::conditional_t< std::is_same< U, void >::value, T, U > dot(const Vector< N2, T2 > &rhs) const
dot product of two vectors
Definition: vector.h:380
Vector< N, T > flipped() const
returns a flipped vector
Definition: vector.h:527
Vector(const std::array< T, N > &a)
Construct a new Vector object from an std::array.
Definition: vector.h:116
T max() const
get the max value of the vector
Definition: vector.h:311
Matrix< N, M, T > operator+(const Matrix< N, M, T > &lhs, const Matrix< N, M, U > &rhs)
plus operator
Definition: matrix.h:884
void sort()
sort vector elements in ascending order
Definition: vector.h:539
Vector(TArgs &&... args)
Construct a new Vector object from a number of N values.
Definition: vector.h:87
T & operator[](size_type idx) noexcept
access an element within the vector
Definition: vector.h:206
Vector(const U &value)
Construct a new Vector object from a single value.
Definition: vector.h:148
T min() const
get the min value of the vector
Definition: vector.h:296
~Vector()=default
Destroy the Vector object.
void flip()
flips this vector, i.e. reverses its elements
Definition: vector.h:517
Vector< N, T > & operator-=(const Vector< N, U > &rhs)
minus equal operator
Definition: vector.h:665
U length() const
euclidean vector length
Definition: vector.h:481
bool operator!=(const Vector< N, U > &rhs) const
unequality operator
Definition: vector.h:634
const T & operator[](size_type idx) const noexcept
const access an element within the vector
Definition: vector.h:218
const_iterator end() const noexcept
returns a const iterator pointing to the element following the last element
Definition: vector.h:287
void sort(const Compare &comp)
sort vector elements by providing a condition
Definition: vector.h:552
std::enable_if_t< std::is_arithmetic< TScalar >::value, Vector< N, T > & > operator+=(const TScalar &scalar)
add a scalar to this vector
Definition: vector.h:729
Definition: literals.h:11
A generic matrix.
Definition: matrix.h:36
Vector< N, T > & operator+=(const Vector< N, U > &rhs)
plus equal operator
Definition: vector.h:648
std::enable_if_t< std::is_arithmetic< TScalar >::value, Vector< N, T > & > operator-=(const TScalar &scalar)
subtract a scalar from every element of this vector
Definition: vector.h:746
std::enable_if_t< std::is_arithmetic< TScalar >::value, Vector< N, T > & > operator*=(const TScalar &scalar)
multiply a scalar with this vector
Definition: vector.h:762
U std() const
calculates the standard deviation
Definition: vector.h:459
iterator begin() noexcept
returns an iterator pointing to the first element
Definition: vector.h:258
constexpr size_type size() const noexcept
returns the size of the vector
Definition: vector.h:249
T sum() const
sum up all the elements of the vector
Definition: vector.h:326
Vector< N, T > & operator*=(const Vector< N, U > &rhs)
multiplication equal operator
Definition: vector.h:682
const T & at(size_type idx) const
const access an element within the vector
Definition: vector.h:240
Vector< N, T > sorted(const Compare &comp) const
Definition: vector.h:579
T & at(size_type idx)
access an element within the vector
Definition: vector.h:229
iterator end() noexcept
returns an iterator pointing to the element following the last element
Definition: vector.h:277
Vector(const std::array< U, N > &a)
Construct a new Vector object from an std::array of a different type.
Definition: vector.h:131
U mean() const
mean of all the elements of the vector
Definition: vector.h:349