Skip to content

Commit 4dbd842

Browse files
committed
add consistent prod_left and prod_right methods
1 parent d786196 commit 4dbd842

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

quatint/quat.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,25 @@ def gcd_right(a: "hurwitzint", b: OP_TYPES) -> "hurwitzint":
927927
return a.gcd_right(b)
928928

929929

930-
def prod_left(x: Iterator[object], start: object = 1):
930+
def prod_right(x: Iterator[OP_TYPES], unit: Union["hurwitzint", None] = None):
931931
"""Simply a helper method to match existing Python prod syntax"""
932-
return functools.reduce(operator.mul, x, 1) * start
932+
product: hurwitzint = hurwitzint(1, 0, 0, 0)
933+
for sub_x in x:
934+
product = sub_x * product
935+
936+
if unit is None:
937+
unit = hurwitzint(1, 0, 0, 0)
938+
939+
return unit * product
940+
941+
942+
def prod_left(x: Iterator[OP_TYPES], unit: Union["hurwitzint", None] = None):
943+
"""Simply a helper method to match existing Python prod syntax"""
944+
primes: hurwitzint = hurwitzint(1, 0, 0, 0)
945+
for sub_x in x:
946+
primes *= sub_x
947+
948+
if unit is None:
949+
unit = hurwitzint(1, 0, 0, 0)
950+
951+
return primes * unit

tests/test_quat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from math import isqrt, prod
1+
from math import isqrt
22
from pathlib import Path
33
from typing import Union
44

55
from hurwitz import HurwitzQuaternion
66

77
import quatint.quat
88

9-
from quatint.quat import hurwitzint, prod_left
9+
from quatint.quat import hurwitzint, prod_left, prod_right
1010

1111
def test_compiled_tests():
1212
"""Verify that we are running these tests with a compiled version of hurwitzint"""
@@ -344,7 +344,7 @@ def test_main(self):
344344
"""Validate factor works as expected."""
345345
factors = self.b_int.factor_right()
346346

347-
ans = prod(reversed(factors.primes), start=factors.unit)
347+
ans = prod_right(factors.primes, unit=factors.unit)
348348

349349
self.assert_equal(self.b_int, ans)
350350

@@ -356,6 +356,6 @@ def test_main(self):
356356
"""Validate factor works as expected."""
357357
factors = self.b_int.factor_left()
358358

359-
ans = prod_left(factors.primes, start=factors.unit)
359+
ans = prod_left(factors.primes, unit=factors.unit)
360360

361361
self.assert_equal(self.b_int, ans)

0 commit comments

Comments
 (0)