|
153 | 153 | (define node (deref brf)) |
154 | 154 | (match node |
155 | 155 | [(? (curry equal? var)) (taylor-exact (adder 0) (adder 1))] |
156 | | - [(? number?) (taylor-exact brf)] |
157 | | - [(? symbol?) (taylor-exact brf)] |
158 | | - [`(,const) (taylor-exact brf)] |
159 | | - [`(+ ,arg1 ,arg2) (taylor-add (recurse arg1) (recurse arg2))] |
160 | | - [`(neg ,arg) (taylor-negate (recurse arg))] |
161 | | - [`(* ,left ,right) (taylor-mult (recurse left) (recurse right))] |
162 | | - [`(/ ,num ,den) |
163 | | - #:when (equal? (deref num) 1) |
164 | | - (taylor-invert (recurse den))] |
165 | | - [`(/ ,num ,den) (taylor-quotient (recurse num) (recurse den))] |
166 | | - [`(sqrt ,arg) (taylor-sqrt var (recurse arg))] |
167 | | - [`(cbrt ,arg) (taylor-cbrt var (recurse arg))] |
168 | | - [`(fabs ,arg) (or (taylor-fabs var (recurse arg)) (taylor-exact brf))] |
| 156 | + [(? number?) (taylor-exact brf)] |
| 157 | + [(? symbol?) (taylor-exact brf)] |
| 158 | + [`(,const) (taylor-exact brf)] |
| 159 | + [`(+ ,arg1 ,arg2) (taylor-add (recurse arg1) (recurse arg2))] |
| 160 | + [`(neg ,arg) (taylor-negate (recurse arg))] |
| 161 | + [`(* ,left ,right) (taylor-mult (recurse left) (recurse right))] |
| 162 | + [`(/ ,num ,den) |
| 163 | + #:when (equal? (deref num) 1) |
| 164 | + (taylor-invert (recurse den))] |
| 165 | + [`(/ ,num ,den) (taylor-quotient (recurse num) (recurse den))] |
| 166 | + [`(sqrt ,arg) (taylor-sqrt var (recurse arg))] |
| 167 | + [`(cbrt ,arg) (taylor-cbrt var (recurse arg))] |
| 168 | + [`(fabs ,arg) (or (taylor-fabs var (recurse arg)) (taylor-exact brf))] |
169 | 169 | [`(exp ,arg) |
170 | 170 | (define arg* (normalize-series (recurse arg))) |
171 | 171 | (if (positive? (series-offset arg*)) |
|
174 | 174 | [`(sin ,arg) |
175 | 175 | (define arg* (normalize-series (recurse arg))) |
176 | 176 | (cond |
177 | | - [(positive? (series-offset arg*)) (taylor-exact brf)] |
178 | | - [(= (series-offset arg*) 0) |
179 | | - ; Our taylor-sin function assumes that a0 is 0, |
180 | | - ; because that way it is especially simple. We correct for this here |
181 | | - ; We use the identity sin (x + y) = sin x cos y + cos x sin y |
182 | | - (taylor-add (taylor-mult (taylor-exact (adder `(sin ,(series-ref arg* 0)))) |
183 | | - (taylor-cos (zero-series arg*))) |
184 | | - (taylor-mult (taylor-exact (adder `(cos ,(series-ref arg* 0)))) |
185 | | - (taylor-sin (zero-series arg*))))] |
186 | | - [else (taylor-sin (zero-series arg*))])] |
| 177 | + [(positive? (series-offset arg*)) (taylor-exact brf)] |
| 178 | + [(= (series-offset arg*) 0) |
| 179 | + ; Our taylor-sin function assumes that a0 is 0, |
| 180 | + ; because that way it is especially simple. We correct for this here |
| 181 | + ; We use the identity sin (x + y) = sin x cos y + cos x sin y |
| 182 | + (taylor-add (taylor-mult (taylor-exact (adder `(sin ,(series-ref arg* 0)))) |
| 183 | + (taylor-cos (zero-series arg*))) |
| 184 | + (taylor-mult (taylor-exact (adder `(cos ,(series-ref arg* 0)))) |
| 185 | + (taylor-sin (zero-series arg*))))] |
| 186 | + [else (taylor-sin (zero-series arg*))])] |
187 | 187 | [`(cos ,arg) |
188 | 188 | (define arg* (normalize-series (recurse arg))) |
189 | 189 | (cond |
190 | | - [(positive? (series-offset arg*)) (taylor-exact brf)] |
191 | | - [(= (series-offset arg*) 0) |
192 | | - ; Our taylor-cos function assumes that a0 is 0, |
193 | | - ; because that way it is especially simple. We correct for this here |
194 | | - ; We use the identity cos (x + y) = cos x cos y - sin x sin y |
195 | | - (taylor-add (taylor-mult (taylor-exact (adder `(cos ,(series-ref arg* 0)))) |
196 | | - (taylor-cos (zero-series arg*))) |
197 | | - (taylor-negate (taylor-mult (taylor-exact (adder `(sin ,(series-ref arg* 0)))) |
198 | | - (taylor-sin (zero-series arg*)))))] |
199 | | - [else (taylor-cos (zero-series arg*))])] |
| 190 | + [(positive? (series-offset arg*)) (taylor-exact brf)] |
| 191 | + [(= (series-offset arg*) 0) |
| 192 | + ; Our taylor-cos function assumes that a0 is 0, |
| 193 | + ; because that way it is especially simple. We correct for this here |
| 194 | + ; We use the identity cos (x + y) = cos x cos y - sin x sin y |
| 195 | + (taylor-add (taylor-mult (taylor-exact (adder `(cos ,(series-ref arg* 0)))) |
| 196 | + (taylor-cos (zero-series arg*))) |
| 197 | + (taylor-negate (taylor-mult (taylor-exact (adder `(sin ,(series-ref arg* 0)))) |
| 198 | + (taylor-sin (zero-series arg*)))))] |
| 199 | + [else (taylor-cos (zero-series arg*))])] |
200 | 200 | [`(log ,arg) (taylor-log var (recurse arg))] |
201 | 201 | [`(pow ,base ,power) |
202 | 202 | #:when (exact-integer? (deref power)) |
|
398 | 398 | (define a0 (deref (series-ref normalized 0))) |
399 | 399 | (cond |
400 | 400 | [(or (not (number? a0)) (zero? a0)) #f] |
401 | | - [(and (even? offset) (negative? a0)) |
402 | | - (taylor-negate normalized)] |
403 | | - [(and (even? offset) (positive? a0)) |
404 | | - normalized] |
| 401 | + [(and (even? offset) (negative? a0)) (taylor-negate normalized)] |
| 402 | + [(and (even? offset) (positive? a0)) normalized] |
405 | 403 | [(odd? offset) |
406 | | - (define scale-factor |
407 | | - (adder `(* (fabs ,var) ,(if (negative? a0) -1 1)))) |
| 404 | + (define scale-factor (adder `(* (fabs ,var) ,(if (negative? a0) -1 1)))) |
408 | 405 | (define new-offset (add1 offset)) |
409 | 406 | (make-series new-offset |
410 | 407 | (λ (f n) |
|
0 commit comments