E.V.E
v2023.02.15
 
Loading...
Searching...
No Matches

◆ cosine_similarity

eve::cosine_similarity = functor<cosine_similarity_t>
inlineconstexpr

Header file

#include <eve/module/core.hpp>

Callable Signatures

namespace eve
{
// Regular overloads
constexpr auto cosine_similarity(kumi::non_empty_product_type xs, kumi::non_empty_product_type ys) noexcept; // 1
// Semantic options
constexpr auto cosine_similarity[widen] (/*any of the above overloads*/) noexcept; // 2
}
constexpr auto cosine_similarity
elementwise_callable object computing the elementwise cosine_similarity of the vector of the first ha...
Definition cosine_similarity.hpp:79
typename decltype(detail::as_translated_type(as< T >{}))::type translate_t
Returns the final translated type of T.
Definition translation.hpp:107
EVE Main Namespace.
Definition abi.hpp:19

Parameters

Return value

  1. cosine_similarity product. \(\frac{\sum_s (x_s*y_s)}{\sqrt{\sum_s (x_s^2)*\sum_s (y_s^2)}}\). It is the cosine of the angle between the two vectors. One or minus one means thaat the vectors are proportionnal, zero that they are orthogonal.
  2. Uses the upgraded type for computations and result
See also
`welford_cosine_similarity` for incremental or parallel cosine_similarity and averages computations.

Example

// revision 0
#include <eve/module/core.hpp>
#include <iostream>
#include "../../vec3.hpp"
int main()
{
kumi::tuple wf0{0.0, 1.0, 2.0, 3.0, -1.0, -2.0, -3.0, -4.0};
kumi::tuple wf1{0.0, -4.0, 1.0, -1.0, 2.0, -2.0, 3.0, -4.0};
std::cout << "<- wf0 = " << wf0 << "\n";
std::cout << "<- wf1 = " << wf1 << "\n";
std::cout << "-> cosine_similarity(wf0, wf1) = " << eve::cosine_similarity(wf0, wf1) << "\n";
std::cout << "-> cosine_similarity(wf0, wf0) = " << eve::cosine_similarity(wf0, wf0) << "\n";
vec3<float> a(1.0, 2.0, 3.0);
vec3<float> b(-3.0, -4.0, -6.0);
std::cout << "a " << a << std::endl;
std::cout << "b " << b << std::endl;
std::cout << "eve::cosine_similarity(a, b) = "<< eve::cosine_similarity(a, b) << std::endl;
std::cout << "eve::cosine_similarity(a, a) = " << eve::cosine_similarity(a, a) << std::endl;
std::cout << "eve::cosine_similarity(b, a) = " << eve::cosine_similarity(b, a) << std::endl;
std::cout << "eve::cosine_similarity(b, b) = " << eve::cosine_similarity(b, b) << std::endl;
// simd cosine_similarity eve::wide<vec3<float>, eve::fixed<4>>;
auto wa = wv3_t(a, a, b, b);
auto wb = wv3_t(b, a, a, b);
std::cout << "wa " << wa << std::endl;
std::cout << "wb " << wb << std::endl;
std::cout << "eve::cosine_similarity[eve::widen](wa, wb) = "<< eve::cosine_similarity(wa, wb) << std::endl;
kumi::tuple x{4.0f, 3.0f, 2.0f, 1.0f};
kumi::tuple y{1.0f, 2.0f, 3.0f, 4.0f};
std::cout << eve::cosine_similarity(x, x) << std::endl;
std::cout << eve::cosine_similarity(x, y) << std::endl;
std::cout << eve::cosine_similarity(y, x) << std::endl;
auto wt1 = kumi::generate<11>([](auto p){return w_t([p](auto q){return float(p+q); }); });
auto wt2 = kumi::generate<11>([](auto p){return w_t([p](auto q){return p*p/float((q+1)); }); });
std::cout << wt1 << std::endl;
std::cout << wt2 << std::endl;
}