3from typing
import Optional
6from .base_approximation
import BaseApproximation
10 Single-threaded Dirac-to-Dirac reduction (double precision).
12 Callable interface for reducing an M-point Dirac mixture
13 to L optimized Dirac points in N dimensions.
25 wX: Optional[numpy.ndarray] =
None,
26 wY: Optional[numpy.ndarray] =
None,
30 Perform Dirac-to-Dirac reduction.
35 Input Dirac locations (shape: M x N).
37 Number of input components.
39 Number of reduced components.
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.
54 Result object containing success flag,
55 minimizer information and optimized points.
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(
68 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
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),
80 ctypes_wrapper.ApproximateOptionsCTypes.from_py_type(options)
84 return python_variant.ApproximationResultPy.from_ctypes(
85 success, minimizer_result, x, L, N
95 wX: Optional[numpy.ndarray] =
None,
96 wY: Optional[numpy.ndarray] =
None,
100 Compute squared modified van Mises distance.
105 Input Dirac locations (M x N).
107 Reduced Dirac locations (L x N).
112 Squared distance value.
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(
124 ctypes.byref(result),
125 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
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))
143 wX: Optional[numpy.ndarray] =
None,
144 wY: Optional[numpy.ndarray] =
None,
148 Compute analytical gradient of squared distance.
153 Input Dirac locations (M x N).
155 Reduced Dirac locations (L x N).
160 Gradient array of shape (L x N).
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(
173 gradientChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
174 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
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))
187 Dirac-to-Dirac reduction with user-defined weight functions.
189 The reduced mixture weights are computed using
190 Python callback functions.
202 wX: python_variant.wXCallbackPythonType,
203 wXD: python_variant.wXDCallbackPythonType,
207 Perform reduction using custom weight functions.
212 Weight function callback.
214 Derivative of weight function.
218 ApproximationResultPy
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)
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(
231 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
235 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
238 ctypes.byref(minimizer_result),
243 ctypes_wrapper.ApproximateOptionsCTypes.from_py_type(options)
247 return python_variant.ApproximationResultPy.from_ctypes(
248 success, minimizer_result, x, L, N
258 wX: python_variant.wXCallbackPythonType,
259 wXD: python_variant.wXDCallbackPythonType,
263 Compute squared modified van Mises distance.
268 Input Dirac locations (M x N).
270 Reduced Dirac locations (L x N).
275 Squared distance value.
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(
287 ctypes.byref(result),
288 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
292 ctypes.c_size_t(bMax),
293 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
306 wX: python_variant.wXCallbackPythonType,
307 wXD: python_variant.wXDCallbackPythonType,
311 Compute analytical gradient of squared distance.
316 Input Dirac locations (M x N).
318 Reduced Dirac locations (L x N).
323 Gradient array of shape (L x N).
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(
336 gradientChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
337 yChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
341 ctypes.c_size_t(bMax),
342 xChecked.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
350 Multi-threaded Dirac-to-Dirac reduction (double precision).
352 Same functionality as the single-threaded version,
353 but internally parallelized.
365 wX: Optional[numpy.ndarray] =
None,
366 wY: Optional[numpy.ndarray] =
None,
370 Perform Dirac-to-Dirac reduction.
375 Input Dirac locations (shape: M x N).
377 Number of input components.
379 Number of reduced components.
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.
393 ApproximationResultPy
394 Result object containing success flag,
395 minimizer information and optimized points.
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)
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)),
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),
419 ctypes_wrapper.ApproximateOptionsCTypes.from_py_type(options)
423 return python_variant.ApproximationResultPy.from_ctypes(
424 success, minimizer_result, x, L, N
434 wX: Optional[numpy.ndarray] =
None,
435 wY: Optional[numpy.ndarray] =
None,
439 Compute squared modified van Mises distance.
444 Input Dirac locations (M x N).
446 Reduced Dirac locations (L x N).
451 Squared distance value.
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)),
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))
482 wX: Optional[numpy.ndarray] =
None,
483 wY: Optional[numpy.ndarray] =
None,
487 Compute analytical gradient of squared distance.
492 Input Dirac locations (M x N).
494 Reduced Dirac locations (L x N).
499 Gradient array of shape (L x N).
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)),
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))
526 Dirac-to-Dirac reduction interface.
528 Python wrapper around the C++ Dirac mixture reduction backend.
532 - Single-threaded reduction
533 - Multi-threaded reduction
534 - Reduction with user-defined weight functions
535 - Distance evaluation
536 - Analytical gradient computation
538 All heavy computations are performed in the C++ core library.
542 cdll = self.__class__.cdll
544 raise OSError(
"C++-Library was not loaded. Unable to continue!!!")
554 cdll = self.__class__.cdll
558 cdll.delete_dirac_to_dirac_approx_short_function_double(self.
d2d_func_double)
Dirac-to-Dirac reduction interface.
approximate_thread_double
approximate_function_double
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.
Dirac-to-Dirac reduction with user-defined weight functions.
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.
Multi-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.
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.