1
0
mirror of https://github.com/janet-lang/janet synced 2024-10-18 16:05:47 +00:00

Add geomean function.

This commit is contained in:
Calvin Rose 2024-07-31 09:46:53 -05:00
parent d1104b5a65
commit aaeaa3a944

View File

@ -711,6 +711,19 @@
(each x xs (+= accum x) (++ total))
(/ accum total))))
(defn geomean
"Returns the geometric mean of xs. If empty, returns NaN."
[xs]
(if (lengthable? xs)
(do
(var accum 0)
(each x xs (+= accum (math/log x)))
(math/exp (/ accum (length xs))))
(do
(var [accum total] [0 0])
(each x xs (+= accum (math/log x)) (++ total))
(math/exp (/ accum total)))))
(defn product
"Returns the product of xs. If xs is empty, returns 1."
[xs]