Skip to content

Commit 478461a

Browse files
committed
Merge pull request #12121 from JuliaLang/ksh/mathtests
More special, eigen, and bitarray tests
2 parents 3dce975 + c9d813c commit 478461a

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

base/linalg/eigen.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,21 @@ function eigvals{T<:Number}(x::T; kwargs...)
8787
return imag(val) == 0 ? [real(val)] : [val]
8888
end
8989

90+
# TO DO: Put message about not being able to sort complex numbers back in!
9091
#Computes maximum and minimum eigenvalue
9192
function eigmax(A::Union{Number, StridedMatrix}; permute::Bool=true, scale::Bool=true)
9293
v = eigvals(A, permute = permute, scale = scale)
93-
iseltype(v,Complex) ? error("DomainError: complex eigenvalues cannot be ordered") : maximum(v)
94+
if iseltype(v,Complex)
95+
throw(DomainError())
96+
end
97+
maximum(v)
9498
end
9599
function eigmin(A::Union{Number, StridedMatrix}; permute::Bool=true, scale::Bool=true)
96100
v = eigvals(A, permute = permute, scale = scale)
97-
iseltype(v,Complex) ? error("DomainError: complex eigenvalues cannot be ordered") : minimum(v)
101+
if iseltype(v,Complex)
102+
throw(DomainError())
103+
end
104+
minimum(v)
98105
end
99106

100107
inv(A::Eigen) = A.vectors * inv(Diagonal(A.values)) / A.vectors

test/bitarray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ b1 = triu(bitrand(n2, n1))
11681168
b1 = bitrand(n1,n1)
11691169
b1 |= b1.'
11701170
@check_bit_operation issym(b1) Bool
1171+
@check_bit_operation ishermitian(b1) Bool
11711172

11721173
b1 = bitrand(n1)
11731174
b2 = bitrand(n2)
@@ -1236,6 +1237,12 @@ uA = bitunpack(A)
12361237
@test svd(A) == svd(uA)
12371238
@test qr(A) == qr(uA)
12381239

1240+
#gradient
1241+
A = bitrand(10)
1242+
fA = bitunpack(A)
1243+
@test gradient(A) == gradient(fA)
1244+
@test gradient(A,1.0) == gradient(fA,1.0)
1245+
12391246
#diag and diagm
12401247

12411248
v = bitrand(10)

test/linalg1.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ debug && println("non-symmetric eigen decomposition")
149149

150150
num_fact = eigfact(one(eltya))
151151
@test num_fact.values[1] == one(eltya)
152+
h = a + a'
153+
@test_approx_eq minimum(eigvals(h)) eigmin(h)
154+
@test_approx_eq maximum(eigvals(h)) eigmax(h)
155+
@test_throws DomainError eigmin(a - a')
156+
@test_throws DomainError eigmax(a - a')
152157
end
153158

154159
debug && println("symmetric generalized eigenproblem")

test/math.jl

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,41 @@ end
106106
@test_approx_eq erfi(1+2im) -0.011259006028815025076+1.0036063427256517509im
107107
@test_approx_eq dawson(1+2im) -13.388927316482919244-11.828715103889593303im
108108

109-
for x in logspace(-200, -0.01)
110-
@test_approx_eq_eps erf(erfinv(x)) x 1e-12*x
111-
@test_approx_eq_eps erf(erfinv(-x)) -x 1e-12*x
112-
@test_approx_eq_eps erfc(erfcinv(2*x)) 2*x 1e-12*x
113-
if x > 1e-20
114-
xf = Float32(x)
115-
@test_approx_eq_eps erf(erfinv(xf)) xf 1e-5*xf
116-
@test_approx_eq_eps erf(erfinv(-xf)) -xf 1e-5*xf
117-
@test_approx_eq_eps erfc(erfcinv(2xf)) 2xf 1e-5*xf
109+
for elty in [Float32,Float64]
110+
for x in logspace(-200, -0.01)
111+
@test_approx_eq_eps erf(erfinv(x)) x 1e-12*x
112+
@test_approx_eq_eps erf(erfinv(-x)) -x 1e-12*x
113+
@test_approx_eq_eps erfc(erfcinv(2*x)) 2*x 1e-12*x
114+
if x > 1e-20
115+
xf = Float32(x)
116+
@test_approx_eq_eps erf(erfinv(xf)) xf 1e-5*xf
117+
@test_approx_eq_eps erf(erfinv(-xf)) -xf 1e-5*xf
118+
@test_approx_eq_eps erfc(erfcinv(2xf)) 2xf 1e-5*xf
119+
end
118120
end
121+
@test erfinv(one(elty)) == Inf
122+
@test erfinv(-one(elty)) == -Inf
123+
@test_throws DomainError erfinv(2.0*one(elty))
124+
125+
@test erfcinv(zero(elty)) == Inf
126+
@test_throws DomainError erfcinv(-one(elty))
119127
end
120128

129+
@test erfinv(one(Int)) == erfinv(1.0)
130+
@test erfcinv(one(Int)) == erfcinv(1.0)
131+
121132
# airy
122133
@test_approx_eq airy(1.8) 0.0470362168668458052247
134+
@test_approx_eq airy(1.8) airyai(1.8)
123135
@test_approx_eq airyprime(1.8) -0.0685247801186109345638
136+
@test_approx_eq airyaiprime(1.8) airyprime(1.8)
124137
@test_approx_eq airybi(1.8) 2.595869356743906290060
125138
@test_approx_eq airybiprime(1.8) 2.98554005084659907283
126139
@test_throws Base.Math.AmosException airy(200im)
127140
@test_throws Base.Math.AmosException airybi(200)
128141
@test_throws ArgumentError airy(5,one(Complex128))
129142
z = 1.8 + 1.0im
143+
@test_approx_eq airyx(z) airyx(0,z)
130144
@test_approx_eq airyx(0, z) airy(0, z) * exp(2/3 * z * sqrt(z))
131145
@test_approx_eq airyx(1, z) airy(1, z) * exp(2/3 * z * sqrt(z))
132146
@test_approx_eq airyx(2, z) airy(2, z) * exp(-abs(real(2/3 * z * sqrt(z))))
@@ -218,6 +232,7 @@ y33 = bessely(3,3.)
218232
@test_throws Base.Math.AmosException bessely(200.5,0.1)
219233
@test_throws DomainError bessely(0.4,-1.0)
220234
@test_throws DomainError bessely(0.4,Float32(-1.0))
235+
@test_throws DomainError bessely(1,Float32(-1.0))
221236

222237
# issue #6653
223238
for f in (besselj,bessely,besseli,besselk,hankelh1,hankelh2)
@@ -473,6 +488,14 @@ with_bigfloat_precision(10_000) do
473488
@test log(2,big(2)^400) == 400
474489
end
475490

491+
for T in (Float32,Float64)
492+
@test log(zero(T)) == -Inf
493+
@test isnan(log(NaN))
494+
@test_throws DomainError log(-one(T))
495+
@test log1p(-one(T)) == -Inf
496+
@test isnan(log1p(NaN))
497+
@test_throws DomainError log1p(-2*one(T))
498+
end
476499
# test vectorization of 2-arg vectorized functions
477500
binary_math_functions = [
478501
copysign, flipsign, log, atan2, hypot, max, min,

0 commit comments

Comments
 (0)