be/src/exprs/function/array/function_array_distance.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "exprs/function/array/function_array_distance.h" |
19 | | |
20 | | #include "exprs/function/simple_function_factory.h" |
21 | | |
22 | | namespace doris { |
23 | | |
24 | | FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN |
25 | 0 | float CosineDistance::distance(const float* x, const float* y, size_t d) { |
26 | 0 | float dot_prod = 0; |
27 | 0 | float squared_x = 0; |
28 | 0 | float squared_y = 0; |
29 | 0 | for (size_t i = 0; i < d; ++i) { |
30 | 0 | dot_prod += x[i] * y[i]; |
31 | 0 | squared_x += x[i] * x[i]; |
32 | 0 | squared_y += y[i] * y[i]; |
33 | 0 | } |
34 | 0 | if (squared_x == 0 or squared_y == 0) { |
35 | 0 | return 2.0f; |
36 | 0 | } |
37 | 0 | return 1 - dot_prod / sqrt(squared_x * squared_y); |
38 | 0 | } |
39 | | FAISS_PRAGMA_IMPRECISE_FUNCTION_END |
40 | | |
41 | | FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN |
42 | 12 | float CosineSimilarity::distance(const float* x, const float* y, size_t d) { |
43 | 12 | float dot_prod = 0; |
44 | 12 | float squared_x = 0; |
45 | 12 | float squared_y = 0; |
46 | 39 | for (size_t i = 0; i < d; ++i) { |
47 | 27 | dot_prod += x[i] * y[i]; |
48 | 27 | squared_x += x[i] * x[i]; |
49 | 27 | squared_y += y[i] * y[i]; |
50 | 27 | } |
51 | 12 | if (squared_x == 0 or squared_y == 0) { |
52 | 4 | return 0.0f; |
53 | 4 | } |
54 | 8 | return dot_prod / sqrt(squared_x * squared_y); |
55 | 12 | } |
56 | | FAISS_PRAGMA_IMPRECISE_FUNCTION_END |
57 | | |
58 | 1 | void register_function_array_distance(SimpleFunctionFactory& factory) { |
59 | 1 | factory.register_function<FunctionArrayDistance<L1Distance>>(); |
60 | 1 | factory.register_function<FunctionArrayDistance<L2Distance>>(); |
61 | 1 | factory.register_function<FunctionArrayDistance<CosineDistance>>(); |
62 | 1 | factory.register_function<FunctionArrayDistance<CosineSimilarity>>(); |
63 | 1 | factory.register_function<FunctionArrayDistance<InnerProduct>>(); |
64 | 1 | factory.register_function<FunctionArrayDistance<L2DistanceApproximate>>(); |
65 | 1 | factory.register_function<FunctionArrayDistance<InnerProductApproximate>>(); |
66 | 1 | } |
67 | | |
68 | | } // namespace doris |