Skip to content

Commit

Permalink
added must_be checks to cplint_util
Browse files Browse the repository at this point in the history
  • Loading branch information
friguzzi committed Dec 8, 2023
1 parent 6221220 commit 40863b2
Showing 1 changed file with 58 additions and 8 deletions.
66 changes: 58 additions & 8 deletions prolog/cplint_util.pl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* a bar for the probability and a bar for one minus the probability.
*/
bar(P,Chart):-
must_be(float,P),
PF is 1.0-P,
Chart = c3{data:_{x:elem, rows:[elem-prob,'T'-P,'F' -PF], type:bar},
axis:_{x:_{type:category}, rotated: true,
Expand All @@ -66,6 +67,7 @@
* a bar for the probability
*/
bar1(P,Chart):-
must_be(float,P),
Chart = c3{data:_{x:elem, rows:[elem-prob,'T'-P], type:bar},
axis:_{x:_{type:category}, rotated: true,
y:_{min:0.0,max:1.0,padding:_{bottom:0.0,top:0.0},
Expand All @@ -79,6 +81,8 @@
* a bar for the number of successes and a bar for the number of failures
*/
bar(S,F,Chart):-
must_be(nonneg,S),
must_be(nonneg,F),
Chart = c3{data:_{x:elem, rows:[elem-prob,'T'-S,'F' -F], type:bar},
axis:_{x:_{type:category}, rotated: true,
y:_{min:0.0,padding:_{bottom:0.0}}},
Expand All @@ -95,10 +99,11 @@
* The size of the bar is given by N.
*/
argbar(ValList,Chart):-
maplist(to_atom,ValList,ValList1),
Chart = c3{data:_{x:elem, rows:[elem-prob|ValList1], type:bar},
axis:_{x:_{type:category}, rotated: true,
y:_{min:0.0,padding:_{bottom:0.0}}},
must_be(list,ValList),
maplist(to_atom,ValList,ValList1),
Chart = c3{data:_{x:elem, rows:[elem-prob|ValList1], type:bar},
axis:_{x:_{type:category}, rotated: true,
y:_{min:0.0,padding:_{bottom:0.0}}},
% size:_{height: 100},
legend:_{show: false}}.

Expand Down Expand Up @@ -135,6 +140,8 @@
* the number of bins for dividing the domain, default value 40
*/
histogram(L0,Chart,Options):-
must_be(list,L0),
must_be(list,Options),
maplist(to_pair,L0,L1),
maplist(key_pair,L1,L2),
max_list(L2,DMax0),
Expand Down Expand Up @@ -188,6 +195,9 @@
* the number of bins for dividing the domain, default value 40
* */
densities(Pri0,Post0,Chart,Options):-
must_be(list,Pri0),
must_be(list,Post0),
must_be(list,Options),
option(nbins(NBins),Options,40),
maplist(to_pair,Pri0,Pri1),
maplist(to_pair,Post0,Post1),
Expand Down Expand Up @@ -276,6 +286,8 @@
* the number of bins for dividing the domain, default value 40
*/
density(Post,Chart,Options):-
must_be(list,Post),
must_be(list,Options),
maplist(to_pair,Post,Post0),
maplist(key_pair,Post0,PoK),
max_list(PoK,DMax0),
Expand Down Expand Up @@ -314,6 +326,8 @@
* the number of bins for dividing the X and Y domains, default value 40
*/
density2d(Post0,D,Options):-
must_be(list,Post0),
must_be(list,Options),
maplist(key_x_y,Post0,X,Y),
max_list(X,DxMax),
min_list(X,DxMin),
Expand Down Expand Up @@ -493,12 +507,9 @@
matrix_sum([A],[B],[C]).

/**
* variance(+Values:list,-Average:float,-Variance:float) is det.
* variance(+Values:list,-Variance:float) is det.
* std_dev(+Values:list,-Average:float,-Dev:float) is det.
* std_dev(+Values:list,-Dev:float) is det.
*
* Computes the variance or standard deviation (and the average) of Values.
* Computes the variance of Values.
* Values can be
*
* * a list of numbers
Expand All @@ -511,14 +522,53 @@
variance(L,Var):-
variance(L,_Av,Var).

/**
* variance(+Values:list,-Average:float,-Variance:float) is det.
*
* Computes the variance the average of Values.
* Values can be
*
* * a list of numbers
* * a list of couples number-weight, in which case each number is multiplied by the weight
* before being considered
* * a list of couples list-weight, in which case list is considered as a matrix of numbers.
* The matrix in each element of List must have the same dimension and are aggregated element-
* wise
*/
variance(L,Av,Var):-
average(L,Av),
maplist(sq_diff(Av),L,LS),
average(LS,Var).

/**
* std_dev(+Values:list,-Dev:float) is det.
*
* Computes the standard deviation of Values.
* Values can be
*
* * a list of numbers
* * a list of couples number-weight, in which case each number is multiplied by the weight
* before being considered
* * a list of couples list-weight, in which case list is considered as a matrix of numbers.
* The matrix in each element of List must have the same dimension and are aggregated element-
* wise
*/
std_dev(L,Dev):-
std_dev(L,_Av,Dev).

/**
* std_dev(+Values:list,-Average:float,-Dev:float) is det.
*
* Computes the standard deviation and the average of Values.
* Values can be
*
* * a list of numbers
* * a list of couples number-weight, in which case each number is multiplied by the weight
* before being considered
* * a list of couples list-weight, in which case list is considered as a matrix of numbers.
* The matrix in each element of List must have the same dimension and are aggregated element-
* wise
*/
std_dev(L,Av,Dev):-
variance(L,Av,Var),
root(Var,Dev).
Expand Down

0 comments on commit 40863b2

Please sign in to comment.