Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Sep 28, 2024
1 parent 49d014c commit a6e61c9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
20 changes: 13 additions & 7 deletions src/pylib/Lib/math.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export e

from ./math_impl/err import raiseDomainErr, raiseRangeErr
from ./math_impl/errnoUtils import setErrno0, setErrno, isErr0, EDOM, ERANGE
from ./math_impl/vec_op/niter_types import toNimIterator
from ./math_impl/vec_op/niter_types import toNimIterator, ClosureIter

template checkErrno(result, exc): bool =
not isErr0() and math_is_error(result, exc)
Expand Down Expand Up @@ -212,19 +212,25 @@ proc iterToFloatSeq[T](it: Iterable[T]): seq[float] =
when T is SomeFloat: e.float
else: e.toFloat

template gen2pointsGetFloat(sym; pypatch: int; listOp; iterOp){.dirty.} =
template sym*[T](p, q: OpenarrayOrNimIter[T]): float {.pysince(3,pypatch).} = n_math.sym(p, q)
template gen2pointsGetFloat(sym; pypatch: int; listOp; iterOp; oaOp; niterOp){.dirty.} =
template sym*[T](p, q: list[T]): float {.pysince(3,pypatch).} =
bind listOp
n_math.sym(listOp p, listOp q)
template sym*[T](p, q: Iterable[T]): float {.pysince(3,pypatch).} =
bind iterOp
n_math.sym(iterOp(p), iterOp(q))

gen2pointsGetFloat dist, 8, `@`, iterToFloatSeq
n_math.sym(iterOp[T](p), iterOp[T](q))
template sym*[T](p, q: openarray[T]): float {.pysince(3,pypatch).} =
bind oaOp
n_math.sym(oaOp(p), oaOp(q))
template sym*[T](p, q: ClosureIter[T]): float {.pysince(3,pypatch).} =
bind niterOp
n_math.sym(niterOp(p), niterOp(q))

template asisOp[T](x: T): T = x
gen2pointsGetFloat sumprod ,12, asisOp, toNimIterator

gen2pointsGetFloat dist, 8, `@`, iterToFloatSeq, asisOp, iterToFloatSeq

gen2pointsGetFloat sumprod ,12, toNimIterator, toNimIterator, toNimIterator, asisOp


expN hypot
Expand Down
8 changes: 4 additions & 4 deletions src/pylib/Lib/math_impl/vec_op.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

from ./vec_op/niter_types import OpenarrayOrNimIter
from ./vec_op/dist import dist
from ./vec_op/sumprod import sumprod
from ./vec_op/dist import nil
from ./vec_op/sumprod import nil

export OpenarrayOrNimIter
export dist
export sumprod
export dist.dist
export sumprod.sumprod

6 changes: 4 additions & 2 deletions src/pylib/Lib/math_impl/vec_op/niter_types.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@


type OpenarrayOrNimIter*[T] = openarray[T] or iterable[T]
type ClosureIter*[T] = iterator (): T
#type NimIterator*[T] = ClosureIter[T] or iterable[T]
type OpenarrayOrNimIter*[T] = openarray[T] or ClosureIter[T]

template toNimIterator*[T](x): iterable[T] =
template toNimIterator*[T](x): ClosureIter[T] =
iterator (): T =
for i in x:
yield i
Expand Down
20 changes: 13 additions & 7 deletions src/pylib/Lib/math_impl/vec_op/sumprod.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from std/lenientops as mixOps import nil ## import no symbols directly

from ../isX import isfinite
from ./niter_types import OpenarrayOrNimIter, toNimIterator
from ./niter_types import OpenarrayOrNimIter, toNimIterator, ClosureIter
from ./private/dl_ops import DoubleLength, dl_mul, dl_sum

type TripleLength = object
Expand Down Expand Up @@ -33,15 +33,21 @@ template isFloatType(T): bool = T is SomeFloat
template PyFloat_AS_DOUBLE(x: SomeFloat): float = float x
template PyLong_AS_DOUBLE(x: SomeInteger or bool): float = float x

template PyNumber_Add[F: SomeNumber](a, b: F): F = system.`+` a, b
template PyNumber_Add(a, b): untyped = mixOps.`+` a, b
template Number_iAdd(a, b): untyped = mixOps.`+=` a, b

template Number_iAdd[F: SomeNumber](a, b: F) = system.`+=` a, b
template Number_iAdd(a, b) = a = mixOps.`+`(a, b)

template PyNumber_Multiply[F: SomeNumber](a, b: F): F = system.`*` a, b
template PyNumber_Multiply(a, b): untyped = mixOps.`*` a, b


template n_next[T](p: iterable[T]): T = p()
template n_iterStopped(p: iterable): bool = finished p
template n_next[T](p: ClosureIter[T]): T = p()
template n_iterStopped(p: ClosureIter): bool = finished p


func math_sumprod_impl[P, Q](p_it: iterable[P]; q_it: iterable[Q]): float =
func sumprod*[P, Q](p_it: ClosureIter[P]; q_it: ClosureIter[Q]): float =
## [clinic input]
##
## Return the sum of products of values from two iterables p and q.
Expand Down Expand Up @@ -145,5 +151,5 @@ func math_sumprod_impl[P, Q](p_it: iterable[P]; q_it: iterable[Q]): float =
Number_iAdd(result, PyNumber_Multiply(p_i, q_i))
Ret

func sumprod*[P, Q](p: OpenarrayOrNimIter[P]; q: OpenarrayOrNimIter[Q]): float =
math_sumprod_impl(p.toNimIterator[P], q.toNimIterator[Q])
func sumprod*[P, Q](p: openarray[P]; q: openarray[Q]): float =
sumprod(p.toNimIterator[P](), q.toNimIterator[Q]())

0 comments on commit a6e61c9

Please sign in to comment.