Deterministic Gaussian Sampling (Python API)
Python bindings for deterministic Gaussian sampling and Dirac mixture reduction.
Loading...
Searching...
No Matches
dirac_to_dirac.py
Go to the documentation of this file.
1import ctypes
2import numpy
3from typing import Optional
6from .base_approximation import BaseApproximation
7
9 """
10 Single-threaded Dirac-to-Dirac reduction (double precision).
11
12 Callable interface for reducing an M-point Dirac mixture
13 to L optimized Dirac points in N dimensions.
14 """
15 def __init__(self, parent):
16 self._parent = parent
17
19 self,
20 y: numpy.ndarray,
21 M: int,
22 L: int,
23 N: int,
24 x: numpy.ndarray,
25 wX: Optional[numpy.ndarray] = None,
26 wY: Optional[numpy.ndarray] = None,
27 options: Optional[python_variant.ApproximateOptionsPy] = None,
29 """
30 Perform Dirac-to-Dirac reduction.
31
32 Parameters
33 ----------
34 y : numpy.ndarray
35 Input Dirac locations (shape: M x N).
36 M : int
37 Number of input components.
38 L : int
39 Number of reduced components.
40 N : int
41 Dimension.
42 x : numpy.ndarray
43 Initial guess (L x N). Overwritten with result.
44 wX : numpy.ndarray, optional
45 Weights of reduced mixture (size L).
46 wY : numpy.ndarray, optional
47 Weights of input mixture (size M).
48 options : ApproximateOptionsPy, optional
49 Optimization parameters.
50
51 Returns
52 -------
53 ApproximationResultPy
54 Result object containing success flag,
55 minimizer information and optimized points.
56 """
57
58 cdll = self._parent.cdll
59 if cdll is None:
60 raise OSError("C++-Library was not loaded. Unable to continue!!!")
61 yChecked = self._parent._check_numpy_ndarray(y, M, N)
62 xChecked = self._parent._check_numpy_ndarray(x, L, N)
63 wXChecked = self._parent._check_weights(wX, L)
64 wYChecked = self._parent._check_weights(wY, M)
66 success: ctypes.c_bool = cdll.dirac_to_dirac_approx_short_double_approximate(
67 self._parent.d2d_short_double,
68 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
69 ctypes.c_size_t(M),
70 ctypes.c_size_t(L),
71 ctypes.c_size_t(N),
72 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
73 None if wXChecked is None else wXChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
74 None if wYChecked is None else wYChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
75 ctypes.byref(minimizer_result),
76 (
77 None
78 if options is None
79 else ctypes.byref(
80 ctypes_wrapper.ApproximateOptionsCTypes.from_py_type(options)
81 )
82 ),
83 )
84 return python_variant.ApproximationResultPy.from_ctypes(
85 success, minimizer_result, x, L, N
86 )
87
89 self,
90 y: numpy.ndarray,
91 M: int,
92 L: int,
93 N: int,
94 x: numpy.ndarray,
95 wX: Optional[numpy.ndarray] = None,
96 wY: Optional[numpy.ndarray] = None,
97 bMax: int = 100
98 ) -> float:
99 """
100 Compute squared modified van Mises distance.
101
102 Parameters
103 ----------
104 y : numpy.ndarray
105 Input Dirac locations (M x N).
106 x : numpy.ndarray
107 Reduced Dirac locations (L x N).
108
109 Returns
110 -------
111 float
112 Squared distance value.
113 """
114 cdll = self._parent.cdll
115 if cdll is None:
116 raise OSError("C++-Library was not loaded. Unable to continue!!!")
117 yChecked = self._parent._check_numpy_ndarray(y, M, N)
118 xChecked = self._parent._check_numpy_ndarray(x, L, N)
119 wXChecked = self._parent._check_weights(wX, L)
120 wYChecked = self._parent._check_weights(wY, M)
121 result = ctypes.c_double(0.0)
122 cdll.dirac_to_dirac_approx_short_double_modified_van_mises_distance_sq(
123 self._parent.d2d_short_double,
124 ctypes.byref(result),
125 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
126 ctypes.c_size_t(M),
127 ctypes.c_size_t(L),
128 ctypes.c_size_t(N),
129 ctypes.c_size_t(bMax),
130 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
131 None if wXChecked is None else wXChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
132 None if wYChecked is None else wYChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
133 )
134 return result.value
135
137 self,
138 y: numpy.ndarray,
139 M: int,
140 L: int,
141 N: int,
142 x: numpy.ndarray,
143 wX: Optional[numpy.ndarray] = None,
144 wY: Optional[numpy.ndarray] = None,
145 bMax: int = 100
146 ) -> numpy.ndarray:
147 """
148 Compute analytical gradient of squared distance.
149
150 Parameters
151 ----------
152 y : numpy.ndarray
153 Input Dirac locations (M x N).
154 x : numpy.ndarray
155 Reduced Dirac locations (L x N).
156
157 Returns
158 -------
159 numpy.ndarray
160 Gradient array of shape (L x N).
161 """
162 cdll = self._parent.cdll
163 if cdll is None:
164 raise OSError("C++-Library was not loaded. Unable to continue!!!")
165 yChecked = self._parent._check_numpy_ndarray(y, M, N)
166 xChecked = self._parent._check_numpy_ndarray(x, L, N)
167 wXChecked = self._parent._check_weights(wX, L)
168 wYChecked = self._parent._check_weights(wY, M)
169 gradient = numpy.zeros((L, N))
170 gradientChecked = self._parent._check_numpy_ndarray(gradient, L, N)
171 cdll.dirac_to_dirac_approx_short_double_modified_van_mises_distance_sq_derivative(
172 self._parent.d2d_short_double,
173 gradientChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
174 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
175 ctypes.c_size_t(M),
176 ctypes.c_size_t(L),
177 ctypes.c_size_t(N),
178 ctypes.c_size_t(bMax),
179 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
180 None if wXChecked is None else wXChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
181 None if wYChecked is None else wYChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
182 )
183 return gradient
184
186 """
187 Dirac-to-Dirac reduction with user-defined weight functions.
188
189 The reduced mixture weights are computed using
190 Python callback functions.
191 """
192 def __init__(self, parent):
193 self._parent = parent
194
196 self,
197 y: numpy.ndarray,
198 M: int,
199 L: int,
200 N: int,
201 x: numpy.ndarray,
202 wX: python_variant.wXCallbackPythonType,
203 wXD: python_variant.wXDCallbackPythonType,
204 options: Optional[python_variant.ApproximateOptionsPy] = None,
206 """
207 Perform reduction using custom weight functions.
208
209 Parameters
210 ----------
211 wX : callable
212 Weight function callback.
213 wXD : callable
214 Derivative of weight function.
215
216 Returns
217 -------
218 ApproximationResultPy
219 Optimization result.
220 """
221 cdll = self._parent.cdll
222 if cdll is None:
223 raise OSError("C++-Library was not loaded. Unable to continue!!!")
224 yChecked = self._parent._check_numpy_ndarray(y, M, N)
225 xChecked = self._parent._check_numpy_ndarray(x, L, N)
226 minimizer_result = ctypes_wrapper.GslMinimizerResultCTypes()
227 wX = python_variant.wx_callback_python_wrapper(wX)
228 wXD = python_variant.wxd_callback_python_wrapper(wXD)
229 success = cdll.dirac_to_dirac_approx_short_function_double_approximate(
230 self._parent.d2d_func_double,
231 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
232 ctypes.c_size_t(M),
233 ctypes.c_size_t(L),
234 ctypes.c_size_t(N),
235 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
236 wX,
237 wXD,
238 ctypes.byref(minimizer_result),
239 (
240 None
241 if options is None
242 else ctypes.byref(
243 ctypes_wrapper.ApproximateOptionsCTypes.from_py_type(options)
244 )
245 ),
246 )
247 return python_variant.ApproximationResultPy.from_ctypes(
248 success, minimizer_result, x, L, N
249 )
250
252 self,
253 y: numpy.ndarray,
254 M: int,
255 L: int,
256 N: int,
257 x: numpy.ndarray,
258 wX: python_variant.wXCallbackPythonType,
259 wXD: python_variant.wXDCallbackPythonType,
260 bMax: int = 100
261 ) -> float:
262 """
263 Compute squared modified van Mises distance.
264
265 Parameters
266 ----------
267 y : numpy.ndarray
268 Input Dirac locations (M x N).
269 x : numpy.ndarray
270 Reduced Dirac locations (L x N).
271
272 Returns
273 -------
274 float
275 Squared distance value.
276 """
277 cdll = self._parent.cdll
278 if cdll is None:
279 raise OSError("C++-Library was not loaded. Unable to continue!!!")
280 yChecked = self._parent._check_numpy_ndarray(y, M, N)
281 xChecked = self._parent._check_numpy_ndarray(x, L, N)
282 result = ctypes.c_double(0.0)
283 wX = python_variant.wx_callback_python_wrapper(wX)
284 wXD = python_variant.wxd_callback_python_wrapper(wXD)
285 cdll.dirac_to_dirac_approx_short_function_double_modified_van_mises_distance_sq(
286 self._parent.d2d_func_double,
287 ctypes.byref(result),
288 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
289 ctypes.c_size_t(M),
290 ctypes.c_size_t(L),
291 ctypes.c_size_t(N),
292 ctypes.c_size_t(bMax),
293 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
294 wX,
295 wXD
296 )
297 return result.value
298
300 self,
301 y: numpy.ndarray,
302 M: int,
303 L: int,
304 N: int,
305 x: numpy.ndarray,
306 wX: python_variant.wXCallbackPythonType,
307 wXD: python_variant.wXDCallbackPythonType,
308 bMax: int = 100
309 ) -> numpy.ndarray:
310 """
311 Compute analytical gradient of squared distance.
312
313 Parameters
314 ----------
315 y : numpy.ndarray
316 Input Dirac locations (M x N).
317 x : numpy.ndarray
318 Reduced Dirac locations (L x N).
319
320 Returns
321 -------
322 numpy.ndarray
323 Gradient array of shape (L x N).
324 """
325 cdll = self._parent.cdll
326 if cdll is None:
327 raise OSError("C++-Library was not loaded. Unable to continue!!!")
328 yChecked = self._parent._check_numpy_ndarray(y, M, N)
329 xChecked = self._parent._check_numpy_ndarray(x, L, N)
330 gradient = numpy.zeros((L, N))
331 gradientChecked = self._parent._check_numpy_ndarray(gradient, L, N)
332 wX = python_variant.wx_callback_python_wrapper(wX)
333 wXD = python_variant.wxd_callback_python_wrapper(wXD)
334 cdll.dirac_to_dirac_approx_short_function_double_modified_van_mises_distance_sq_derivative(
335 self._parent.d2d_func_double,
336 gradientChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
337 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
338 ctypes.c_size_t(M),
339 ctypes.c_size_t(L),
340 ctypes.c_size_t(N),
341 ctypes.c_size_t(bMax),
342 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
343 wX,
344 wXD
345 )
346 return gradient
347
349 """
350 Multi-threaded Dirac-to-Dirac reduction (double precision).
351
352 Same functionality as the single-threaded version,
353 but internally parallelized.
354 """
355 def __init__(self, parent):
356 self._parent = parent
357
359 self,
360 y: numpy.ndarray,
361 M: int,
362 L: int,
363 N: int,
364 x: numpy.ndarray,
365 wX: Optional[numpy.ndarray] = None,
366 wY: Optional[numpy.ndarray] = None,
367 options: Optional[python_variant.ApproximateOptionsPy] = None,
369 """
370 Perform Dirac-to-Dirac reduction.
371
372 Parameters
373 ----------
374 y : numpy.ndarray
375 Input Dirac locations (shape: M x N).
376 M : int
377 Number of input components.
378 L : int
379 Number of reduced components.
380 N : int
381 Dimension.
382 x : numpy.ndarray
383 Initial guess (L x N). Overwritten with result.
384 wX : numpy.ndarray, optional
385 Weights of reduced mixture (size L).
386 wY : numpy.ndarray, optional
387 Weights of input mixture (size M).
388 options : ApproximateOptionsPy, optional
389 Optimization parameters.
390
391 Returns
392 -------
393 ApproximationResultPy
394 Result object containing success flag,
395 minimizer information and optimized points.
396 """
397 cdll = self._parent.cdll
398 if cdll is None:
399 raise OSError("C++-Library was not loaded. Unable to continue!!!")
400 yChecked = self._parent._check_numpy_ndarray(y, M, N)
401 xChecked = self._parent._check_numpy_ndarray(x, L, N)
402 wXChecked = self._parent._check_weights(wX, L)
403 wYChecked = self._parent._check_weights(wY, M)
404 minimizer_result = ctypes_wrapper.GslMinimizerResultCTypes()
405 success = cdll.dirac_to_dirac_approx_short_thread_double_approximate(
406 self._parent.d2d_thread_double,
407 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
408 ctypes.c_size_t(M),
409 ctypes.c_size_t(L),
410 ctypes.c_size_t(N),
411 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
412 None if wXChecked is None else wXChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
413 None if wYChecked is None else wYChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
414 ctypes.byref(minimizer_result),
415 (
416 None
417 if options is None
418 else ctypes.byref(
419 ctypes_wrapper.ApproximateOptionsCTypes.from_py_type(options)
420 )
421 ),
422 )
423 return python_variant.ApproximationResultPy.from_ctypes(
424 success, minimizer_result, x, L, N
425 )
426
428 self,
429 y: numpy.ndarray,
430 M: int,
431 L: int,
432 N: int,
433 x: numpy.ndarray,
434 wX: Optional[numpy.ndarray] = None,
435 wY: Optional[numpy.ndarray] = None,
436 bMax: int = 100
437 ) -> float:
438 """
439 Compute squared modified van Mises distance.
440
441 Parameters
442 ----------
443 y : numpy.ndarray
444 Input Dirac locations (M x N).
445 x : numpy.ndarray
446 Reduced Dirac locations (L x N).
447
448 Returns
449 -------
450 float
451 Squared distance value.
452 """
453 cdll = self._parent.cdll
454 if cdll is None:
455 raise OSError("C++-Library was not loaded. Unable to continue!!!")
456 yChecked = self._parent._check_numpy_ndarray(y, M, N)
457 xChecked = self._parent._check_numpy_ndarray(x, L, N)
458 wXChecked = self._parent._check_weights(wX, L)
459 wYChecked = self._parent._check_weights(wY, M)
460 result = ctypes.c_double(0.0)
461 cdll.dirac_to_dirac_approx_short_thread_double_modified_van_mises_distance_sq(
462 self._parent.d2d_thread_double,
463 ctypes.byref(result),
464 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
465 ctypes.c_size_t(M),
466 ctypes.c_size_t(L),
467 ctypes.c_size_t(N),
468 ctypes.c_size_t(bMax),
469 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
470 None if wXChecked is None else wXChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
471 None if wYChecked is None else wYChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
472 )
473 return result.value
474
476 self,
477 y: numpy.ndarray,
478 M: int,
479 L: int,
480 N: int,
481 x: numpy.ndarray,
482 wX: Optional[numpy.ndarray] = None,
483 wY: Optional[numpy.ndarray] = None,
484 bMax: int = 100
485 ) -> numpy.ndarray:
486 """
487 Compute analytical gradient of squared distance.
488
489 Parameters
490 ----------
491 y : numpy.ndarray
492 Input Dirac locations (M x N).
493 x : numpy.ndarray
494 Reduced Dirac locations (L x N).
495
496 Returns
497 -------
498 numpy.ndarray
499 Gradient array of shape (L x N).
500 """
501 cdll = self._parent.cdll
502 if cdll is None:
503 raise OSError("C++-Library was not loaded. Unable to continue!!!")
504 yChecked = self._parent._check_numpy_ndarray(y, M, N)
505 xChecked = self._parent._check_numpy_ndarray(x, L, N)
506 wXChecked = self._parent._check_weights(wX, L)
507 wYChecked = self._parent._check_weights(wY, M)
508 gradient = numpy.zeros((L, N))
509 gradientChecked = self._parent._check_numpy_ndarray(gradient, L, N)
510 cdll.dirac_to_dirac_approx_short_thread_double_modified_van_mises_distance_sq_derivative(
511 self._parent.d2d_thread_double,
512 gradientChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
513 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
514 ctypes.c_size_t(M),
515 ctypes.c_size_t(L),
516 ctypes.c_size_t(N),
517 ctypes.c_size_t(bMax),
518 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
519 None if wXChecked is None else wXChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
520 None if wYChecked is None else wYChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
521 )
522 return gradient
523
525 """
526 Dirac-to-Dirac reduction interface.
527
528 Python wrapper around the C++ Dirac mixture reduction backend.
529
530 This class provides:
531
532 - Single-threaded reduction
533 - Multi-threaded reduction
534 - Reduction with user-defined weight functions
535 - Distance evaluation
536 - Analytical gradient computation
537
538 All heavy computations are performed in the C++ core library.
539 """
540 def __init__(self):
541 super().__init__()
542 cdll = self.__class__.cdll
543 if cdll is None:
544 raise OSError("C++-Library was not loaded. Unable to continue!!!")
545 self.d2d_short_double = cdll.create_dirac_to_dirac_approx_short_double()
546 self.d2d_func_double = cdll.create_dirac_to_dirac_approx_short_function_double()
547 self.d2d_thread_double = cdll.create_dirac_to_dirac_approx_short_thread_double()
548
552
553 def __del__(self):
554 cdll = self.__class__.cdll
555 if cdll is None:
556 return
557 cdll.delete_dirac_to_dirac_approx_short_double(self.d2d_short_double)
558 cdll.delete_dirac_to_dirac_approx_short_function_double(self.d2d_func_double)
559 cdll.delete_dirac_to_dirac_approx_short_thread_double(self.d2d_thread_double)
560
Single-threaded Dirac-to-Dirac reduction (double precision).
numpy.ndarray modified_van_mises_distance_sq_derivative(self, numpy.ndarray y, int M, int L, int N, numpy.ndarray x, Optional[numpy.ndarray] wX=None, Optional[numpy.ndarray] wY=None, int bMax=100)
Compute analytical gradient of squared distance.
float modified_van_mises_distance_sq(self, numpy.ndarray y, int M, int L, int N, numpy.ndarray x, Optional[numpy.ndarray] wX=None, Optional[numpy.ndarray] wY=None, int bMax=100)
Compute squared modified van Mises distance.
python_variant.ApproximationResultPy __call__(self, numpy.ndarray y, int M, int L, int N, numpy.ndarray x, Optional[numpy.ndarray] wX=None, Optional[numpy.ndarray] wY=None, Optional[python_variant.ApproximateOptionsPy] options=None)
Perform Dirac-to-Dirac reduction.
float modified_van_mises_distance_sq(self, numpy.ndarray y, int M, int L, int N, numpy.ndarray x, python_variant.wXCallbackPythonType wX, python_variant.wXDCallbackPythonType wXD, int bMax=100)
Compute squared modified van Mises distance.
numpy.ndarray modified_van_mises_distance_sq_derivative(self, numpy.ndarray y, int M, int L, int N, numpy.ndarray x, python_variant.wXCallbackPythonType wX, python_variant.wXDCallbackPythonType wXD, int bMax=100)
Compute analytical gradient of squared distance.
python_variant.ApproximationResultPy __call__(self, numpy.ndarray y, int M, int L, int N, numpy.ndarray x, python_variant.wXCallbackPythonType wX, python_variant.wXDCallbackPythonType wXD, Optional[python_variant.ApproximateOptionsPy] options=None)
Perform reduction using custom weight functions.
numpy.ndarray modified_van_mises_distance_sq_derivative(self, numpy.ndarray y, int M, int L, int N, numpy.ndarray x, Optional[numpy.ndarray] wX=None, Optional[numpy.ndarray] wY=None, int bMax=100)
Compute analytical gradient of squared distance.
python_variant.ApproximationResultPy __call__(self, numpy.ndarray y, int M, int L, int N, numpy.ndarray x, Optional[numpy.ndarray] wX=None, Optional[numpy.ndarray] wY=None, Optional[python_variant.ApproximateOptionsPy] options=None)
Perform Dirac-to-Dirac reduction.
float modified_van_mises_distance_sq(self, numpy.ndarray y, int M, int L, int N, numpy.ndarray x, Optional[numpy.ndarray] wX=None, Optional[numpy.ndarray] wY=None, int bMax=100)
Compute squared modified van Mises distance.