Skip to content

Commit e3ee454

Browse files
authored
Merge pull request #1405 from herbie-fp/autofix-51-1
Automated Resyntax fixes
2 parents 7df6f5f + 042173b commit e3ee454

File tree

2 files changed

+94
-87
lines changed

2 files changed

+94
-87
lines changed

src/core/batch-reduce.rkt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
(define (gather-multiplicative-terms brf recurse)
168168
(match (deref brf)
169169
[+nan.0 (nan-term)]
170-
[(? number? n) `(,n . ())]
170+
[(? number? n) (list n)]
171171
[(? symbol?) `(1 . ((1 . ,brf)))]
172172
[`(neg ,arg)
173173
(define terms (recurse arg))
@@ -201,10 +201,10 @@
201201
(cons exact-cbrt
202202
(for/list ([term (cdr terms)])
203203
(cons (/ (car term) 3) (cdr term))))
204-
(cons 1
205-
(list* (cons 1 (batch-add! batch `(cbrt ,(car terms))))
206-
(for/list ([term (cdr terms)])
207-
(cons (/ (car term) 3) (cdr term))))))])]
204+
(list* 1
205+
(cons 1 (batch-add! batch `(cbrt ,(car terms))))
206+
(for/list ([term (cdr terms)])
207+
(cons (/ (car term) 3) (cdr term)))))])]
208208
[`(pow ,arg ,(app deref 0))
209209
(define terms (recurse arg))
210210
(if (equal? (car terms) +nan.0)

src/core/taylor.rkt

Lines changed: 89 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323

2424
(define (taylor-coefficients batch brfs vars transforms-to-try)
2525
(define expander (expand-taylor! batch))
26-
(define taylor-coeffs
27-
(for*/list ([var (in-list vars)]
28-
#:do [(define taylorer (taylor var batch))]
29-
[transform-type transforms-to-try])
30-
(match-define (list name f finv) transform-type)
31-
(define replacer (batch-replace-expression! batch var (f var)))
32-
(for/list ([brf (in-list brfs)])
33-
(taylorer (expander (reducer (replacer brf)))))))
34-
taylor-coeffs)
26+
(for*/list ([var (in-list vars)]
27+
#:do [(define taylorer (taylor var batch))]
28+
[transform-type transforms-to-try])
29+
(match-define (list name f finv) transform-type)
30+
(define replacer (batch-replace-expression! batch var (f var)))
31+
(for/list ([brf (in-list brfs)])
32+
(taylorer (expander (reducer (replacer brf)))))))
3533

3634
(define (approximate taylor-approxs
3735
batch
@@ -235,15 +233,16 @@
235233
(define (series-ref s n)
236234
(define cache (series-cache s))
237235
(define builder (series-f s))
238-
(define fetch (λ (i) (dvector-ref cache i)))
236+
(define (fetch i)
237+
(dvector-ref cache i))
239238
(when (>= n (dvector-length cache))
240239
(for ([i (in-range (dvector-length cache) (add1 n))])
241240
(define value (reducer (adder (builder fetch i))))
242241
(dvector-set! cache i value)))
243242
(dvector-ref cache n))
244243

245-
(define (series-function s)
246-
(λ (n) (series-ref s n)))
244+
(define ((series-function s) n)
245+
(series-ref s n))
247246

248247
(define (taylor-add left right)
249248
;(-> term? term? term?)
@@ -331,23 +330,24 @@
331330
(define offset (series-offset normalized))
332331
(define coeffs (series-function normalized))
333332
(define offset* (+ offset (modulo (- offset) n)))
334-
(if (= offset offset*)
335-
normalized
336-
(let ([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)))))))
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))))]))
351351

352352
(define (taylor-sqrt var num)
353353
;(-> symbol? term? term?)
@@ -423,56 +423,62 @@
423423
;(-> (-> number? batchref?) term?)
424424
(make-series 0
425425
(λ (f n)
426-
(if (zero? n)
427-
`(exp ,(coeffs 0))
428-
(let* ([coeffs* (list->vector (map coeffs (range 1 (+ n 1))))]
429-
[nums (for/list ([i (in-range 1 (+ n 1))]
430-
[coeff (in-vector coeffs*)]
431-
#:unless (equal? (deref coeff) 0))
432-
i)])
433-
`(* (exp ,(coeffs 0))
434-
(+ ,@(for/list ([p (all-partitions n (sort nums >))])
435-
`(* ,@(for/list ([(count num) (in-dict p)])
436-
`(/ (pow ,(vector-ref coeffs* (- num 1)) ,count)
437-
,(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)))))))]))))
438440

439441
(define (taylor-sin coeffs)
440442
;(-> (-> number? batchref?) term?)
441443
(make-series 0
442444
(λ (f n)
443-
(if (zero? n)
444-
0
445-
(let* ([coeffs* (list->vector (map coeffs (range 1 (+ n 1))))]
446-
[nums (for/list ([i (in-range 1 (+ n 1))]
447-
[coeff (in-vector coeffs*)]
448-
#:unless (equal? (deref coeff) 0))
449-
i)])
450-
`(+ ,@(for/list ([p (all-partitions n (sort nums >))])
451-
(if (= (modulo (apply + (map car p)) 2) 1)
452-
`(* ,(if (= (modulo (apply + (map car p)) 4) 1) 1 -1)
453-
,@(for/list ([(count num) (in-dict p)])
454-
`(/ (pow ,(vector-ref coeffs* (- num 1)) ,count)
455-
,(factorial count))))
456-
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)))]))))
457461

458462
(define (taylor-cos coeffs)
459463
;(-> (-> number? batchref?) term?)
460464
(make-series 0
461465
(λ (f n)
462-
(if (zero? n)
463-
1
464-
(let* ([coeffs* (list->vector (map coeffs (range 1 (+ n 1))))]
465-
[nums (for/list ([i (in-range 1 (+ n 1))]
466-
[coeff (in-vector coeffs*)]
467-
#:unless (equal? (deref coeff) 0))
468-
i)])
469-
`(+ ,@(for/list ([p (all-partitions n (sort nums >))])
470-
(if (= (modulo (apply + (map car p)) 2) 0)
471-
`(* ,(if (= (modulo (apply + (map car p)) 4) 0) 1 -1)
472-
,@(for/list ([(count num) (in-dict p)])
473-
`(/ (pow ,(vector-ref coeffs* (- num 1)) ,count)
474-
,(factorial count))))
475-
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)))]))))
476482

477483
;; This is a hyper-specialized symbolic differentiator for log(f(x))
478484

@@ -522,19 +528,20 @@
522528
(define base
523529
(make-series 0
524530
(λ (f n)
525-
(if (zero? n)
526-
`(log ,(maybe-negate (coeffs 0)))
527-
(let ([tmpl (logcompute n)])
528-
`(/ (+ ,@(for/list ([term tmpl])
529-
(match-define `(,coeff ,k ,ps ...) term)
530-
`(* ,coeff
531-
(/ (* ,@(for/list ([i (in-naturals 1)]
532-
[p ps])
533-
(if (= p 0)
534-
1
535-
`(pow (* ,(factorial i) ,(coeffs i)) ,p))))
536-
(exp (* ,(- k) ,(f 0)))))))
537-
,(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))]))))
538545

539546
(if (zero? shift)
540547
base

0 commit comments

Comments
 (0)