File: distance_common.cpp

package info (click to toggle)
groonga 16.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 188,416 kB
  • sloc: ansic: 772,827; cpp: 52,396; ruby: 40,556; javascript: 10,250; yacc: 7,045; sh: 5,627; python: 2,821; makefile: 1,679
file content (109 lines) | stat: -rw-r--r-- 3,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
  Copyright (C) 2024  Sutou Kouhei <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "grn_distance.hpp"

namespace grn {
  namespace distance {
    template <typename Arch, typename ElementType>
    float
    l2_norm::operator()(Arch, const ElementType *vector_raw, size_t n_elements)
    {
      float square_sum = 0;
      for (size_t i = 0; i < n_elements; ++i) {
        square_sum += vector_raw[i] * vector_raw[i];
      }
      return std::sqrt(square_sum);
    }

    template <typename Arch, typename ElementType>
    float
    difference_l1_norm::operator()(Arch,
                                   const ElementType *vector_raw1,
                                   const ElementType *vector_raw2,
                                   size_t n_elements)
    {
      float absolute_sum = 0;
      for (size_t i = 0; i < n_elements; ++i) {
        auto difference = vector_raw1[i] - vector_raw2[i];
        absolute_sum += difference < 0 ? -difference : difference;
      }
      return absolute_sum;
    }

    template <typename Arch, typename ElementType>
    float
    difference_l2_norm_squared::operator()(Arch,
                                           const ElementType *vector_raw1,
                                           const ElementType *vector_raw2,
                                           size_t n_elements)
    {
      float square_sum = 0;
      for (size_t i = 0; i < n_elements; ++i) {
        auto difference = vector_raw1[i] - vector_raw2[i];
        square_sum += difference * difference;
      }
      return square_sum;
    }

    template <typename Arch, typename ElementType>
    float
    inner_product::operator()(Arch,
                              const ElementType *vector_raw1,
                              const ElementType *vector_raw2,
                              size_t n_elements)
    {
      float multiplication_sum = 0;
      for (size_t i = 0; i < n_elements; ++i) {
        multiplication_sum += vector_raw1[i] * vector_raw2[i];
      }
      return multiplication_sum;
    }

    template <typename Arch, typename ElementType>
    float
    cosine::operator()(Arch,
                       const ElementType *vector_raw1,
                       const ElementType *vector_raw2,
                       size_t n_elements)
    {
      ElementType inner_product = 0;
      ElementType square_sum1 = 0;
      ElementType square_sum2 = 0;
      for (size_t i = 0; i < n_elements; ++i) {
        ElementType value1 = vector_raw1[i];
        ElementType value2 = vector_raw2[i];
        inner_product += value1 * value2;
        square_sum1 += value1 * value1;
        square_sum2 += value2 * value2;
      }
      if (numeric::is_zero(inner_product)) {
        return 1;
      } else {
        return 1 - (inner_product /
                    (std::sqrt(square_sum1) * std::sqrt(square_sum2)));
      }
    }
  } // namespace distance
} // namespace grn

#define GRN_INSTANTIATION_SIMSIMD_ARCH serial
#define GRN_INSTANTIATION_XSIMD_ARCH   xsimd::common
#include "grn_distance_instantiation.hpp"
#undef GRN_INSTANTIATION_SIMSIMD_ARCH
#undef GRN_INSTANTIATION_XSIMD_ARCH