1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
;; -*-theme-d-*-
;; Copyright (C) 2014 Tommi Höynälänmaa
;; Distributed under GNU Lesser General Public License version 3,
;; see file doc/LGPL-3.
(define-body (standard-library complex)
(import (standard-library bitwise-arithmetic)
(standard-library basic-math)
(standard-library string-utilities))
(define-simple-virtual-method complex
(((re1 <real-number>) (im1 <real-number>)) <complex> (pure))
(raise-simple 'complex:dispatch-error))
(define-simple-virtual-method complex
(((re1 <real>) (im1 <real>)) <complex> (pure))
(create <complex> re1 im1))
(define-simple-virtual-method complex
(((re1 <real>) (im1 <integer>)) <complex> (pure))
(create <complex> re1 (integer->real im1)))
(define-simple-virtual-method complex
(((re1 <real>) (im1 <rational>)) <complex> (pure))
(create <complex> re1 (rational->real im1)))
(define-simple-virtual-method complex
(((re1 <integer>) (im1 <real>)) <complex> (pure))
(create <complex> (integer->real re1) im1))
(define-simple-virtual-method complex
(((re1 <integer>) (im1 <integer>)) <complex> (pure))
(create <complex> (integer->real re1) (integer->real im1)))
(define-simple-virtual-method complex
(((re1 <integer>) (im1 <rational>)) <complex> (pure))
(create <complex> (integer->real re1) (rational->real im1)))
(define-simple-virtual-method complex
(((re1 <rational>) (im1 <real>)) <complex> (pure))
(create <complex> (rational->real re1) im1))
(define-simple-virtual-method complex
(((re1 <rational>) (im1 <integer>)) <complex> (pure))
(create <complex> (rational->real re1) (integer->real im1)))
(define-simple-virtual-method complex
(((re1 <rational>) (im1 <rational>)) <complex> (pure))
(create <complex> (rational->real re1) (rational->real im1)))
(define-simple-method real->complex (((r <real>)) <complex> (pure))
(create <complex> r 0.0))
(define-simple-method integer->complex (((n <integer>)) <complex> (pure))
(create <complex> (integer->real n) 0.0))
(define-simple-method rational->complex (((rat <rational>)) <complex> (pure))
(create <complex> (rational->real rat) 0.0))
(define-simple-method simplify-complex
(((cx <complex>)) (:union <complex> <real>) pure)
(if (= (field-ref cx 'im) 0.0)
(field-ref cx 're)
cx))
(define-simple-method complex=? (((cx1 <complex>) (cx2 <complex>)) <boolean>
pure)
(and (real=? (field-ref cx1 're) (field-ref cx2 're))
(real=? (field-ref cx1 'im) (field-ref cx2 'im))))
(define-simple-method complex= (((cx1 <complex>) (cx2 <complex>)) <boolean>
pure)
(and (real= (field-ref cx1 're) (field-ref cx2 're))
(real= (field-ref cx1 'im) (field-ref cx2 'im))))
(define-simple-method complex-integer=
(((cx <complex>) (i <integer>)) <boolean> pure)
(and (real-integer= (field-ref cx 're) i)
(real= (field-ref cx 'im) 0.0)))
(define-simple-method integer-complex=
(((i <integer>) (cx <complex>)) <boolean> pure)
(and (real-integer= (field-ref cx 're) i)
(real= (field-ref cx 'im) 0.0)))
(define-simple-method complex-real=
(((cx <complex>) (r <real>)) <boolean> pure)
(and (real= (field-ref cx 're) r)
(real= (field-ref cx 'im) 0.0)))
(define-simple-method real-complex=
(((r <real>) (cx <complex>)) <boolean> pure)
(and (real= (field-ref cx 're) r)
(real= (field-ref cx 'im) 0.0)))
(define-simple-method complex-rational=
(((cx <complex>) (rat <rational>)) <boolean> pure)
(and (real-rational= (field-ref cx 're) rat)
(real= (field-ref cx 'im) 0.0)))
(define-simple-method rational-complex=
(((rat <rational>) (cx <complex>)) <boolean> pure)
(and (real-rational= (field-ref cx 're) rat)
(real= (field-ref cx 'im) 0.0)))
(include-virtual-methods equal? complex=?)
(include-virtual-methods = complex=)
(include-virtual-methods = complex-integer=)
(include-virtual-methods = integer-complex=)
(include-virtual-methods = complex-real=)
(include-virtual-methods = real-complex=)
(include-virtual-methods = complex-rational=)
(include-virtual-methods = rational-complex=)
(define-simple-method real-part (((cx <complex>)) <real> (pure))
(field-ref cx 're))
(define-simple-method imag-part (((cx <complex>)) <real> (pure))
(field-ref cx 'im))
(define-simple-method make-polar (((mgn <real>) (phase <real>))
<complex>
(pure))
(create <complex> (* mgn (r-cos phase)) (* mgn (r-sin phase))))
(define-simple-method c-abs (((cx <complex>)) <real> (pure))
(let ((re1 (field-ref cx 're))
(im1 (field-ref cx 'im)))
(r-sqrt (real+ (real* re1 re1) (real* im1 im1)))))
(define-simple-method c-angle (((cx <complex>)) <real> (pure))
(r-atan2 (field-ref cx 'im) (field-ref cx 're)))
(define-simple-method c-neg (((cx <complex>)) <complex> (pure))
(create <complex> (r-neg (field-ref cx 're)) (r-neg (field-ref cx 'im))))
(define-simple-method c-square (((cx <complex>)) <complex> (pure))
(let ((re1 (field-ref cx 're))
(im1 (field-ref cx 'im)))
(create <complex> (real- (r-square re1) (r-square im1))
(integer-real* 2 (real* re1 im1)))))
(define-simple-method c-conj (((cx <complex>)) <complex> pure)
(create <complex> (field-ref cx 're) (- (field-ref cx 'im))))
(include-virtual-methods abs c-abs)
(include-virtual-methods - c-neg)
(include-virtual-methods square c-square)
(include-virtual-methods conj c-conj)
(define-simple-method complex+
(((cx1 <complex>) (cx2 <complex>)) <complex> (pure))
(create <complex>
(real+ (field-ref cx1 're) (field-ref cx2 're))
(real+ (field-ref cx1 'im) (field-ref cx2 'im))))
(include-virtual-methods + complex+)
(define-simple-method complex-
(((cx1 <complex>) (cx2 <complex>)) <complex> (pure))
(create <complex>
(real- (field-ref cx1 're) (field-ref cx2 're))
(real- (field-ref cx1 'im) (field-ref cx2 'im))))
(include-virtual-methods - complex-)
(define-simple-method complex*
(((cx1 <complex>) (cx2 <complex>)) <complex> (pure))
(let ((re1 (field-ref cx1 're))
(im1 (field-ref cx1 'im))
(re2 (field-ref cx2 're))
(im2 (field-ref cx2 'im)))
(create <complex>
(real- (real* re1 re2) (real* im1 im2))
(real+ (real* re1 im2) (real* im1 re2)))))
(include-virtual-methods * complex*)
(define-simple-method complex/
(((cx1 <complex>) (cx2 <complex>)) <complex> (pure))
(let* ((re1 (field-ref cx1 're))
(im1 (field-ref cx1 'im))
(re2 (field-ref cx2 're))
(im2 (field-ref cx2 'im))
(denom (real+ (r-square re2) (r-square im2)))
(x (real+ (real* re1 re2) (real* im1 im2)))
(y (real- (real* im1 re2) (real* re1 im2))))
(create <complex> (real/ x denom) (real/ y denom))))
(include-virtual-methods / complex/)
(define-simple-method real-complex+
(((r <real>) (cx <complex>)) <complex> (pure))
(create <complex> (real+ r (field-ref cx 're)) (field-ref cx 'im)))
(define-simple-method complex-real+
(((cx <complex>) (r <real>)) <complex> (pure))
(create <complex> (real+ r (field-ref cx 're)) (field-ref cx 'im)))
(define-simple-method integer-complex+
(((n <integer>) (cx <complex>)) <complex> (pure))
(create <complex> (integer-real+ n (field-ref cx 're)) (field-ref cx 'im)))
(define-simple-method complex-integer+
(((cx <complex>) (n <integer>)) <complex> (pure))
(create <complex> (integer-real+ n (field-ref cx 're)) (field-ref cx 'im)))
(define-simple-method rational-complex+
(((rat <rational>) (cx <complex>)) <complex> (pure))
(create <complex>
(rational-real+ rat (field-ref cx 're))
(field-ref cx 'im)))
(define-simple-method complex-rational+
(((cx <complex>) (rat <rational>)) <complex> (pure))
(create <complex>
(rational-real+ rat (field-ref cx 're))
(field-ref cx 'im)))
(define-simple-method real-complex-
(((r <real>) (cx <complex>)) <complex> (pure))
(create <complex> (real- r (field-ref cx 're)) (r-neg (field-ref cx 'im))))
(define-simple-method complex-real-
(((cx <complex>) (r <real>)) <complex> (pure))
(create <complex> (real- (field-ref cx 're) r) (field-ref cx 'im)))
(define-simple-method integer-complex-
(((n <integer>) (cx <complex>)) <complex> (pure))
(create <complex> (integer-real- n (field-ref cx 're))
(r-neg (field-ref cx 'im))))
(define-simple-method complex-integer-
(((cx <complex>) (n <integer>)) <complex> (pure))
(create <complex> (real-integer- (field-ref cx 're) n) (field-ref cx 'im)))
(define-simple-method rational-complex-
(((rat <rational>) (cx <complex>)) <complex> (pure))
(create <complex>
(rational-real- rat (field-ref cx 're))
(r-neg (field-ref cx 'im))))
(define-simple-method complex-rational-
(((cx <complex>) (rat <rational>)) <complex> (pure))
(create <complex> (real-rational- (field-ref cx 're) rat) (field-ref cx 'im)))
(define-simple-method real-complex*
(((r <real>) (cx <complex>)) <complex> (pure))
(create <complex> (real* r (field-ref cx 're)) (real* r (field-ref cx 'im))))
(define-simple-method complex-real*
(((cx <complex>) (r <real>)) <complex> (pure))
(create <complex> (real* r (field-ref cx 're)) (real* r (field-ref cx 'im))))
(define-simple-method integer-complex*
(((n <integer>) (cx <complex>)) <complex> (pure))
(create <complex>
(integer-real* n (field-ref cx 're))
(integer-real* n (field-ref cx 'im))))
(define-simple-method complex-integer*
(((cx <complex>) (n <integer>)) <complex> (pure))
(create <complex>
(integer-real* n (field-ref cx 're))
(integer-real* n (field-ref cx 'im))))
(define-simple-method rational-complex*
(((rat <rational>) (cx <complex>)) <complex> (pure))
(create <complex>
(rational-real* rat (field-ref cx 're))
(rational-real* rat (field-ref cx 'im))))
(define-simple-method complex-rational*
(((cx <complex>) (rat <rational>)) <complex> (pure))
(create <complex>
(rational-real* rat (field-ref cx 're))
(rational-real* rat (field-ref cx 'im))))
(define-simple-method real-complex/
(((r <real>) (cx <complex>)) <complex> (pure))
(let* ((re2 (field-ref cx 're))
(im2 (field-ref cx 'im))
(denom (real+ (r-square re2) (r-square im2)))
(x (real* r re2))
(y (r-neg (real* r im2))))
(create <complex> (real/ x denom) (real/ y denom))))
(define-simple-method complex-real/
(((cx <complex>) (r <real>)) <complex> (pure))
(create <complex> (real/ (field-ref cx 're) r) (real/ (field-ref cx 'im) r)))
(define-simple-method integer-complex/
(((n <integer>) (cx <complex>)) <complex> (pure))
(let* ((re2 (field-ref cx 're))
(im2 (field-ref cx 'im))
(denom (real+ (r-square re2) (r-square im2)))
(x (integer-real* n re2))
(y (r-neg (integer-real* n im2))))
(create <complex> (real/ x denom) (real/ y denom))))
(define-simple-method complex-integer/
(((cx <complex>) (n <integer>)) <complex> (pure))
(create <complex>
(real-integer/ (field-ref cx 're) n)
(real-integer/ (field-ref cx 'im) n)))
(define-simple-method rational-complex/
(((rat <rational>) (cx <complex>)) <complex> (pure))
(let* ((re2 (field-ref cx 're))
(im2 (field-ref cx 'im))
(denom (real+ (r-square re2) (r-square im2)))
(x (rational-real* rat re2))
(y (r-neg (rational-real* rat im2))))
(create <complex> (real/ x denom) (real/ y denom))))
(define-simple-method complex-rational/
(((cx <complex>) (rat <rational>)) <complex> (pure))
(create <complex>
(real-rational/ (field-ref cx 're) rat)
(real-rational/ (field-ref cx 'im) rat)))
(include-virtual-methods + real-complex+)
(include-virtual-methods + complex-real+)
(include-virtual-methods + integer-complex+)
(include-virtual-methods + complex-integer+)
(include-virtual-methods + rational-complex+)
(include-virtual-methods + complex-rational+)
(include-virtual-methods - real-complex-)
(include-virtual-methods - complex-real-)
(include-virtual-methods - integer-complex-)
(include-virtual-methods - complex-integer-)
(include-virtual-methods - rational-complex-)
(include-virtual-methods - complex-rational-)
(include-virtual-methods * real-complex*)
(include-virtual-methods * complex-real*)
(include-virtual-methods * integer-complex*)
(include-virtual-methods * complex-integer*)
(include-virtual-methods * rational-complex*)
(include-virtual-methods * complex-rational*)
(include-virtual-methods / real-complex/)
(include-virtual-methods / complex-real/)
(include-virtual-methods / integer-complex/)
(include-virtual-methods / complex-integer/)
(include-virtual-methods / rational-complex/)
(include-virtual-methods / complex-rational/)
(define-simple-method c-sqrt (((cx <complex>)) <complex> (pure))
(let ((abs-value (c-abs cx))
(phase (c-angle cx)))
(make-polar (r-sqrt abs-value) (/ phase 2))))
(define-simple-method c-exp (((cx <complex>)) <complex> (pure))
(let ((coeff (r-exp (field-ref cx 're)))
(im1 (field-ref cx 'im)))
(create <complex> (* coeff (r-cos im1)) (* coeff (r-sin im1)))))
(define-simple-method c-log (((cx <complex>)) <complex> (pure))
(let ((abs-value (c-abs cx))
(phase (c-angle cx)))
(create <complex> (r-log abs-value) phase)))
(define-simple-method c-log10 (((cx <complex>)) <complex> (pure))
(/ (c-log cx) (r-log 10.0)))
(define-simple-method c-expt (((base <complex>) (exponent <complex>))
<complex>
(pure))
(c-exp (* exponent (c-log base))))
(define-simple-method c-sin (((cx <complex>)) <complex> (pure))
(let* ((re1 (field-ref cx 're))
(im1 (field-ref cx 'im))
(exp-term1 (r-exp im1))
(exp-term2 (r-exp (- im1)))
(cos-term (r-cos re1))
(sin-term (r-sin re1))
(a (* (- exp-term2 exp-term1) cos-term))
(b (* (+ exp-term2 exp-term1) sin-term)))
(create <complex> (/ b 2) (- (/ a 2)))))
(define-simple-method c-cos (((cx <complex>)) <complex> (pure))
(let* ((re1 (field-ref cx 're))
(im1 (field-ref cx 'im))
(exp-term1 (r-exp im1))
(exp-term2 (r-exp (- im1)))
(cos-term (r-cos re1))
(sin-term (r-sin re1))
(a (* (+ exp-term2 exp-term1) cos-term))
(b (* (- exp-term2 exp-term1) sin-term)))
(create <complex> (/ a 2) (/ b 2))))
(define-simple-method c-tan (((cx <complex>)) <complex> (pure))
(let* ((re1 (field-ref cx 're))
(im1 (field-ref cx 'im))
(exp-term1 (r-exp im1))
(exp-term2 (r-exp (- im1)))
(cos-term (r-cos re1))
(sin-term (r-sin re1))
(a (* (- exp-term2 exp-term1) cos-term))
(b (* (+ exp-term2 exp-term1) sin-term))
(c (* (+ exp-term2 exp-term1) cos-term))
(d (* (- exp-term2 exp-term1) sin-term)))
(/ (create <complex> b (- a)) (create <complex> c d))))
(define-simple-method c-asin (((cx <complex>)) <complex> (pure))
(let* ((unit (create <complex> 1.0 0.0))
(imag-unit (create <complex> 0.0 1.0))
(term1 (* imag-unit cx))
(sq (c-square cx))
(term2 (c-sqrt (- unit sq)))
(log-term (c-log (+ term1 term2))))
(* (create <complex> 0.0 -1.0) log-term)))
(define-simple-method c-acos (((cx <complex>)) <complex> (pure))
(let* ((unit (create <complex> 1.0 0.0))
(imag-unit (create <complex> 0.0 1.0))
(sq (c-square cx))
(term2 (* imag-unit (c-sqrt (- unit sq))))
(log-term (c-log (- cx term2))))
(* imag-unit log-term)))
(define-simple-method c-atan (((cx <complex>)) <complex> (pure))
(let* ((unit (create <complex> 1.0 0.0))
(imag-unit (create <complex> 0.0 1.0))
(iz (create <complex> (- (field-ref cx 'im)) (field-ref cx 're)))
(term1 (c-log (- unit iz)))
(term2 (c-log (+ unit iz))))
(* (create <complex> 0.0 0.5) (- term1 term2))))
(define-simple-method c-sinh (((cx <complex>)) <complex> (pure))
(let ((term1 (c-exp cx))
(term2 (c-exp (- cx))))
(* 0.5 (- term1 term2))))
(define-simple-method c-cosh (((cx <complex>)) <complex> (pure))
(let ((term1 (c-exp cx))
(term2 (c-exp (- cx))))
(* 0.5 (+ term1 term2))))
(define-simple-method c-tanh (((cx <complex>)) <complex> (pure))
(let* ((exp-term1 (c-exp cx))
(exp-term2 (c-exp (- cx))))
(/ (- exp-term1 exp-term2) (+ exp-term1 exp-term2))))
(define-simple-method c-asinh (((cx <complex>)) <complex> (pure))
(let* ((unit (create <complex> 1.0 0.0))
(sq (c-square cx))
(term2 (c-sqrt (+ sq unit))))
(c-log (+ cx term2))))
(define-simple-method c-acosh (((cx <complex>)) <complex> (pure))
(let* ((unit (create <complex> 1.0 0.0))
(sq (c-square cx))
(term2 (c-sqrt (- sq unit))))
(c-log (+ cx term2))))
(define-simple-method c-atanh (((cx <complex>)) <complex> (pure))
(let* ((unit (create <complex> 1.0 0.0)))
(* 0.5 (c-log (/ (+ unit cx) (- unit cx))))))
(define-simple-method r-complex-log (((r <real>)) <complex> pure)
(if (>= r 0.0)
(complex (r-log r) 0.0)
(complex (r-log (r-abs r)) gl-r-pi)))
(define-simple-method complex-real-expt (((base <complex>) (exponent <real>))
<complex> pure)
(c-exp (* exponent (c-log base))))
(define-simple-method real-complex-expt (((base <real>) (exponent <complex>))
<complex> pure)
(c-exp (* exponent (r-complex-log base))))
(define-simple-method r-log-neg (((r <real>)) <complex> pure)
(complex (r-log (r-abs r)) gl-r-pi))
(define-simple-method r-log10-neg (((r <real>)) <complex> pure)
(complex (r-log10 (r-abs r)) gl-r-pi/ln10))
(define-simple-method r-complex-expt (((base <real>) (exponent <real>))
<complex> pure)
(c-exp (* exponent (r-complex-log base))))
(define-simple-method c-exp2 (((exponent <complex>)) <complex> pure)
(c-exp (* exponent gl-r-ln2)))
;; Return 1.0+0.0i also for 0^0.
(define-simple-method c-nonneg-int-expt
(((cx-base <complex>) (i-exponent <integer>)) <complex> pure)
(assert (>= i-exponent 0))
(let-mutable ((i-cur-exponent <integer> i-exponent)
(cx-cur-base <complex> cx-base)
(cx-sum <complex> (complex 1.0 0.0)))
(until ((or (equal? i-cur-exponent 0) (equal? i-cur-exponent 1)))
(if (not (equal? (bitwise-and i-cur-exponent 1) 0))
(set! cx-sum (* cx-sum cx-cur-base)))
(set! cx-cur-base (square cx-cur-base))
(set! i-cur-exponent (ash i-cur-exponent -1)))
(cond
((= i-cur-exponent 0) cx-sum)
((= i-cur-exponent 1) (* cx-sum cx-cur-base))
(else (raise-simple 'c-nonneg-int-expt:internal-error)))))
(define-simple-method c-int-expt (((cx-base <complex>) (i-expt <integer>))
<complex> pure)
(if (>= i-expt 0)
(c-nonneg-int-expt cx-base i-expt)
(/ 1.0 (c-nonneg-int-expt cx-base (i-neg i-expt)))))
(define-simple-method complex->string
(((cx <complex>)) <string> (pure))
(let* ((re1 (field-ref cx 're))
(im1 (field-ref cx 'im))
(ch (if (and (>= im1 0) (finite? im1)) "+" "")))
(string-append (real->string re1)
ch
(real->string im1)
"i")))
(include-virtual-methods object->string complex->string))
|