(* 
  1) Expanding each input bracket as a summation of

	   n Mm[ X1, X2, X3, ...],         (1)

   where $n$ is a rational number, Mm[] denotes the non-commutative
   product of X1, X2, ...  
 
   All possible distinct non-commutative products in the expansions of
   all input brackets form a basis of a linear space, and each
   input bracket corresponding a vector in the space.

	 NumBasis:   the dimension of the space
	 TakeCoef:   extracting n in (1)
	 TakeMm:     extracting the non-commutative product in (1)
	 vcmt[i]:    vector representation of the ith bracket

                           Ren-Cang Li, June 1, 1996
                           na.rcli@na-net.ornl.gov   
*)

(* Two Functions *)
TakeCoef[n_ x_Mm]:=n; TakeCoef[x_Mm]:=1; NumBasis:=0; 
TakeMm[n_ x_Mm]:=x;   TakeMm[x_Mm]:=x; 

Do[ Do[vcmt[ic][j]=0,{j,MaxL}], {ic,NumBrackets}];

(* Operate on cmt[1] First *)
NumBasis=0;
LL=Length[cmt[1]];
Do[{tmp=cmt[1][[i]]; NumBasis=NumBasis+1;
    vcmt[1][i]=TakeCoef[tmp]; MmBasis[NumBasis]=TakeMm[tmp]},
   {i, LL}];

(* Complete the rest *)
Do[{
    LL=Length[cmt[ic]]; preNumBs=NumBasis;
    Do[{
	tmp=cmt[ic][[i]]; vtmp=TakeCoef[tmp]; MmBstmp=TakeMm[tmp];
	ichk=0;
	Do[ If[SameQ[MmBstmp,MmBasis[im]],
	       {vcmt[ic][im]=vtmp; ichk=1}], 
	    {im, preNumBs}];  
	If[ichk==0, {NumBasis=NumBasis+1; MmBasis[NumBasis]=MmBstmp;
		     vcmt[ic][NumBasis]=vtmp}]
       }, {i, LL}]
    }, {ic, 2,NumBrackets}];

(* Show the vectors *)
(*
Do[{
    Print[" ic = ", ic];
    Do[Print[vcmt[ic][i]],{i,NumBasis}];
    Pause[3]
   }, {ic, NumBrackets}];
*)

Do[ib[i]=i,{i,NumBrackets}];
going=1; k=1; NumIndepBrk=-1;

pL=IdentityMatrix[NumBrackets]; 
tmpM=Array[tmpA,{NumBrackets,NumBrackets}];
Do[ Do[tmpA[i,j]=0,{i,NumBrackets},{j,NumBrackets}] ];
Do[tmpA[i,i]=1,{i,NumBrackets}];
While[going !=0 && k<=NumBrackets,
	 {going=0;
	  Do[ Do[ If[going==0,
		     If[vcmt[i][j] !=0,{going=1,ic=i,jc=j}]
		    ],
		  {j,k,MaxL}],
	      {i,k,NumBrackets}];
	  If[going==1,
	     {
	      If[jc != k,
		 Do[{tmp=vcmt[i][k]; vcmt[i][k]=vcmt[i][jc]; vcmt[i][jc]=tmp},
		    {i,NumBrackets}] ];
	      If[ic !=k, {tmp=ib[ic]; ib[ic]=ib[k]; ib[k]=tmp;
		 Do[{tmp=vcmt[ic][j]; vcmt[ic][j]=vcmt[k][j]; vcmt[k][j]= tmp},
		    {j,k,MaxL}]} ];
(*            Print["The pivot"]; Print[k]; Print[vcmt[k][k]];  *)
	      Do[{tmp=vcmt[i][k]/vcmt[k][k]; tmpA[i,k]=tmp;
		  Do[vcmt[i][j]=vcmt[i][j]-tmp vcmt[k][j],
		     {j,k,MaxL}]
		  },
	      {i,k+1,NumBrackets}] 
	     },
	     NumIndepBrk=k-1];
	k=k+1}];
If[NumIndepBrk==-1,NumIndepBrk=k-1];
   
AA=Inverse[tmpM]; 

Mtx=Array[A,{NumBrackets,MaxL}];
Do[ Do[A[i,j]=vcmt[i][j],{j,MaxL}],{i,NumBrackets}];

Print["The number of the  input  brackets is ", NumBrackets];

Print["The number of independent brackets is ",NumIndepBrk];

Print["A basis consists of Brackets:"];
Do[Print[ib[i]],{i,NumIndepBrk}];

Print["Linear dependency's relations: "];
Do[Print["Relation ", i-NumIndepBrk, ":   ", 
	 Sum[Brk[ib[j]] AA[[i,j]],{j,i}],"=",
	 Expand[ Sum[cmt[ib[j]] AA[[i,j]],{j,i}] ]
	],
   {i,NumIndepBrk+1,NumBrackets}];
Print[" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"]
Print["Remark: If you see any non-zero(s)  in  the  right  hand"]; 
Print["        side(s), something is wrong.   Please email this"]; 
Print["        example to Ren-Cang Li (na.rcli@na-net.ornl.gov)."]; 
Print["        Thanks."];
