Debian Codegraph @

lib/proc/proc_distance.c

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_ctx.h"
20 #include "../grn_distance.h"
21
22 #include <groonga/plugin.h>
23
24 static grn_obj *
25 func_distance_cosine(grn_ctx *ctx,
26 int n_args,
27 grn_obj **args,
28 grn_user_data *user_data)
29 {
30 const char *function_name = "distance_cosine";
31
32 grn_obj *distance = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_FLOAT32, 0);
33 GRN_FLOAT32_SET(ctx, distance, 0.0);
34
35 if (n_args != 2) {
36 GRN_PLUGIN_ERROR(ctx,
37 GRN_INVALID_ARGUMENT,
38 "%s(): wrong number of arguments (%d for 2)",
39 function_name,
40 n_args);
41 return distance;
42 }
43
44 grn_obj *vector1 = args[0];
45 grn_obj *vector2 = args[1];
46 float distance_raw = grn_distance_cosine(ctx, vector1, vector2);
47 GRN_FLOAT32_SET(ctx, distance, distance_raw);
48
49 return distance;
50 }
51
52 void
53 grn_proc_init_distance_cosine(grn_ctx *ctx)
54 {
55 grn_obj *proc = grn_proc_create(ctx,
56 "distance_cosine",
57 -1,
58 GRN_PROC_FUNCTION,
59 func_distance_cosine,
60 NULL,
61 NULL,
62 0,
63 NULL);
64 grn_proc_set_applier(ctx, proc, grn_distance_cosine_applier);
65 }
66
67 static grn_obj *
68 func_distance_inner_product(grn_ctx *ctx,
69 int n_args,
70 grn_obj **args,
71 grn_user_data *user_data)
72 {
73 const char *function_name = "distance_inner_product";
74
75 grn_obj *distance = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_FLOAT32, 0);
76 GRN_FLOAT32_SET(ctx, distance, 0.0);
77
78 if (n_args != 2) {
79 GRN_PLUGIN_ERROR(ctx,
80 GRN_INVALID_ARGUMENT,
81 "%s(): wrong number of arguments (%d for 2)",
82 function_name,
83 n_args);
84 return distance;
85 }
86
87 grn_obj *vector1 = args[0];
88 grn_obj *vector2 = args[1];
89 float distance_raw = grn_distance_inner_product(ctx, vector1, vector2);
90 GRN_FLOAT32_SET(ctx, distance, distance_raw);
91
92 return distance;
93 }
94
95 void
96 grn_proc_init_distance_inner_product(grn_ctx *ctx)
97 {
98 grn_obj *proc = grn_proc_create(ctx,
99 "distance_inner_product",
100 -1,
101 GRN_PROC_FUNCTION,
102 func_distance_inner_product,
103 NULL,
104 NULL,
105 0,
106 NULL);
107 grn_proc_set_applier(ctx, proc, grn_distance_inner_product_applier);
108 }
109
110 static grn_obj *
111 func_distance_l1_norm(grn_ctx *ctx,
112 int n_args,
113 grn_obj **args,
114 grn_user_data *user_data)
115 {
116 const char *function_name = "distance_l1_norm";
117
118 grn_obj *distance = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_FLOAT32, 0);
119 GRN_FLOAT32_SET(ctx, distance, 0.0);
120
121 if (n_args != 2) {
122 GRN_PLUGIN_ERROR(ctx,
123 GRN_INVALID_ARGUMENT,
124 "%s(): wrong number of arguments (%d for 2)",
125 function_name,
126 n_args);
127 return distance;
128 }
129
130 grn_obj *vector1 = args[0];
131 grn_obj *vector2 = args[1];
132 float distance_raw = grn_distance_l1_norm(ctx, vector1, vector2);
133 GRN_FLOAT32_SET(ctx, distance, distance_raw);
134
135 return distance;
136 }
137
138 void
139 grn_proc_init_distance_l1_norm(grn_ctx *ctx)
140 {
141 grn_obj *proc = grn_proc_create(ctx,
142 "distance_l1_norm",
143 -1,
144 GRN_PROC_FUNCTION,
145 func_distance_l1_norm,
146 NULL,
147 NULL,
148 0,
149 NULL);
150 grn_proc_set_applier(ctx, proc, grn_distance_l1_norm_applier);
151 }
152
153 static grn_obj *
154 func_distance_l2_norm_squared(grn_ctx *ctx,
155 int n_args,
156 grn_obj **args,
157 grn_user_data *user_data)
158 {
159 const char *function_name = "distance_l2_norm_squared";
160
161 grn_obj *distance = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_FLOAT32, 0);
162 GRN_FLOAT32_SET(ctx, distance, 0.0);
163
164 if (n_args != 2) {
165 GRN_PLUGIN_ERROR(ctx,
166 GRN_INVALID_ARGUMENT,
167 "%s(): wrong number of arguments (%d for 2)",
168 function_name,
169 n_args);
170 return distance;
171 }
172
173 grn_obj *vector1 = args[0];
174 grn_obj *vector2 = args[1];
175 float distance_raw = grn_distance_l2_norm_squared(ctx, vector1, vector2);
176 GRN_FLOAT32_SET(ctx, distance, distance_raw);
177
178 return distance;
179 }
180
181 void
182 grn_proc_init_distance_l2_norm_squared(grn_ctx *ctx)
183 {
184 grn_obj *proc = grn_proc_create(ctx,
185 "distance_l2_norm_squared",
186 -1,
187 GRN_PROC_FUNCTION,
188 func_distance_l2_norm_squared,
189 NULL,
190 NULL,
191 0,
192 NULL);
193 grn_proc_set_applier(ctx, proc, grn_distance_l2_norm_squared_applier);
194 }