8template<
typename T, std::
size_t N>
9inline T
summation(
const std::array<T, N>& values,
const T initialValue)
15inline T
summation(
const std::vector<T>& values,
const T initialValue)
20template<
typename T, std::
size_t EXTENT>
23 return std::accumulate(values.begin(), values.end(), initialValue);
26template<
typename T, std::
size_t N>
27inline T
product(
const std::array<T, N>& values,
const T initialValue)
33inline T
product(
const std::vector<T>& values,
const T initialValue)
38template<
typename T, std::
size_t EXTENT>
41 T result = initialValue;
42 for(
auto&& value : values)
49template<
typename T, std::
size_t N>
50inline T
length(
const std::array<T, N>& vec)
56inline T
length(
const std::vector<T>& vec)
61template<
typename T, std::
size_t EXTENT>
67template<
typename T, std::
size_t N>
79template<
typename T, std::
size_t EXTENT>
83 for(
const T val : vec)
90template<
typename T, std::
size_t N>
91inline T
p_norm(
const std::array<T, N>& vec)
97inline T
p_norm(
const std::vector<T>& vec)
102template<std::
size_t P,
typename T, std::
size_t EXTENT>
105 static_assert(P >= 1);
110 for(
const T val : vec)
112 result += std::abs(val);
116 else if constexpr(P == 2)
123 for(
const T val : vec)
125 if constexpr(P % 2 == 0)
127 result += std::pow(val, P);
131 static_assert(P % 2 == 1);
133 result += std::pow(std::abs(val), P);
137 return static_cast<T
>(std::pow(result, 1.0 / P));
141template<
typename T, std::
size_t N>
153template<
typename T, std::
size_t EXTENT>
156 if constexpr(std::is_floating_point_v<T>)
166 static_assert(std::is_integral_v<T>);
168 std::fill(vec.begin(), vec.end(),
static_cast<T
>(0));
175 constexpr auto EMPTY_IDX =
static_cast<std::size_t
>(-1);
177 auto nonZeroIdx = EMPTY_IDX;
178 for(std::size_t i = 0; i < EXTENT; ++i)
180 if(vec[i] !=
static_cast<T
>(0))
183 if(nonZeroIdx == EMPTY_IDX)
196 if(nonZeroIdx != EMPTY_IDX)
198 PH_ASSERT_NE(vec[nonZeroIdx],
static_cast<T
>(0));
200 vec[nonZeroIdx] =
static_cast<T
>(
sign(vec[nonZeroIdx]));
205template<
typename T, std::
size_t N>
206inline T
dot_product(
const std::array<T, N>& vecA,
const std::array<T, N>& vecB)
212inline T
dot_product(
const std::vector<T>& vecA,
const std::vector<T>& vecB)
217template<
typename T, std::
size_t EXTENT>
220 PH_ASSERT_EQ(vecA.size(), vecB.size());
223 for(std::size_t i = 0; i < vecA.size(); ++i)
225 result += vecA[i] * vecB[i];
Miscellaneous math utilities.
Math functions and utilities.
Definition TransformInfo.h:10
T dot_product(const std::array< T, N > &vecA, const std::array< T, N > &vecB)
Treating input values as vectors and calculates their dot product.
Definition math.ipp:206
T length_squared(const std::array< T, N > &vec)
Treating input values as a vector and calculates its squared length.
Definition math.ipp:68
T summation(const std::array< T, N > &values, T initialValue=0)
Sum all values within a container together.
Definition math.ipp:9
void normalize(std::array< T, N > &vec)
Treating input values as a vector and normalize it. Notice that normalizing a integer typed vector wi...
Definition math.ipp:142
T p_norm(const std::array< T, N > &vec)
Treating input values as a vector and calculates its p-norm.
Definition math.ipp:91
T length(const std::array< T, N > &vec)
Treating input values as a vector and calculates its length.
Definition math.ipp:50
T product(const std::array< T, N > &values, T initialValue=1)
Multiplies all values within a container together.
Definition math.ipp:27
int sign(const T value)
Extract the sign of value.
Definition math.h:154
std::span< const T, EXTENT > TSpanView
Same as TSpan, except that the objects are const-qualified. Note that for pointer types,...
Definition TSpan.h:19
std::span< T, EXTENT > TSpan
A contiguous sequence of objects of type T. Effectively the same as std::span.
Definition TSpan.h:12