mirror of
https://github.com/nim-lang/Nim.git
synced 2026-08-31 02:43:41 +00:00
math: Add cumprod and cumproded (#23416)
This pull request adds the `cumproded` function along with its in-place equivalent, `cumprod`, to the math library. These functions provide functionality similar to `cumsum` and `cumsummed`, allowing users to calculate the cumulative sum of elements. The `cumprod` function computes the cumulative product of elements in-place, while `cumproded` additionally returns the prod seq.
This commit is contained in:
@@ -245,6 +245,25 @@ template main() =
|
||||
empty.cumsum
|
||||
doAssert empty == @[]
|
||||
|
||||
block: # cumprod
|
||||
block: #cumprod int seq return
|
||||
let counts = [ 1, 2, 3, 4 ]
|
||||
doAssert counts.cumproded == [ 1, 2, 6, 24 ]
|
||||
|
||||
block: # cumprod float seq return
|
||||
let counts = [ 1.0, 2.0, 3.0, 4.0 ]
|
||||
doAssert counts.cumproded == [ 1.0, 2.0, 6.0, 24.0 ]
|
||||
|
||||
block: # cumprod int in-place
|
||||
var counts = [ 1, 2, 3, 4 ]
|
||||
counts.cumprod
|
||||
doAssert counts == [ 1, 2, 6, 24 ]
|
||||
|
||||
block: # cumprod float in-place
|
||||
var counts = [ 1.0, 2.0, 3.0, 4.0 ]
|
||||
counts.cumprod
|
||||
doAssert counts == [ 1.0, 2.0, 6.0, 24.0 ]
|
||||
|
||||
block: # ^ compiles for valid types
|
||||
doAssert: compiles(5 ^ 2)
|
||||
doAssert: compiles(5.5 ^ 2)
|
||||
@@ -525,3 +544,4 @@ when not defined(js) and not defined(danger):
|
||||
|
||||
doAssertRaises(OverflowDefect):
|
||||
discard sum(x)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user