Skip to content

Commit 042173b

Browse files
Fix 5 occurrences of if-let-to-cond
`cond` with internal definitions is preferred over `if` with `let`, to reduce nesting
1 parent 75a2779 commit 042173b

File tree

1 file changed

+78
-70
lines changed

1 file changed

+78
-70
lines changed

src/core/taylor.rkt

Lines changed: 78 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -330,23 +330,24 @@
330330
(define offset (series-offset normalized))
331331
(define coeffs (series-function normalized))
332332
(define offset* (+ offset (modulo (- offset) n)))
333-
(if (= offset offset*)
334-
normalized
335-
(let ([cache (make-dvector 2)]) ;; never called more than twice
336-
(define (coeffs* i)
337-
(unless (and (> (dvector-capacity cache) i) (dvector-ref cache i))
338-
(define res
339-
(match i
340-
[0
341-
(adder (make-sum (for/list ([j (in-range (modulo offset n))])
342-
`(* ,(coeffs j) (pow ,var ,(+ j (modulo (- offset) n)))))))]
343-
[_
344-
#:when (< i n)
345-
(adder 0)]
346-
[_ (coeffs (+ (- i n) (modulo offset n)))]))
347-
(dvector-set! cache i res))
348-
(dvector-ref cache i))
349-
(make-series offset* (λ (f i) (deref (coeffs* i)))))))
333+
(cond
334+
[(= offset offset*) normalized]
335+
[else
336+
(define cache (make-dvector 2)) ;; never called more than twice
337+
(define (coeffs* i)
338+
(unless (and (> (dvector-capacity cache) i) (dvector-ref cache i))
339+
(define res
340+
(match i
341+
[0
342+
(adder (make-sum (for/list ([j (in-range (modulo offset n))])
343+
`(* ,(coeffs j) (pow ,var ,(+ j (modulo (- offset) n)))))))]
344+
[_
345+
#:when (< i n)
346+
(adder 0)]
347+
[_ (coeffs (+ (- i n) (modulo offset n)))]))
348+
(dvector-set! cache i res))
349+
(dvector-ref cache i))
350+
(make-series offset* (λ (f i) (deref (coeffs* i))))]))
350351

351352
(define (taylor-sqrt var num)
352353
;(-> symbol? term? term?)
@@ -422,56 +423,62 @@
422423
;(-> (-> number? batchref?) term?)
423424
(make-series 0
424425
(λ (f n)
425-
(if (zero? n)
426-
`(exp ,(coeffs 0))
427-
(let* ([coeffs* (list->vector (map coeffs (range 1 (+ n 1))))]
428-
[nums (for/list ([i (in-range 1 (+ n 1))]
429-
[coeff (in-vector coeffs*)]
430-
#:unless (equal? (deref coeff) 0))
431-
i)])
432-
`(* (exp ,(coeffs 0))
433-
(+ ,@(for/list ([p (all-partitions n (sort nums >))])
434-
`(* ,@(for/list ([(count num) (in-dict p)])
435-
`(/ (pow ,(vector-ref coeffs* (- num 1)) ,count)
436-
,(factorial count))))))))))))
426+
(cond
427+
[(zero? n) `(exp ,(coeffs 0))]
428+
[else
429+
(define coeffs* (list->vector (map coeffs (range 1 (+ n 1)))))
430+
(define nums
431+
(for/list ([i (in-range 1 (+ n 1))]
432+
[coeff (in-vector coeffs*)]
433+
#:unless (equal? (deref coeff) 0))
434+
i))
435+
`(* (exp ,(coeffs 0))
436+
(+ ,@(for/list ([p (all-partitions n (sort nums >))])
437+
`(* ,@(for/list ([(count num) (in-dict p)])
438+
`(/ (pow ,(vector-ref coeffs* (- num 1)) ,count)
439+
,(factorial count)))))))]))))
437440

438441
(define (taylor-sin coeffs)
439442
;(-> (-> number? batchref?) term?)
440443
(make-series 0
441444
(λ (f n)
442-
(if (zero? n)
443-
0
444-
(let* ([coeffs* (list->vector (map coeffs (range 1 (+ n 1))))]
445-
[nums (for/list ([i (in-range 1 (+ n 1))]
446-
[coeff (in-vector coeffs*)]
447-
#:unless (equal? (deref coeff) 0))
448-
i)])
449-
`(+ ,@(for/list ([p (all-partitions n (sort nums >))])
450-
(if (= (modulo (apply + (map car p)) 2) 1)
451-
`(* ,(if (= (modulo (apply + (map car p)) 4) 1) 1 -1)
452-
,@(for/list ([(count num) (in-dict p)])
453-
`(/ (pow ,(vector-ref coeffs* (- num 1)) ,count)
454-
,(factorial count))))
455-
0))))))))
445+
(cond
446+
[(zero? n) 0]
447+
[else
448+
(define coeffs* (list->vector (map coeffs (range 1 (+ n 1)))))
449+
(define nums
450+
(for/list ([i (in-range 1 (+ n 1))]
451+
[coeff (in-vector coeffs*)]
452+
#:unless (equal? (deref coeff) 0))
453+
i))
454+
`(+ ,@(for/list ([p (all-partitions n (sort nums >))])
455+
(if (= (modulo (apply + (map car p)) 2) 1)
456+
`(* ,(if (= (modulo (apply + (map car p)) 4) 1) 1 -1)
457+
,@(for/list ([(count num) (in-dict p)])
458+
`(/ (pow ,(vector-ref coeffs* (- num 1)) ,count)
459+
,(factorial count))))
460+
0)))]))))
456461

457462
(define (taylor-cos coeffs)
458463
;(-> (-> number? batchref?) term?)
459464
(make-series 0
460465
(λ (f n)
461-
(if (zero? n)
462-
1
463-
(let* ([coeffs* (list->vector (map coeffs (range 1 (+ n 1))))]
464-
[nums (for/list ([i (in-range 1 (+ n 1))]
465-
[coeff (in-vector coeffs*)]
466-
#:unless (equal? (deref coeff) 0))
467-
i)])
468-
`(+ ,@(for/list ([p (all-partitions n (sort nums >))])
469-
(if (= (modulo (apply + (map car p)) 2) 0)
470-
`(* ,(if (= (modulo (apply + (map car p)) 4) 0) 1 -1)
471-
,@(for/list ([(count num) (in-dict p)])
472-
`(/ (pow ,(vector-ref coeffs* (- num 1)) ,count)
473-
,(factorial count))))
474-
0))))))))
466+
(cond
467+
[(zero? n) 1]
468+
[else
469+
(define coeffs* (list->vector (map coeffs (range 1 (+ n 1)))))
470+
(define nums
471+
(for/list ([i (in-range 1 (+ n 1))]
472+
[coeff (in-vector coeffs*)]
473+
#:unless (equal? (deref coeff) 0))
474+
i))
475+
`(+ ,@(for/list ([p (all-partitions n (sort nums >))])
476+
(if (= (modulo (apply + (map car p)) 2) 0)
477+
`(* ,(if (= (modulo (apply + (map car p)) 4) 0) 1 -1)
478+
,@(for/list ([(count num) (in-dict p)])
479+
`(/ (pow ,(vector-ref coeffs* (- num 1)) ,count)
480+
,(factorial count))))
481+
0)))]))))
475482

476483
;; This is a hyper-specialized symbolic differentiator for log(f(x))
477484

@@ -521,19 +528,20 @@
521528
(define base
522529
(make-series 0
523530
(λ (f n)
524-
(if (zero? n)
525-
`(log ,(maybe-negate (coeffs 0)))
526-
(let ([tmpl (logcompute n)])
527-
`(/ (+ ,@(for/list ([term tmpl])
528-
(match-define `(,coeff ,k ,ps ...) term)
529-
`(* ,coeff
530-
(/ (* ,@(for/list ([i (in-naturals 1)]
531-
[p ps])
532-
(if (= p 0)
533-
1
534-
`(pow (* ,(factorial i) ,(coeffs i)) ,p))))
535-
(exp (* ,(- k) ,(f 0)))))))
536-
,(factorial n)))))))
531+
(cond
532+
[(zero? n) `(log ,(maybe-negate (coeffs 0)))]
533+
[else
534+
(define tmpl (logcompute n))
535+
`(/ (+ ,@(for/list ([term tmpl])
536+
(match-define `(,coeff ,k ,ps ...) term)
537+
`(* ,coeff
538+
(/ (* ,@(for/list ([i (in-naturals 1)]
539+
[p ps])
540+
(if (= p 0)
541+
1
542+
`(pow (* ,(factorial i) ,(coeffs i)) ,p))))
543+
(exp (* ,(- k) ,(f 0)))))))
544+
,(factorial n))]))))
537545

538546
(if (zero? shift)
539547
base

0 commit comments

Comments
 (0)