BeginPackage["UVW`Zeroone`",{"Graphics`Animation`"}]
(* Version 2.0, 08/15/97 *)


RSZeroOne::usage= "
RSZeroOne[p,n] returns a random sample of n zeros and ones. One is chosen 
with probability p."

PlotZeroOne::usage= "
PlotZeroOne[listofzeroones] represents graphically a list of zeros and 
ones as black and white squares on a grey background."

AnimateShift::usage= "
AnimateShift[listofzeroones] forms a list of zeros and ones three times 
as long as the initial list, by adding first a list of same length of 
random digits, then copying the initial list at the end. Then the successive 
shifts are animated as arrays of black and white squares."

Binary::usage= "
Binary[functionf,listofzeroones] computes the real in [0,1] the binary 
decomposition of which is that of the list. Then the image by functionf 
(from [0,1] into R) is computed. The decimal part of it is returned under 
binary form as a new list of zeroones."

ActualLength::usage= "
ActualLength[listofzeroones] returns the length of the list obtained when 
all zeros before the first one are droppped in listofzeroones."
       
Weight::usage= "
Weight[listofzeroones] returns the number of ones in the list."
	 
WeightedAlphabeticalOrder::usage= "
WeightedAlphabeticalOrder[listofzeroones] computes the rank of the given 
list of zeros and ones among lists of same actual length, when they are 
ranked according to increasing weights and alphabetical order for lists 
of same weight. That rank is returned in base 2 as another list of zeros 
and ones."
	  
BinaryNumbers::usage= "
BinaryNumbers[n] returns a list of all the 2^n lists of binary digits with
length n."

Entropy::usage= "
Entropy[t] returns -tLog[2,t]-(1-t)Log[2,1-t]"


Begin["`Private`"]

RSZeroOne[p_,n_]:= Table[If[Random[]<p,1,0],{n}];


PlotZeroOne[listofzeroones_List,opts___]:=

      Block[{complete,dim}, 
      
	   dim      = Ceiling[Sqrt[N[Length[listofzeroones]]]];
	   complete = Partition[listofzeroones,dim];
      
	   Show[Graphics[Raster[complete]],
				  opts,
				  AspectRatio->0.9,
				  Background->GrayLevel[0.5]]

	   ]      
	   

AnimateShift[listofzeroones_List]:=

      Block[{dim,dim2,complete,nbshift,walt,disney,i},
	 
	   dim      = Length[listofzeroones];
	   dim2     = Ceiling[Sqrt[N[Length[listofzeroones]]]];
	  
	   complete = Join[listofzeroones,RSZeroOne[0.5,dim],listofzeroones];
	   nbshift  = 2*dim+1;
	   walt     = Table[Partition[
			Take[complete,{i,i+dim-1}],dim2],{i,1,nbshift}];

	   disney   = Table[Graphics[Raster[walt[[i]]]],{i,1,nbshift}];
	   ShowAnimation[disney]

	 ]

Binary[f_,listofzeroones_List]:=

      Block[{x,y},

	   x = listofzeroones.Table[0.5^i, {i,1,Length[listofzeroones]}];
	   y = f[x];
	   y = Mod[y,1.];
	   Return[ Drop[ Flatten[ RealDigits[y,2] ] ,-1] ];
 
	   ]

ActualLength[listofzeroones_List]:=

      Block[{j=1},
	   While[listofzeroones[[j]]==0,
		 j=j+1];
	   Return[Length[listofzeroones]-j+1];
	   ];


Weight[listofzeroones_List]:=Apply[Plus,listofzeroones];



WeightedAlphabeticalOrder[listofzeroones_List]:=

  Block[{r,p,u,l,i},

       p = Weight[listofzeroones];
       l = Length[listofzeroones];
       r =  0;

	 u=0;
	 i=0;
	 While[u<p,
	      (
	      i=i+1;
	      If[listofzeroones[[i]]==1, 
		      (r=r+Binomial[l,u]+Binomial[l-i,p-u] ; u=u+1)
		]
	      )
	      ];
      
       Return[IntegerDigits[r,2]];
       ];
  
BinaryNumbers[n_Integer]:=

  Block[{m,l,i,number},
    
       m = 2^n-1;
       l = {Table[O,{n}]};
       i = 0;
       Do[(
	   i=i+1;
	   number=IntegerDigits[i,2];
	   If[Length[number]<n,
		number = Join[Table[0,{n-Length[number]}],number]];
	   l = Append[l,number]
	  ),{m}];

       Return[l];
       ];
    

Entropy[t_]:=-t Log[2,t] -(1-t) Log[2,t]; 


End[]


EndPackage[]
