8namespace spark::math::details
10 template <
template <
typename>
typename Impl,
typename T, std::size_t N>
15 explicit constexpr Vector(std::array<std::reference_wrapper<T>, N> values)
25 for (std::size_t i = 0; i < m_values.size(); i++)
26 if (m_values[i].get() != other.m_values[i].get())
46 constexpr Impl<T>
operator+(
const Impl<T>& other)
const noexcept
49 for (std::size_t i = 0; i < N; ++i)
50 result.m_values[i].get() = m_values[i] + other.m_values[i];
59 constexpr Impl<T>
operator-(
const Impl<T>& other)
const noexcept
62 for (std::size_t i = 0; i < N; ++i)
63 result.m_values[i].get() = m_values[i] - other.m_values[i];
72 constexpr Impl<T>
operator*(
const Impl<T>& other)
const noexcept
75 for (std::size_t i = 0; i < N; ++i)
76 result.m_values[i].get() = m_values[i] * other.m_values[i];
85 constexpr Impl<T>
operator/(
const Impl<T>& other)
const noexcept
88 for (std::size_t i = 0; i < N; ++i)
89 result.m_values[i].get() = m_values[i] / other.m_values[i];
98 constexpr Impl<T>
operator*(
const T& value)
const noexcept
101 for (std::size_t i = 0; i < N; ++i)
102 result.m_values[i].get() = m_values[i] * value;
111 constexpr Impl<T>
operator/(
const T& value)
const noexcept
114 for (std::size_t i = 0; i < N; ++i)
115 result.m_values[i].get() = m_values[i] / value;
126 for (std::size_t i = 0; i < N; ++i)
127 m_values[i] += other.m_values[i];
128 return static_cast<Impl<T>&
>(*this);
138 for (std::size_t i = 0; i < N; ++i)
139 m_values[i] -= other.m_values[i];
140 return static_cast<Impl<T>&
>(*this);
150 for (std::size_t i = 0; i < N; ++i)
151 m_values[i] *= other.m_values[i];
152 return static_cast<Impl<T>&
>(*this);
162 for (std::size_t i = 0; i < N; ++i)
163 m_values[i] /= value;
164 return static_cast<Impl<T>&
>(*this);
174 for (std::size_t i = 0; i < N; ++i)
175 result.m_values[i].get() = -m_values[i];
183 [[nodiscard]]
constexpr T
norm() const noexcept
185 static_assert(std::is_floating_point_v<T>,
"The norm can only be computed for floating point types.");
188 for (std::size_t i = 0; i < N; ++i)
189 value += m_values[i].get() * m_values[i].get();
190 return std::sqrt(value);
199 return *
this /
norm();
207 [[nodiscard]]
constexpr T
dot(
const Impl<T>& other)
const noexcept
210 for (std::size_t i = 0; i < N; ++i)
211 value += m_values[i].get() * other.m_values[i].get();
222 for (std::size_t i = 0; i < N; ++i)
224 T temp = lhs.m_values[i];
225 lhs.m_values[i].get() = rhs.m_values[i].get();
226 rhs.m_values[i].get() = temp;
231 std::array<std::reference_wrapper<T>, N> m_values;
constexpr Vector(std::array< std::reference_wrapper< T >, N > values)
Initializes a new Vector.
Definition Vector.h:15
constexpr Impl< T > operator*(const T &value) const noexcept
Multiplies a Vector by a value of type T.
Definition Vector.h:98
constexpr Impl< T > operator-() const noexcept
Negates all components of the current Vector.
Definition Vector.h:171
constexpr Impl< T > operator*(const Impl< T > &other) const noexcept
Multiplies two Vector.
Definition Vector.h:72
constexpr Impl< T > normalized() const noexcept
Gets the normalized Vector. (v / ||v||)
Definition Vector.h:197
constexpr Impl< T > operator/(const T &value) const noexcept
Divides a Vector by a value of type T.
Definition Vector.h:111
constexpr Impl< T > & operator+=(const Vector &other) noexcept
Adds the value of other to the current vector.
Definition Vector.h:124
constexpr Impl< T > operator-(const Impl< T > &other) const noexcept
Subtracts two Vector.
Definition Vector.h:59
friend constexpr void swap(Vector &lhs, Vector &rhs) noexcept
Swaps two Vector.
Definition Vector.h:220
constexpr T norm() const noexcept
Computes the norm of the Vector. (||v|| = sqrt(v1^2 + v2^2 + ... + vn^2))
Definition Vector.h:183
constexpr Impl< T > operator/(const Impl< T > &other) const noexcept
Divides two Vector.
Definition Vector.h:85
constexpr bool operator==(const Vector &other) const noexcept
Compares two Vector for equality.
Definition Vector.h:23
constexpr T dot(const Impl< T > &other) const noexcept
Computes the dot product between the current Vector and other.
Definition Vector.h:207
constexpr Impl< T > & operator/=(const T &value) noexcept
Divides the value of other from the current vector.
Definition Vector.h:160
constexpr Impl< T > operator+(const Impl< T > &other) const noexcept
Adds two Vector.
Definition Vector.h:46
constexpr Impl< T > & operator*=(const Vector &other) noexcept
Multiplies the value of other to the current vector.
Definition Vector.h:148
constexpr bool operator!=(const Vector &other) const noexcept
Compares two Vector for inequality.
Definition Vector.h:36
constexpr Impl< T > & operator-=(const Vector &other) noexcept
Subtracts the value of other to the current vector.
Definition Vector.h:136