|
213 | 213 | (define (taylor-exact . terms) |
214 | 214 | ;(->* () #:rest (listof batchref?) term?) |
215 | 215 | (define items (list->vector (map reducer terms))) |
216 | | - (cons 0 |
217 | | - (λ (n) |
218 | | - (if (<= (length terms) n) |
219 | | - (adder 0) |
220 | | - (vector-ref items n))))) |
| 216 | + (define len (vector-length items)) |
| 217 | + (make-series 0 |
| 218 | + (λ (f n) |
| 219 | + (if (< n len) |
| 220 | + (deref (vector-ref items n)) |
| 221 | + 0)))) |
221 | 222 |
|
222 | 223 | (define (first-nonzero-exp f) |
223 | 224 | ;(-> (-> number? batchref?) number?) |
|
497 | 498 | `(neg ,x) |
498 | 499 | x)) |
499 | 500 |
|
500 | | - (define series-cache (make-dvector 10 #f)) |
501 | | - (dvector-set! series-cache 0 (reducer (adder `(log ,(maybe-negate (coeffs 0)))))) |
502 | | - |
503 | | - (define (series n) |
504 | | - (unless (and (> (dvector-capacity series-cache) n) (dvector-ref series-cache n)) |
505 | | - (define tmpl (logcompute n)) |
506 | | - (define res |
507 | | - (reducer (adder `(/ (+ ,@(for/list ([term tmpl]) |
508 | | - (match-define `(,coeff ,k ,ps ...) term) |
509 | | - `(* ,coeff |
510 | | - (/ (* ,@(for/list ([i (in-naturals 1)] |
511 | | - [p ps]) |
512 | | - (if (= p 0) |
513 | | - 1 |
514 | | - `(pow (* ,(factorial i) ,(coeffs i)) ,p)))) |
515 | | - (exp (* ,(- k) ,(series 0))))))) |
516 | | - ,(factorial n))))) |
517 | | - (dvector-set! series-cache n res)) |
518 | | - (dvector-ref series-cache n)) |
519 | | - |
520 | | - (cons 0 |
521 | | - (λ (n) |
522 | | - (if (and (= n 0) (not (zero? shift))) |
523 | | - (reducer (adder `(+ (* (neg ,shift) (log ,(maybe-negate var))) ,(series 0)))) |
524 | | - (series n))))) |
| 501 | + (define base |
| 502 | + (make-series 0 |
| 503 | + (λ (f n) |
| 504 | + (if (zero? n) |
| 505 | + `(log ,(maybe-negate (coeffs 0))) |
| 506 | + (let ([tmpl (logcompute n)]) |
| 507 | + `(/ (+ ,@(for/list ([term tmpl]) |
| 508 | + (match-define `(,coeff ,k ,ps ...) term) |
| 509 | + `(* ,coeff |
| 510 | + (/ (* ,@(for/list ([i (in-naturals 1)] |
| 511 | + [p ps]) |
| 512 | + (if (= p 0) |
| 513 | + 1 |
| 514 | + `(pow (* ,(factorial i) ,(coeffs i)) ,p)))) |
| 515 | + (exp (* ,(- k) ,(f 0))))))) |
| 516 | + ,(factorial n))))))) |
| 517 | + |
| 518 | + (if (zero? shift) |
| 519 | + base |
| 520 | + (taylor-add base |
| 521 | + (make-series 0 |
| 522 | + (λ (f n) |
| 523 | + (if (zero? n) |
| 524 | + `(* (neg ,shift) (log ,(maybe-negate var))) |
| 525 | + 0)))))) |
525 | 526 |
|
526 | 527 | (module+ test |
527 | 528 | (require rackunit) |
|
0 commit comments