1
1
module TestUtils
2
2
3
- const __ATOL = 1e-9
4
- const __RTOL = 1e-9
5
-
6
3
using Distances
7
4
using LinearAlgebra
8
5
using KernelFunctions
9
6
using Random
10
7
using Test
11
8
9
+ # default tolerance values for test_interface:
10
+ const __ATOL = sqrt (eps (Float64))
11
+ const __RTOL = sqrt (eps (Float64))
12
+ # ≈ 1.5e-8; chosen for no particular reason other than because it seems to
13
+ # satisfy our own test cases within KernelFunctions.jl
14
+
12
15
"""
13
16
test_interface(
14
17
k::Kernel,
15
18
x0::AbstractVector,
16
19
x1::AbstractVector,
17
20
x2::AbstractVector;
18
21
atol=__ATOL,
22
+ rtol=__RTOL,
19
23
)
20
24
21
25
Run various consistency checks on `k` at the inputs `x0`, `x1`, and `x2`.
22
26
`x0` and `x1` should be of the same length with different values, while `x0` and `x2` should
23
27
be of different lengths.
24
28
25
- test_interface([rng::AbstractRNG], k::Kernel, T::Type{<:AbstractVector}; atol=__ATOL)
29
+ These tests are intended to pick up on really substantial issues with a kernel implementation
30
+ (e.g. substantial asymmetry in the kernel matrix, large negative eigenvalues), rather than to
31
+ test the numerics in detail, which can be kernel-specific.
32
+ The default value of `__ATOL` and `__RTOL` is `sqrt(eps(Float64)) ≈ 1.5e-8`, which satisfied
33
+ this intention in the cases tested within KernelFunctions.jl itself.
34
+
35
+ test_interface([rng::AbstractRNG], k::Kernel, T::Type{<:Real}; atol=__ATOL, rtol=__RTOL)
26
36
27
- `test_interface` offers certain types of test data generation to make running these tests
28
- require less code for common input types. For example, `Vector{<:Real}`, `ColVecs{<:Real}`,
29
- and `RowVecs{<:Real}` are supported. For other input vector types, please provide the data
30
- manually.
37
+ `test_interface` offers automated test data generation for kernels whose inputs are reals.
38
+ This will run the tests for `Vector{T}`, `Vector{Vector{T}}`, `ColVecs{T}`, and `RowVecs{T}`.
39
+ For other input vector types, please provide the data manually.
31
40
"""
32
41
function test_interface (
33
42
k:: Kernel ,
@@ -50,11 +59,12 @@ function test_interface(
50
59
@test size (kernelmatrix (k, x0, x2)) == (length (x0), length (x2))
51
60
52
61
# Check that elementwise is consistent with pairwise.
53
- @test kernelmatrix_diag (k, x0, x1) ≈ diag (kernelmatrix (k, x0, x1)) atol = atol
62
+ @test kernelmatrix_diag (k, x0, x1) ≈ diag (kernelmatrix (k, x0, x1)) atol = atol rtol =
63
+ rtol
54
64
55
65
# Check additional binary elementwise properties for kernels.
56
66
@test kernelmatrix_diag (k, x0, x1) ≈ kernelmatrix_diag (k, x1, x0)
57
- @test kernelmatrix (k, x0, x2) ≈ kernelmatrix (k, x2, x0)' atol = atol
67
+ @test kernelmatrix (k, x0, x2) ≈ kernelmatrix (k, x2, x0)' atol = atol rtol = rtol
58
68
59
69
# Check that unary elementwise basically works.
60
70
@test kernelmatrix_diag (k, x0) isa AbstractVector
@@ -63,10 +73,10 @@ function test_interface(
63
73
# Check that unary pairwise basically works.
64
74
@test kernelmatrix (k, x0) isa AbstractMatrix
65
75
@test size (kernelmatrix (k, x0)) == (length (x0), length (x0))
66
- @test kernelmatrix (k, x0) ≈ kernelmatrix (k, x0)' atol = atol
76
+ @test kernelmatrix (k, x0) ≈ kernelmatrix (k, x0)' atol = atol rtol = rtol
67
77
68
78
# Check that unary elementwise is consistent with unary pairwise.
69
- @test kernelmatrix_diag (k, x0) ≈ diag (kernelmatrix (k, x0)) atol = atol
79
+ @test kernelmatrix_diag (k, x0) ≈ diag (kernelmatrix (k, x0)) atol = atol rtol = rtol
70
80
71
81
# Check that unary pairwise produces a positive definite matrix (approximately).
72
82
@test eigmin (Matrix (kernelmatrix (k, x0))) > - atol
0 commit comments