Skip to main content

ReduceAcc

Trait ReduceAcc 

Source
pub trait ReduceAcc: Element + Copy {
    type Acc: Element + Copy + Add<Output = Self::Acc> + Mul<Output = Self::Acc>;

    // Required method
    fn widen(self) -> Self::Acc;
}
Expand description

Maps an element type T to the type NumPy uses to accumulate (and return) sum / prod / cumsum / cumprod over it.

NumPy promotes any integer dtype of less precision than the default platform integer before reducing, so a narrow-int reduction can never overflow and the result dtype is the platform integer:

“The dtype of a is used by default unless a has an integer dtype of less precision than the default platform integer. In that case, if a is signed then the platform integer is used while if a is unsigned then an unsigned integer of the same precision as the platform integer is used.” — numpy/_core/fromnumeric.py:2321-2327 (sum), :3306-3312 (prod)

The reduction itself is umr_sum = um.add.reduce / umr_prod = um.multiply.reduce (numpy/_core/_methods.py:20-21), i.e. the add/multiply ufunc whose loop dtype is the promoted accumulator.

The mapping (platform integer = 64-bit, matching ferray’s intp/int64):

  • i8 / i16 / i32 → i64, i64 → i64, i128 → i128
  • u8 / u16 / u32 → u64, u64 → u64, u128 → u128
  • bool → i64 (NumPy reduces bool as the platform integer, counting true)
  • f32 → f32, f64 → f64, complex stays itself (no promotion)

Wider-or-equal dtypes map to themselves, so existing f64/i64/complex reductions are unchanged — only narrow-int callers observe the promoted return type.

Required Associated Types§

Source

type Acc: Element + Copy + Add<Output = Self::Acc> + Mul<Output = Self::Acc>

The accumulator-and-result element type for reductions over Self.

Required Methods§

Source

fn widen(self) -> Self::Acc

Widen one element into the accumulator type before reducing, matching NumPy’s promotion of the loop dtype (true → 1 for bool).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ReduceAcc for Complex<f32>

Source§

type Acc = Complex<f32>

Source§

fn widen(self) -> Self

Source§

impl ReduceAcc for Complex<f64>

Source§

type Acc = Complex<f64>

Source§

fn widen(self) -> Self

Source§

impl ReduceAcc for bool

Source§

type Acc = i64

Source§

fn widen(self) -> i64

Source§

impl ReduceAcc for f32

Source§

type Acc = f32

Source§

fn widen(self) -> f32

Source§

impl ReduceAcc for f64

Source§

type Acc = f64

Source§

fn widen(self) -> f64

Source§

impl ReduceAcc for i8

Source§

type Acc = i64

Source§

fn widen(self) -> i64

Source§

impl ReduceAcc for i16

Source§

type Acc = i64

Source§

fn widen(self) -> i64

Source§

impl ReduceAcc for i32

Source§

type Acc = i64

Source§

fn widen(self) -> i64

Source§

impl ReduceAcc for i64

Source§

type Acc = i64

Source§

fn widen(self) -> i64

Source§

impl ReduceAcc for i128

Source§

impl ReduceAcc for u8

Source§

type Acc = u64

Source§

fn widen(self) -> u64

Source§

impl ReduceAcc for u16

Source§

type Acc = u64

Source§

fn widen(self) -> u64

Source§

impl ReduceAcc for u32

Source§

type Acc = u64

Source§

fn widen(self) -> u64

Source§

impl ReduceAcc for u64

Source§

type Acc = u64

Source§

fn widen(self) -> u64

Source§

impl ReduceAcc for u128

Implementors§