Skip to content

Commit 067b183

Browse files
committed
Complex erf, start fresnel
1 parent 7f437e7 commit 067b183

File tree

2 files changed

+93
-49
lines changed

2 files changed

+93
-49
lines changed

math-lib/math/private/functions/erf.rkt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,48 @@ IEEE Transactions on Communications, 2000, vol 48, pp 529--532
198198

199199
;; ===================================================================================================
200200

201+
(: complex-erf (Number -> Number))
202+
(define 2π (* 2 pi))
203+
(define (complex-erf z)
204+
(cond
205+
[(<= (magnitude z) 8)
206+
(define nn 32)
207+
(define x (real-part z))
208+
(define y (imag-part z))
209+
(define k1 (* (/ 2 pi) (exp (* -1 x x))))
210+
(define k2 (exp (* -i 2 x y)))
211+
(define f (+ (erf x)
212+
(if (= x 0)
213+
(* (/ +i pi) y)
214+
(* (/ k1 (* 4 x)) (- 1 k2)))))
215+
(cond
216+
[(= y 0) f]
217+
[else
218+
(define s5
219+
(for/fold : Number
220+
([s5 : Number 0])
221+
([n (in-range 1 (+ nn 1))])
222+
(define s3 (/ (exp (* -1 n n 1/4)) (+ (* n n) (* 4 x x))))
223+
(define s4 (- (* 2 x) (* k2 (- (* 2 x (cosh (* n y))) (* +i n (sinh (* n y)))))))
224+
(+ s5 (* s3 s4))))
225+
(+ f (* k1 s5))])]
226+
[else
227+
(define neg? (< (real-part z) 0))
228+
(define z+ (if neg? (- z) z))
229+
(define nmax 193)
230+
(define y (* 2 z z))
231+
(define s (for/fold : Number
232+
([s : Number 1])
233+
([n (in-range nmax 0 -2)])
234+
(- 1 (* n (/ s y)))))
235+
(define f (* (if neg? -1 1)
236+
(- 1 (* s (/ (exp (* -1 z z)) (* sqrtpi z))))))
237+
(if (= (real-part z) 0)
238+
(- f 1)
239+
f)]))
240+
241+
;; ===================================================================================================
242+
201243
(: erf (case-> (Zero -> Zero)
202244
(Flonum -> Flonum)
203245
(Real -> (U Zero Flonum))))

math-lib/math/private/functions/fresnel.rkt

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,50 +27,52 @@ would like to extend this together with erf to the complex plane
2727

2828
;------------------------------
2929
;polinomials for the rational assymptotic aproximation for S & C
30-
(define fn (make-flpolyfun ( 3.76329711269987889006E-20
31-
1.34283276233062758925E-16
32-
1.72010743268161828879E-13
33-
1.02304514164907233465E-10
34-
3.05568983790257605827E-8
35-
4.63613749287867322088E-6
36-
3.45017939782574027900E-4
37-
1.15220955073585758835E-2
38-
1.43407919780758885261E-1
39-
4.21543555043677546506E-1)))
40-
(define fd (make-flpolyfun ( 1.25443237090011264384E-20
41-
4.52001434074129701496E-17
42-
5.88754533621578410010E-14
43-
3.60140029589371370404E-11
44-
1.12699224763999035261E-8
45-
1.84627567348930545870E-6
46-
1.55934409164153020873E-4
47-
6.44051526508858611005E-3
48-
1.16888925859191382142E-1
49-
7.51586398353378947175E-1
50-
1.0)))
51-
(define gn (make-flpolyfun ( 1.86958710162783235106E-22
52-
8.36354435630677421531E-19
53-
1.37555460633261799868E-15
54-
1.08268041139020870318E-12
55-
4.45344415861750144738E-10
56-
9.82852443688422223854E-8
57-
1.15138826111884280931E-5
58-
6.84079380915393090172E-4
59-
1.87648584092575249293E-2
60-
1.97102833525523411709E-1
61-
5.04442073643383265887E-1)))
62-
(define gd (make-flpolyfun ( 1.86958710162783236342E-22
63-
8.39158816283118707363E-19
64-
1.38796531259578871258E-15
65-
1.10273215066240270757E-12
66-
4.60680728146520428211E-10
67-
1.04314589657571990585E-7
68-
1.27545075667729118702E-5
69-
8.14679107184306179049E-4
70-
2.53603741420338795122E-2
71-
3.37748989120019970451E-1
72-
1.47495759925128324529E0
73-
1.0)))
30+
(define fn/d (make-quotient-flpolyfun
31+
( 3.76329711269987889006E-20
32+
1.34283276233062758925E-16
33+
1.72010743268161828879E-13
34+
1.02304514164907233465E-10
35+
3.05568983790257605827E-8
36+
4.63613749287867322088E-6
37+
3.45017939782574027900E-4
38+
1.15220955073585758835E-2
39+
1.43407919780758885261E-1
40+
4.21543555043677546506E-1)
41+
( 1.25443237090011264384E-20
42+
4.52001434074129701496E-17
43+
5.88754533621578410010E-14
44+
3.60140029589371370404E-11
45+
1.12699224763999035261E-8
46+
1.84627567348930545870E-6
47+
1.55934409164153020873E-4
48+
6.44051526508858611005E-3
49+
1.16888925859191382142E-1
50+
7.51586398353378947175E-1
51+
1.0)))
52+
(define gn/d (make-quotient-flpolyfun
53+
( 1.86958710162783235106E-22
54+
8.36354435630677421531E-19
55+
1.37555460633261799868E-15
56+
1.08268041139020870318E-12
57+
4.45344415861750144738E-10
58+
9.82852443688422223854E-8
59+
1.15138826111884280931E-5
60+
6.84079380915393090172E-4
61+
1.87648584092575249293E-2
62+
1.97102833525523411709E-1
63+
5.04442073643383265887E-1)
64+
( 1.86958710162783236342E-22
65+
8.39158816283118707363E-19
66+
1.38796531259578871258E-15
67+
1.10273215066240270757E-12
68+
4.60680728146520428211E-10
69+
1.04314589657571990585E-7
70+
1.27545075667729118702E-5
71+
8.14679107184306179049E-4
72+
2.53603741420338795122E-2
73+
3.37748989120019970451E-1
74+
1.47495759925128324529E0
75+
1.0)))
7476

7577
;------------------------------
7678
;polinomials for the rational powerseries aproximation for S
@@ -100,8 +102,8 @@ would like to extend this together with erf to the complex plane
100102
(define t (fl* pi (fl* x x)))
101103
(define t/2 (fl/ t 2.0))
102104
(define U (fl/ 1.0 (fl* t t)))
103-
(define f (fl- 1.0 (fl* U (fl/ (fn U)(fd U)))))
104-
(define g (fl/ (fl/ (gn U)(gd U)) t))
105+
(define f (fl- 1.0 (fl* U (fn/d U))))
106+
(define g (fl/ (gn/d U) t))
105107
(fl- 0.5 (fl/ (fl+ (fl* f (flcos t/2))(fl* g (flsin t/2)))
106108
(fl* pi x)))]))
107109

@@ -133,8 +135,8 @@ would like to extend this together with erf to the complex plane
133135
(define t (fl* pi (fl* x x)))
134136
(define t/2 (fl/ t 2.0))
135137
(define U (fl/ 1.0 (fl* t t)))
136-
(define f (fl- 1.0 (fl* U (fl/ (fn U)(fd U)))))
137-
(define g (fl/ (fl/ (gn U)(gd U)) t))
138+
(define f (fl- 1.0 (fl* U (fn/d U))))
139+
(define g (fl/ (gn/d U) t))
138140
(fl+ 0.5 (fl/ (fl- (fl* f (flsin t/2))(fl* g (flcos t/2)))
139141
(fl* pi x)))]))
140142

@@ -207,7 +209,7 @@ would like to extend this together with erf to the complex plane
207209
(bfFresnel-RC (bf* (bfsqrt (bf/ pi.bf (bf 2))) x) maxp)))
208210
)
209211

210-
#;(module* test racket/base
212+
(module* test racket/base
211213
;some functions to check the function.
212214
;commented out, because (partly) put in function-tests from math-test
213215
(require math/bigfloat

0 commit comments

Comments
 (0)