| 1 |
% $Id: pst-tools.pro 249 2021-09-14 10:22:55Z herbert $ |
| 2 |
% |
| 3 |
%% PostScript tools prologue for pstricks.tex. |
| 4 |
%% Version 0.06, 2017/12/03 |
| 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 |
/Pi2 1.57079632679489661925640 def |
| 12 |
/factorial { % n on stack, returns n! |
| 13 |
dup 0 eq { 1 }{ |
| 14 |
dup 1 gt { dup 1 sub factorial mul } if } |
| 15 |
ifelse } def |
| 16 |
% |
| 17 |
/MoverN { % m n on stack, returns the binomial coefficient m over n |
| 18 |
2 dict begin |
| 19 |
/n exch def /m exch def |
| 20 |
n 0 eq { 1 }{ |
| 21 |
m n eq { 1 }{ |
| 22 |
m factorial n factorial m n sub factorial mul div } ifelse } ifelse |
| 23 |
end |
| 24 |
} def |
| 25 |
% |
| 26 |
/ps@ReverseOrderOfPoints { % on stack [P1 P2 P3 ...Pn]=>[Pn,Pn-1,...,P2,P1] |
| 27 |
5 dict begin % all local |
| 28 |
aload length /n ED % number of coors |
| 29 |
n 2 div cvi /m ED % number of Points |
| 30 |
/n1 n def |
| 31 |
m { n1 2 roll /n1 n1 2 sub def } repeat |
| 32 |
n array astore |
| 33 |
end |
| 34 |
} def |
| 35 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 36 |
% subroutines for complex numbers, given as an array [a b] |
| 37 |
% which is a+bi = Real+i Imag |
| 38 |
% |
| 39 |
/cxadd { % [a1 b1] [a2 b2] = [a1+a2 b1+b2] |
| 40 |
dup 0 get % [a1 b1] [a2 b2] a2 |
| 41 |
3 -1 roll % [a2 b2] a2 [a1 b1] |
| 42 |
dup 0 get % [a2 b2] a2 [a1 b1] a1 |
| 43 |
3 -1 roll % [a2 b2] [a1 b1] a1 a2 |
| 44 |
add % [a2 b2] [a1 b1] a1+a2 |
| 45 |
3 1 roll % a1+a2 [a2 b2] [a1 b1] |
| 46 |
1 get % a1+a2 [a2 b2] b1 |
| 47 |
exch 1 get % a1+a2 b1 b2 |
| 48 |
add 2 array astore |
| 49 |
} def |
| 50 |
% |
| 51 |
/cxneg { % [a b] |
| 52 |
dup 1 get % [a b] b |
| 53 |
exch 0 get % b a |
| 54 |
neg exch neg % -a -b |
| 55 |
2 array astore |
| 56 |
} def |
| 57 |
% |
| 58 |
/cxsub { cxneg cxadd } def % same as negative addition |
| 59 |
% |
| 60 |
% [a1 b1][a2 b2] = [a1a2-b1b2 a1b2+b1a2] = [a3 b3] |
| 61 |
/cxmul { % [a1 b1] [a2 b2] |
| 62 |
dup 0 get % [a1 b1] [a2 b2] a2 |
| 63 |
exch 1 get % [a1 b1] a2 b2 |
| 64 |
3 -1 roll % a2 b2 [a1 b1] |
| 65 |
dup 0 get % a2 b2 [a1 b1] a1 |
| 66 |
exch 1 get % a2 b2 a1 b1 |
| 67 |
dup % a2 b2 a1 b1 b1 |
| 68 |
5 -1 roll dup % b2 a1 b1 b1 a2 a2 |
| 69 |
3 1 roll mul % b2 a1 b1 a2 b1a2 |
| 70 |
5 -2 roll dup % b1 a2 b1a2 b2 a1 a1 |
| 71 |
3 -1 roll dup % b1 a2 b1a2 a1 a1 b2 b2 |
| 72 |
3 1 roll mul % b1 a2 b1a2 a1 b2 a1b2 |
| 73 |
4 -1 roll add % b1 a2 a1 b2 b3 |
| 74 |
4 2 roll mul % b1 b2 b3 a1a2 |
| 75 |
4 2 roll mul sub % b3 a3 |
| 76 |
exch 2 array astore |
| 77 |
} def |
| 78 |
% |
| 79 |
% [a b]^2 = [a^2-b^2 2ab] = [a2 b2] |
| 80 |
/cxsqr { % [a b] square root |
| 81 |
dup 0 get exch 1 get % a b |
| 82 |
dup dup mul % a b b^2 |
| 83 |
3 -1 roll % b b^2 a |
| 84 |
dup dup mul % b b^2 a a^2 |
| 85 |
3 -1 roll sub % b a a2 |
| 86 |
3 1 roll mul 2 mul % a2 b2 |
| 87 |
2 array astore |
| 88 |
} def |
| 89 |
% |
| 90 |
/cxsqrt { % [a b] |
| 91 |
% dup cxnorm sqrt /r exch def |
| 92 |
% cxarg 2 div RadtoDeg dup cos r mul exch sin r mul cxmake2 |
| 93 |
cxlog % log[a b] |
| 94 |
2 cxrdiv % log[a b]/2 |
| 95 |
aload pop exch % b a |
| 96 |
2.781 exch exp % b exp(a) |
| 97 |
exch cxconv exch % [Re +iIm] exp(a) |
| 98 |
cxrmul % |
| 99 |
} def |
| 100 |
% |
| 101 |
/cxarg { % [a b] |
| 102 |
aload pop % a b |
| 103 |
exch atan % arctan b/a |
| 104 |
DegtoRad % arg(z)=atan(b/a) |
| 105 |
} def |
| 106 |
% |
| 107 |
% log[a b] = [a^2-b^2 2ab] = [a2 b2] |
| 108 |
/cxlog { % [a b] |
| 109 |
dup % [a b][a b] |
| 110 |
cxnorm % [a b] |z| |
| 111 |
log % [a b] log|z| |
| 112 |
exch % log|z|[a b] |
| 113 |
cxarg % log|z| Theta |
| 114 |
cxmake2 % [log|z| Theta] |
| 115 |
} def |
| 116 |
% |
| 117 |
% square of magnitude of complex number |
| 118 |
/cxnorm2 { % [a b] |
| 119 |
dup 0 get exch 1 get % a b |
| 120 |
dup mul % a b^2 |
| 121 |
exch dup mul add % a^2+b^2 |
| 122 |
} def |
| 123 |
% |
| 124 |
/cxnorm { % [a b] |
| 125 |
cxnorm2 sqrt |
| 126 |
} def |
| 127 |
% |
| 128 |
/cxconj { % conjugent complex |
| 129 |
dup 0 get exch 1 get % a b |
| 130 |
neg 2 array astore % [a -b] |
| 131 |
} def |
| 132 |
% |
| 133 |
/cxre { 0 get } def % real value |
| 134 |
/cxim { 1 get } def % imag value |
| 135 |
% |
| 136 |
% 1/[a b] = ([a -b]/(a^2+b^2) |
| 137 |
/cxrecip { % [a b] |
| 138 |
dup cxnorm2 exch % n2 [a b] |
| 139 |
dup 0 get exch 1 get % n2 a b |
| 140 |
3 -1 roll % a b n2 |
| 141 |
dup % a b n2 n2 |
| 142 |
4 -1 roll exch div % b n2 a/n2 |
| 143 |
3 1 roll div % a/n2 b/n2 |
| 144 |
neg 2 array astore |
| 145 |
} def |
| 146 |
% |
| 147 |
/cxmake1 { 0 2 array astore } def % make a complex number, real given |
| 148 |
/cxmake2 { 2 array astore } def % dito, both given |
| 149 |
% |
| 150 |
/cxdiv { cxrecip cxmul } def |
| 151 |
% |
| 152 |
% multiplikation by a real number |
| 153 |
/cxrmul { % [a b] r |
| 154 |
exch aload pop % r a b |
| 155 |
3 -1 roll dup % a b r r |
| 156 |
3 1 roll mul % a r b*r |
| 157 |
3 1 roll mul % b*r a*r |
| 158 |
exch 2 array astore % [a*r b*r] |
| 159 |
} def |
| 160 |
% |
| 161 |
% division by a real number |
| 162 |
/cxrdiv { % [a b] r |
| 163 |
1 exch div % [a b] 1/r |
| 164 |
cxrmul |
| 165 |
} def |
| 166 |
% |
| 167 |
% exp(i theta) = cos(theta)+i sin(theta) polar<->cartesian |
| 168 |
/cxconv { % theta |
| 169 |
RadtoDeg dup sin exch cos cxmake2 |
| 170 |
} def |
| 171 |
|
| 172 |
%%%%% ### bubblesort ### |
| 173 |
%% syntax : array bubblesort --> array2 trie par ordre croissant |
| 174 |
%% code de Bill Casselman |
| 175 |
%% http://www.math.ubc.ca/people/faculty/cass/graphics/text/www/ |
| 176 |
/bubblesort { % on stack must be an array [ ... ] |
| 177 |
4 dict begin |
| 178 |
/a exch def |
| 179 |
/n a length 1 sub def |
| 180 |
n 0 gt { |
| 181 |
% at this point only the n+1 items in the bottom of a remain to |
| 182 |
% the sorted largest item in that blocks is to be moved up into |
| 183 |
% position n |
| 184 |
n { |
| 185 |
0 1 n 1 sub { |
| 186 |
/i exch def |
| 187 |
a i get a i 1 add get gt { |
| 188 |
% if a[i] > a[i+1] swap a[i] and a[i+1] |
| 189 |
a i 1 add |
| 190 |
a i get |
| 191 |
a i a i 1 add get |
| 192 |
% set new a[i] = old a[i+1] |
| 193 |
put |
| 194 |
% set new a[i+1] = old a[i] |
| 195 |
put |
| 196 |
} if |
| 197 |
} for |
| 198 |
/n n 1 sub def |
| 199 |
} repeat |
| 200 |
} if |
| 201 |
a % return the sorted array |
| 202 |
end |
| 203 |
} def |
| 204 |
% |
| 205 |
/concatstringarray{ % [(a) (b) ... (z)] --> (ab...z) 20100422 |
| 206 |
0 1 index { length add } forall |
| 207 |
string |
| 208 |
0 3 2 roll |
| 209 |
{ 3 copy putinterval length add }forall |
| 210 |
pop |
| 211 |
} bind def |
| 212 |
% |
| 213 |
/concatstrings{ % (a) (b) -> (ab) |
| 214 |
exch dup length |
| 215 |
2 index length add string |
| 216 |
dup dup 4 2 roll copy length |
| 217 |
4 -1 roll putinterval |
| 218 |
} def |
| 219 |
% |
| 220 |
/reversestring { % (aBC) -> (CBa) |
| 221 |
5 dict begin |
| 222 |
/str exch def |
| 223 |
/L str length def |
| 224 |
/strTemp L string def |
| 225 |
/i 0 def |
| 226 |
L { |
| 227 |
/I L 1 sub i sub def |
| 228 |
strTemp i str I 1 getinterval putinterval |
| 229 |
/i i 1 add def |
| 230 |
} repeat |
| 231 |
strTemp |
| 232 |
end |
| 233 |
} def |
| 234 |
% |
| 235 |
/concatarray{ % [a c] [b d] -> [a c b d] |
| 236 |
2 dict begin |
| 237 |
/a2 exch def |
| 238 |
/a1 exch def |
| 239 |
[ a1 aload pop a2 aload pop ] |
| 240 |
end |
| 241 |
} def |
| 242 |
% |
| 243 |
/dot2comma {% on stack a string (...) |
| 244 |
2 dict begin |
| 245 |
/Output exch def |
| 246 |
0 1 Output length 1 sub { |
| 247 |
/Index exch def |
| 248 |
Output Index get 46 eq { Output Index 44 put } if |
| 249 |
} for |
| 250 |
Output |
| 251 |
end |
| 252 |
} def |
| 253 |
% |
| 254 |
/rightTrim { % on stack the string and the character number to be stripped |
| 255 |
1 dict begin |
| 256 |
/charNo exch def |
| 257 |
dup |
| 258 |
length 1 sub -1 0 { |
| 259 |
/i exch def dup i get charNo ne { exit } if |
| 260 |
} for |
| 261 |
0 i 1 add getinterval |
| 262 |
dup length string copy |
| 263 |
end |
| 264 |
} bind def % leaves the stripped string on the stack |
| 265 |
|
| 266 |
/psStringwidth /stringwidth load def |
| 267 |
/psShow /show load def |
| 268 |
|
| 269 |
%/stringwidth{ 32 rightTrim psStringwidth } bind def |
| 270 |
|
| 271 |
%/show { 32 rightTrim psShow } bind def |
| 272 |
%-----------------------------------------------------------------------------% |
| 273 |
|
| 274 |
/pgffunctions { |
| 275 |
/pgfsc{}bind def% stroke color is empty by default |
| 276 |
/pgffc{}bind def% fill color is empty by default |
| 277 |
/pgfstr{stroke}bind def% |
| 278 |
/pgffill{fill}bind def% |
| 279 |
/pgfeofill{eofill}bind def% |
| 280 |
/pgfe{a dup 0 rlineto exch 0 exch rlineto neg 0 rlineto closepath}bind def% rectangle |
| 281 |
/pgfw{setlinewidth}bind def% setlinewidth |
| 282 |
/pgfs{save pgfpd 72 Resolution div 72 VResolution div neg scale |
| 283 |
magscale{1 DVImag div dup scale}if |
| 284 |
pgfx neg pgfy neg translate pgffoa .setopacityalpha}bind def% save |
| 285 |
/pgfr{pgfsd restore}bind def %restore |
| 286 |
userdict begin% |
| 287 |
/pgfo{pgfsd /pgfx currentpoint /pgfy exch def def @beginspecial}bind def %open |
| 288 |
/pgfc{newpath @endspecial pgfpd}bind def %close |
| 289 |
/pgfsd{globaldict /pgfdelta /delta where {pop delta} {0} ifelse put}bind def% save delta |
| 290 |
/pgfpd{/delta globaldict /pgfdelta get def}bind def % put delta |
| 291 |
/.setopacityalpha where {pop} {/.setopacityalpha{pop}def} ifelse % install .setopacityalpha |
| 292 |
/.pgfsetfillopacityalpha{/pgffoa exch def |
| 293 |
/pgffill{gsave pgffoa .setopacityalpha fill 1 .setopacityalpha newpath fill grestore newpath}bind def |
| 294 |
/pgfeofill{gsave pgffoa .setopacityalpha eofill 1 .setopacityalpha newpath eofill grestore newpath}bind def}bind def |
| 295 |
/.pgfsetstrokeopacityalpha{/pgfsoa exch def /pgfstr{gsave pgfsoa .setopacityalpha stroke grestore newpath}bind def}bind def |
| 296 |
/pgffoa 1 def |
| 297 |
/pgfsoa 1 def |
| 298 |
end |
| 299 |
} def |
| 300 |
%-----------------------------------------------------------------------------% |
| 301 |
% END pst-tools.pro |