| 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"cxx . . $ `<file>/lib/grn_distance.hpp`/ |
| 20 | |
| 21 | namespace grncxx . . $ grn/ { |
| 22 | namespace distancecxx . . $ grn/distance/ { |
| 23 | template <typename Arch, typename ElementType> |
| 24 | float |
| 25 | l2_normcxx . . $ grn/distance/l2_norm#::operatorcxx . . $ grn/distance/l2_norm#`operator()`(3899f77a1e5bc2de).()(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 stdcxx . . $ std/::sqrt(square_sum); |
| 32 | } |
| 33 | |
| 34 | template <typename Arch, typename ElementType> |
| 35 | float |
| 36 | difference_l1_normcxx . . $ grn/distance/difference_l1_norm#::operatorcxx . . $ grn/distance/difference_l1_norm#`operator()`(3d31c8686a2d08d5).()(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_squaredcxx . . $ grn/distance/difference_l2_norm_squared#::operatorcxx . . $ grn/distance/difference_l2_norm_squared#`operator()`(3d31c8686a2d08d5).()(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_productcxx . . $ grn/distance/inner_product#::operatorcxx . . $ grn/distance/inner_product#`operator()`(3d31c8686a2d08d5).()(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 | cosinecxx . . $ grn/distance/cosine#::operatorcxx . . $ grn/distance/cosine#`operator()`(3d31c8686a2d08d5).()(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 (numericcxx . . $ grn/numeric/::is_zerocxx . . $ grn/numeric/is_zero(a142b9e4da30b6a4).(inner_product)) { |
| 96 | return 1; |
| 97 | } else { |
| 98 | return 1 - (inner_product / |
| 99 | (stdcxx . . $ std/::sqrtcxx . . $ std/sqrt(a358fd9634d01384).(square_sum1) * stdcxx . . $ std/::sqrtcxx . . $ std/sqrt(a358fd9634d01384).(square_sum2))); |
| 100 | } |
| 101 | } |
| 102 | } // namespace distance |
| 103 | } // namespace grn |
| 104 | |
| 105 | #define GRN_INSTANTIATION_SIMSIMD_ARCHcxx . . $ `lib/distance_common.cpp:105:9`! serial |
| 106 | #define GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/distance_common.cpp:106:9`! xsimd::common |
| 107 | #include "grn_distance_instantiation.hpp"cxx . . $ `<file>/lib/grn_distance_instantiation.hpp`/ |
| 108 | #undef GRN_INSTANTIATION_SIMSIMD_ARCHcxx . . $ `lib/distance_common.cpp:105:9`! |
| 109 | #undef GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/distance_common.cpp:106:9`! |