function elements=compound(form)
% Function compound takes a compound formula as a string and returns
% a cell with the atomic number, symbol, and number of atoms of each
% element present in that compound.
% Example: >> compound('Ca(OH)2') 
%
%             ans = 
%
%                 [20]    [8]    [1]
%                 'Ca'    'O'    'H'
%                 [ 1]    [2]    [2]




atsy=['H ';'He';'Li';'Be';'B ';'C ';'N ';'O ';'F ';
 'Ne';'Na';'Mg';'Al';'Si';'P ';'S ';'Cl';'Ar';'K ';'Ca';
 'Sc';'Ti';'V ';'Cr';'Mn';'Fe';'Co';'Ni';'Cu';'Zn';'Ga';
 'Ge';'As';'Se';'Br';'Kr';'Rb';'Sr';'Y ';'Zr';'Nb';'Mo';
 'Tc';'Ru';'Rh';'Pd';'Ag';'Cd';'In';'Sn';'Sb';'Te';'I ';
 'Xe';'Cs';'Ba';'La';'Ce';'Pr';'Nd';'Pm';'Sm';'Eu';'Gd';
 'Tb';'Dy';'Ho';'Er';'Tm';'Yb';'Lu';'Hf';'Ta';'W ';'Re';
 'Os';'Ir';'Pt';'Au';'Hg';'Tl';'Pb';'Bi';'Po';'At';'Rn';
 'Fr';'Ra';'Ac';'Th';'Pa';'U '];

cellatsy=cellstr(atsy);
catsy=deblank(cellatsy);
num_char=size(form,2);

char_vec=isletter(form);
num_poss_elem=sum(char_vec);
elements_cell_name=cell(num_poss_elem,1);
a=1;
elem=0;
stop=0;
mult=1;
error=0;
end_str=0;
while stop~=1
 if a>=num_char
 stop=1;
 else 
  z=1;
  t=1;
  p=1;
  next_element=0;
  next_elementa=0;
  if char_vec(a)==0
     if form(a)=='('
        endparenth=findstr(')',form);
        if size(endparenth)==0
            disp('ERROR.  There is an unclosed parenthesis.')
            error=1;
            stop=1;
        else
            if endparenth==num_char
               mult=1;
            elseif isletter(endparenth+1)==1
               mult=1;
            else
               aa=0;
               bb=endparenth+1;
               while aa~=1
                   if bb==num_char+1
                     mult=str2num(form(endparenth+1:bb-1));
                     aa=1;
                   else
                   cc=isletter(form(bb));
                   if cc==1
                      mult=str2num(form(endparenth+1:bb-1));
                      aa=1;
                   else
                      bb=bb+1;
                   end
                   end
               end
            end
            a=a+1;
        end
     elseif form(a)==')'
        mult=1;
        a=a+1;
     else
     a=a+1;
     end
  else   
     b=char_vec(a)+char_vec(a+1);
     if b==2     
         dbl_char=form(a:a+1);
         c=strmatch(dbl_char,catsy,'exact');
         d=size(c,1);
         if d~=0
              elem=elem+1;
              elements_num(elem)=c;
	      if a==num_char-1
                num_atoms(elem)=1*mult;
                a=a+2;
                end_str=1;
              else
                a=a+2;
                y=a;
                x=isletter(form(y));
                if x==1
                     num_atoms(elem)=1*mult;
                elseif form(y)=='('
                     num_atoms(elem)=1*mult;
                elseif form(y)==')'
                     num_atoms(elem)=1*mult;
                else
                     while z~=0
                          if y==num_char+1
                               next_element=1;
                          end
                          if next_element~=1
                          if isletter(form(y))==1
                               next_element=1;
                          end
                          if form(y)=='('
                               next_element=1;
                          end
                          if form(y)==')'
                               next_element=1;
                          end
                          end
                          if next_element==1                                 
                                num_atoms(elem)=str2num(form(a:y-1))*mult;
                                 z=0;
                            else
                                 y=y+1;
                            end
                          end
                       end
                     end
         else
	      one_char=form(a);
              e=strmatch(one_char,catsy,'exact');
              if size(e,1)==0
                 stop=1;
                 error=1;
                 disp('ERROR. An element was entered that is not in database. Check for typo.')
              else
              elem=elem+1;
              elements_num(elem)=e;
              a=a+1;
              num_atoms(elem)=1*mult;
              end
         end     
     else
         one_char=form(a);
         f=strmatch(one_char,catsy,'exact');
         if size(f,1)==0;
            stop=1;
            error=1;
            disp('ERROR. An element was entered that is not in database. Check for a typo.')
         else
            elem=elem+1;
            elements_num(elem)=f;
            if a==num_char
              num_atoms(elem)=1;
              z=0;
            else
                a=a+1;
                r=a;
                q=isletter(form(r));
                if q==1
                     num_atoms(elem)=1*mult;   
                elseif form(r)=='('
                     num_atoms(elem)=1*mult;
                elseif form(r)==')'
                     num_atoms(elem)=1*mult;
                else
                     while p~=0
                          if r==num_char+1
                               next_elementa=1;
                          end
                          if next_elementa~=1
                          if isletter(form(r))==1
                               next_elementa=1;
                          end
                          if form(r)=='('
                               next_elementa=1;
                          end
                          if form(r)==')'
                               next_elementa=1;
                          end
                          end
                          if next_elementa==1   
                                 num_atoms(elem)=str2num(form(a:r-1))*mult;
                                 p=0;
                            else
                                 r=r+1;
                            end
                          end
                       end   
                     end

          end
       end
 end
  end
end
if end_str~=1
if error==0
final=isletter(form(num_char));
if final==1
     one_char=form(num_char);
     finalel=strmatch(one_char,catsy,'exact');
     if size(finalel,1)==0
        error=1;
        disp('ERROR. An element was entered that is not in database. Check for typo.')
     else
     elem=elem+1;
     elements_num(elem)=finalel;
     num_atoms(elem)=1;
     end
end
end
end
if error==0
elements_name=cell(1,elem);
for g=1:elem
     elements_name{g}=catsy{elements_num(g)};
end

%Now check for multiple entries of the same atom:
done=0;
one_elem=0;
tot_elem=size(elements_num,2);
while done~=1
     if one_elem==tot_elem
          done=1;
     else
          
          
          doneloop=0;
          one_elem=one_elem+1;
          elem_check=one_elem;
          rest_elem=one_elem+1;
          counter=0;
          while doneloop~=1
            
            counter=counter+1;
            
            if counter>tot_elem
              doneloop=1;
            elseif rest_elem>tot_elem
              doneloop=1;
            else
               if elements_num(rest_elem)==elements_num(one_elem)
                    num_atoms(one_elem)=num_atoms(one_elem)+num_atoms(rest_elem);
                    if rest_elem~=tot_elem
                    elements_num=[elements_num(1:rest_elem-1) elements_num(rest_elem+1:tot_elem)];
                    elements_name=[elements_name(1:rest_elem-1) elements_name(rest_elem+1:tot_elem)];
                    num_atoms=[num_atoms(1:rest_elem-1) num_atoms(rest_elem+1:tot_elem)];
                    tot_elem=tot_elem-1;
                    else
                    elements_num=[elements_num(1:rest_elem-1)];
                    elements_name=[elements_name(1:rest_elem-1)];
                    num_atoms=[num_atoms(1:rest_elem-1)];
                    tot_elem=tot_elem-1;
                    end
               else
               rest_elem=rest_elem+1;
               end
            
            end
           end
      end
end
elements_num_subt=size(elements_num,2);
donelast=0;

var=0;

for counter2=1:elements_num_subt-1
donelast=0;
c2=0;
while donelast~=1
c2=c2+1;
     if c2==elements_num_subt
     donelast=1;
     elseif elements_num(c2)==elements_num(elements_num_subt)
          elements_num=[elements_num(1:elements_num_subt-1)];
          elements_name=[elements_name(1:elements_num_subt-1)];
          num_atoms(c2)=num_atoms(c2)+num_atoms(elements_num_subt);
          num_atoms=[num_atoms(1:elements_num_subt-1)];
          elements_num_subt=elements_num_subt-1;
          var=1;
          donelast=1;
      else
     end
end
end

if var==0
c3=0;
  for counter3=1:elements_num_subt-2
  c3=c3+1;
     if elements_num(c3)==elements_num(elements_num_subt-1)
         elements_num_subt
         c3
          elements_num=[elements_num(1:elements_num_subt-2) elements_num(elements_num_subt)];
          elements_name=[elements_name(1:elements_num_subt-2) elements_name(elements_num_subt)];

          num_atoms(c3)=num_atoms(c3)+num_atoms(elements_num_subt-1);
          num_atoms=[num_atoms(1:elements_num_subt-2) num_atoms(elements_num_subt)];
     end
  end
else
end

     
elements=cell(3,size(elements_num,2));
for ans=1:size(elements_num,2)
  elements{1,ans}=elements_num(ans);
  elements{2,ans}=elements_name{ans};
  elements{3,ans}=num_atoms(ans);
end
end
