Skip to content

Commit f0908ed

Browse files
committed
Revision
Added tests Reformulated functions, and added lighter ones for real numbers as suggested in JuliaMath/SpecialFunctions.jl#407
1 parent f300f8f commit f0908ed

File tree

3 files changed

+104
-17
lines changed

3 files changed

+104
-17
lines changed

Project.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
name = "FresnelIntegrals"
22
uuid = "88497964-e39a-11e9-0fb5-b1bf0ffe80fe"
33
authors = ["Kiran Shila\n <[email protected]>"]
4-
version = "0.1.0"
4+
version = "0.1.1"
55

66
[deps]
7+
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
78
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
89

910
[compat]
1011
julia = "1"
1112

1213
[extras]
13-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1414
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
15+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1516

1617
[targets]
17-
test = ["Test","QuadGK"]
18+
test = ["Test", "QuadGK"]

src/FresnelIntegrals.jl

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,97 @@
11
module FresnelIntegrals
22
using SpecialFunctions
3-
export fresnelc
4-
export fresnels
5-
export fresnel
3+
using IrrationalConstants: sqrtπ
4+
export fresnelc, fresnels, fresnel
65

76
"""
8-
fresnelc(z)
9-
Calculates the Fresnel cosine integral for the number z for
10-
``C(z) = \\int_{0}^{z} \\cos{\\left(\\frac{\\pi t^2}{2}\\right)}dt``
7+
fresnelc(z::Number)
8+
9+
Calculate the normalized Fresnel cosine integral
10+
```math
11+
C(z) = \\int_{0}^{z} \\cos{\\left(\\frac{\\pi t^2}{2}\\right)} \\, \\mathrm{d}t
12+
```
13+
for the number ``z``.
1114
"""
12-
fresnelc(z::Number) = 0.25*(1-1im)*(1im*erf(0.5*(1-1im)*z*√(π)) + erf(0.5*(1+1im)*z*√(π)))
15+
function fresnelc(z::Number)
16+
x = (z * sqrtπ) / 2
17+
re_x, im_x = reim(x)
18+
a = (re_x + im_x) + (im_x - re_x) * im
19+
b = (re_x - im_x) + (im_x + re_x) * im
20+
re_erf_a, im_erf_a = reim(erf(a))
21+
re_erf_b, im_erf_b = reim(erf(b))
22+
re_y = (re_erf_a - im_erf_a + re_erf_b + im_erf_b) / 4
23+
im_y = (im_erf_a + re_erf_a - re_erf_b + im_erf_b) / 4
24+
y = re_y + im_y * im
25+
return y
26+
end
27+
function fresnelc(z::Real)
28+
x = (z * sqrtπ) / 2
29+
a = x + x * im
30+
re_erf_a, im_erf_a = reim(erf(a))
31+
y = (re_erf_a + im_erf_a) / 2
32+
return y
33+
end
1334

1435
"""
15-
fresnels(z)
16-
Calculates the Fresnel sine integral for the number z for
17-
``S(z) = \\int_{0}^{z} \\sin{\\left(\\frac{\\pi t^2}{2}\\right)}dt``
36+
fresnels(z::Number)
37+
38+
Calculate the normalized Fresnel sine integral
39+
```math
40+
S(z) = \\int_{0}^{z} \\sin{\\left(\\frac{\\pi t^2}{2}\\right)} \\, \\mathrm{d}t
41+
```
42+
for the number ``z``.
1843
"""
19-
fresnels(z::Number) = 0.25*(1+1im)*(-1im*erf(0.5*(1-1im)*z*√(π)) + erf(0.5*(1+1im)*z*√(π)))
44+
function fresnels(z::Number)
45+
x = (z * sqrtπ) / 2
46+
re_x, im_x = reim(x)
47+
a = (re_x + im_x) + (im_x - re_x) * im
48+
b = (re_x - im_x) + (im_x + re_x) * im
49+
re_erf_a, im_erf_a = reim(erf(a))
50+
re_erf_b, im_erf_b = reim(erf(b))
51+
re_y = (re_erf_a + im_erf_a + re_erf_b - im_erf_b) / 4
52+
im_y = (im_erf_a - re_erf_a + re_erf_b + im_erf_b) / 4
53+
y = re_y + im_y * im
54+
return y
55+
end
56+
function fresnels(z::Real)
57+
x = (z * sqrtπ) / 2
58+
a = x + x * im
59+
re_erf_a, im_erf_a = reim(erf(a))
60+
y = (re_erf_a - im_erf_a) / 2
61+
return y
62+
end
2063

2164
"""
22-
fresnel(z)
23-
Calculates the cosine and sine fresnel integrals
65+
fresnel(z::Number)
66+
67+
Calculate the normalized cosine and sine fresnel integrals.
68+
69+
See also [`fresnels`](@ref), [`fresnelc`](@ref).
2470
"""
25-
fresnel(z::Number) = (fresnelc(z),fresnels(z))
71+
function fresnel(z::Number)
72+
x = (z * sqrtπ) / 2
73+
re_x, im_x = reim(x)
74+
a = (re_x + im_x) + (im_x - re_x) * im
75+
b = (re_x - im_x) + (im_x + re_x) * im
76+
re_erf_a, im_erf_a = reim(erf(a))
77+
re_erf_b, im_erf_b = reim(erf(b))
78+
re_y_sin = (re_erf_a + im_erf_a + re_erf_b - im_erf_b) / 4
79+
im_y_sin = (im_erf_a - re_erf_a + re_erf_b + im_erf_b) / 4
80+
re_y_cos = (re_erf_a - im_erf_a + re_erf_b + im_erf_b) / 4
81+
im_y_cos = (im_erf_a + re_erf_a - re_erf_b + im_erf_b) / 4
82+
y_sin = re_y_sin + im_y_sin * im
83+
y_cos = re_y_cos + im_y_cos * im
84+
return (y_cos, y_sin)
85+
end
86+
function fresnel(z::Real)
87+
x = (z * sqrtπ) / 2
88+
a = x + x * im
89+
re_erf_a, im_erf_a = reim(erf(a))
90+
y_sin = (re_erf_a - im_erf_a) / 2
91+
y_cos = (re_erf_a + im_erf_a) / 2
92+
return (y_cos, y_sin)
93+
end
94+
95+
2696

2797
end # module

test/runtests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,20 @@ using QuadGK
1010
@test fresnels(z) quadgk(t->sin*t^2/2),0,z)[1]
1111
# Test just for code coverage 😄
1212
@test (fresnelc(z),fresnels(z)) == fresnel(z)
13+
14+
# Generate random real number
15+
z = randn(Float64)
16+
# Test by comparing to numeric solution
17+
@test fresnelc(z) quadgk(t->cos*t^2/2),0,z)[1]
18+
@test fresnels(z) quadgk(t->sin*t^2/2),0,z)[1]
19+
# Test just for code coverage 😄
20+
@test (fresnelc(z),fresnels(z)) == fresnel(z)
21+
22+
# Precise values come from WolframAlpha calculator
23+
# One could add more decimals and more tests if needed
24+
25+
@test fresnels(1.) 0.4382591473903
26+
@test fresnelc(1.) 0.7798934003768
27+
@test fresnels(sqrt(2)*im) -0.7139722140219*im
28+
@test fresnelc(sqrt(2)*im) 0.5288915951112*im
1329
end

0 commit comments

Comments
 (0)