Skip to content

Commit 05fe340

Browse files
authored
use only() instead of first() (#403)
* use only() instead of first() for 1-"vectors" that were for the benefit of Flux * fix one test that should not have worked as it was * add missing scalar Sinus constructor
1 parent 2d17212 commit 05fe340

File tree

18 files changed

+48
-43
lines changed

18 files changed

+48
-43
lines changed

src/basekernels/constant.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ end
7373

7474
@functor ConstantKernel
7575

76-
kappa::ConstantKernel, x::Real) = first.c) * one(x)
76+
kappa::ConstantKernel, x::Real) = only.c) * one(x)
7777

7878
metric(::ConstantKernel) = Delta()
7979

80-
Base.show(io::IO, κ::ConstantKernel) = print(io, "Constant Kernel (c = ", first.c), ")")
80+
Base.show(io::IO, κ::ConstantKernel) = print(io, "Constant Kernel (c = ", only.c), ")")

src/basekernels/exponential.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ end
137137

138138
@functor GammaExponentialKernel
139139

140-
kappa::GammaExponentialKernel, d::Real) = exp(-d^first.γ))
140+
kappa::GammaExponentialKernel, d::Real) = exp(-d^only.γ))
141141

142142
metric(k::GammaExponentialKernel) = k.metric
143143

144144
iskroncompatible(::GammaExponentialKernel) = true
145145

146146
function Base.show(io::IO, κ::GammaExponentialKernel)
147147
return print(
148-
io, "Gamma Exponential Kernel (γ = ", first.γ), ", metric = ", κ.metric, ")"
148+
io, "Gamma Exponential Kernel (γ = ", only.γ), ", metric = ", κ.metric, ")"
149149
)
150150
end

src/basekernels/fbm.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ function (κ::FBMKernel)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real})
2828
modX = sum(abs2, x)
2929
modY = sum(abs2, y)
3030
modXY = sqeuclidean(x, y)
31-
h = first.h)
31+
h = only.h)
3232
return (modX^h + modY^h - modXY^h) / 2
3333
end
3434

3535
function::FBMKernel)(x::Real, y::Real)
36-
return (abs2(x)^first.h) + abs2(y)^first.h) - abs2(x - y)^first.h)) / 2
36+
return (abs2(x)^only.h) + abs2(y)^only.h) - abs2(x - y)^only.h)) / 2
3737
end
3838

3939
function Base.show(io::IO, κ::FBMKernel)
40-
return print(io, "Fractional Brownian Motion Kernel (h = ", first.h), ")")
40+
return print(io, "Fractional Brownian Motion Kernel (h = ", only.h), ")")
4141
end
4242

4343
_fbm(modX, modY, modXY, h) = (modX^h + modY^h - modXY^h) / 2

src/basekernels/matern.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ MaternKernel(; nu::Real=1.5, ν::Real=nu, metric=Euclidean()) = MaternKernel(ν,
3434
@functor MaternKernel
3535

3636
@inline function kappa::MaternKernel, d::Real)
37-
result = _matern(first.ν), d)
37+
result = _matern(only.ν), d)
3838
return ifelse(iszero(d), one(result), result)
3939
end
4040

@@ -46,7 +46,7 @@ end
4646
metric(k::MaternKernel) = k.metric
4747

4848
function Base.show(io::IO, κ::MaternKernel)
49-
return print(io, "Matern Kernel (ν = ", first.ν), ", metric = ", κ.metric, ")")
49+
return print(io, "Matern Kernel (ν = ", only.ν), ", metric = ", κ.metric, ")")
5050
end
5151

5252
## Matern12Kernel = ExponentialKernel aliased in exponential.jl

src/basekernels/polynomial.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ LinearKernel(; c::Real=0.0) = LinearKernel(c)
2626

2727
@functor LinearKernel
2828

29-
kappa::LinearKernel, xᵀy::Real) = xᵀy + first.c)
29+
kappa::LinearKernel, xᵀy::Real) = xᵀy + only.c)
3030

3131
metric(::LinearKernel) = DotProduct()
3232

33-
Base.show(io::IO, κ::LinearKernel) = print(io, "Linear Kernel (c = ", first.c), ")")
33+
Base.show(io::IO, κ::LinearKernel) = print(io, "Linear Kernel (c = ", only.c), ")")
3434

3535
"""
3636
PolynomialKernel(; degree::Int=2, c::Real=0.0)
@@ -53,7 +53,7 @@ struct PolynomialKernel{Tc<:Real} <: SimpleKernel
5353

5454
function PolynomialKernel{Tc}(degree::Int, c::Vector{Tc}) where {Tc}
5555
@check_args(PolynomialKernel, degree, degree >= one(degree), "degree ≥ 1")
56-
@check_args(PolynomialKernel, c, first(c) >= zero(Tc), "c ≥ 0")
56+
@check_args(PolynomialKernel, c, only(c) >= zero(Tc), "c ≥ 0")
5757
return new{Tc}(degree, c)
5858
end
5959
end
@@ -68,10 +68,10 @@ function Functors.functor(::Type{<:PolynomialKernel}, x)
6868
return (c=x.c,), reconstruct_polynomialkernel
6969
end
7070

71-
kappa::PolynomialKernel, xᵀy::Real) = (xᵀy + first.c))^κ.degree
71+
kappa::PolynomialKernel, xᵀy::Real) = (xᵀy + only.c))^κ.degree
7272

7373
metric(::PolynomialKernel) = DotProduct()
7474

7575
function Base.show(io::IO, κ::PolynomialKernel)
76-
return print(io, "Polynomial Kernel (c = ", first.c), ", degree = ", κ.degree, ")")
76+
return print(io, "Polynomial Kernel (c = ", only.c), ", degree = ", κ.degree, ")")
7777
end

src/basekernels/rational.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ end
3232
@functor RationalKernel
3333

3434
function kappa::RationalKernel, d::Real)
35-
return (one(d) + d / first.α))^(-first.α))
35+
return (one(d) + d / only.α))^(-only.α))
3636
end
3737

3838
metric(k::RationalKernel) = k.metric
3939

4040
function Base.show(io::IO, κ::RationalKernel)
41-
return print(io, "Rational Kernel (α = ", first.α), ", metric = ", κ.metric, ")")
41+
return print(io, "Rational Kernel (α = ", only.α), ", metric = ", κ.metric, ")")
4242
end
4343

4444
"""
@@ -72,18 +72,18 @@ end
7272
@functor RationalQuadraticKernel
7373

7474
function kappa::RationalQuadraticKernel, d::Real)
75-
return (one(d) + d^2 / (2 * first.α)))^(-first.α))
75+
return (one(d) + d^2 / (2 * only.α)))^(-only.α))
7676
end
7777
function kappa::RationalQuadraticKernel{<:Real,<:Euclidean}, d²::Real)
78-
return (one(d²) +/ (2 * first.α)))^(-first.α))
78+
return (one(d²) +/ (2 * only.α)))^(-only.α))
7979
end
8080

8181
metric(k::RationalQuadraticKernel) = k.metric
8282
metric(::RationalQuadraticKernel{<:Real,<:Euclidean}) = SqEuclidean()
8383

8484
function Base.show(io::IO, κ::RationalQuadraticKernel)
8585
return print(
86-
io, "Rational Quadratic Kernel (α = ", first.α), ", metric = ", κ.metric, ")"
86+
io, "Rational Quadratic Kernel (α = ", only.α), ", metric = ", κ.metric, ")"
8787
)
8888
end
8989

@@ -122,7 +122,7 @@ end
122122
@functor GammaRationalKernel
123123

124124
function kappa::GammaRationalKernel, d::Real)
125-
return (one(d) + d^first.γ) / first.α))^(-first.α))
125+
return (one(d) + d^only.γ) / only.α))^(-only.α))
126126
end
127127

128128
metric(k::GammaRationalKernel) = k.metric
@@ -131,9 +131,9 @@ function Base.show(io::IO, κ::GammaRationalKernel)
131131
return print(
132132
io,
133133
"Gamma Rational Kernel (α = ",
134-
first.α),
134+
only.α),
135135
", γ = ",
136-
first.γ),
136+
only.γ),
137137
", metric = ",
138138
κ.metric,
139139
")",

src/distances/sinus.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ struct Sinus{T} <: Distances.UnionSemiMetric
22
r::Vector{T}
33
end
44

5+
Sinus(r::Real) = Sinus([r])
6+
57
Distances.parameters(d::Sinus) = d.r
68
@inline Distances.eval_op(::Sinus, a::Real, b::Real, p::Real) = abs2(sinpi(a - b) / p)
79
@inline (dist::Sinus)(a::AbstractArray, b::AbstractArray) = Distances._evaluate(dist, a, b)
8-
@inline (dist::Sinus)(a::Number, b::Number) = abs2(sinpi(a - b) / first(dist.r))
10+
@inline (dist::Sinus)(a::Number, b::Number) = abs2(sinpi(a - b) / only(dist.r))
911

1012
Distances.result_type(::Sinus{T}, Ta::Type, Tb::Type) where {T} = promote_type(T, Ta, Tb)
1113

src/kernels/scaledkernel.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ end
2323

2424
@functor ScaledKernel
2525

26-
(k::ScaledKernel)(x, y) = first(k.σ²) * k.kernel(x, y)
26+
(k::ScaledKernel)(x, y) = only(k.σ²) * k.kernel(x, y)
2727

2828
function kernelmatrix::ScaledKernel, x::AbstractVector, y::AbstractVector)
2929
return κ.σ² .* kernelmatrix.kernel, x, y)
@@ -75,5 +75,5 @@ Base.show(io::IO, κ::ScaledKernel) = printshifted(io, κ, 0)
7575

7676
function printshifted(io::IO, κ::ScaledKernel, shift::Int)
7777
printshifted(io, κ.kernel, shift)
78-
return print(io, "\n" * ("\t"^(shift + 1)) * "- σ² = $(first.σ²))")
78+
return print(io, "\n" * ("\t"^(shift + 1)) * "- σ² = $(only.σ²))")
7979
end

src/kernels/transformedkernel.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ function (k::TransformedKernel{<:SimpleKernel,<:ScaleTransform})(
2828
end
2929

3030
function _scale(t::ScaleTransform, metric::Euclidean, x, y)
31-
return first(t.s) * evaluate(metric, x, y)
31+
return only(t.s) * evaluate(metric, x, y)
3232
end
3333
function _scale(t::ScaleTransform, metric::Union{SqEuclidean,DotProduct}, x, y)
34-
return first(t.s)^2 * evaluate(metric, x, y)
34+
return only(t.s)^2 * evaluate(metric, x, y)
3535
end
3636
_scale(t::ScaleTransform, metric, x, y) = evaluate(metric, t(x), t(y))
3737

src/transform/ardtransform.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ end
3232

3333
dim(t::ARDTransform) = length(t.v)
3434

35-
(t::ARDTransform)(x::Real) = first(t.v) * x
35+
(t::ARDTransform)(x::Real) = only(t.v) * x
3636
(t::ARDTransform)(x) = t.v .* x
3737

3838
_map(t::ARDTransform, x::AbstractVector{<:Real}) = t.v' .* x

0 commit comments

Comments
 (0)