Debian Codegraph @

texmf-dist/dvips/pstricks/pst-algparser.pro

raw
1 % $Id: pst-algparser.pro 4 2020-06-09 08:32:19Z herbert $
2 %%
3 %% PostScript prologue for PSTricks algorithm parser
4 %% Version 0.07, 2014/08/01
5 %%
6 %% This program can be redistributed and/or modified under the terms
7 %% of the LaTeX Project Public License Distributed from CTAN archives
8 %% in directory macros/latex/base/lppl.txt.
9 %%
10 %%-----------------------------------------------------------------------------%
11 %
12 %currentdict /Pi known not { /Pi 3.14159265359 def } if
13 %
14 /AlgParser { tx@AlgToPs begin AlgToPs end } def % Dominique Rodriguez
15 /I2P { AlgParser cvx exec } def % Infix to Postfix
16 %
17 /tx@CoreAnalyzerDict 100 dict def tx@CoreAnalyzerDict begin
18 %
19 % PS ANALYZER FOR ALGEBRAIC EXPRESSION V1.13
20 %
21 % 09/2011 DR factorial with ! added
22 %
23 % E->T|E+T
24 % T->FS|T*FS
25 % FS -> F | +FS | -FS
26 % F->P|F^SF|P!
27 % P->(E)|literal
28 % literal->number|var|var[E]|func(params)
29 % params->E|E,param
30 % number->TOBEFINISHED
31 %
32 %% E expression, T term, SF signed factor, F factor, P power
33 %
34 %% parser
35 %
36 %% str
37 %
38 %% C->E<condition_operators>E
39 %% STR index -> STR index+lenExpr
40 /AnalyzeCond { AnalyzeExpr ReadCondOp AnalyzeExpr EvalCondOp } def
41 %
42 %% analyze Expression List (separator , or | )
43 %% STR index -> STR index+lenExpr
44 %% /AnalyzeListOfE {
45 %% { NextNonBlankChar pop AnalyzeExpr%%dup Strlen eq { exit } if NextNonBlankChar
46 %% NextNonBlankChar dup 0 eq { pop exit } if
47 %% dup 44 ne 1 index 124 ne and { dup 41 ne { PROBLEMCONTACTBILLOU } { pop exit } ifelse } if
48 %% pop NextNonBlankChar dup 0 eq { exit } if 124 ne { PROBLEMCONTACTBILLOU } if 1 add NextNonBlankChar 0 eq {toto} if } loop
49 %% AnalyzeListOfEPostHook
50 %% } def
51 /AnalyzeListOfE {
52 /NotFirst false def
53 { NextNonBlankChar pop AnalyzeExpr
54 NotFirst { EvalListOfExpr } { /NotFirst true def } ifelse
55 dup Strlen eq { exit } if NextNonBlankChar
56 dup 44 ne 1 index 124 ne and
57 { dup 41 ne { PROBLEMCONTACTBILLOU } { pop exit } ifelse }
58 if pop 1 add } loop
59 AnalyzeListOfEPostHook
60 } def
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 %% E->T|E+T
63 %% STR index -> STR index+lenExpr
64 /AnalyzeExpr {
65 AnalyzePreHook AnalyzeTerm IsEndingExpr
66 { dup 0 ne { 32 eq { NextNonBlankChar } if } { pop } ifelse }
67 { { RollOp 1 add NextNonBlankChar pop AnalyzeTerm PreEvalHook EvalAddSub IsEndingExpr { pop exit } if } loop }
68 ifelse
69 AnalyzePostHook
70 } def
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %% T->FS|T*FS
73 %% STR index
74 /AnalyzeTerm {
75 AnalyzePreHook AnalyzeSignedFactor IsEndingTerm
76 { dup 0 ne { 32 eq { NextNonBlankChar } if } { pop } ifelse }
77 { { RollOp 1 add NextNonBlankChar pop AnalyzeSignedFactor PreEvalHook EvalMulDiv IsEndingTerm { pop exit } if} loop }
78 ifelse
79 AnalyzePostHook
80 } def
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %% FS -> F | +FS | -FS
83 %% STR index
84 /AnalyzeSignedFactor {
85 AnalyzePreHook 2 copy get dup IsUnaryOp
86 { RollOp 1 add NextNonBlankChar pop AnalyzeSignedFactor EvalUnaryOp }
87 { pop AnalyzeFactor }
88 ifelse AnalyzePostHook
89 } def
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 %% F->P|F^P|P!
92 %% STR index
93 /AnalyzeFactor {
94 AnalyzePreHook AnalyzePower IsEndingFactor
95 { dup 0 ne { 32 eq { NextNonBlankChar } if } { pop } ifelse }
96 { { dup 33 eq%% is there a ! DR 09/2011
97 { pop 1 add NextNonBlankChar pop EvalFactorial }
98 { RollOp 1 add NextNonBlankChar pop AnalyzePower PreEvalHook EvalPower }
99 ifelse
100 IsEndingFactor { pop exit } if } loop }
101 ifelse AnalyzePostHook
102 } def
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 %% P->(E)|literal
105 %% STR index
106 /AnalyzePower {
107 %% depending of first char either a number, or a literal
108 2 copy get dup 40 eq%%an open par
109 { pop 1 add NextNonBlankChar pop AnalyzeExpr 1 add NextNonBlankChar pop }
110 { AnalyzeLiteral }
111 ifelse
112 } def
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 %% STR index STR[index] -> STR index
115 %/AnalyzeLiteral { IsNumber { EvalNumber } { EvalLiteral } ifelse } def
116 /AnalyzeLiteral { dup IsUnaryOp exch IsNumber or { EvalNumber } { EvalLiteral } ifelse } def%%dr 09102006
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 %% recognize + or -
119 %% chr -> T/F
120 /IsUnaryOp { dup 43 eq exch 45 eq or } bind def
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 %% a number can contain only : 0123456789.
123 %% chr -> T/F
124 /IsNumber { dup 48 ge exch dup 57 le 3 -1 roll and exch 46 eq or } bind def
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 %% STR index -> STR index number
127 %% a number can be of the form [0-9]*.[0-9]*\([eE][+-]?[0-9]+\)?
128 %% STR index -> STR index' number
129 /ReadNumber {
130 exch dup 3 -1 roll dup 3 1 roll
131 %%read mantissa
132 { 1 add 2 copy dup Strlen eq { pop pop 0 exit } if get dup IsNumber not { exit } if pop } loop
133 dup 101 eq exch 69 eq or
134 %%% there is a "e" or "E" -> read exponant
135 { 1 add 2 copy get dup IsUnaryOp
136 { pop 1 add 2 copy get } if
137 { IsNumber not { exit } if 1 add 2 copy get } loop }
138 if
139 dup 4 1 roll
140 3 -1 roll exch 1 index sub getinterval
141 } def
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 %% a number can contain only : 0123456789.
144 %% chr -> T/F
145 /IsCondOp { dup 30 eq exch dup 60 ge exch 62 le and or } bind def
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 %% STR index -> STR index number
148 %% a number can be of the form [0-9]*.[0-9]*\([eE][+-]?[0-9]+\)?
149 %% STR index -> STR index' number
150 /ReadCondOp {
151 NextNonBlankChar 1 index 4 1 roll
152 { IsCondOp not { exit } if 1 add 2 copy get } loop
153 2 copy 5 -1 roll
154 exch 1 index sub getinterval 3 1 roll
155 } def
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %% a literal can contain only : 0123456789.
158 %% chr -> T/F
159 /IsLiteral {%
160 dup 48 ge exch dup 57 le 3 -1 roll and exch
161 dup 65 ge exch dup 90 le 3 -1 roll and 3 -1 roll or exch
162 dup 97 ge exch 122 le and or } bind def
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 %% a literal can be of the form [a-zA-Z][a-zA-Z0-9]*\(\((Expression)\)|\(\[Expression\]\)\)?
165 %% STR index -> literal STR index' nextchr
166 /ReadLiteral {
167 exch dup 3 -1 roll dup 3 1 roll
168 %%read literal core
169 { 2 copy dup Strlen eq { pop pop 0 exit } if get dup IsLiteral not { exit } if pop 1 add } loop
170 4 1 roll dup 5 1 roll 3 -1 roll exch 1 index sub getinterval 4 1 roll
171 } def
172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 %% expr is ended by end of str or a clpar
174 %% STR index -> STR index STR[index] T/F
175 /IsEndingExpr {%
176 2 copy dup Strlen eq
177 %% if end of str is reached -> end !
178 { pop pop 0 true }
179 %% ending chr -> clpar, comma, |, <, >, =, !,
180 {get dup dup 41 eq
181 exch dup 124 eq
182 exch dup 93 eq
183 exch dup 44 eq
184 exch dup 30 eq
185 exch dup 60 ge exch 62 le and or or or or or}
186 ifelse } def
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 %% expr is ended by end of str or a +-
189 %% STR index -> STR index STR[index] T/F
190 /IsEndingTerm { IsEndingExpr { true } { dup dup 43 eq exch 45 eq or } ifelse } def
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 %% expr is ended by end of str or */
193 %% STR index -> STR index STR[index] T/F
194 /IsEndingFactor { IsEndingTerm { true } { dup dup 42 eq exch 47 eq or } ifelse } def
195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196 %% expr is ended by end of str or ^
197 %% STR index -> STR index STR[index] T/F
198 /IsEndingPower { IsEndingFactor { true } { dup 94 eq } ifelse } def
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 %% STR index -> STR index STR[index]
201 /NextNonBlankChar { { dup Strlen eq { 0 exit } if 2 copy get dup neBlkChar { exit } if pop 1 add } loop } bind def
202 /neBlkChar { dup 32 ne exch dup 10 ne exch 9 ne and and } bind def
203 %%%%%%%%%%%%%%%%%%%%%%%%
204 %% DEBUG
205 /BRK {false} def
206 /BRKtrue {/BRK true def} def
207 /BRKStop {BRK {BRKtoto} if } def
208 /BRKEvalStop {BRK exch if } def
209 /BRKBRK2true {BRK {BRK2true} if } def
210 /BRK2 {false} def
211 /BRK2true {/BRK2 true def} def
212 /BRK2Stop {BRK2 {BRK2toto} if } def/BRK {false} def
213 end
214 %
215 %-------------------------------------------------------------------------------%
216 %
217 /tx@AlgToPs 12 dict def tx@AlgToPs begin
218 %
219 %% algExpr -> PSVector
220 /AlgToPs { tx@CoreAnalyzerDict begin InitParser AnalyzeListOfE pop pop EndingSequence end } def
221 /EndingSequence { ExpressionVector aload length /end cvx exch 1 add array astore } def
222 /InitParser { /ExpressionVector [ /tx@AddMathFunc cvx /begin cvx ] def dup length /Strlen exch def 0 } def
223 /Strlen 0 def
224 /EvalListOfExpr {} def%
225 /EvalNumber {%
226 ReadNumber cvr /ExpressionVector ExpressionVector aload length dup 3 add -1 roll cvx
227 exch 1 add array astore def NextNonBlankChar pop } def
228 /EvalAddSub {%
229 /ExpressionVector ExpressionVector aload length dup 5 add -1 roll
230 43 eq { /add } { /sub } ifelse cvx exch 1 add array astore def
231 } def
232 /EvalMulDiv {%
233 /ExpressionVector ExpressionVector aload length dup 5 add -1 roll
234 42 eq { /mul } { /div } ifelse cvx exch 1 add array astore def
235 } def
236 /EvalPower {%
237 /ExpressionVector ExpressionVector aload length dup 5 add -1 roll
238 pop /exp cvx exch 1 add array astore def
239 } def
240 /EvalFactorial {% DR 09/2011
241 /ExpressionVector ExpressionVector aload length
242 /fact cvx exch 1 add array astore def
243 } def
244 /EvalLiteral {%
245 ReadLiteral
246 dup 40 eq%%% there is an open par -> function call
247 { pop 2 index
248 dup (Sum) eq { EvalSum }
249 { dup (IfTE) eq { EvalCond }
250 { dup (Derive) eq { pop EvalDerive }
251 { pop 1 add NextNonBlankChar pop AnalyzeListOfE 2 index TrigoFunc
252 /ExpressionVector ExpressionVector aload length dup 5 add -1 roll cvn cvx
253 exch 1 add array astore def 1 add NextNonBlankChar pop } ifelse } ifelse} ifelse }
254 { /ExpressionVector ExpressionVector aload length dup 6 add -1 roll cvn cvx exch 1 add array astore def
255 dup 91 eq%%% there is an open bracket -> vector element
256 { pop 1 add NextNonBlankChar pop AnalyzeExpr
257 /ExpressionVector ExpressionVector aload length /cvi cvx exch /get cvx exch 2 add array astore def 1 add }
258 { pop NextNonBlankChar pop }
259 ifelse}
260 ifelse
261 } def
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 %% the derive function : Derive(n,f(x))
264 %% firstparindex lastparindex ->
265 /EvalDerive {
266 %% manage the function descripiton
267 1 add ReadNumber 3 1 roll NextNonBlankChar
268 44 ne { ANALYZER_ERROR_missing_second_comma_in_Sum } if
269 1 add NextNonBlankChar pop
270 3 -1 roll cvi
271 dup 0 eq
272 { pop AnalyzeExpr 3 -1 roll pop 1 add }
273 { 1 sub 3 1 roll (x) exch tx@Derive begin DeriveIndexed end 4 -1 roll
274 { (x) tx@Derive begin Derive end } repeat
275 ExpressionVector exch /ExpressionVector [] def
276 AlgToPs aload length
277 /ExpressionVector 1 index 3 add -1 roll aload length dup 3 add -1 roll /l2 exch def /l1 exch def
278 l1 l2 add 1 add l2 neg roll l1 l2 add array astore def 3 -1 roll pop 1 add
279 1 index length /Strlen exch def } ifelse
280 } def
281 /EvalSum {%
282 pop 1 add NextNonBlankChar pop
283 %% read the variable name
284 ReadLiteral pop NextNonBlankChar
285 44 ne { ANALYZER_ERROR_missing_first_comma_in_Sum } if
286 %% read the initial value
287 1 add NextNonBlankChar pop ReadNumber cvi 3 1 roll
288 2 copy get 44 ne { ANALYZER_ERROR_missing_second_comma_in_Sum } if
289 %% read the increment value
290 1 add NextNonBlankChar pop ReadNumber cvi 3 1 roll
291 2 copy get 44 ne { ANALYZER_ERROR_missing_second_comma_in_Sum } if
292 %% read the limit value
293 1 add NextNonBlankChar pop ReadNumber cvi 3 1 roll
294 2 copy get 44 ne { ANALYZER_ERROR_missing_second_comma_in_Sum } if
295 /ExpressionVector ExpressionVector aload length dup 7 add -3 roll 0 4 1 roll
296 5 -1 roll 4 add array astore def
297 %% keep ExpressionVector for later and create a new one for internal Sum computation
298 ExpressionVector 3 1 roll /ExpressionVector [ 6 -1 roll cvn /exch cvx /def cvx ] def
299 1 add NextNonBlankChar pop AnalyzeExpr
300 %% add each term
301 /ExpressionVector ExpressionVector aload length 1 add /add cvx exch array astore def
302 /ExpressionVector 4 -1 roll aload length ExpressionVector cvx /for cvx 3 -1 roll 2 add
303 array astore def 3 -1 roll pop 1 add
304 } def
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 %% Convert to radians if trigo function call
307 %% (name) ->
308 /TrigoFunc {
309 dup (cos) eq 1 index (sin) eq or exch (tan) eq or
310 { /ExpressionVector ExpressionVector aload length 3.1415926 /div cvx 180 /mul cvx 5 -1 roll 4 add
311 array astore def
312 } if
313 } def
314 /EvalCond {%
315 pop 1 add AnalyzeCond NextNonBlankChar
316 44 ne { ANALYZER_ERROR_missing_first_comma_in_IfTE } if
317 ExpressionVector 3 1 roll /ExpressionVector [] def
318 1 add AnalyzeExpr ExpressionVector 3 1 roll /ExpressionVector [] def
319 NextNonBlankChar 44 ne { ANALYZER_ERROR_missing_second_comma_in_IfTE } if
320 1 add AnalyzeExpr
321 NextNonBlankChar 41 ne { ANALYZER_ERROR_missing_ending parenthesis_in_IfTE } if
322 ExpressionVector
323 /ExpressionVector 6 -1 roll aload length dup
324 6 add -1 roll cvx exch dup 4 add -1 roll cvx /ifelse cvx 3 -1 roll 3 add array astore def
325 1 add 3 -1 roll pop
326 } def
327 %% CondOp STR index
328 /EvalCondOp {%
329 3 -1 roll
330 dup (=) eq { /eq } {%
331 dup (<) eq { /lt } {%
332 dup (>) eq { /gt } {%
333 dup (>=) eq { /ge } {%
334 dup (<=) eq { /ge } {%
335 dup (!=) eq { /ne } { ERROR_non_valid_conditional_operator }
336 ifelse } ifelse } ifelse } ifelse } ifelse } ifelse
337 cvx exch pop
338 /ExpressionVector ExpressionVector aload length dup 3 add -1 roll exch 1 add array astore def } def
339 /EvalUnaryOp {
340 3 -1 roll 45 eq { /ExpressionVector ExpressionVector aload length /neg cvx exch 1 add array astore def } if
341 } def
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 %% H O O K S
344 /AnalyzePreHook {} bind def
345 /PreEvalHook {} bind def
346 /AnalyzeListOfEPostHook {} bind def
347 /AnalyzePostHook {} def
348 /RollOp { 3 1 roll } bind def
349 end %tx@CoreAnalyzerDict
350 %
351 %--------------------------------------------------------------------%
352 %
353 /tx@Derive 41 dict def tx@Derive begin
354 %%increase ^^ for each function added
355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356 %% algExpr variable -> PSVector
357 /Derive {
358 10240 string 3 1 roll 0 3 1 roll
359 /Variable exch def
360 tx@CoreAnalyzerDict begin InitParser AnalyzeListOfE end
361 } def
362 /Strlen 0 def
363 /InitParser { dup length /Strlen exch def 0 } def
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 %% algExpr variable index -> PSVector
366 /DeriveIndexed {%
367 3 1 roll 10240 string 3 1 roll 0 3 1 roll
368 /Variable exch def
369 tx@CoreAnalyzerDict begin InitParser pop 4 -1 roll AnalyzeExpr 4 -2 roll pop pop 4 -2 roll exch pop end
370 } def
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 %% (u,v)'=-(u',v')
373 /EvalListOfExpr {%
374 4 2 roll 2 copy 9 -1 roll dup length 4 1 roll putinterval add AddPipe
375 2 copy 7 -1 roll dup length 4 1 roll putinterval add
376 6 -2 roll pop pop
377 2 copy pop 0 6 2 roll GetIntervalNewStr 5 1 roll 2 copy 0 exch getinterval 6 1 roll } def
378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 %% (-u)'=-(u')
380 /EvalUnaryOp {
381 4 -2 roll 4 index (0) eq
382 { (0) StrConcat 7 -1 roll pop }
383 { 7 -1 roll 45 eq
384 { AddSub AddOpPar true } { false } ifelse
385 3 1 roll 5 index StrConcat 3 -1 roll { AddClPar } if } ifelse
386 2 copy pop 0 6 2 roll GetIntervalNewStr
387 7 -2 roll pop pop 2 index 6 index dup 4 index exch sub getinterval exch 6 2 roll
388 } def
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 %% (number)'=0
391 /EvalNumber { ReadNumber (0) 6 2 roll } def
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 %% (u+v)'=u'+v'
394 /EvalAddSub {%
395 7 index dup (0) eq
396 { pop true }%% du=0 nothing added
397 { dup length exch 5 index 5 index 3 -1 roll putinterval 4 -1 roll add 3 1 roll false }
398 ifelse
399 5 index dup (0) eq
400 { pop { (0) } { 4 -2 roll 2 copy pop 0 6 2 roll GetIntervalNewStr } ifelse }%%dv=0
401 { exch
402 { 5 -2 roll 7 index 45 eq { AddSub } if false } %%nothing yet added
403 { 5 -2 roll 7 index 43 eq%%something yet added
404 { AddAdd false } { AddSub AddOpPar true } ifelse }
405 ifelse 11 1 roll
406 3 -1 roll StrConcat 10 -1 roll { AddClPar } if
407 2 copy pop 0 6 2 roll GetIntervalNewStr }
408 ifelse
409 mark 11 -5 roll cleartomark 2 index 6 index dup 4 index exch sub getinterval exch 6 2 roll
410 } def
411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412 %% (u*v)' or (u/v)'
413 /EvalMulDiv { 6 index 42 eq {EvalMul} {EvalDiv} ifelse } def
414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
415 %% (u*v)'=u'*v+u*v'
416 /EvalMul {%
417 4 -2 roll 7 index dup (0) eq
418 { pop false }%%du=0
419 { (1) eq%%du=1
420 { false }
421 { AddOpPar 7 index StrConcat AddClPar AddMul AddOpPar true } ifelse
422 3 1 roll 6 index StrConcat 3 -1 roll { AddClPar } if
423 true }%%du!=0
424 ifelse
425 5 1 roll 5 index (0) eq
426 { 5 -1 roll not { (0) StrConcat } if }%%dv=0
427 { 5 -1 roll { AddAdd } if
428 4 index (1) eq
429 { 8 index StrConcat }
430 { AddOpPar 8 index StrConcat AddClPar AddMul AddOpPar 4 index StrConcat AddClPar }
431 ifelse
432 }%%dv!=0
433 ifelse
434 2 copy pop 0 6 2 roll GetIntervalNewStr
435 mark 11 -5 roll cleartomark 2 index 6 index dup 4 index exch sub getinterval exch 6 2 roll
436 } def
437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 %% (u/v)'=(u'*v-u*v')/v^2
439 /EvalDiv {%
440 4 -2 roll
441 4 index (0) eq%%dv=0 -> u'/v
442 { 7 index (0) eq { (0) StrConcat } { AddOpPar 7 index StrConcat AddClPar AddDiv 5 index StrConcat } ifelse }
443 { 7 index dup (0) eq
444 { pop }%%du=0
445 { (1) eq%%du=1
446 { false }
447 { AddOpPar 7 index StrConcat AddClPar AddMul AddOpPar true } ifelse
448 3 1 roll 6 index StrConcat 3 -1 roll { AddClPar } if}%%du!=0
449 ifelse
450 AddSub
451 4 index (1) eq
452 { 8 index StrConcat }
453 { AddOpPar 8 index StrConcat AddClPar AddMul AddOpPar 4 index StrConcat AddClPar }
454 ifelse
455 %}%%dv!=0
456 2 copy GetIntervalNewStr 3 1 roll pop 0 AddOpPar 3 -1 roll StrConcat AddClPar
457 AddDiv AddOpPar 5 index StrConcat AddClPar 2 copy (^2) putinterval 2 add }
458 ifelse
459 2 copy pop 0 6 2 roll GetIntervalNewStr
460 mark 11 -5 roll cleartomark 2 index 6 index dup 4 index exch sub getinterval exch 6 2 roll
461 } def
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 %% str1 index str2 -> str1 index
464 /StrConcat { dup length 4 2 roll 2 copy 6 -1 roll putinterval 3 -1 roll add } bind def
465 /GetIntervalNewStr { 0 exch getinterval dup length string copy } bind def
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 %% (u^v)'=(u^v)'=u'vu^(v-1)+v'u^(v)ln(u)
468 /EvalPower {%
469 4 -2 roll 7 index (0) eq
470 {%%if du=0 then (u^v)'=v'ln(u)u^v
471 4 index (0) eq
472 { (0) StrConcat }%%if dv=0 then (u^v)'=0
473 { 4 index (1) ne { AddOpPar 4 index StrConcat (\)*) StrConcat } if
474 8 index (e) ne { (ln\() StrConcat 8 index StrConcat (\)*) StrConcat } if
475 AddOpPar 8 index StrConcat (\)^\() StrConcat 5 index StrConcat AddClPar } ifelse
476 }
477 {%%du!=0
478 4 index (0) eq
479 {%%if dv=0 then (u^v)'=vu'u^(v-1)
480 5 index dup IsStrNumber
481 { dup (0) eq
482 { StrConcat }
483 { dup dup (1) eq exch (1.0) eq or
484 { StrConcat }
485 { StrConcat
486 7 index dup (1) ne exch (1.0) ne and%%%dr 09102006 insert du if <> 1
487 { (*\() StrConcat 7 index StrConcat (\)) StrConcat } if%%%dr 09102006
488 (*\() StrConcat 8 index StrConcat (\)) StrConcat
489 5 index dup dup (2) eq exch (2.0) eq or
490 { pop } { cvr 1 sub 20 string cvs 3 1 roll (^) StrConcat 3 -1 roll StrConcat } ifelse } ifelse } ifelse }
491 { pop AddOpPar 5 index StrConcat (\)*\() StrConcat 8 index StrConcat (\)^\() StrConcat
492 5 index StrConcat (-1\)) StrConcat } ifelse
493 }
494 {%%if dv!=0 and du!=0 then (u^v)'=u'vu^(v-1)+v'u^(v)ln(u)
495 7 index (1) ne { AddOpPar 7 index StrConcat (\)*) StrConcat } if
496 AddOpPar 5 index StrConcat (\)*\() StrConcat
497 8 index StrConcat (\)^\() StrConcat
498 5 index StrConcat (-1\)+\() StrConcat
499 4 index (1) ne { 4 index StrConcat (\)*\() StrConcat } if
500 8 index StrConcat (\)^\() StrConcat
501 5 index StrConcat (\)*ln\() StrConcat
502 8 index StrConcat AddClPar
503 } ifelse
504 } ifelse
505 2 copy pop 0 6 2 roll GetIntervalNewStr
506 mark 11 -5 roll cleartomark 2 index 6 index dup 4 index exch sub getinterval exch 6 2 roll
507 } def
508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509 %% No derivative for factorial ! only cst => null derivative
510 /EvalFactorial {% DR 09/2011
511 4 index (0) eq
512 { (0) mark 8 -2 roll cleartomark 2 index 7 index dup 4 index exch sub getinterval exch 6 2 roll }
513 { DERIVATIVE_ENGINE_ERROR_no_variable_in_factorial } ifelse
514 } def
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 %% str -> true/false
517 /IsStrNumber {%
518 true exch
519 { dup 48 lt exch dup 57 gt 3 -1 roll or
520 exch dup 46 ne%%.
521 exch dup 43 ne%%+
522 exch 45 ne%%-
523 and and and { pop false } if } forall
524 } def
525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
526 %% literal switch -> func call, vector, variables
527 /EvalLiteral {%
528 ReadLiteral dup 40 eq%%% there is an open par -> function call
529 { pop (EvalFunc_ ) 9 4 index StrConcat 0 exch getinterval cvn cvx exec }
530 { dup 91 eq%%% there is an open bracket -> vector element
531 { DERIVATIVE_ENGINE_ERROR_vector_not_yet_implemented }
532 { pop EvalVariable }
533 ifelse }
534 ifelse
535 } def
536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537 %% first last parpos Expr[first:parpos-1] ->
538 /EvalVariable { 2 index Variable eq { (1) } { (0) } ifelse 4 -1 roll exch 6 2 roll } def
539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
540 %% (f(u))'=u'f'(u)
541 /EvalFunc {
542 4 2 roll 4 index (1) ne
543 { AddOpPar 4 index StrConcat (\)*) StrConcat } if
544 (Eval ) 4 8 index StrConcat 0 exch getinterval cvn cvx exec
545 2 copy pop 0 6 2 roll GetIntervalNewStr
546 mark 9 -3 roll cleartomark 2 index 6 index dup 4 index exch sub getinterval exch 6 2 roll
547 } def
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549 %% Func derivative -> Eval<func>
550 /EvalFunc_sin {%
551 PreCommonFunc
552 { (cos\() StrConcat 5 index StrConcat AddClPar } if
553 PostCommonFunc } def
554 /EvalFunc_cos {%
555 PreCommonFunc
556 { (\(-sin\() StrConcat 5 index StrConcat (\)\)) StrConcat } if
557 PostCommonFunc } def
558 /EvalFunc_tan {%
559 PreCommonFunc
560 { dup 0 eq { (1) StrConcat } { 1 sub } ifelse (/cos\() StrConcat 5 index StrConcat (\)^2) StrConcat } if
561 PostCommonFunc } def
562 /EvalFunc_asin {%
563 PreCommonFunc
564 { (1/sqrt\(1-\() StrConcat 5 index StrConcat (\)^2\)\)) StrConcat } if
565 PostCommonFunc } def
566 /EvalFunc_acos {%
567 PreCommonFunc
568 { (-1/sqrt\(1-\() StrConcat 5 index StrConcat (\)^2\)\)) StrConcat } if
569 PostCommonFunc } def
570 /EvalFunc_atg {%
571 PreCommonFunc
572 { (1/\(1+\() StrConcat 5 index StrConcat (\)^2\)\)) StrConcat } if
573 PostCommonFunc } def
574 /EvalFunc_floor {%
575 PreCommonFunc
576 { dup 0 eq { (1) StrConcat } { 1 sub } ifelse (/\() StrConcat 5 index StrConcat AddClPar } if
577 PostCommonFunc } def
578 /EvalFunc_ln {%
579 PreCommonFunc
580 { dup 0 eq { (1) StrConcat } { 1 sub } ifelse (/\() StrConcat 5 index StrConcat AddClPar } if
581 PostCommonFunc } def
582 /EvalFunc_exp {%
583 PreCommonFunc
584 { (exp\() StrConcat 5 index StrConcat AddClPar } if
585 PostCommonFunc } def
586 /EvalFunc_EXP {%
587 PreCommonFunc
588 { (EXP\() StrConcat 5 index StrConcat AddClPar } if
589 PostCommonFunc } def
590 /EvalFunc_sqrt {%
591 PreCommonFunc
592 { dup 0 eq { (1) StrConcat } { 1 sub } ifelse (/\(2*sqrt\() StrConcat 5 index StrConcat (\)\)) StrConcat } if
593 PostCommonFunc } def
594 /EvalFunc_Fact {%
595 PreCommonFunc { DERIVATIVE_ENGINE_ERROR_no_variable_expression_in_Fact } if
596 PostCommonFunc } def
597 /EvalFunc_sh {%
598 PreCommonFunc
599 { (ch\() StrConcat 5 index StrConcat AddClPar } if
600 PostCommonFunc } def
601 /EvalFunc_ch {%
602 PreCommonFunc
603 { (sh\() StrConcat 5 index StrConcat AddClPar } if
604 PostCommonFunc } def
605 /EvalFunc_th {%
606 PreCommonFunc
607 { dup 0 eq { (1) StrConcat } { 1 sub } ifelse (/ch\() StrConcat 5 index StrConcat (\)^2) StrConcat } if
608 PostCommonFunc } def
609 /EvalFunc_Argsh {%
610 PreCommonFunc
611 { (1/sqrt\(1+\() StrConcat 5 index StrConcat (\)^2\)\)) StrConcat } if
612 PostCommonFunc } def
613 /EvalFunc_Argch {%
614 PreCommonFunc
615 { (1/sqrt\(\() StrConcat 5 index StrConcat (\)^2-1\)\)) StrConcat } if
616 PostCommonFunc } def
617 /EvalFunc_Argth {%
618 PreCommonFunc
619 { (1/\(1-\() StrConcat 5 index StrConcat (\)^2\)\)) StrConcat } if
620 PostCommonFunc } def
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622 /PreCommonFunc {
623 1 add NextNonBlankChar pop 3 -1 roll 5 1 roll AnalyzeExpr 1 add NextNonBlankChar pop
624 4 2 roll 4 index (0) eq
625 { (0) StrConcat false }
626 { 4 index (1) ne { AddOpPar 4 index StrConcat (\)*) StrConcat } if true } ifelse
627 } def
628 /PostCommonFunc {
629 2 copy pop 0 6 2 roll GetIntervalNewStr
630 mark 9 -3 roll cleartomark 2 index 6 index dup 4 index exch sub getinterval exch 6 2 roll
631 } def
632 /EvalFunc_Derive {%
633 1 add ReadNumber cvi 1 add dup cvr log 1 add cvi string cvs
634 4 -1 roll pop 5 1 roll 1 add NextNonBlankChar pop AnalyzeExpr 1 add
635 4 -2 roll (Derive\() StrConcat 7 -1 roll StrConcat (,) StrConcat 6 -1 roll StrConcat AddClPar
636 2 copy pop 0 6 2 roll GetIntervalNewStr 6 -1 roll pop 2 index 6 index dup 4 index exch sub getinterval
637 exch 6 2 roll } def
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 %% literal switch -> func call, vector, variables
640 /EvalFunc_Sum {%
641 1 add NextNonBlankChar pop
642 %% read the variable name
643 ReadLiteral pop 3 -1 roll pop NextNonBlankChar
644 44 ne { ANALYZER_ERROR_missing_first_comma_in_Sum } if
645 %% read the initial value
646 1 add NextNonBlankChar pop ReadNumber pop
647 2 copy get 44 ne { ANALYZER_ERROR_missing_second_comma_in_Sum } if
648 %% read the increment value
649 1 add NextNonBlankChar pop ReadNumber pop
650 2 copy get 44 ne { ANALYZER_ERROR_missing_third_comma_in_Sum } if
651 %% read the limit value
652 1 add NextNonBlankChar pop ReadNumber pop
653 2 copy get 44 ne { ANALYZER_ERROR_missing_fourth_comma_in_Sum } if
654 1 add NextNonBlankChar pop dup 6 1 roll 3 -1 roll pop AnalyzeExpr 1 add NextNonBlankChar pop
655 4 -2 roll 3 index 8 index dup 9 index exch sub getinterval StrConcat
656 4 index StrConcat AddClPar
657 2 copy pop 0 6 2 roll GetIntervalNewStr
658 mark 9 -3 roll cleartomark 2 index 6 index dup 4 index exch sub getinterval exch 6 2 roll
659 } def
660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
661 %% literal switch -> func call, vector, variables
662 /EvalFunc_IfTE {%
663 3 -1 roll pop 1 add NextNonBlankChar pop SkipCond
664 NextNonBlankChar
665 44 ne { ANALYZER_ERROR_missing_first_comma_in_IfTE } if
666 1 add NextNonBlankChar pop dup 5 1 roll
667 AnalyzeExpr NextNonBlankChar
668 44 ne { ANALYZER_ERROR_missing_second_comma_in_IfTE } if
669 1 add NextNonBlankChar pop
670 AnalyzeExpr 1 add NextNonBlankChar pop
671 4 -2 roll 3 index 10 index dup 11 index exch sub getinterval StrConcat
672 6 index StrConcat (,) StrConcat 4 index StrConcat AddClPar
673 2 copy pop 0 6 2 roll GetIntervalNewStr
674 mark 11 -5 roll cleartomark 2 index 6 index dup 4 index exch sub getinterval exch 6 2 roll
675 } def
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 %% advance in str until a comma is found (no error detection!)
678 %% str index -> str index'
679 /SkipCond { { 1 add 2 copy get 44 eq {exit } if } loop } bind def
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 %% Convert to radians if trigo function call
682 %% (name) ->
683 /TrigoFunc {
684 dup (cos) eq 1 index (sin) eq or exch (tan) eq or
685 { /ExpressionVector ExpressionVector aload length Pi /div cvx 180 /mul cvx 5 -1 roll 4 add
686 array astore def
687 } if
688 } def
689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690 %% No derivative for condition....
691 /EvalCondOp { 3 -1 roll pop } bind def
692 /PutIntervalOneAdd {putinterval 1 add} bind def
693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
694 %% Add open parenthesis in string at the given index
695 %% str index -> str index+1
696 /AddOpPar {2 copy (\() PutIntervalOneAdd} bind def
697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 %% Add close parenthesis in string at the given index
699 %% str index -> str index+1
700 /AddClPar {2 copy (\)) PutIntervalOneAdd} bind def
701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702 %% Add 0 in string at the given index
703 %% str index -> str index+1
704 /AddZero {2 copy (0) PutIntervalOneAdd} bind def
705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 %% Add open parenthesis in string at the given index
707 %% str index -> str index+1
708 /AddMul {2 copy (*) PutIntervalOneAdd} bind def
709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710 %% Add open parenthesis in string at the given index
711 %% str index -> str index+1
712 /AddDiv {2 copy (/) PutIntervalOneAdd} bind def
713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
714 %% Add a plus sign in string at the given index
715 %% str index -> str index+1
716 /AddAdd {2 copy (+) PutIntervalOneAdd} bind def
717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
718 %% Add a minus sign in string at the given index
719 %% str index -> str index+1
720 /AddSub {2 copy (-) PutIntervalOneAdd} bind def
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722 %% Add a pipe sign in string at the given index
723 %% str index -> str index+1
724 /AddPipe {2 copy (|) PutIntervalOneAdd} bind def
725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 %% H O O K S
727 /AnalyzePreHook { dup 5 1 roll } bind def
728 /PreEvalHook {} def
729 /AnalyzePostHook { 7 -1 roll pop } bind def
730 /AnalyzeListOfEPostHook { 6 -1 roll mark 6 1 roll cleartomark } bind def
731 /RollOp { 5 1 roll } bind def
732 end%%%tx@CoreAnalyzerDict
733 /tx@AddMathFunc 12 dict def tx@AddMathFunc begin
734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735 %% NEW FUNC
736 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
737 %% function arcsine in radians asin(x)=atan(x/sqrt(1-x^2))
738 %% x -> theta
739 /asin {%
740 dup abs 1 gt { EQDFasinrangeerror } if
741 dup dup dup mul 1 exch sub sqrt atan exch 0 lt { 360 sub } if DegtoRad
742 } def
743 %% function arccosine in radians acos(x)=atan(sqrt(1-x^2)/x)
744 %% x -> theta
745 /acos {%
746 dup abs 1 gt { EQDFacosrangeerror } if
747 dup dup mul 1 exch sub sqrt exch atan DegtoRad
748 } def
749 %% function arctangent in radians
750 %% x -> theta
751 /atg { 1 atan dup 90 gt { 360 sub } if DegtoRad } bind def
752 %% HYPERBOLIC FUNCTIONS
753 /sh { dup Ex exch neg Ex sub 2 div } def
754 /ch { dup Ex exch neg Ex add 2 div } def
755 /th { dup sh exch ch div } def
756 /Argsh { dup dup mul 1 add sqrt add ln } def
757 /Argch { dup dup mul 1 sub sqrt add ln } def
758 /Argth { dup 1 add exch 1 exch sub div ln 2 div } def
759 %% modified exponential funtion for 0
760 %% x n -> x^n
761 /Exp { dup 0 eq { pop pop 1 } { exp } ifelse } bind def
762 %% modified exponential funtion for 0
763 %% x -> e^x
764 /Ex { Euler exch exp } bind def
765 %%
766 %% factorial function
767 %% n -> n!
768 /Fact { 1 exch 2 exch 1 exch { mul } for } bind def
769 /fact { Fact } bind def
770 /! { Fact } bind def
771 end
772 %
773 % END pst-algparser.pro