# Variable calculations should be done in this file.
# It is read and interpreted by wims for all valid calls to the module.


# how to detect the need to compute?
!if $cmd=reply
 	error=
 	compute=yes
	pari_precision=20
	octave_precision=7
	wims_multiexec=pari,octave
	wims_backslash_insmath=yes
	ins_align=middle
	!if $simul=$empty
		error=!append item bad_data to $error
		!exit
	!endif

	!for i in $simul
		u=!positionof item $i in $liste
		v=!item $u of $liste2
		w=!item $u of $liste3
		parm_liste=!replace internal ; by $\
$ in $(parm_$i)
		parm_lcnt=!linecnt $parm_liste
		!for j= 1 to $parm_lcnt
			parmj=!line $j of $parm_liste
			parm_cnt= !itemcnt $parmj
			!if $parm_cnt <>$w
				error=!append item parm$i to $error
			!endif
			!distribute item $parmj into parm1, parm2
			!if $parm1=$empty
				error=!append item bad_data to $error
			!endif
			!if $i isitemof binomial, pascal,chi2,student,fisher and $[floor($parm1)]<>$parm1
				error=!append item integer$i to $error
			!endif
			!if ($i isitemof binomial, pascal and (($parm2>=1) or ($parm2<=0))) or ($i isin geometric and  (($parm1>=1) or ($parm1<=0)))
				error= !append item neg$i to $error
			!endif
			!if ($i isitemof poisson,exponentiel,weibull,gamma,beta and $parm1<=0) or ($i isitemof normal,weibull,gamma,beta and $parm2<=0)
				error=!append item pos$i to $error
			!endif
		!next j
		!if $number=$empty and $simul<>vide
	error=!append item  err_number to $error
		!endif
	!next i
	!if $error<>$empty
		!exit
	!endif
	
	!if $simul <> vide
		!if $simul iswordof laplace logistic
			parm_$i=$empty
			!read slib/stat/$simul $number
		!else
			!read slib/stat/$simul $number,$(parm_$simul)
		!endif
		data=$slib_out
	!endif
	data1=!sort numeric item $data
	data=!replace internal , by , $empty  in $data1
	data_cnt=!itemcnt $data
	!for i in $show
		!if $i isin arithmean,variance,range,median
			!read slib/stat/$i $data
			ans_$i= !item 1 of $slib_out
		!endif
		!if $i=inv
			
			quantile1=!values floor(x*$data_cnt) for x in $quantile
			ans_inv=!item $quantile1 of $data1
		!endif
		!if $i isin histo,repart,quantile	
			data1=!sort numeric item $data
			z1=!item 1 of $data1
			z2=!item -1 of $data1
			arr=$[floor(log($z2-$z1)/log(10))]
			!if $simul iswordof binomial geometric hypergeometric poisson pascal
				baton=baton
				xtics=noxtics
				h=1
				listx=!values x*$h for x = $[floor($z1/$h)] to $[ceil($z2/$h)]
				listx=[$listx]
				h=$[5*10^($arr-1)]	
				listx2=!values 2*$h*x for x=$[floor($z1/(2*$h))] to $[ceil($z2/(2*$h))+1]
			!else
				baton=$empty
				listx=$empty
				xtics=noxtics
				h=$[5*10^($arr-1)]	
				arrz=	$[floor(log(abs($z1))/log(10))]	
				
				listx2=!values x*$h for x=$[floor($z1/$h)] to $[ceil($z2/$h)+1]
			!endif
			!if $i=histo 
				!read slib/stat/effectif  [$data],[$listx]
				data_e=$slib_out
				!read slib/stat/histo $data_e,$xtics,noytics,color blue,$baton
				dessin= $slib_out			
			!endif
			!if $i=repart
			dessin=xrange $[($z1)*(1-sign($z1)*0.01)], $[($z2)*(1+sign($z2)*0.01)]\
yrange -0.5,1.5\
hline 0,0,black\
vline 0,0,black\
dhline 0,1,black\
lines red
				k1=$[1/$data_cnt]
				Z=$z1
				!for j =1 to $[$data_cnt-1]
					z=!item $[$j+1] of $data1
					k=$[($j+1)/$data_cnt]
					!if $Z<>$z or $j=$[$data_cnt-1]
						dessin=!append item $Z,$k1,$z,$k1 to $dessin
						Z=$z
						k1=$k
					!endif				
				!next j
			!endif
			!for j in $listx2
				dessin=!append line  text black,$j,0,small, $j to $dessin
			!next j		
			ans_$i= $dessin\
			parallel 0,-0.05,0,0.05,$h,0,100,blue\
parallel 0,-0.05,0,0.05,-$h,0,100,blue
		!endif
		!if $i=quantile
			dessin=xrange -0.2,1.5\
yrange $[($z1)*(1-sign($z1)*0.3)], $[($z2)*(1+sign($z2)*0.2)]\
hline 0,0,black\
vline 0,0,black\
dvline 1,0,black\
parallel 0,$[-($z2-$z1)/20],0,$[-($z2-$z1)/20],0.2,0,10,black\
text black ,-0.2, $z1*(1+sign($z1)*0.3),small,$z1\
text black, -0.2, $z2,small,$z2\
dhline -0.2, $z2,black\
dhline -0.2, $z1,black\
lines red
				k1=$[1/$data_cnt]
				Z=$z1
				!for j =1 to $[$data_cnt-1]
					z=!item $[$j+1] of $data1
					k=$[($j+1)/$data_cnt]
					!if $Z<>$z or $j=$[$data_cnt-1]
						dessin=!append item $k1,$Z,$k1,$z to $dessin
						Z=$z
						k1=$k
					!endif				
				!next j
				!for j =0 to 5
					j1=$[$j/5]
					dessin=!append line  text black,$j1,0,small, $j1 to $dessin
				!next j
				ans_$i= $dessin
			!endif	
	!next i
	
!endif

wims_module_log=$cmd: simul=$simul
