Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Thatcher committed Aug 6, 2016
1 parent a8b2a6a commit 71ee5ff
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/interface.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ Pairwise Computation
\qquad \text{where} \quad
\mathbf{\mu}_\phi = \frac{1}{n} \sum_{i=1}^n \phi(\mathbf{x}_i)

----------------
Kernel Centering
----------------

.. function:: centerkernelmatrix(X)

Same as ``centerkernelmatrix!`` but makes a copy of ``X``.
Expand Down
11 changes: 11 additions & 0 deletions src/pairwise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ function pairwise{T1<:AbstractFloat,T2<:Real,T3<:Real}(f::RealFunction{T1}, x::T
pairwise(convert(RealFunction{T}, f), convert(T, x), convert(T, y))
end

function unsafe_pairwise{T1<:AbstractFloat,T2<:Real,T3<:Real}(
f::RealFunction{T1},
x::AbstractArray{T2},
y::AbstractArray{T3}
)
T = promote_type(T1, T2, T3)
u = convert(AbstractArray{T}, x)
v = convert(AbstractArray{T}, y)
unsafe_pairwise(convert(RealFunction{T}, f), u, v)
end

function pairwise{T1<:AbstractFloat,T2<:Real,T3<:Real}(
f::RealFunction{T1},
x::AbstractArray{T2},
Expand Down
33 changes: 33 additions & 0 deletions test/pairwise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,49 @@ for T in FloatingPointTypes
x = rand(T,p)
y = rand(T,p)

x_U = convert(Vector{Float64}, x)
y_U = convert(Vector{Float32}, y)
U = promote_type(T, Float64)

for f_test in test_pairwise_functions
f = convert(RealFunction{T}, f_test)
s = MOD.pairwise_initiate(f)
for i in eachindex(y,x)
s = MOD.pairwise_aggregate(f, s, x[i], y[i])
end
@test MOD.unsafe_pairwise(f, x, y) == MOD.pairwise_return(f, s)

@test typeof(MOD.unsafe_pairwise(f, x_U, y_U)) == U
end

for h_test in test_composite_functions
h = convert(RealFunction{T}, h_test)
s = MOD.composition(h.g, MOD.unsafe_pairwise(h.f, x, y))
@test MOD.unsafe_pairwise(h, x, y) == s

@test typeof(MOD.unsafe_pairwise(h, x_U, y_U)) == U
end

for f_test in test_sample
h = convert(RealFunction{T}, 2*f_test+1)
s = h.a*MOD.unsafe_pairwise(h.f, x, y) + h.c
@test MOD.unsafe_pairwise(h, x, y) == s

@test typeof(MOD.unsafe_pairwise(h, x_U, y_U)) == U
end

for scalar_op in (+, *), f_test1 in test_sample, f_test2 in test_sample
h = convert(RealFunction{T}, (scalar_op)(f_test1, f_test2))
s = (scalar_op)(MOD.unsafe_pairwise(h.f, x, y), MOD.unsafe_pairwise(h.g, x, y))
@test MOD.unsafe_pairwise(h, x, y) == s

@test typeof(MOD.unsafe_pairwise(h, x_U, y_U)) == U

h = convert(RealFunction{T}, (scalar_op)(2*f_test1+1, f_test2))
s = (scalar_op)(MOD.unsafe_pairwise(h.f, x, y), MOD.unsafe_pairwise(h.g, x, y))
@test MOD.unsafe_pairwise(h, x, y) == s

@test typeof(MOD.unsafe_pairwise(h, x_U, y_U)) == U
end
end

Expand All @@ -48,15 +62,23 @@ for T in FloatingPointTypes
x = rand(T,p)
y = rand(T,p)

x_U = convert(Vector{Float64}, x)
y_U = convert(Vector{Float32}, y)
U = promote_type(T, Float64)

for f_test in test_pairwise_functions
f = convert(RealFunction{T}, f_test)

s1 = MOD.pairwise_return(f, MOD.pairwise_aggregate(f, MOD.pairwise_initiate(f), x[1], y[1]))
@test MOD.pairwise(f, x[1], y[1]) == s1

@test typeof(MOD.pairwise(f, x_U[1], y_U[1])) == U

s2 = MOD.unsafe_pairwise(f, x, y)
@test MOD.pairwise(f, x, y) == s2

@test typeof(MOD.pairwise(f, x_U, y_U)) == U

@test_throws DimensionMismatch MOD.pairwise(f, Array(T,p), Array(T,p+1))
@test_throws DimensionMismatch MOD.pairwise(f, Array(T,p+1), Array(T,p))
end
Expand All @@ -65,8 +87,13 @@ for T in FloatingPointTypes
h = convert(RealFunction{T}, h_test)

@test MOD.pairwise(h, x[1], y[1]) == MOD.composition(h.g, MOD.pairwise(h.f, x[1], y[1]))

@test typeof(MOD.pairwise(h, x_U[1], y_U[1])) == U

@test MOD.pairwise(h, x, y) == MOD.composition(h.g, MOD.unsafe_pairwise(h.f, x, y))

@test typeof(MOD.pairwise(h, x_U, y_U)) == U

@test_throws DimensionMismatch MOD.pairwise(h, Array(T,p), Array(T,p+1))
@test_throws DimensionMismatch MOD.pairwise(h, Array(T,p+1), Array(T,p))
end
Expand All @@ -82,6 +109,8 @@ for T in FloatingPointTypes

@test_throws DimensionMismatch MOD.pairwise(h, Array(T,p), Array(T,p+1))
@test_throws DimensionMismatch MOD.pairwise(h, Array(T,p+1), Array(T,p))

@test typeof(MOD.pairwise(h, x_U, y_U)) == U
end

for scalar_op in (+, *), f_test1 in test_sample, f_test2 in test_sample
Expand All @@ -90,9 +119,13 @@ for T in FloatingPointTypes
s1 = (scalar_op)(MOD.pairwise(h.f, x[1], y[1]), MOD.pairwise(h.g, x[1], y[1]))
@test MOD.pairwise(h, x[1], y[1]) == s1

@test typeof(MOD.pairwise(h, x_U[1], y_U[1])) == U

s2 = (scalar_op)(MOD.pairwise(h.f, x, y), MOD.pairwise(h.g, x, y))
@test MOD.pairwise(h, x, y) == s2

@test typeof(MOD.pairwise(h, x_U, y_U)) == U

@test_throws DimensionMismatch MOD.pairwise(h, Array(T,p), Array(T,p+1))
@test_throws DimensionMismatch MOD.pairwise(h, Array(T,p+1), Array(T,p))
end
Expand Down

0 comments on commit 71ee5ff

Please sign in to comment.