Skip to content

Commit

Permalink
Compare function sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
bleudev committed Sep 5, 2024
1 parent bb03788 commit b57ff2a
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions ufpy/algebra/func_sequence.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from __future__ import annotations

__all__ = (
'FunctionSequence',
)

from typing import Callable, TypeVar, Generic, Literal, overload, Any

from ufpy.utils import mul
from ufpy.cmp import cmp_generator

VT = TypeVar('VT', int, float, int | float)
KT = TypeVar('KT', int, float, int | float)

VT2 = TypeVar('VT2', int, float, int | float)
KT2 = TypeVar('KT2', int, float, int | float)

@cmp_generator
class FunctionSequence(Generic[VT, KT]):
def __resolve_item(self, kwarg: tuple[str, VT]) -> tuple[int, VT] | None:
name, value = kwarg
Expand Down Expand Up @@ -63,14 +70,14 @@ def __init__(self, f: Callable[[int, KT, VT, int], VT], k: KT = None, **kwargs:
def __call__(self, n: int) -> VT: ...
@overload
def __call__(self, start: int, end: int) -> list[VT]: ...
def __call__(self, start_or_end: int, end: int = None):
start = start_or_end
def __call__(self, start: int, end: int | None = None):
start1 = start
if not end:
end = start_or_end
end = start

r = []

for i in range(start, end+1):
for i in range(start1, end+1):
r.append(self.__process_float(self.f(i)))

return r if len(r) > 1 else r[0]
Expand All @@ -80,6 +87,9 @@ def __getitem__(self, n: int | slice):
start, end = x if (x := n.start) else 1, n.stop
return self(start, end)
return self(n)

def __cmp__(self, other: FunctionSequence[VT2, KT2]):
return self.k - other.k

def __check_for_list(self, l_or_v: list | Any):
if isinstance(l_or_v, list):
Expand All @@ -90,20 +100,16 @@ def __check_for_list(self, l_or_v: list | Any):
def s(self, n: int) -> VT: ...
@overload
def s(self, start: int, end: int) -> VT: ...
def s(self, start_or_n: int, end: int = None) -> VT:
if end:
start = start_or_n
else:
start, end = 1, start_or_n
def s(self, start: int, end: int | None = None) -> VT:
if end is None:
start, end = 1, start
return sum(self.__check_for_list(self(start, end)))

@overload
def p(self, n: int) -> VT: ...
@overload
def p(self, start: int, end: int) -> VT: ...
def p(self, start_or_n: int, end: int = None) -> VT:
if end:
start = start_or_n
else:
start, end = 1, start_or_n
def p(self, start: int, end: int | None = None) -> VT:
if end is None:
start, end = 1, start
return mul(self.__check_for_list(self(start, end)))

0 comments on commit b57ff2a

Please sign in to comment.