wordconverter.m

来自「student teacher and proferssor llove thi」· M 代码 · 共 1,499 行 · 第 1/5 页

M
1,499
字号
function [outflag,nif,suborfun]=wordconverter(i,j,suborfun);% This will change the Matlab function to its Fortran equivalent syntax.% outflag(1) is 1 if its taken care of here. 0 if not.% outflag(2) is 1 if we need to update funstr, NOTE if you added to fortranfunwords, this is set automagically.% outflag(3) indicates this can be translated on a per/call basis (newer method)declare_globalsnif=cell(2,1);fortranfunwordslength=length(fortranfunwords);outflag=[0 0 0];r=char(10);s=size(funstr,1);arr={'r';'c';'i';'l'}; noi={'r';'c';'s';'t';'v'}; com={'c';'t'}; log={'l';'v'};if length(find(strcmp(filename_all,funstrwords{i}{j})))>0 outflag(1)=1; temp=find(strcmp(filename_all,funstrwords{i}{j})); [howmany,subscripts,centercomma,parens]=hassubscript(i,j); [howmany2,subscripts2,centercomma2,parens2]=hasoutput(i,j); if howmany2==1  suborfun(temp)=1;  tempstr=makeMATLABcallstring(howmany,subscripts,centercomma,parens,i,j);  tempfun=funstr{i};  funstr{i}=funstr{i}(1:parens(1));  repstr='dble';  for ii=1:howmany   if ii~=howmany    if any(strcmp(tempstr(ii),typs{7}))     goon=1;     if length(subscripts{ii})>3      if strncmp('dble',subscripts{ii},4)       goon=0;      end     end     if goon      funstr{i}=[funstr{i},'dble(',subscripts{ii},'),'];      outflag(2)=1;     else      funstr{i}=[funstr{i},subscripts{ii},','];     end    else     funstr{i}=[funstr{i},subscripts{ii},','];    end   else    if any(strcmp(tempstr(ii),typs{7}))     goon=1;     if length(subscripts{ii})>3      if strncmp('dble',subscripts{ii},4)       goon=0;      end     end     if goon      funstr{i}=[funstr{i},'dble(',subscripts{ii},')',tempfun(parens(2):length(tempfun))];      outflag(2)=1;     else      funstr{i}=[funstr{i},subscripts{ii},tempfun(parens(2):length(tempfun))];     end    else     funstr{i}=[funstr{i},subscripts{ii},tempfun(parens(2):length(tempfun))];    end   end  end  if outflag(2)==1   fortranfunwords{length(fortranfunwords)+1}=repstr;  end elseif howmany2>1  suborfun(temp)=0;  tempstr=makeMATLABcallstring(howmany2,subscripts2,centercomma2,parens2,i,j);  tempstr2=['call ',filename_all{temp},'('];  for ii=1:howmany2   if any(strcmp(tempstr(ii),typs{7}))    tempstr2=[tempstr2,'dble(',subscripts2{ii},'),'];   else    tempstr2=[tempstr2,subscripts2{ii},','];   end  end  tempstr=makeMATLABcallstring(howmany,subscripts,centercomma,parens,i,j);  for ii=1:howmany   if ii~=howmany    if any(strcmp(tempstr(ii),typs{7}))     tempstr2=[tempstr2,'dble(',subscripts{ii},'),'];    else     tempstr2=[tempstr2,subscripts{ii},','];    end   else    if any(strcmp(tempstr(ii),typs{7}))     tempstr2=[tempstr2,'dble(',subscripts{ii},'));'];    else     tempstr2=[tempstr2,subscripts{ii},');'];    end   end  end  funstr{i}=tempstr2; endelseswitch funstrwords{i}{j} case 'ss2in'  repstr='ss2inf';fortranfunwords{length(fortranfunwords)+1}=repstr;outflag(1)=1;  funstr=replaceword(i,j,funstr,funstrwords,funstrwords_b,funstrwords_e,repstr);   outflag(3)=1; case 'max'  [howmany,subscripts,centercomma,parens]=hassubscript(i,j);  tempstr=makeMATLABcallstring(howmany,subscripts,centercomma,parens,i,j);  goon=1;  foo=1;  foo2=0;  %funstr{i},foo,foo2,'what?',kb  temp=cell(howmany,2);  for k=1:howmany   if any(strcmp(tempstr(k),typs{3}))    temp{k,1}='abs(';temp{k,2}=')';    outflag(2)=1;  %Because we need an update   else    temp{k,1}='';temp{k,2}='';   end  end  if howmany==1                         % One argument   if any(strcmp(typs{11},tempstr))        % 2-D Array argument    ;% Decide what to do based on the shape    if foo     if any(strcmp(tempstr(1),typs{1}))     % Full matrix, just add ,1 to the min statement      funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'spread(maxval(',temp{1,1},funstr{i}(parens(1)+1:parens(2)-1),temp{1,2},',1),1,1)',funstr{i}(parens(2)+1:length(funstr{i}))];     elseif any(strcmp(tempstr(1),typs{9})) % Row, OK, fortran mins entire thing      funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'maxval(',temp{1,1},funstr{i}(parens(1)+1:parens(2)-1),temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];     elseif any(strcmp(tempstr(1),typs{10}))% column, OK, fortran mins entire thing      funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'maxval(',temp{1,1},funstr{i}(parens(1)+1:parens(2)-1),temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];     end     goon=0;   outflag(2)=1;    end   elseif any(strcmp(typs{4},tempstr))    % 1-D Array argument    funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'maxval(',temp{1,1},subscripts{1},temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];    goon=0;    outflag(2)=1;   else                                   % Scalar argument    funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),funstr{i}((funstrwords_e{i}(j)+1):length(funstr{i}))];    goon=0;    outflag(2)=1;   end  elseif howmany==2                     % Two arguments   for k=1:length(tempstr)    if ~isempty(find(strcmp(tempstr(k),barr)))     [a(k),b(k)]=find(strcmp(tempstr(k),barr));    else     [a(k),b(k)]=find(strcmp(tempstr(k),barr2));     b(k)=b(k)+3;    end   end   if b(1)~=b(2) & ~(b(1)==3 | b(2)==3)    disp(['Problem with min. Array sizes don''t match.'])    disp(['First subscript ',subscripts{1}])    disp(['Second subscript ',subscripts{2}])    funstr{i},keyboard   else    goon=0;%Do nothing except add abs if needed. Use min as is in fortran    funstr{i}=[funstr{i}(1:parens(1)),temp{1,1},funstr{i}(parens(1)+1:centercomma-1),temp{1,2},',',temp{2,1},funstr{i}(centercomma+1:parens(2)),temp{2,2},funstr{i}(parens(2)+1:length(funstr{i}))];   end  elseif howmany==3                     % three arguments   if j>1    if strcmp(funstrwords{i}{j-1},'spread')     foo=0; foo2=0;    end   end   [tempval]=determineshape(howmany,subscripts,centercomma,parens,i,j,3);   if ~((tempval==1)|(tempval==2))      foo=0; foo2=1;     end   if foo    temp2=cell(1,2);temp2{1}='';temp2{2}='';    if ~any(strcmp(tempstr(3),typs{5})),temp2{1}='mxs(';temp2{2}=')';end    if tempval==1     temp5{1}='';                temp5{2}='';    else     temp5{1}='transpose(';      temp5{2}=')';    end    if any(strcmp(tempstr(1),typs{1}))     % Full matrix, just add ,1 to the sum statement     funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),temp5{1},'spread(maxval(',temp{1,1},subscripts{1},temp{1,2},',',temp2{1},temp{3,1},subscripts{3},temp{3,2},temp2{2},'),1,1)',temp5{2},funstr{i}(parens(2)+1:length(funstr{i}))];    elseif any(strcmp(tempstr(1),typs{9})) %Row     if tempval==1 %remove min and 2nd sub      funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),funstr{i}(funstrwords_e{i}(j)+1:centercomma(1)-1),funstr{i}(parens(2):length(funstr{i}))];     elseif tempval==2      funstr{i}=[funstr{i}(1:funstrwords_b{i}(j)-1),'maxval(',temp{1,1},subscripts{1},temp{1,2},funstr{i}(parens(2):length(funstr{i}))];     end    elseif any(strcmp(tempstr(1),typs{10}))% column     if tempval==1      funstr{i}=[funstr{i}(1:funstrwords_b{i}(j)-1),'maxval(',temp{1,1},subscripts{1},temp{1,2},funstr{i}(parens(2):length(funstr{i}))];     elseif tempval==2      funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),funstr{i}(funstrwords_e{i}(j)+1:centercomma(1)-1),funstr{i}(parens(2):length(funstr{i}))];     end    elseif any(strcmp(tempstr(1),typs{4}))% 1-D     funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'maxval(',temp{1,1},subscripts{1},temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];    elseif any(strcmp(tempstr(1),typs{5}))% scalar     funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'(',temp{1,1},subscripts{1},temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];    end    goon=0;      outflag(2)=1;   else    if foo2      goon=1;     else      goon=0;     end   end  end  if goon   repstr='maxf';fortranfunwords{length(fortranfunwords)+1}=repstr;outflag(1)=1;   funstr=replaceword(i,j,funstr,funstrwords,funstrwords_b,funstrwords_e,repstr);   outflag(3)=1;  else   outflag(1)=1;  end  %funstr{i},goon,outflag,kb case 'min'  [howmany,subscripts,centercomma,parens]=hassubscript(i,j);  [howmany2,subscripts2,centercomma2,parens2]=hasoutput(i,j);  tempstr=makeMATLABcallstring(howmany,subscripts,centercomma,parens,i,j);  goon=1;  foo=1;  foo2=0;  %funstr{i},foo,foo2,'what?',kb  temp=cell(howmany,2);  for k=1:howmany   if any(strcmp(tempstr(k),typs{3}))    temp{k,1}='abs(';temp{k,2}=')';    outflag(2)=1;  %Because we need an update    %foo=0; foo2=1; Uncomment if you want to make a minf for the complexes   else    temp{k,1}='';temp{k,2}='';   end  end  if foo   if howmany==1                         % One argument    if any(strcmp(typs{11},tempstr))        % 2-D Array argument     ;% Decide what to do based on the shape     if foo      if any(strcmp(tempstr(1),typs{1}))     % Full matrix, just add ,1 to the min statement       funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'spread(minval(',temp{1,1},funstr{i}(parens(1)+1:parens(2)-1),temp{1,2},',1),1,1)',funstr{i}(parens(2)+1:length(funstr{i}))];      elseif any(strcmp(tempstr(1),typs{9})) % Row, OK, fortran mins entire thing       funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'minval(',temp{1,1},funstr{i}(parens(1)+1:parens(2)-1),temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];      elseif any(strcmp(tempstr(1),typs{10}))% column, OK, fortran mins entire thing       funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'minval(',temp{1,1},funstr{i}(parens(1)+1:parens(2)-1),temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];      end      goon=0;   outflag(2)=1;     end    elseif any(strcmp(typs{4},tempstr))    % 1-D Array argument     funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'minval(',temp{1,1},subscripts{1},temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];     goon=0;    outflag(2)=1;    else                                   % Scalar argument     funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),funstr{i}((funstrwords_e{i}(j)+1):length(funstr{i}))];     goon=0;    outflag(2)=1;    end   elseif howmany==2                     % Two arguments    for k=1:length(tempstr)     if ~isempty(find(strcmp(tempstr(k),barr)))      [a(k),b(k)]=find(strcmp(tempstr(k),barr));     else      [a(k),b(k)]=find(strcmp(tempstr(k),barr2));      b(k)=b(k)+3;     end    end    if b(1)~=b(2) & ~(b(1)==3 | b(2)==3)     disp(['Problem with min. Array sizes don''t match.'])     disp(['First subscript ',subscripts{1}])     disp(['Second subscript ',subscripts{2}])     funstr{i},keyboard    else     goon=0;    outflag(2)=1;%Do nothing except add abs if needed. Use min as is in fortran     funstr{i}=[funstr{i}(1:parens(1)),temp{1,1},funstr{i}(parens(1)+1:centercomma-1),temp{1,2},',',temp{2,1},funstr{i}(centercomma+1:parens(2)),temp{2,2},funstr{i}(parens(2)+1:length(funstr{i}))];    end   elseif howmany==3                     % three arguments    if j>1     if strcmp(funstrwords{i}{j-1},'spread')      foo=0; foo2=0;     end    end    [tempval]=determineshape(howmany,subscripts,centercomma,parens,i,j,3);    if ~((tempval==1)|(tempval==2))      foo=0; foo2=1;     end    if foo     temp2=cell(1,2);temp2{1}='';temp2{2}='';     if ~any(strcmp(tempstr(3),typs{5})),temp2{1}='mxs(';temp2{2}=')';end     if tempval==1      temp5{1}='';                temp5{2}='';     else      temp5{1}='transpose(';      temp5{2}=')';     end     if any(strcmp(tempstr(1),typs{1}))     % Full matrix, just add ,1 to the sum statement      funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),temp5{1},'spread(minval(',temp{1,1},subscripts{1},temp{1,2},',',temp2{1},temp{3,1},subscripts{3},temp{3,2},temp2{2},'),1,1)',temp5{2},funstr{i}(parens(2)+1:length(funstr{i}))];     elseif any(strcmp(tempstr(1),typs{9})) %Row      if tempval==1 %remove min and 2nd sub       funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),funstr{i}(funstrwords_e{i}(j)+1:centercomma(1)-1),funstr{i}(parens(2):length(funstr{i}))];      elseif tempval==2       funstr{i}=[funstr{i}(1:funstrwords_b{i}(j)-1),'minval(',temp{1,1},subscripts{1},temp{1,2},funstr{i}(parens(2):length(funstr{i}))];      end     elseif any(strcmp(tempstr(1),typs{10}))% column      if tempval==1       funstr{i}=[funstr{i}(1:funstrwords_b{i}(j)-1),'minval(',temp{1,1},subscripts{1},temp{1,2},funstr{i}(parens(2):length(funstr{i}))];      elseif tempval==2       funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),funstr{i}(funstrwords_e{i}(j)+1:centercomma(1)-1),funstr{i}(parens(2):length(funstr{i}))];      end     elseif any(strcmp(tempstr(1),typs{4}))% 1-D      funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'minval(',temp{1,1},subscripts{1},temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];     elseif any(strcmp(tempstr(1),typs{5}))% scalar      funstr{i}=[funstr{i}(1:(funstrwords_b{i}(j)-1)),'(',temp{1,1},subscripts{1},temp{1,2},')',funstr{i}(parens(2)+1:length(funstr{i}))];     end     goon=0;      outflag(2)=1;    else     if foo2      goon=1;     else      goon=0;     end    end   end  else   if howmany==3 %This should eliminate 2nd sub, and come back here for a minf    funstr{i}=[funstr{i}(1:centercomma(1)),subscripts{3},funstr{i}(parens(2):end)];    goon=0;      outflag(2)=1;   else    if foo2      goon=1;     else      goon=0;     end   end  end  if goon   %funstr{i},'minnnnnnnnnn',kb

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?