Skip to content

Commit

Permalink
Deprecate @test_approx_eq_eps; delete Test.test_approx_eq_modphase
Browse files Browse the repository at this point in the history
Instead, define Test.test_approx_eq_modphase in the two files where
it is needed. This is a *very* specific function and certainly not
one that needs to live in Julia's standard library code.

Closes #4615.
  • Loading branch information
StefanKarpinski committed Jan 6, 2017
1 parent 554f224 commit 29ff301
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 35 deletions.
37 changes: 8 additions & 29 deletions base/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Test
export @test, @test_throws, @test_broken, @test_skip
export @testset
# Legacy approximate testing functions, yet to be included
export @test_approx_eq_eps, @inferred
export @inferred
export detect_ambiguities
export GenericString

Expand Down Expand Up @@ -953,6 +953,8 @@ end
#-----------------------------------------------------------------------
# Legacy approximate testing functions, yet to be included

# BEGIN TODO: deprecated in 0.6, delete in 1.0
# vvv
approx_full(x::AbstractArray) = x
approx_full(x::Number) = x
approx_full(x) = full(x)
Expand Down Expand Up @@ -999,8 +1001,11 @@ Test two floating point numbers `a` and `b` for equality taking into account
a margin of tolerance given by `tol`.
"""
macro test_approx_eq_eps(a, b, c)
Base.depwarn(string("@test_approx_eq_eps is deprecated, use `@test ", a, "", b, " atol=", c, "` instead"),
Symbol("@test_approx_eq_eps"))
:(test_approx_eq($(esc(a)), $(esc(b)), $(esc(c)), $(string(a)), $(string(b))))
end
export @test_approx_eq_eps

"""
@test_approx_eq(a, b)
Expand All @@ -1014,6 +1019,8 @@ macro test_approx_eq(a, b)
:(test_approx_eq($(esc(a)), $(esc(b)), $(string(a)), $(string(b))))
end
export @test_approx_eq
# ^^^
# END TODO

_args_and_call(args...; kwargs...) = (args[1:end-1], kwargs, args[end](args[1:end-1]...; kwargs...))
"""
Expand Down Expand Up @@ -1091,34 +1098,6 @@ macro inferred(ex)
end)
end

# Test approximate equality of vectors or columns of matrices modulo floating
# point roundoff and phase (sign) differences.
#
# This function is designed to test for equality between vectors of floating point
# numbers when the vectors are defined only up to a global phase or sign, such as
# normalized eigenvectors or singular vectors. The global phase is usually
# defined consistently, but may occasionally change due to small differences in
# floating point rounding noise or rounding modes, or through the use of
# different conventions in different algorithms. As a result, most tests checking
# such vectors have to detect and discard such overall phase differences.
#
# Inputs:
# a, b:: StridedVecOrMat to be compared
# err :: Default: m^3*(eps(S)+eps(T)), where m is the number of rows
#
# Raises an error if any columnwise vector norm exceeds err. Otherwise, returns
# nothing.
function test_approx_eq_modphase{S<:Real,T<:Real}(
a::StridedVecOrMat{S}, b::StridedVecOrMat{T}, err=nothing)
@test indices(a,1) == indices(b,1) && indices(a,2) == indices(b,2)
m = length(indices(a,1))
err === nothing && (err=m^3*(eps(S)+eps(T)))
for i in indices(a,2)
v1, v2 = a[:, i], b[:, i]
@test_approx_eq_eps min(abs(norm(v1-v2)),abs(norm(v1+v2))) 0.0 err
end
end

"""
detect_ambiguities(mod1, mod2...; imported=false)
Expand Down
35 changes: 32 additions & 3 deletions test/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
using Base.Test
import Base.LinAlg: BlasReal, BlasFloat

# Test approximate equality of vectors or columns of matrices modulo floating
# point roundoff and phase (sign) differences.
#
# This function is designed to test for equality between vectors of floating point
# numbers when the vectors are defined only up to a global phase or sign, such as
# normalized eigenvectors or singular vectors. The global phase is usually
# defined consistently, but may occasionally change due to small differences in
# floating point rounding noise or rounding modes, or through the use of
# different conventions in different algorithms. As a result, most tests checking
# such vectors have to detect and discard such overall phase differences.
#
# Inputs:
# a, b:: StridedVecOrMat to be compared
# err :: Default: m^3*(eps(S)+eps(T)), where m is the number of rows
#
# Raises an error if any columnwise vector norm exceeds err. Otherwise, returns
# nothing.
isdefined(:test_approx_eq_modphase) ||
function test_approx_eq_modphase{S<:Real,T<:Real}(
a::StridedVecOrMat{S}, b::StridedVecOrMat{T}, err=nothing)
@test indices(a,1) == indices(b,1) && indices(a,2) == indices(b,2)
m = length(indices(a,1))
err === nothing && (err=m^3*(eps(S)+eps(T)))
for i in indices(a,2)
v1, v2 = a[:, i], b[:, i]
@test min(abs(norm(v1-v2)),abs(norm(v1+v2))) 0.0 atol=err
end
end

n = 10 #Size of test matrix
srand(1)

Expand Down Expand Up @@ -182,7 +211,7 @@ srand(1)
d2, v2 = eig(map(elty<:Complex ? Complex128 : Float64,Tfull))
@test (isupper ? d1 : reverse(d1)) d2
if elty <: Real
Test.test_approx_eq_modphase(v1, isupper ? v2 : v2[:,n:-1:1])
test_approx_eq_modphase(v1, isupper ? v2 : v2[:,n:-1:1])
end
end
end
Expand All @@ -195,8 +224,8 @@ srand(1)
u2, d2, v2 = svd(T)
@test d1 d2
if elty <: Real
Test.test_approx_eq_modphase(u1, u2)
Test.test_approx_eq_modphase(v1, v2)
test_approx_eq_modphase(u1, u2)
test_approx_eq_modphase(v1, v2)
end
@test 0 vecnorm(u2*diagm(d2)*v2'-Tfull) atol=n*max(n^2*eps(relty),vecnorm(u1*diagm(d1)*v1'-Tfull))
@inferred svdvals(T)
Expand Down
35 changes: 32 additions & 3 deletions test/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@ module TridiagTest
using Base.Test
debug = false

# Test approximate equality of vectors or columns of matrices modulo floating
# point roundoff and phase (sign) differences.
#
# This function is designed to test for equality between vectors of floating point
# numbers when the vectors are defined only up to a global phase or sign, such as
# normalized eigenvectors or singular vectors. The global phase is usually
# defined consistently, but may occasionally change due to small differences in
# floating point rounding noise or rounding modes, or through the use of
# different conventions in different algorithms. As a result, most tests checking
# such vectors have to detect and discard such overall phase differences.
#
# Inputs:
# a, b:: StridedVecOrMat to be compared
# err :: Default: m^3*(eps(S)+eps(T)), where m is the number of rows
#
# Raises an error if any columnwise vector norm exceeds err. Otherwise, returns
# nothing.
isdefined(:test_approx_eq_modphase) ||
function test_approx_eq_modphase{S<:Real,T<:Real}(
a::StridedVecOrMat{S}, b::StridedVecOrMat{T}, err=nothing)
@test indices(a,1) == indices(b,1) && indices(a,2) == indices(b,2)
m = length(indices(a,1))
err === nothing && (err=m^3*(eps(S)+eps(T)))
for i in indices(a,2)
v1, v2 = a[:, i], b[:, i]
@test min(abs(norm(v1-v2)),abs(norm(v1+v2))) 0.0 atol=err
end
end

# basic tridiagonal operations
n = 5

Expand Down Expand Up @@ -138,7 +167,7 @@ for elty in (Float32, Float64, Complex64, Complex128, Int)
@test abs.(VT'Vecs) eye(elty, n)
@test eigvecs(Ts) == eigvecs(Fs)
#call to LAPACK.stein here
Test.test_approx_eq_modphase(eigvecs(Ts,eigvals(Ts)),eigvecs(Fs))
test_approx_eq_modphase(eigvecs(Ts,eigvals(Ts)),eigvecs(Fs))
elseif elty != Int
# check that undef is determined accurately even if type inference
# bails out due to the number of try/catch blocks in this code.
Expand Down Expand Up @@ -313,13 +342,13 @@ let n = 12 #Size of matrix problem to test
debug && println("stegr! call with index range")
F = eigfact(SymTridiagonal(a, b),1:2)
fF = eigfact(Symmetric(Array(SymTridiagonal(a, b))),1:2)
Test.test_approx_eq_modphase(F[:vectors], fF[:vectors])
test_approx_eq_modphase(F[:vectors], fF[:vectors])
@test F[:values] fF[:values]

debug && println("stegr! call with value range")
F = eigfact(SymTridiagonal(a, b),0.0,1.0)
fF = eigfact(Symmetric(Array(SymTridiagonal(a, b))),0.0,1.0)
Test.test_approx_eq_modphase(F[:vectors], fF[:vectors])
test_approx_eq_modphase(F[:vectors], fF[:vectors])
@test F[:values] fF[:values]
end

Expand Down

0 comments on commit 29ff301

Please sign in to comment.