comparison m-toolbox/classes/+utils/@prog/convertComString.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function output = convertComString(varargin)
2 % replaceString changes the input string accordingly to a predefined list of rules
3 %
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 %
6 % DESCRIPTION: CONVERTCOMSTRING hanges the input string accordingly to a
7 % predefined list of rules, to convert between MATLAB, MUPAD
8 % and MATHEMATICA command string syntax.
9 % This supports conversions: MATLAB <-> MUPAD
10 % MATLAB <-> MATHEMATICA
11 %
12 % CALL: output = convertComString('string', conversion);
13 %
14 % PARAMETERS:
15 % conversion A string defining the conversion:
16 % - 'ToSymbolic' MATLAB --> Symbolic (MUPAD)
17 % - 'ToMathematica' MATLAB --> MATHEMATICA
18 % - 'FromSymbolic' MUPAD (Symbolic) --> MATLAB
19 % - 'FromMathematica' MATHEMATICA --> MATLAB
20 %
21 % EXAMPLES:
22 % output = convertComString('(a.*b).*sin(pi)','ToMathematica')
23 % output = convertComString('Sin[3*Pi]/Cos[Pi]','FromMathematica')
24 % output = convertComString('(a.*b).*sin(pi)','ToSymbolic')
25 % output = convertComString('sin(3*PI)/cos(PI)','FromSymbolic')
26 % output = convertComString('PI*sin(3*PI)/cos(PI)','FromSymbolic')
27 %
28 % VERSION: $Id: convertComString.m,v 1.6 2010/03/20 14:50:49 hewitson Exp $
29 % Alpha version
30 %
31 % HISTORY: 27-05-2009 N Tateo
32 % Creation
33 %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36 output = varargin{1};
37 direct = varargin{2};
38
39 % Generic substitutions (tipically the bidirectional ones, such as unique
40 % combinations of chars) are defined in the following list.
41 % Perticular substitutions (such as the imaginary 'i', but not the 'i'
42 % inside a string) are performed preliminarly in the preliminaryCheck
43 % function.
44
45 % Rules to/from MATHEMATICA:
46 % These rules are bidirectional;
47 mathematicaRule = {...
48 './' , '/' ; ...
49 '.*' , '*' ; ...
50 '.^' , '^' ; ...
51 };
52
53 % Rules to/from MUPAD:
54 % These rules are bidirectional;
55 mupadRule = {...
56 './' , '/' ; ...
57 '.*' , '*' ; ...
58 '.^' , '^' ; ...
59 '1I' , 'I' ; ...
60 };
61
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63
64 switch lower(direct)
65 case 'tosymbolic'
66 rule = mupadRule;
67 output = preliminaryCheckSymb(output,1);
68 direction = 1;
69 case 'tomathematica'
70 rule = mathematicaRule;
71 output = preliminaryCheckMath(output,1);
72 direction = 1;
73 case 'fromsymbolic'
74 rule = mupadRule;
75 output = preliminaryCheckSymb(output,2);
76 direction = 0;
77 case 'frommathematica'
78 rule = mathematicaRule;
79 output = preliminaryCheckMath(output,2);
80 direction = 0;
81 otherwise
82 error('*** ''conversion'' parameter not recognized.')
83 end
84
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86
87 % Now performs the standard substitution, according to the rule matrix:
88 if direction % from Matlab
89 for ii=1:size(rule,1)
90 output = strrep(output,rule{ii,1},rule{ii,2});
91 output = regexprep(output, '([0-9]+)I', '$1*I');
92 end
93 else % to Matlab
94 for ii=1:size(rule,1)
95 output = strrep(output,rule{ii,2},rule{ii,1});
96 end
97 end
98
99
100
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104
105 function myout = preliminaryCheckMath(mystr,mynumb)
106 % Performs a list of complex substitutions:
107 % - search for function names and substitute the corresponding
108 % parentheses; for example, sin(...) --> Sin[...]
109 % - search for e (as in 4E5) and converts in 10^ .
110 % - search for i (as in 2+4i) and converts in I.
111 % - search for pi (not inside strings) and converts in Pi.
112 %
113 % Parameter: mynumb = 1 MATLAB --> MATHEMATICA,
114 % = 2 MATHEMATICA --> MATLAB
115 %
116
117 if mynumb ==1 % MATLAB --> MATHEMATICA
118 % 'e' substitution:
119 idx = regexp(mystr, '\de\d') + 1;
120 for m = numel(idx):-1:1, mystr = [mystr(1:idx(m)-1) '10^' mystr(idx(m)+1:end)]; end
121 % 'i' substitution:
122 idx = cell2mat(regexp(mystr, {'\di','\*i'})) + 1;
123 for m = numel(idx):-1:1, mystr = [mystr(1:idx(m)-1) '*I' mystr(idx(m)+1:end)]; end
124 % 'pi' substitution:
125 idx = regexp(mystr,'\Wpi') + 1;
126 for m = numel(idx):-1:1, mystr(idx(m)) = 'P'; end
127
128 else % MATHEMATICA --> MATLAB
129 % 'i' substitution:
130 idx = cell2mat(regexp(mystr, {'\dI','\*I'})) + 1;
131 for m = numel(idx):-1:1, mystr = [mystr(1:idx(m)-1) '*i' mystr(idx(m)+1:end)]; end
132 % 'pi' substitution:
133 idx = regexp(mystr,'\WPi') + 1;
134 for m = numel(idx):-1:1, mystr(idx(m)) = 'p'; end
135
136 end
137
138 funcList = {'sin','cos','exp','sqrt','abs'; ...
139 'Sin','Cos','Exp','Sqrt','Abs'};
140 if mynumb==1
141 par = {'(',')' ; '[',']'};
142 else par = {'[',']' ; '(',')'};
143 end
144
145 for j=1:size(funcList,2)
146 idx = strfind(mystr,funcList{mynumb,j});
147 if ~isempty(idx)
148 for jj=numel(idx):-1:1
149 startIdx = idx(jj)+numel(funcList{mynumb,j}); % this is the index of the opening parenthesys
150 k = startIdx+1; openPar = 0;
151 while k~=0
152 if k>numel(mystr), endIdx = k-1; break; end
153 if mystr(k)==par{1,2} && openPar == 0
154 endIdx = k;
155 break
156 elseif mystr(k)==par{1,2} && openPar == 1
157 openPar = openPar-1;
158 elseif mystr(k)==par{1,1}
159 openPar = openPar+1;
160 end
161 k = k+1;
162 end
163 % now startIdx e endIdx are the indexes of the parentheses
164 mystr(startIdx)= par{2,1};
165 mystr(endIdx) = par{2,2};
166 temp = funcList;
167 temp(mynumb,:) = [];
168 mystr(idx(jj):idx(jj)+numel(temp{j})-1) = temp{j};
169 end
170 end
171 end
172 myout = mystr;
173
174 end
175
176 function myout = preliminaryCheckSymb(mystr,mynumb)
177 % Performs a list of complex substitutions:
178 % - search for i (as in 2+4i) and converts in I.
179 % - search for pi (not inside strings) and converts in Pi.
180 %
181 % Parameter: mynumb = 1 MATLAB --> Symbolic (MUPAD),
182 % = 2 Symbolic (MUPAD) --> MATLAB
183 %
184
185 if mynumb ==1 % MATLAB --> Symbolic (MUPAD)
186 % 'i' substitution:
187 idx = cell2mat(regexp(mystr, {'\di','\*i'})) + 1;
188 for m = numel(idx):-1:1, mystr(idx(m)) = 'I'; end
189 % 'pi' substitution:
190 idx = regexp(mystr,'\Wpi') + 1;
191 for m = numel(idx):-1:1, mystr(idx(m)) = 'P'; mystr(idx(m)+1) = 'I';end
192
193 else % Symbolic (MUPAD) --> MATLAB
194 % 'i' substitution:
195 idx = cell2mat(regexp(mystr, {'\dI','\*I'})) + 1;
196 % for m = numel(idx):-1:1, mystr = [mystr(1:idx-1) '*i' mystr(idx+1:end)]; end
197 mystr(idx)='i';
198 % 'pi' substitution:
199 idx = regexp(mystr,'PI\W*');
200 for m = numel(idx):-1:1, mystr(idx(m)) = 'p'; mystr(idx(m)+1) = 'i';end
201
202 end
203
204 myout = mystr;
205
206 end
207
208
209 end