Skip to main content

sc_neurocore_engine/bindings/
cordiv.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Commercial license available
3// © Concepts 1996–2026 Miroslav Šotek. All rights reserved.
4// © Code 2020–2026 Miroslav Šotek. All rights reserved.
5// ORCID: 0009-0009-3560-0851
6// Contact: www.anulum.li | protoscience@anulum.li
7// SC-NeuroCore — CORDIV PyO3 bindings
8
9//! Python bindings for CORDIV stochastic division and stream-length planning.
10
11use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
12use pyo3::prelude::*;
13
14/// Register CORDIV and adaptive stream-length functions with the extension module.
15pub(crate) fn register(module: &Bound<'_, PyModule>) -> PyResult<()> {
16    module.add_function(wrap_pyfunction!(py_cordiv, module)?)?;
17    module.add_function(wrap_pyfunction!(py_adaptive_length, module)?)?;
18    Ok(())
19}
20
21/// Divide two byte-encoded stochastic bitstreams with the CORDIV recurrence.
22#[pyfunction]
23fn py_cordiv(
24    py: Python<'_>,
25    numerator: PyReadonlyArray1<'_, u8>,
26    denominator: PyReadonlyArray1<'_, u8>,
27) -> PyResult<Py<PyArray1<u8>>> {
28    let numerator = numerator.as_slice()?;
29    let denominator = denominator.as_slice()?;
30    let quotient = crate::cordiv::cordiv(numerator, denominator);
31    Ok(quotient.into_pyarray(py).into())
32}
33
34/// Compute a power-of-two stream length from the Hoeffding bound.
35#[pyfunction]
36fn py_adaptive_length(epsilon: f64, confidence: f64) -> usize {
37    crate::cordiv::adaptive_length_hoeffding(epsilon, confidence)
38}