This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmath.jl
231 lines (204 loc) · 8.65 KB
/
math.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# frexp,ldexp,significand,exponent
@test frexp(12.8) == (0.8,4)
@test ldexp(0.8,4) == 12.8
@test significand(12.8) == 1.6
@test exponent(12.8) == 3
# degree-based trig functions
for T = (Float32,Float64)
for x = -400:40:400
@test_approx_eq_eps sind(convert(T,x))::T convert(T,sin(pi/180*x)) eps(deg2rad(convert(T,x)))
@test_approx_eq_eps cosd(convert(T,x))::T convert(T,cos(pi/180*x)) eps(deg2rad(convert(T,x)))
end
for x = 0.0:180:720
@test sind(convert(T,x)) === zero(T)
@test sind(-convert(T,x)) === -zero(T)
end
for x = -3:0.3:3
@test_approx_eq_eps sinpi(convert(T,x))::T convert(T,sin(pi*x)) eps(pi*convert(T,x))
@test_approx_eq_eps cospi(convert(T,x))::T convert(T,cos(pi*x)) eps(pi*convert(T,x))
end
for x = 0.0:1.0:4.0
@test sinpi(convert(T,x)) === zero(T)
@test sinpi(-convert(T,x)) === -zero(T)
end
end
# check type stability
for T = (Float32,Float64,BigFloat)
for f = (sind,cosd,sinpi,cospi)
@test Base.return_types(f,(T,)) == [T]
end
end
# error functions
@test_approx_eq erf(1) 0.84270079294971486934
@test_approx_eq erfc(1) 0.15729920705028513066
@test_approx_eq erfcx(1) 0.42758357615580700442
@test_approx_eq erfi(1) 1.6504257587975428760
@test_approx_eq erfinv(0.84270079294971486934) 1
@test_approx_eq erfcinv(0.15729920705028513066) 1
@test_approx_eq dawson(1) 0.53807950691276841914
# TODO: complex versions only supported on 64-bit for now
@unix_only if WORD_SIZE==64
@test_approx_eq erf(1+2im) -0.53664356577856503399-5.0491437034470346695im
@test_approx_eq erfc(1+2im) 1.5366435657785650340+5.0491437034470346695im
@test_approx_eq erfcx(1+2im) 0.14023958136627794370-0.22221344017989910261im
@test_approx_eq erfi(1+2im) -0.011259006028815025076+1.0036063427256517509im
@test_approx_eq dawson(1+2im) -13.388927316482919244-11.828715103889593303im
end
for x in logspace(-200, -0.01)
@test_approx_eq_eps erf(erfinv(x)) x 1e-12*x
@test_approx_eq_eps erf(erfinv(-x)) -x 1e-12*x
@test_approx_eq_eps erfc(erfcinv(2*x)) 2*x 1e-12*x
if x > 1e-20
xf = float32(x)
@test_approx_eq_eps erf(erfinv(xf)) xf 1e-5*xf
@test_approx_eq_eps erf(erfinv(-xf)) -xf 1e-5*xf
@test_approx_eq_eps erfc(erfcinv(2xf)) 2xf 1e-5*xf
end
end
# airy
@test_approx_eq airy(1.8) 0.0470362168668458052247
@test_approx_eq airyprime(1.8) -0.0685247801186109345638
@test_approx_eq airybi(1.8) 2.595869356743906290060
@test_approx_eq airybiprime(1.8) 2.98554005084659907283
@test_throws airy(200im)
@test_throws airybi(200)
# besselh
true_h133 = 0.30906272225525164362 - 0.53854161610503161800im
@test_approx_eq besselh(3,1,3) true_h133
@test_approx_eq besselh(-3,1,3) -true_h133
@test_approx_eq besselh(3,2,3) conj(true_h133)
@test_approx_eq besselh(-3,2,3) -conj(true_h133)
@test_throws besselh(1,0)
# besseli
true_i33 = 0.95975362949600785698
@test_approx_eq besseli(3,3) true_i33
@test_approx_eq besseli(-3,3) true_i33
@test_approx_eq besseli(3,-3) -true_i33
@test_approx_eq besseli(-3,-3) -true_i33
@test_throws besseli(1,1000)
# besselj
@test besselj(0,0) == 1
for i = 1:5
@test besselj(i,0) == 0
@test besselj(-i,0) == 0
end
j33 = besselj(3,3.)
@test besselj(3,3) == j33
@test besselj(-3,-3) == j33
@test besselj(-3,3) == -j33
@test besselj(3,-3) == -j33
j43 = besselj(4,3.)
@test besselj(4,3) == j43
@test besselj(-4,-3) == j43
@test besselj(-4,3) == j43
@test besselj(4,-3) == j43
@test_approx_eq j33 0.30906272225525164362
@test_approx_eq j43 0.13203418392461221033
@test_throws besselj(0.1, -0.4)
@test_approx_eq besselj(0.1, complex(-0.4)) 0.820421842809028916 + 0.266571215948350899im
@test_approx_eq besselj(3.2, 1.3+0.6im) 0.01135309305831220201 + 0.03927719044393515275im
@test_approx_eq besselj(1, 3im) 3.953370217402609396im
@test_throws besselj(20,1000im)
# besselk
true_k33 = 0.12217037575718356792
@test_approx_eq besselk(3,3) true_k33
@test_approx_eq besselk(-3,3) true_k33
true_k3m3 = -0.1221703757571835679 - 3.0151549516807985776im
@test_throws besselk(3,-3)
@test_approx_eq besselk(3,complex(-3)) true_k3m3
@test_approx_eq besselk(-3,complex(-3)) true_k3m3
@test_throws besselk(200,0.01)
# bessely
y33 = bessely(3,3.)
@test bessely(3,3) == y33
@test_approx_eq bessely(-3,3) -y33
@test_approx_eq y33 -0.53854161610503161800
@test_throws bessely(3,-3)
@test_approx_eq bessely(3,complex(-3)) 0.53854161610503161800 - 0.61812544451050328724im
@test_throws bessely(200.5,0.1)
# beta, lbeta
@test_approx_eq beta(3/2,7/2) 5π/128
@test_approx_eq beta(3,5) 1/105
@test_approx_eq lbeta(5,4) log(beta(5,4))
@test_approx_eq beta(5,4) beta(4,5)
@test_approx_eq beta(-1/2, 3) -16/3
@test_approx_eq lbeta(-1/2, 3) log(16/3)
# gamma, lgamma (complex argument)
@test_approx_eq gamma(1/2) sqrt(π)
@test_approx_eq gamma(-1/2) -2sqrt(π)
@test_approx_eq lgamma(-1/2) log(abs(gamma(-1/2)))
@test_approx_eq lgamma(1.4+3.7im) -3.7094025330996841898 + 2.4568090502768651184im
@test_approx_eq lgamma(1.4+3.7im) log(gamma(1.4+3.7im))
# digamma
for elty in (Float32, Float64)
@test_approx_eq digamma(convert(elty, 9)) convert(elty, 2.140641477955609996536345)
@test_approx_eq digamma(convert(elty, 2.5)) convert(elty, 0.7031566406452431872257)
@test_approx_eq digamma(convert(elty, 0.1)) convert(elty, -10.42375494041107679516822)
@test_approx_eq digamma(convert(elty, 7e-4)) convert(elty, -1429.147493371120205005198)
@test_approx_eq digamma(convert(elty, 7e-5)) convert(elty, -14286.29138623969227538398)
@test_approx_eq digamma(convert(elty, 7e-6)) convert(elty, -142857.7200612932791081972)
@test_approx_eq digamma(convert(elty, 2e-6)) convert(elty, -500000.5772123750382073831)
@test_approx_eq digamma(convert(elty, 1e-6)) convert(elty, -1000000.577214019968668068)
@test_approx_eq digamma(convert(elty, 7e-7)) convert(elty, -1428572.005785942019703646)
@test_approx_eq digamma(convert(elty, -0.5)) convert(elty, .03648997397857652055902367)
@test_approx_eq digamma(convert(elty, -1.1)) convert(elty, 10.15416395914385769902271)
@test_approx_eq digamma(convert(elty, 0.1)) convert(elty, -10.42375494041108)
@test_approx_eq digamma(convert(elty, 1/2)) convert(elty, -γ - log(4))
@test_approx_eq digamma(convert(elty, 1)) convert(elty, -γ)
@test_approx_eq digamma(convert(elty, 2)) convert(elty, 1 - γ)
@test_approx_eq digamma(convert(elty, 3)) convert(elty, 3/2 - γ)
@test_approx_eq digamma(convert(elty, 4)) convert(elty, 11/6 - γ)
@test_approx_eq digamma(convert(elty, 5)) convert(elty, 25/12 - γ)
@test_approx_eq digamma(convert(elty, 10)) convert(elty, 7129/2520 - γ)
end
# trigamma
for elty in (Float32, Float64)
@test_approx_eq trigamma(convert(elty, 0.1)) convert(elty, 101.433299150792758817)
@test_approx_eq trigamma(convert(elty, 1/2)) convert(elty, π^2/2)
@test_approx_eq trigamma(convert(elty, 1)) convert(elty, π^2/6)
@test_approx_eq trigamma(convert(elty, 2)) convert(elty, π^2/6 - 1)
@test_approx_eq trigamma(convert(elty, 3)) convert(elty, π^2/6 - 5/4)
@test_approx_eq trigamma(convert(elty, 4)) convert(elty, π^2/6 - 49/36)
@test_approx_eq trigamma(convert(elty, 5)) convert(elty, π^2/6 - 205/144)
@test_approx_eq trigamma(convert(elty, 10)) convert(elty, π^2/6 - 9778141/6350400)
end
# invdigamma
for elty in (Float32, Float64)
for val in [0.001, 0.01, 0.1, 1.0, 10.0]
@test abs(invdigamma(digamma(convert(elty, val))) - convert(elty, val)) < 1e-8
end
end
# eta, zeta
@test_approx_eq eta(1) log(2)
@test_approx_eq eta(2) pi^2/12
@test_approx_eq zeta(0) -0.5
@test_approx_eq zeta(2) pi^2/6
@test_approx_eq zeta(4) pi^4/90
# quadgk
@test_approx_eq quadgk(cos, 0,0.7,1)[1] sin(1)
@test_approx_eq quadgk(x -> exp(im*x), 0,0.7,1)[1] (exp(1im)-1)/im
@test_approx_eq quadgk(x -> exp(im*x), 0,1im)[1] -1im*expm1(-1)
@test_approx_eq_eps quadgk(cos, 0,BigFloat(1),order=40)[1] sin(BigFloat(1)) 1000*eps(BigFloat)
@test_approx_eq quadgk(x -> exp(-x), 0,0.7,Inf)[1] 1.0
@test_approx_eq quadgk(x -> exp(x), -Inf,0)[1] 1.0
@test_approx_eq quadgk(x -> exp(-x^2), -Inf,Inf)[1] sqrt(pi)
@test_approx_eq quadgk(x -> [exp(-x), exp(-2x)], 0, Inf)[1] [1,0.5]
@test_approx_eq quadgk(cos, 0,0.7,1, norm=abs)[1] sin(1)
# Ensure subnormal flags functions don't segfault
@test any(ccall("jl_zero_subnormals", Uint8, (Uint8,), 1) .== [0x00 0x01])
@test any(ccall("jl_zero_subnormals", Uint8, (Uint8,), 0) .== [0x00 0x01])
# isqrt (issue #4884)
@test isqrt(9223372030926249000) == 3037000498
@test isqrt(typemax(Int128)) == int128("13043817825332782212")
@test isqrt(int128(typemax(Int64))^2-1) == 9223372036854775806
@test isqrt(0) == 0
for i = 1:1000
n = rand(Uint128)
s = isqrt(n)
@test s*s <= n
@test (s+1)*(s+1) > n
n = rand(Uint64)
s = isqrt(n)
@test s*s <= n
@test (s+1)*(s+1) > n
end