| 1 |
%% $Id: pst-func.pro 694 2023-04-05 19:17:32Z herbert $ |
| 2 |
%% |
| 3 |
%% This is file `pst-func.pro', |
| 4 |
%% |
| 5 |
%% IMPORTANT NOTICE: |
| 6 |
%% |
| 7 |
%% Package `pst-func' |
| 8 |
%% |
| 9 |
%% Herbert Voss <[email protected]> |
| 10 |
%% |
| 11 |
%% This program can be redistributed and/or modified under the terms |
| 12 |
%% of the LaTeX Project Public License Distributed from CTAN archives |
| 13 |
%% in directory macros/latex/base/lppl.txt. |
| 14 |
%% |
| 15 |
%% DESCRIPTION: |
| 16 |
%% `pst-func' is a PSTricks package to plot special math functions |
| 17 |
%% |
| 18 |
%% |
| 19 |
%% version 0.20 / 2023-04-04 Herbert Voss |
| 20 |
% |
| 21 |
/tx@FuncDict 100 dict def |
| 22 |
tx@FuncDict begin |
| 23 |
% |
| 24 |
/eps4 1.0e-04 def |
| 25 |
/eps5 1.0e-05 def |
| 26 |
/eps8 1.0e-08 def |
| 27 |
% |
| 28 |
/PiHalf 1.57079632679489661925640 def |
| 29 |
/CEuler 0.5772156649 def % Euler-Mascheroni constant |
| 30 |
% |
| 31 |
/factorialR { |
| 32 |
dup 1e32 gt { pop 1e32 } { |
| 33 |
dup 0 eq % check for the argument being 0 |
| 34 |
{ pop 1 } % if so, the result is 1 |
| 35 |
{ dup 1 sub factorial % call recursively with n - 1 |
| 36 |
mul % multiply the result with n |
| 37 |
} ifelse |
| 38 |
} ifelse |
| 39 |
} def |
| 40 |
% |
| 41 |
%Iterative |
| 42 |
/factorial { |
| 43 |
1 % initial value for the product |
| 44 |
1 1 % for's start value and increment |
| 45 |
4 -1 roll % bring the argument to the top as for's end value |
| 46 |
{ mul } for |
| 47 |
} def |
| 48 |
% |
| 49 |
% |
| 50 |
/MoverN { % m n on stack, returns the binomial coefficient m over n |
| 51 |
2 dict begin |
| 52 |
/n exch def /m exch def |
| 53 |
n 0 eq { 1 }{ |
| 54 |
m n eq { 1 }{ |
| 55 |
m factorial n factorial m n sub factorial mul div } ifelse } ifelse |
| 56 |
end |
| 57 |
} def |
| 58 |
% |
| 59 |
/Pascal [ |
| 60 |
[ 1 ] % 0 |
| 61 |
[ 1 1 ] % 1 |
| 62 |
[ 1 2 1 ] % 2 |
| 63 |
[ 1 3 3 1 ] % 3 |
| 64 |
[ 1 4 6 4 1 ] % 4 |
| 65 |
[ 1 5 10 10 5 1 ] % 5 |
| 66 |
[ 1 6 15 20 15 6 1 ] % 6 |
| 67 |
[ 1 7 21 35 35 21 7 1 ] % 7 |
| 68 |
[ 1 8 28 56 70 56 28 8 1 ] % 8 |
| 69 |
[ 1 9 36 84 126 126 84 36 9 1 ] % 9 |
| 70 |
] def |
| 71 |
% |
| 72 |
/GetBezierCoor { % t on stack |
| 73 |
10 dict begin % hold all local |
| 74 |
/t ED |
| 75 |
/t1 1 t sub def % t1=1-t |
| 76 |
/Coeff Pascal BezierType get def % get the coefficients |
| 77 |
0 0 % initial values for x y |
| 78 |
BezierType -1 0 { % BezierType,...,2,1,0 |
| 79 |
/I ED % I=BezierType,...,2,1,0 |
| 80 |
/J BezierType I sub def % J=0,1,2,...,BezierType |
| 81 |
/T t I exp Coeff J get mul def % coeff(J)*t^I |
| 82 |
/T1 t1 J exp def % t1^J |
| 83 |
Points I dup add 1 add get % y(2*I+1) |
| 84 |
T mul T1 mul add % the y coordinate |
| 85 |
exch % y x |
| 86 |
Points I dup add get % x(2*I) |
| 87 |
T mul T1 mul add % the x coordinate |
| 88 |
exch % x y |
| 89 |
} for % x y on stack |
| 90 |
end |
| 91 |
} def |
| 92 |
|
| 93 |
/BezierCurve { % on stack [ coors psk@plotpoints BezierType |
| 94 |
% 10 dict begin |
| 95 |
/BezierType ED % 2,3,4,5,6,... |
| 96 |
1 exch div /epsilon ED % step for Bezier =1/plotpoints |
| 97 |
] % [ yi xi ... y3 x3 y2 x2 y1 x1 y0 x0] |
| 98 |
ps@ReverseOrderOfPoints % [y0 x0 y1 x1 ... yi xi] |
| 99 |
/Points ED % save Points array |
| 100 |
epsilon GetBezierCoor % next Bezier point |
| 101 |
Points 0 get Points 1 get % starting point |
| 102 |
ArrowA lineto |
| 103 |
epsilon epsilon 1 epsilon sub { % on stack is the loop variable |
| 104 |
GetBezierCoor lineto |
| 105 |
} for |
| 106 |
1 epsilon sub GetBezierCoor |
| 107 |
1 GetBezierCoor |
| 108 |
ArrowB lineto moveto |
| 109 |
% end |
| 110 |
} def |
| 111 |
|
| 112 |
/Bernstein { % on stack tStart tEnd plotpoints i n |
| 113 |
12 dict begin % hold all local |
| 114 |
/envelope ED % plot envelope? |
| 115 |
/n ED |
| 116 |
/i ED |
| 117 |
/ni n i sub def |
| 118 |
/epsilon ED % step=1/plotpoints |
| 119 |
/tEnd ED |
| 120 |
/tStart ED |
| 121 |
% |
| 122 |
% B_{i,n}(t)=\binom{n}{i}t^i(1-t)^{n-i} (Bernstein) |
| 123 |
% f_n(x)=\frac{1}{\sqrt{\pi n\cdot x(1-x)}} (envelope) |
| 124 |
% |
| 125 |
n i MoverN /noveri ED % \binom{n}{i} |
| 126 |
[ % for the array of points |
| 127 |
tStart epsilon tEnd { |
| 128 |
dup dup /t ED % leave one on stack |
| 129 |
neg 1 add /t1 ED % t1=1-t |
| 130 |
envelope |
| 131 |
{ t t1 mul 4 mul PiHalf mul n mul sqrt 1 exch Div } % envelope |
| 132 |
{ noveri t i exp mul t1 ni exp mul } ifelse % t f(t) |
| 133 |
ScreenCoor % convert to screen coor |
| 134 |
} for |
| 135 |
end |
| 136 |
false /Lineto /lineto load def Line |
| 137 |
} def |
| 138 |
%% |
| 139 |
/Si { % integral sin from 0 to x (arg on stack) |
| 140 |
10 dict begin % hold all local |
| 141 |
dup 0 eq |
| 142 |
{ pop 0 } |
| 143 |
{ |
| 144 |
/arg exch def % x |
| 145 |
/arg2 arg dup mul def |
| 146 |
/Sum arg def % |
| 147 |
/sign -1 def |
| 148 |
/I 3 def |
| 149 |
/Frac arg2 arg mul 6 div def |
| 150 |
{ % a sequence of x - x^3/(3*3!) + x^5/(5*5!) -...+... |
| 151 |
Frac I div sign mul |
| 152 |
dup abs eps5 lt { pop exit } if |
| 153 |
Sum add /Sum exch def |
| 154 |
/sign sign neg def |
| 155 |
/I I 2 add def |
| 156 |
Frac arg2 mul I 1 sub I mul div /Frac ED |
| 157 |
% arg I Power dup abs 1e30 gt { pop exit } if |
| 158 |
% I factorial div I div sign mul |
| 159 |
% dup abs eps8 lt { pop exit } if |
| 160 |
% Sum add /Sum exch def |
| 161 |
% /sign sign neg def |
| 162 |
% /I I 2 add def |
| 163 |
} loop |
| 164 |
Sum |
| 165 |
} ifelse |
| 166 |
end |
| 167 |
} def |
| 168 |
% |
| 169 |
/si { % integral sin from x to infty -> si(x)=Si(x)-pi/2 |
| 170 |
Si PiHalf sub |
| 171 |
} def |
| 172 |
% |
| 173 |
/Ci { % integral cosin from x to infty (arg on stack) |
| 174 |
10 dict begin % hold all local |
| 175 |
abs /arg exch def |
| 176 |
arg 0 eq { 0 } { |
| 177 |
/arg2 arg dup mul def |
| 178 |
/Sum CEuler arg ln add def |
| 179 |
/sign -1 def |
| 180 |
/I 2 def |
| 181 |
/Frac arg2 2 div def % first fraction |
| 182 |
{ Frac I div sign mul |
| 183 |
dup abs eps5 lt { pop exit } if |
| 184 |
Sum add /Sum exch def |
| 185 |
/sign sign neg def |
| 186 |
/I I 2 add def |
| 187 |
Frac arg2 mul I 1 sub I mul div /Frac ED |
| 188 |
} loop |
| 189 |
Sum |
| 190 |
} ifelse |
| 191 |
end |
| 192 |
} def |
| 193 |
% |
| 194 |
/ci { % integral cosin from x to infty -> ci(x)=-Ci(x)+ln(x)+CEuler |
| 195 |
dup Ci neg exch abs ln add CEuler add |
| 196 |
} def |
| 197 |
% |
| 198 |
/MaxIter 255 def |
| 199 |
/func { coeff Derivation FuncValue } def |
| 200 |
/func' { coeff Derivation 1 add FuncValue } def |
| 201 |
/func'' { coeff Derivation 2 add FuncValue } def |
| 202 |
% |
| 203 |
/NewtonMehrfach {% the start value must be on top of the stack |
| 204 |
/Nx exch def |
| 205 |
/Iter 0 def |
| 206 |
{ |
| 207 |
/Iter Iter 1 add def |
| 208 |
Nx func /F exch def % f(Nx) |
| 209 |
F abs eps4 lt { exit } if |
| 210 |
Nx func' /FS exch def % f'(Nx) |
| 211 |
FS 0 eq { /FS 1.0e-06 def } if |
| 212 |
Nx func'' /F2S exch def % f''(Nx) |
| 213 |
1.0 1.0 F F2S mul FS dup mul div sub div /J exch def |
| 214 |
J F mul FS div /Diff exch def |
| 215 |
/Nx Nx Diff sub def |
| 216 |
Diff abs eps5 lt Iter MaxIter gt or { exit } if |
| 217 |
} loop |
| 218 |
Nx % the returned value ist the zero point |
| 219 |
} def |
| 220 |
|
| 221 |
/Steffensen {% the start value must be on top of the stack |
| 222 |
/y0 exch def % the start value |
| 223 |
/Iter 0 def /MaxIter 200 def |
| 224 |
{ pstack |
| 225 |
y0 func /F exch def |
| 226 |
F abs eps4 lt { exit } if |
| 227 |
y0 F sub /Phi exch def |
| 228 |
Phi func /F2 exch def |
| 229 |
F2 abs eps4 le { exit }{ |
| 230 |
Phi y0 sub dup mul Phi F2 sub 2 Phi mul sub y0 add Div /Diff exch def |
| 231 |
y0 Diff sub /y0 exch def |
| 232 |
Diff abs eps5 le { exit } if |
| 233 |
} ifelse |
| 234 |
/Iter Iter 1 add def |
| 235 |
Iter MaxIter gt { exit } if |
| 236 |
} loop |
| 237 |
y0 28 mul % the returned value ist the zero point |
| 238 |
0 |
| 239 |
3 0 360 arc gsave 0 0 1 setrgbcolor fill grestore 1 setlinewidth stroke |
| 240 |
} def |
| 241 |
% |
| 242 |
/Horner {% x [coeff] must be on top of the stack |
| 243 |
aload length |
| 244 |
dup 2 add -1 roll |
| 245 |
exch 1 sub { |
| 246 |
dup 4 1 roll |
| 247 |
mul add exch |
| 248 |
} repeat |
| 249 |
pop % the y value is on top of the stack |
| 250 |
} def |
| 251 |
% |
| 252 |
/FuncValue {% x [coeff] Derivation must be on top of the stack |
| 253 |
{ |
| 254 |
aload % a0 a1 a2 ... a(n-1) [array] |
| 255 |
length % a0 a1 a2 ... a(n-1) n |
| 256 |
1 sub /grad exch def % a0 a1 a2 ... a(n-1) |
| 257 |
grad -1 1 { % for n=grad step -1 until 1 |
| 258 |
/n exch def % Laufvariable speichern |
| 259 |
n % a0 a1 a2 ... a(n-1) n |
| 260 |
mul % a0 a1 a2 ... a(n-1)*n |
| 261 |
grad 1 add % a0 a1 a2 ... a(n-1)*n grad+1 |
| 262 |
1 roll % an*na0 a1 a2 ... a(n-2) |
| 263 |
} for |
| 264 |
pop % loesche a0 |
| 265 |
grad array astore % [ a1 a2 ... a(n-2)] |
| 266 |
} repeat |
| 267 |
Horner |
| 268 |
} def |
| 269 |
% |
| 270 |
/FindZeros { % dxN dxZ must be on top of the stack (x0..x1 the intervall) => [] |
| 271 |
12 dict begin |
| 272 |
/dxZ exch def /dxN exch def |
| 273 |
/pstZeros [] def |
| 274 |
x0 dxZ x1 { % suche Nullstellen |
| 275 |
/xWert exch def |
| 276 |
xWert NewtonMehrfach |
| 277 |
%xWert Steffensen |
| 278 |
/xNull exch def |
| 279 |
pstZeros aload length /Laenge exch def % now test if value is a new one |
| 280 |
Laenge 0 eq |
| 281 |
{ xNull 1 } |
| 282 |
{ /newZero true def |
| 283 |
Laenge { |
| 284 |
xNull sub abs dxN lt { /newZero false def } if |
| 285 |
} repeat |
| 286 |
pstZeros aload pop |
| 287 |
newZero { xNull Laenge 1 add } { Laenge } ifelse } ifelse |
| 288 |
array astore |
| 289 |
/pstZeros exch def |
| 290 |
} for |
| 291 |
pstZeros % the end array is now on the stack |
| 292 |
end |
| 293 |
} def |
| 294 |
% |
| 295 |
/Simpson { % on stack must be a b M useXVal --- simple version --- |
| 296 |
% /SFunc must be defined |
| 297 |
/useX ED % for algebraic functions which uses f(x) |
| 298 |
/M ED /b ED /a ED |
| 299 |
/h b a sub M 2 mul div def |
| 300 |
/s1 0 def |
| 301 |
/s2 0 def |
| 302 |
1 1 M { |
| 303 |
/simp_k exch def |
| 304 |
/xVal simp_k 2 mul 1 sub h mul a add def |
| 305 |
/s1 s1 xVal useX { /x exch def } if SFunc add def |
| 306 |
} for |
| 307 |
1 1 M 1 sub { |
| 308 |
/simp_k exch def |
| 309 |
/xVal simp_k 2 mul h mul a add def |
| 310 |
/s2 s2 xVal useX { /x exch def } if SFunc add def |
| 311 |
} for |
| 312 |
/I a useX { /x exch def } if SFunc b useX { /x exch def } if SFunc add s1 4 mul add s2 2 mul add 3 div h mul def |
| 313 |
} def |
| 314 |
% |
| 315 |
% |
| 316 |
/LogGamma { 5 dict begin % z on stack |
| 317 |
/z ED |
| 318 |
/sum 0 def |
| 319 |
/logGamma_k 1 def |
| 320 |
{ |
| 321 |
z logGamma_k div dup 1 add ln sub dup |
| 322 |
abs eps8 lt { pop exit } if |
| 323 |
sum add /sum exch def |
| 324 |
/logGamma_k logGamma_k 1 add def |
| 325 |
} loop |
| 326 |
sum z ln sub CEuler z mul sub |
| 327 |
end |
| 328 |
} def |
| 329 |
% |
| 330 |
/ChebyshevT { |
| 331 |
5 dict begin % z on stack |
| 332 |
/xtmp exch def |
| 333 |
/n exch def |
| 334 |
0 0 1 n .5 mul floor { |
| 335 |
/cheb_k exch def |
| 336 |
xtmp xtmp mul 1 sub cheb_k exp |
| 337 |
xtmp n 2 cheb_k mul sub exp mul |
| 338 |
n 2 cheb_k mul MoverN mul |
| 339 |
add |
| 340 |
} for |
| 341 |
end |
| 342 |
} def |
| 343 |
% |
| 344 |
/ChebyshevU { |
| 345 |
5 dict begin % z on stack |
| 346 |
/xtmp exch def |
| 347 |
/n exch def |
| 348 |
0 0 1 n .5 mul floor { |
| 349 |
/cheb_k exch def |
| 350 |
xtmp xtmp mul 1 sub cheb_k exp |
| 351 |
xtmp n 2 cheb_k mul sub exp mul |
| 352 |
n 1 add 2 cheb_k mul 1 add MoverN mul |
| 353 |
add |
| 354 |
} for |
| 355 |
end |
| 356 |
} def |
| 357 |
% |
| 358 |
/vasicek{ %density=sqrt((1-R2)/R2)*exp(1/2*(norminv(x)2 - (1/sqrt(R2)*((sqrt(1-R2)*norminv(x)-norminv(pd)))2)) |
| 359 |
2 dict begin |
| 360 |
/pd where { pop }{ /pd 0.22 def } ifelse % element of (0,1) probability of default of portfolio |
| 361 |
/R2 where { pop }{ /R2 0.11 def } ifelse % element of (0,1) R_Squared of portfolio |
| 362 |
dup % x x |
| 363 |
norminv % x norminv(x) |
| 364 |
dup mul % x norminv(x)^2 |
| 365 |
exch % norminv(x)2 x |
| 366 |
norminv % norminv(x)2 norminv(x) |
| 367 |
1 R2 sub sqrt mul % norminv(x)2 sqrt(1-R2)*norminv(x) |
| 368 |
pd norminv sub % norminv(x)2 sqrt(1-R2)*norminv(x)-norminv(pd) |
| 369 |
R2 sqrt div % norminv(x)2 1/sqrt(R2)*(sqrt(1-R2)*norminv(x)-norminv(pd)) |
| 370 |
dup mul % norminv(x)2 (1/sqrt(R2)*(sqrt(1-R2)*norminv(x)-norminv(pd)))2 |
| 371 |
sub % norminv(x)2 -(1/sqrt(R2)*(sqrt(1-R2)*norminv(x)-norminv(pd)))2 |
| 372 |
2 div % 1/2*(norminv(x)2 -(1/sqrt(R2)*(sqrt(1-R2)*norminv(x)-norminv(pd)))2) |
| 373 |
ENeperian exch exp % exp(1/2*(norminv(x)2 -(1/sqrt(R2)*(sqrt(1-R2)*norminv(x)-norminv(pd)))2) |
| 374 |
1 R2 sub % exp(1/2*(norminv(x)2 -(1/sqrt(R2)*(sqrt(1-R2)*norminv(x)-norminv(pd)))2) 1-R2 |
| 375 |
R2 div % exp(1/2*(norminv(x)2 -(1/sqrt(R2)*(sqrt(1-R2)*norminv(x)-norminv(pd)))2) (1-R2)/R2 |
| 376 |
sqrt % exp(1/2*(norminv(x)2 -(1/sqrt(R2)*(sqrt(1-R2)*norminv(x)-norminv(pd)))2) sqrt((1-R2)/R2) |
| 377 |
mul % sqrt((1-R2)/R2)*exp(1/2*(norminv(x)2 - (1/sqrt(R2)*((sqrt(1-R2)*norminv(x)-norminv(pd)))2)) |
| 378 |
end |
| 379 |
} def |
| 380 |
%end{vasicek density} |
| 381 |
% |
| 382 |
% https://mathworld.wolfram.com/ConfluentHypergeometricFunctionoftheFirstKind.html |
| 383 |
/ConfHyperFunc { % Confluent Hypergeometric Funtion of the First Kind |
| 384 |
% on stack must be a b z |
| 385 |
15 dict begin |
| 386 |
/z ED /b ED /a ED |
| 387 |
/sum 1 def |
| 388 |
/k 0 def |
| 389 |
/nom 1 def |
| 390 |
/denom 1 def |
| 391 |
{ |
| 392 |
nom a k add mul z mul /nom ED |
| 393 |
denom b k add mul k 1 add mul /denom ED |
| 394 |
nom denom div /diff ED |
| 395 |
sum diff add /sum ED |
| 396 |
diff 1e-5 lt { exit } { k 1 add /k ED } ifelse |
| 397 |
k 1000 ge { (Error ConfHyperFunc: k =1000) == exit } if |
| 398 |
} loop |
| 399 |
sum |
| 400 |
end |
| 401 |
} def |
| 402 |
% |
| 403 |
% https://mathworld.wolfram.com/BetaFunction.html |
| 404 |
/BETA {% BETA function ((p-1)!(q-1)!)/(p+q-1)! |
| 405 |
% on stack must be p q |
| 406 |
2 dict begin |
| 407 |
/q ED |
| 408 |
/p ED |
| 409 |
p 1 sub factorial q 1 sub factorial |
| 410 |
mul |
| 411 |
p q add 1 sub factorial |
| 412 |
div |
| 413 |
end |
| 414 |
} def |
| 415 |
% |
| 416 |
end |
| 417 |
% |
| 418 |
/arraySum { % on stack the array |
| 419 |
5 dict begin |
| 420 |
dup length 0 eq |
| 421 |
{} |
| 422 |
{ /xArray exch def |
| 423 |
/xSum 0 def |
| 424 |
/i 0 def |
| 425 |
xArray length { |
| 426 |
/xSum xSum xArray i get add def |
| 427 |
/i i 1 add def |
| 428 |
} repeat |
| 429 |
} ifelse |
| 430 |
xSum |
| 431 |
end |
| 432 |
} def |
| 433 |
% |
| 434 |
/arrayProd { % on stack the array |
| 435 |
5 dict begin |
| 436 |
dup length 0 eq |
| 437 |
{} |
| 438 |
{ /xArray exch def |
| 439 |
/xProd 1 def |
| 440 |
/i 0 def |
| 441 |
xArray length { |
| 442 |
/xProd xProd xArray i get mul def |
| 443 |
/i i 1 add def |
| 444 |
} repeat |
| 445 |
} ifelse |
| 446 |
xProd |
| 447 |
end |
| 448 |
} def |
| 449 |
% |
| 450 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 451 |
% subroutines for complex numbers, given as an array [a b] |
| 452 |
% which is a+bi = Real+i Imag |
| 453 |
%% Global defined |
| 454 |
% |
| 455 |
/cxadd { % [a1 b1] [a2 b2] = [a1+a2 b1+b2] |
| 456 |
dup 0 get % [a1 b1] [a2 b2] a2 |
| 457 |
3 -1 roll % [a2 b2] a2 [a1 b1] |
| 458 |
dup 0 get % [a2 b2] a2 [a1 b1] a1 |
| 459 |
3 -1 roll % [a2 b2] [a1 b1] a1 a2 |
| 460 |
add % [a2 b2] [a1 b1] a1+a2 |
| 461 |
3 1 roll % a1+a2 [a2 b2] [a1 b1] |
| 462 |
1 get % a1+a2 [a2 b2] b1 |
| 463 |
exch 1 get % a1+a2 b1 b2 |
| 464 |
add 2 array astore |
| 465 |
} def |
| 466 |
% |
| 467 |
/cxneg { % [a b] |
| 468 |
dup 1 get % [a b] b |
| 469 |
exch 0 get % b a |
| 470 |
neg exch neg % -a -b |
| 471 |
2 array astore |
| 472 |
} def |
| 473 |
% |
| 474 |
/cxsub { cxneg cxadd } def % same as negative addition |
| 475 |
% |
| 476 |
% [a1 b1][a2 b2] = [a1a2-b1b2 a1b2+b1a2] = [a3 b3] |
| 477 |
/cxmul { % [a1 b1] [a2 b2] |
| 478 |
dup 0 get % [a1 b1] [a2 b2] a2 |
| 479 |
exch 1 get % [a1 b1] a2 b2 |
| 480 |
3 -1 roll % a2 b2 [a1 b1] |
| 481 |
dup 0 get % a2 b2 [a1 b1] a1 |
| 482 |
exch 1 get % a2 b2 a1 b1 |
| 483 |
dup % a2 b2 a1 b1 b1 |
| 484 |
5 -1 roll dup % b2 a1 b1 b1 a2 a2 |
| 485 |
3 1 roll mul % b2 a1 b1 a2 b1a2 |
| 486 |
5 -2 roll dup % b1 a2 b1a2 b2 a1 a1 |
| 487 |
3 -1 roll dup % b1 a2 b1a2 a1 a1 b2 b2 |
| 488 |
3 1 roll mul % b1 a2 b1a2 a1 b2 a1b2 |
| 489 |
4 -1 roll add % b1 a2 a1 b2 b3 |
| 490 |
4 2 roll mul % b1 b2 b3 a1a2 |
| 491 |
4 2 roll mul sub % b3 a3 |
| 492 |
exch 2 array astore |
| 493 |
} def |
| 494 |
% |
| 495 |
% [a b]^2 = [a^2-b^2 2ab] = [a2 b2] |
| 496 |
/cxsqr { % [a b] square root |
| 497 |
dup 0 get exch 1 get % a b |
| 498 |
dup dup mul % a b b^2 |
| 499 |
3 -1 roll % b b^2 a |
| 500 |
dup dup mul % b b^2 a a^2 |
| 501 |
3 -1 roll sub % b a a2 |
| 502 |
3 1 roll mul 2 mul % a2 b2 |
| 503 |
2 array astore |
| 504 |
} def |
| 505 |
% |
| 506 |
/cxsqrt { % [a b] |
| 507 |
% dup cxnorm sqrt /r exch def |
| 508 |
% cxarg 2 div RadtoDeg dup cos r mul exch sin r mul cxmake2 |
| 509 |
cxlog % log[a b] |
| 510 |
2 cxrdiv % log[a b]/2 |
| 511 |
aload pop exch % b a |
| 512 |
2.781 exch exp % b exp(a) |
| 513 |
exch cxconv exch % [Re +iIm] exp(a) |
| 514 |
cxrmul % |
| 515 |
} def |
| 516 |
% |
| 517 |
/cxarg { % [a b] |
| 518 |
aload pop % a b |
| 519 |
exch atan % arctan b/a |
| 520 |
DegtoRad % arg(z)=atan(b/a) |
| 521 |
} def |
| 522 |
% |
| 523 |
% log[a b] = [a^2-b^2 2ab] = [a2 b2] |
| 524 |
/cxlog { % [a b] |
| 525 |
dup % [a b][a b] |
| 526 |
cxnorm % [a b] |z| |
| 527 |
log % [a b] log|z| |
| 528 |
exch % log|z|[a b] |
| 529 |
cxarg % log|z| Theta |
| 530 |
cxmake2 % [log|z| Theta] |
| 531 |
} def |
| 532 |
% |
| 533 |
% square of magnitude of complex number |
| 534 |
/cxnorm2 { % [a b] |
| 535 |
dup 0 get exch 1 get % a b |
| 536 |
dup mul % a b^2 |
| 537 |
exch dup mul add % a^2+b^2 |
| 538 |
} def |
| 539 |
% |
| 540 |
/cxnorm { % [a b] |
| 541 |
cxnorm2 sqrt |
| 542 |
} def |
| 543 |
% |
| 544 |
/cxconj { % conjugent complex |
| 545 |
dup 0 get exch 1 get % a b |
| 546 |
neg 2 array astore % [a -b] |
| 547 |
} def |
| 548 |
% |
| 549 |
/cxre { 0 get } def % real value |
| 550 |
/cxim { 1 get } def % imag value |
| 551 |
% |
| 552 |
% 1/[a b] = ([a -b]/(a^2+b^2) |
| 553 |
/cxrecip { % [a b] |
| 554 |
dup cxnorm2 exch % n2 [a b] |
| 555 |
dup 0 get exch 1 get % n2 a b |
| 556 |
3 -1 roll % a b n2 |
| 557 |
dup % a b n2 n2 |
| 558 |
4 -1 roll exch div % b n2 a/n2 |
| 559 |
3 1 roll div % a/n2 b/n2 |
| 560 |
neg 2 array astore |
| 561 |
} def |
| 562 |
% |
| 563 |
/cxmake1 { 0 2 array astore } def % make a complex number, real given |
| 564 |
/cxmake2 { 2 array astore } def % dito, both given |
| 565 |
% |
| 566 |
/cxdiv { cxrecip cxmul } def |
| 567 |
% |
| 568 |
% multiplikation by a real number |
| 569 |
/cxrmul { % [a b] r |
| 570 |
exch aload pop % r a b |
| 571 |
3 -1 roll dup % a b r r |
| 572 |
3 1 roll mul % a r b*r |
| 573 |
3 1 roll mul % b*r a*r |
| 574 |
exch 2 array astore % [a*r b*r] |
| 575 |
} def |
| 576 |
% |
| 577 |
% division by a real number |
| 578 |
/cxrdiv { % [a b] r |
| 579 |
1 exch div % [a b] 1/r |
| 580 |
cxrmul |
| 581 |
} def |
| 582 |
% |
| 583 |
% exp(i theta) = cos(theta)+i sin(theta) polar<->cartesian |
| 584 |
/cxconv { % theta |
| 585 |
RadtoDeg dup sin exch cos cxmake2 |
| 586 |
} def |
| 587 |
% |
| 588 |
% cxexp z^k with k as a natural number |
| 589 |
/cxexp { % z k |
| 590 |
3 dict begin |
| 591 |
dup 0 eq { pop pop [1 0] }{ |
| 592 |
/k ED |
| 593 |
/z ED |
| 594 |
/sol [1 0] def |
| 595 |
k { sol z cxmul /sol ED } repeat |
| 596 |
sol } ifelse |
| 597 |
end |
| 598 |
} def |
| 599 |
% |