function yyy = parse(buuu) %*************************************************************** % Parse a Dirac Notation string: Part of the Dirac Package % syntax: parse(' ',0) or parse(' O1=| a >< b | ',0) etc. % supports exp( anyobject) sqrt( realscalar ) % supports assignments like |b> = | a >< b | c > % supports assignments like < a | (equiv. to above) % if used outside Dirac: % you must first define in MATLAB % GLOBALIZE= ' ' %*************************************************************** % This is a patched extensively tested version % It must be soon edited - to replace the patches % July 28th 1995 % and Division sign extensions/patches % August 7th 95 global TESTOUT; TESTOUT = 0; global GLOBALIZE; eval(GLOBALIZE); QUOT=setstr(39); teststr=buuu; blanc=setstr(32); NONE=''; Colon=':'; %remove all double blanks while findstr(teststr,[blanc blanc])~=[] teststr=strrep(teststr,[blanc blanc],blanc); end %remove blanc at beginning and end if teststr(length(teststr))==blanc teststr=teststr(1:length(teststr)-1) ; end if teststr(1)==blanc teststr=teststr(2:length(teststr)) ; end %remove all stars teststr=strrep(teststr,'*',NONE); %replace blancs by : teststr=strrep(teststr,blanc,':'); %Replace exp by ^ teststr=strrep(teststr,'exp','^') ; % Elimination of blancs where not necessary csigns='|<+->()=^/'; NoOfSigns=length(csigns); % for kk=1:NoOfSigns teststr=strrep(teststr,[csigns(kk) Colon],csigns(kk)); teststr=strrep(teststr,[Colon csigns(kk)],csigns(kk)); end Tleng=length(teststr); Oldname=''; % This is used to check for sin, cos etc .... %localize all <||> + - ( ) = ^ : CONTROLS = sort( [ findstr(teststr,'<') findstr(teststr,'|') ... findstr(teststr,'>') findstr(teststr,'+') findstr(teststr,'-') ... findstr(teststr,'=') findstr(teststr,'(') findstr(teststr,')') ... findstr(teststr,':') findstr(teststr,'^') findstr(teststr,'/')] ); if(TESTOUT==1) teststr CONTROLS end assignment=findstr(teststr,'='); % find total number of Dirac Signs conts=length(CONTROLS); if CONTROLS==[] yyy=buuu; else NotStartControl=0; yyy=' ';StarMissing = 0; if( CONTROLS(1) ~= 1 ) yyy=teststr(1:CONTROLS(1)-1); StarMissing = 1; Oldname= teststr(1:CONTROLS(1)-1); NotStartControl=1; end if CONTROLS(conts)~=Tleng CONTROLS = [CONTROLS (Tleng+1) ]; teststr=[teststr ' ']; conts=conts+1; end patchflag=0; ProtectedName= 0; for kk=1:conts-1 % if (length(Oldname)==3 ) if Oldname=='sin' | Oldname=='cos' ProtectedName=1; end end if (length(Oldname)==4 ) if Oldname=='expm' | Oldname=='sqrt' ProtectedName=1; end end OperSign= teststr(CONTROLS(kk) ); NextOperSign = teststr(CONTROLS(kk+1) ); if(CONTROLS(kk+1)-CONTROLS(kk)>1) NaME=teststr(CONTROLS(kk)+1:CONTROLS(kk+1)-1); Oldname=NaME; else NaME=''; end if OperSign=='^' yyy = [ yyy 'expm' ]; ProtectedName=1; % Patch here too PATCH end % two signs after each other if( ( CONTROLS(kk+1)- CONTROLS(kk) ) ==1 ) % two signs after each other if( OperSign=='>') % case > if( NextOperSign~='+' & NextOperSign~='-' & ... NextOperSign~='=' & NextOperSign~=')' & ... NextOperSign~='/' ) yyy=[ yyy '*' ];StarMissing = 0; end end if( OperSign=='|') % case | if( NextOperSign~='+' & NextOperSign~='-' & ... NextOperSign~='=' & NextOperSign~=')' & ... NextOperSign~='/' ) yyy=[ yyy '*' ]; StarMissing = 0; end end if( OperSign== '(' & NextOperSign=='|' & ProtectedName~= 1) if (kk==1 & NotStartControl==0) yyy=[ yyy OperSign NaME]; StarMissing = 0; patchflag=1; else yyy=[ yyy '*' OperSign NaME]; StarMissing = 0; patchflag=1; %PATCHED HERE end else if( OperSign=='+' | OperSign=='-' | OperSign=='=' | ... OperSign=='(' | OperSign==')' ) % case + - = ( ) yyy=[ yyy OperSign ]; StarMissing = 0; if(OperSign~=')' ) StartFlag = 1 ; StarMissing = 0; end end % end % PATCH .... if( OperSign== ')' & NextOperSign=='(' ) yyy=[ yyy '*' ]; StarMissing = 0; end if( OperSign== ')' & NextOperSign=='|' ) % NEXT PATCH yyy=[ yyy '*' ]; StarMissing = 0; end % Division sign patches if( ( OperSign== ')' ) & NextOperSign=='/' ) yyy=[ yyy OperSign ]; StarMissing = 0; end if( OperSign== '>' | OperSign== '|' )... & NextOperSign=='/' StarMissing = 0; end % Other side Division sign patches if( ( OperSign== '/' ) & NextOperSign=='(' ) yyy=[ yyy OperSign ] ;StarMissing = 0; end if OperSign=='/' ... & ( NextOperSign== '<'| NextOperSign== '|' ) yyy=[ yyy OperSign ] StarMissing = 0; end % done two signs after each other (there is no name) %%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%% else % there is a name %%%%%% if( OperSign=='<') if ( StarMissing == 1 ) yyy=[ yyy '*' ]; StarMissing = 0; end yyy=[ yyy '(' NaME ')' QUOT ] ; % yyy=[ yyy 'conj(' NaME ')' QUOT ] ; end if( OperSign=='|' ) if( CONTROLS(kk)== 1 | StartFlag == 1) yyy=[ yyy NaME] ; StarMissing = 1; % if first sign else if (patchflag==1 ) yyy=[ yyy NaME] ; StarMissing = 1; % if not first sign patchflag=0; else yyy=[ yyy '*' NaME] ; StarMissing = 1; % if not first sign end end end if( OperSign=='+' | OperSign=='-' | ... OperSign=='=' ) yyy=[ yyy OperSign NaME]; StarMissing = 1; end if OperSign=='(' if ProtectedName== 1 | (kk==1 & NotStartControl==0) ... | StarMissing == 0 % PATCH AGAIN (TRIPLE) yyy=[ yyy OperSign NaME]; StarMissing = 0; ProtectedName=0; else yyy=[ yyy '*' OperSign NaME]; StarMissing = 0; end end if OperSign==':' yyy=[ yyy '*' NaME]; StarMissing = 0; end if OperSign==')' yyy=[ yyy OperSign '*' NaME]; % additional; fixing StarMissing = 0; end % Another Division sign patch if OperSign=='/' yyy=[ yyy OperSign NaME ]; end StartFlag =0; end %%%%%%%%%%%%%%%%%% there was a name %%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%% end % for all control signs LastOperSign = teststr(CONTROLS(conts) ); if( LastOperSign==')' ) yyy=[ yyy LastOperSign ] ; end if CONTROLS(conts) ~= Tleng yyy=[ yyy teststr(CONTROLS(conts)+1: Tleng)]; end % One more patch: equal sign followed by a star Tleng=length(yyy); unwant='=*'; punwant=findstr(yyy,unwant); if(length(punwant)>0 ) yyy=[ yyy(1:punwant(1)) yyy(punwant(1)+2:Tleng)]; end % One more patch: star followed by a star Tleng=length(yyy); unwant='**'; punwant=findstr(yyy,unwant); if(length(punwant)>0 ) yyy=[ yyy(1:punwant(1)) yyy(punwant(1)+2:Tleng)]; end % When the string contains an assignment, pre-pend a blanc (not used) if assignment ~= [] yyy= [ ' ' yyy ]; end end % postprocessing PosConj = findstr(yyy,[ QUOT '=']); if PosConj~=[] Tleng = length(yyy); www = yyy(1:PosConj-1); www=strrep(www,'(',NONE); www=strrep(www,')',NONE); www = [www '=(' yyy(PosConj+2: Tleng ) ')' QUOT ]; yyy = www; end