FrankenNumPy

Docs/Overview

Quick Example

On this page 4 sections

Rust

use fnp_dtype::DType;
use fnp_ufunc::{BinaryOp, UFuncArray};

// Build an array and z-score normalize it.
let data = UFuncArray::new(vec![5], vec![1.0, 2.0, 3.0, 4.0, 5.0], DType::F64)?;
let mean = data.reduce_mean(None, false)?;       // shape [] (0-dim scalar)
let std  = data.reduce_std(None, false, 0)?;     // shape [] (0-dim scalar)
// NumPy auto-broadcasts scalars; the explicit Rust API requires
// broadcasting the 0-dim mean/std to [5] before elementwise ops.
let z = data
    .elementwise_binary(&mean.broadcast_to(&[5])?, BinaryOp::Sub)?
    .elementwise_binary(&std .broadcast_to(&[5])?, BinaryOp::Div)?;
// z ≈ [-1.414, -0.707, 0.0, 0.707, 1.414]

// Sort, cumsum, percentile chain.
let sorted = data.sort(None, None)?;        // [1, 2, 3, 4, 5]
let cumsum = sorted.cumsum(None)?;          // [1, 3, 6, 10, 15]
let median = data.percentile(50.0, None)?;  // 3.0

// Linear algebra.
let a = UFuncArray::new(vec![2, 2], vec![3.0, 1.0, 1.0, 2.0], DType::F64)?;
let b = UFuncArray::new(vec![2], vec![9.0, 8.0], DType::F64)?;
let x = a.solve(&b)?;                       // [2.0, 3.0]

Bit-exact NumPy-compatible RNG

use fnp_random::Generator;

let mut rng = Generator::from_pcg64_dxsm(12345)?;
let normals = rng.standard_normal(1000);             // identical to numpy.random.Generator(PCG64DXSM(12345)).standard_normal(1000)
let samples = rng.binomial(10, 0.5, 100)?;           // BTPE algorithm, NumPy-exact
let perm    = rng.permutation(&[1.0, 2.0, 3.0, 4.0, 5.0])?; // Fisher–Yates via random_interval, matches NumPy's _shuffle_raw

Python (via the fnp_python PyO3 extension)

import fnp_python as np

# NumPy's own objects where fnp has no native route, native Rust fast-paths
# on the hot ones.
print(np.__version__)
print(np.__numpy_version__)        # the numpy used as fallback oracle
print(np.linalg.solve([[3, 1], [1, 2]], [9, 8]))   # 2-D solve calls numpy.linalg.solve (LAPACK)

rng = np.random.default_rng(12345)
print(rng.standard_normal(5))      # bit-exact NumPy parity

Supported NumPy oracle: 2.3 or newer

FrankenNumPy's parity claims — including every bit-exactness claim — are defined against NumPy >= 2.3. Older NumPy is not a supported oracle, and the conformance harness fails fast with a named reason rather than emitting byte mismatches across unrelated suites.

The floor exists because NumPy's own observable behaviour changed at 2.3 in ways that no single implementation can satisfy on both sides at once. Measured on NumPy 2.2.4:

Surface NumPy 2.2.4 NumPy >= 2.3
count_nonzero(...) (no axis) returns a Python int returns int64
inspect.signature(np.add) no x1 / x2 parameters exposes x1, x2
a[mask].sum() reduces in an order departing from NumPy's own documented pairwise tree above ~25k elements follows the documented tree
import numpy.char fails — numpy.strings has no slice imports

In every case FrankenNumPy matches the newer NumPy. The alternative — keying each expectation to a NumPy version inside the test suite — would have meant encoding two contradictory answers per surface and weakening the gates that make the parity claims worth anything, so the project declares a floor instead.


This section in the README on GitHub