SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cosine_similarity.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_COSINE_SIMILARITY_HPP
9 #define SIG_UTIL_COSINE_SIMILARITY_HPP
10 
11 #include <numeric>
12 #include "norm.hpp"
13 #include "comparable_check.hpp"
14 
15 namespace sig
16 {
17 
20 {
21  template<class C1, class C2>
22  double operator()(C1 const& vec1, C2 const& vec2) const
23  {
24  using T = typename std::common_type<typename impl::container_traits<C1>::value_type, typename impl::container_traits<C2>::value_type>::type;
25 
26  assert(is_comparable(vec1, vec2, impl::NumericVectorTag()));
27 
28  return std::inner_product(std::begin(vec1), std::end(vec1), std::begin(vec2), static_cast<T>(0)) / (norm_L2(vec1) * norm_L2(vec2));
29  }
30 };
31 
33 
50 
51 }
52 #endif
const CosineSimilarity cosine_similarity
コサイン類似度を求める関数(関数オブジェクト)
double operator()(C1 const &vec1, C2 const &vec2) const
const Norm< 2 > norm_L2
L2ノルムを求める関数(関数オブジェクト)
Definition: norm.hpp:82
auto end(C &&c) -> std::move_iterator< typename RC::iterator >
bool is_comparable(C1 const &v1, C2 const &v2, impl::NumericVectorTag)
2つのベクトルが比較可能か確認
コサイン類似度(Cosine Similarity)
Definition: array.hpp:15
auto begin(C &&c) -> std::move_iterator< typename RC::iterator >