Skip to content

Implement pythonstl.numeric module #13

@AnshMNSoni

Description

@AnshMNSoni

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

  • accumulate
  • inner_product
  • partial_sum
  • adjacent_difference
  • iota
  • gcd, lcm
  • reduce
  • exclusive_scan, inclusive_scan
  • Unit tests for all functions
  • Docstrings with O() complexity

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions