43 template <
typename T_>
44 inline std::vector<T_>
sub(
const std::vector<T_>& left,
const std::vector<T_>& right) {
45 if (left.size() != right.size()) {
46 SHLOG_E(
"Vectors have different sizes.");
48 throw std::runtime_error(
"Vectors have different sizes.");
52 std::vector<T_> result;
53 result.resize(left.size());
56 for (
auto& v : result) {
57 v = left[i] - right[i];
69 template <
typename T_>
70 inline std::vector<T_>
add(
const std::vector<T_>& left,
const std::vector<T_>& right) {
71 if (left.size() != right.size()) {
72 SHLOG_E(
"Vectors have different sizes.");
74 throw std::runtime_error(
"Vectors have different sizes.");
78 std::vector<T_> result;
79 result.resize(left.size());
82 for (
auto& v : result) {
83 v = left[i] + right[i];
95 template <
typename T_,
typename S_>
96 inline std::vector<T_>
mult(
const std::vector<T_>& left, S_ right) {
97 std::vector<T_> result(left.size());
99 std::transform(std::execution::par_unseq, left.begin(), left.end(), result.begin(),
100 [right](
const T_& l) { return l * right; });
110 template <
typename T_>
111 inline std::vector<T_>
mult(
const std::vector<T_>& left,
const std::vector<T_>& right) {
112 if (left.size() != right.size()) {
113 SHLOG_E(
"Vectors have different sizes.");
115 throw std::runtime_error(
"Vectors have different sizes.");
119 std::vector<T_> result;
121 std::transform(left.begin(), left.end(), right.begin(), std::back_inserter(result),
122 [](
const T_& l,
const T_& r) { return l * r; });
132 template <
typename T_>
133 inline T_
dot(
const std::vector<T_>& left,
const std::vector<T_>& right) {
134 if (left.size() != right.size()) {
135 SHLOG_E(
"Vectors have different sizes.");
137 throw std::runtime_error(
"Vectors have different sizes.");
141 std::vector<T_> sum = mult<T_>(left, right);
143 return std::accumulate(sum.begin(), sum.end(), 0.0f, std::plus<T_>());
146 template <
typename T_>
147 inline std::vector<T_>
mat_mult(
const std::vector<std::vector<T_>>& mat,
const std::vector<T_>& vec) {
148 if (mat.empty() || mat[0].size() != vec.size()) {
149 SHLOG_E(
"Vectors have different sizes.");
151 throw std::runtime_error(
"Vectors have different sizes.");
155 std::vector<T_> result;
156 result.resize(mat.size());
159 for (
auto&& mat_row_vec : mat) result[i++] =
dot(mat_row_vec, vec);
164 template <
typename T_>
165 inline float length(
const std::vector<T_>& left) {
166 return sqrtf(
dot(left, left));
169 template <
typename T_>
170 inline std::vector<T_>
normalize(
const std::vector<T_>& left) {
171 float vec_size =
length(left);
174 return mult(left, (1.0f / vec_size));
178 throw std::runtime_error(
"Zero vector!");
180 return std::vector<T_>{};
File implementing distance calculations on vectors.
std::vector< T_ > mult(const std::vector< T_ > &left, S_ right)
Computes the element-wise multiplication of the given vector and constant right.
Definition: vector.hpp:96
T_ dot(const std::vector< T_ > &left, const std::vector< T_ > &right)
Computes the dot product of the given vectors.
Definition: vector.hpp:133
float length(const std::vector< T_ > &left)
Definition: vector.hpp:165
std::vector< T_ > normalize(const std::vector< T_ > &left)
Definition: vector.hpp:170
std::vector< T_ > mat_mult(const std::vector< std::vector< T_ >> &mat, const std::vector< T_ > &vec)
Definition: vector.hpp:147
std::vector< T_ > sub(const std::vector< T_ > &left, const std::vector< T_ > &right)
Computes the substraction of the given vectors (left - right).
Definition: vector.hpp:44
std::vector< T_ > add(const std::vector< T_ > &left, const std::vector< T_ > &right)
Computes the addition of the given vectors (left + right).
Definition: vector.hpp:70
Definition: vector.hpp:35
#define SHLOG_E(x)
Definition: static-logger.hpp:157