Problem
The library has no numeric module. C++ <numeric> provides essential mathematical range operations that are widely used in algorithms and data processing, none of which are currently available in PythonSTL.
Proposed Module: pythonstl.numeric
| Function |
C++ Equivalent |
Description |
accumulate(arr, init) |
std::accumulate |
Sum/fold over a range with initial value |
inner_product(a, b, init) |
std::inner_product |
Dot product of two ranges |
partial_sum(arr) |
std::partial_sum |
Prefix sum array |
adjacent_difference(arr) |
std::adjacent_difference |
Differences between consecutive elements |
iota(arr, start) |
std::iota |
Fills array with incrementing values from start |
gcd(a, b) |
std::gcd (C++17) |
Greatest common divisor |
lcm(a, b) |
std::lcm (C++17) |
Least common multiple |
reduce(arr, init) |
std::reduce (C++17) |
Like accumulate but order-independent |
exclusive_scan(arr, init) |
std::exclusive_scan (C++17) |
Prefix sum excluding current element |
inclusive_scan(arr) |
std::inclusive_scan (C++17) |
Prefix sum including current element |
Expected API
from pythonstl.numeric import accumulate, partial_sum, iota, gcd, lcm
arr = [1, 2, 3, 4, 5]
print(accumulate(arr, 0)) # 15
print(partial_sum(arr)) # [1, 3, 6, 10, 15]
buf = [0] * 5
iota(buf, 1)
print(buf) # [1, 2, 3, 4, 5]
print(gcd(12, 8)) # 4
print(lcm(4, 6)) # 12
Checklist
Notes
accumulate, partial_sum, iota, gcd, and lcm are the highest priority as they are most commonly used.
- C++17 additions (
reduce, exclusive_scan, inclusive_scan) can be implemented in a later phase.
Problem
The library has no
numericmodule. C++<numeric>provides essential mathematical range operations that are widely used in algorithms and data processing, none of which are currently available in PythonSTL.Proposed Module:
pythonstl.numericaccumulate(arr, init)std::accumulateinner_product(a, b, init)std::inner_productpartial_sum(arr)std::partial_sumadjacent_difference(arr)std::adjacent_differenceiota(arr, start)std::iotagcd(a, b)std::gcd(C++17)lcm(a, b)std::lcm(C++17)reduce(arr, init)std::reduce(C++17)exclusive_scan(arr, init)std::exclusive_scan(C++17)inclusive_scan(arr)std::inclusive_scan(C++17)Expected API
Checklist
accumulateinner_productpartial_sumadjacent_differenceiotagcd,lcmreduceexclusive_scan,inclusive_scanNotes
accumulate,partial_sum,iota,gcd, andlcmare the highest priority as they are most commonly used.reduce,exclusive_scan,inclusive_scan) can be implemented in a later phase.