Debian Codegraph @

lib/distance_common.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2024 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn_distance.hpp"
20
21 namespace grn {
22 namespace distance {
23 template <typename Arch, typename ElementType>
24 float
25 l2_norm::operator()(Arch, const ElementType *vector_raw, size_t n_elements)
26 {
27 float square_sum = 0;
28 for (size_t i = 0; i < n_elements; ++i) {
29 square_sum += vector_raw[i] * vector_raw[i];
30 }
31 return std::sqrt(square_sum);
32 }
33
34 template <typename Arch, typename ElementType>
35 float
36 difference_l1_norm::operator()(Arch,
37 const ElementType *vector_raw1,
38 const ElementType *vector_raw2,
39 size_t n_elements)
40 {
41 float absolute_sum = 0;
42 for (size_t i = 0; i < n_elements; ++i) {
43 auto difference = vector_raw1[i] - vector_raw2[i];
44 absolute_sum += difference < 0 ? -difference : difference;
45 }
46 return absolute_sum;
47 }
48
49 template <typename Arch, typename ElementType>
50 float
51 difference_l2_norm_squared::operator()(Arch,
52 const ElementType *vector_raw1,
53 const ElementType *vector_raw2,
54 size_t n_elements)
55 {
56 float square_sum = 0;
57 for (size_t i = 0; i < n_elements; ++i) {
58 auto difference = vector_raw1[i] - vector_raw2[i];
59 square_sum += difference * difference;
60 }
61 return square_sum;
62 }
63
64 template <typename Arch, typename ElementType>
65 float
66 inner_product::operator()(Arch,
67 const ElementType *vector_raw1,
68 const ElementType *vector_raw2,
69 size_t n_elements)
70 {
71 float multiplication_sum = 0;
72 for (size_t i = 0; i < n_elements; ++i) {
73 multiplication_sum += vector_raw1[i] * vector_raw2[i];
74 }
75 return multiplication_sum;
76 }
77
78 template <typename Arch, typename ElementType>
79 float
80 cosine::operator()(Arch,
81 const ElementType *vector_raw1,
82 const ElementType *vector_raw2,
83 size_t n_elements)
84 {
85 ElementType inner_product = 0;
86 ElementType square_sum1 = 0;
87 ElementType square_sum2 = 0;
88 for (size_t i = 0; i < n_elements; ++i) {
89 ElementType value1 = vector_raw1[i];
90 ElementType value2 = vector_raw2[i];
91 inner_product += value1 * value2;
92 square_sum1 += value1 * value1;
93 square_sum2 += value2 * value2;
94 }
95 if (numeric::is_zero(inner_product)) {
96 return 1;
97 } else {
98 return 1 - (inner_product /
99 (std::sqrt(square_sum1) * std::sqrt(square_sum2)));
100 }
101 }
102 } // namespace distance
103 } // namespace grn
104
105 #define GRN_INSTANTIATION_SIMSIMD_ARCH serial
106 #define GRN_INSTANTIATION_XSIMD_ARCH xsimd::common
107 #include "grn_distance_instantiation.hpp"
108 #undef GRN_INSTANTIATION_SIMSIMD_ARCH
109 #undef GRN_INSTANTIATION_XSIMD_ARCH