```"""Radial kernel functions.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)

Notes
-----
We chose not to apply the normalization coefficients used in some references
in order that the kernel functions integrate to 1 on the Euclidean space of
dimension 1.
"""

import geomstats.backend as gs

def _check_distance(distance):
"""Check if the distance if a non-negative real number."""
if gs.any(distance < 0):
raise ValueError("The distance should be a non-negative real number.")
distance = gs.array(distance, dtype=float)
return distance

def _check_bandwidth(bandwidth):
"""Check if the bandwidth is a positive real number."""
if gs.any(bandwidth <= 0):
raise ValueError("The bandwidth should be a positive real number.")
bandwidth = gs.array(bandwidth, dtype=float)
return bandwidth

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.where(
scaled_distance < 1,
gs.ones(distance.shape, dtype=float),
gs.zeros(distance.shape, dtype=float),
)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.where(
scaled_distance < 1, 1 - scaled_distance, gs.zeros(distance.shape, dtype=float)
)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.where(
scaled_distance < 1,
1 - scaled_distance**2,
gs.zeros(distance.shape, dtype=float),
)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.where(
scaled_distance < 1,
(1 - scaled_distance**2) ** 2,
gs.zeros(distance.shape, dtype=float),
)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.where(
scaled_distance < 1,
(1 - scaled_distance**2) ** 3,
gs.zeros(distance.shape, dtype=float),
)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.where(
scaled_distance < 1,
(1 - scaled_distance**3) ** 3,
gs.zeros(distance.shape, dtype=float),
)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.exp(-(scaled_distance**2) / 2)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.where(
scaled_distance < 1,
gs.cos((gs.pi / 2) * scaled_distance),
gs.zeros(distance.shape, dtype=float),
)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
https://en.wikipedia.org/wiki/Kernel_(statistics)
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = 1 / (gs.exp(scaled_distance) + 2 + gs.exp(-scaled_distance))
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = 1 / (gs.exp(scaled_distance) + gs.exp(-scaled_distance))
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.where(
scaled_distance < 1,
gs.exp(-1 / (1 - scaled_distance**2)),
gs.zeros(distance.shape, dtype=float),
)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = 1 / (1 + scaled_distance**2)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

References
----------
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = 1 / (1 + scaled_distance**2) ** (1 / 2)
return weight

[docs]

Parameters
----------
distance : array-like
Array of non-negative real values.
bandwidth : float, optional (default=1.0)
Positive scale parameter of the kernel.

Returns
-------
weight : array-like
Array of non-negative real values of the same shape than
parameter 'distance'.

Returns
-------
http://crsouza.com/2010/03/17/
kernel-functions-for-machine-learning-applications/
https://data-flair.training/blogs/svm-kernel-functions/
"""
distance = _check_distance(distance)
bandwidth = _check_bandwidth(bandwidth)
scaled_distance = distance / bandwidth
weight = gs.exp(-scaled_distance)
return weight

```