/*
Geometric mean in Picat.
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import util.
% import cp.
main => go.
go =>
_ = random2(),
L1 = 1..10,
println(prod=geometric_mean_prod(L1)),
println(geometric_mean(L1)),
println(prod=geometric_mean_prod(L1)),
nl,
R1 = [1+random() mod 10 : _I in 1..100],
println(R1),
println(geometric_mean(R1)),
println(prod=geometric_mean_prod(R1)),
nl,
R2 = [1+random() mod 10 : _I in 1..10000],
println(r2length=R2.length),
println(geometric_mean(R2)),
garbage_collect(90000000),
println(geometric_mean_prod(R2)), % give stack overflow or inf
nl.
geometric_mean(List) = exp(avg(List.map(log))).
% this can't handle too large values
geometric_mean_prod(List) = prod(List)**(1/List.length).