1:- module(math, [mul/3, sub/3, add/3, neg/2, divby/3, recip/2, log/2, exp/2, gammaln/2,
2 max/3, equal/3, stoch/3, to_float/2, prodlist/2, map_sum/3, map_sum/4]). 3
4:- use_module(library(apply_macros)). 5
6max(X,Y,Z) :- Z is max(X,Y).
7divby(X,Y,Z) :- Z is Y/X.
8sub(X,Y,Z) :- Z is Y-X.
9add(X,Y,Z) :- Z is Y+X.
10neg(X,Y) :- Y is -X.
11mul(X,Y,Z) :- Z is X*Y.
12equal(X,Y,V) :- X=Y -> V=1; V=0.
13recip(X,Y) :- Y is 1/X.
14gammaln(X,Y) :- Y is lgamma(X).
15log(X,Y) :- Y is log(X).
16exp(X,Y) :- Y is exp(X).
17
18prodlist(L,X) :- prodlist(L,1,X).
19prodlist([],X,X) :- !.
20prodlist([A|AX],X,Z) :- Y is A*X, prodlist(AX,Y,Z).
28stoch(H,P,N) :- sumlist(H,N), N>0, maplist(divby(N),H,P).
33to_float(X,Y) :- Y is float(X).
40:- meta_predicate map_sum(2,+,-), map_sum(3,+,+,-). 41map_sum(P,X,Y,Sum) :- maplist(P,X,Y,Z), sum_list(Z,Sum).
42map_sum(P,X,Sum) :- maplist(P,X,Y), sum_list(Y,Sum).
43
44user:goal_expansion(map_sum(P,X,Sum), (maplist(P,X,Y), sum_list(Y,Sum))).
45user:goal_expansion(map_sum(P,X,Y,Sum), (maplist(P,X,Y,Z), sum_list(Z,Sum)))