29 #ifndef COVARIANCE_KERNEL_HPP_INCLUDED
30 #define COVARIANCE_KERNEL_HPP_INCLUDED
32 #include "../core/field_dimension.hpp"
33 #include "../core/global_definitions.hpp"
34 #include "../core/kernel.hpp"
35 #include "../core/gaussian_quadrature.hpp"
41 template <
class Space,
class Dimension>
45 namespace kernel_traits_ns
47 template <
class Space,
class Dimension>
50 template<
class Space,
class Dimension>
53 typedef Eigen::Matrix<typename Space::scalar_t, Dimension::value, Dimension::value> type;
56 template <
class Space,
class Dimension>
59 template <
class Space,
class Dimension>
62 template <
class Space,
class Dimension>
68 template <
class Space,
class Dimension>
74 template <
class Space,
class Dimension>
78 template <
class Space,
class Dimension>
83 template <
class Space,
class Dimension>
85 public kernel_base<exponential_covariance_kernel<Space, Dimension> >
92 typedef typename space_t::scalar_t distance_t;
95 typedef Dimension dimension_t;
96 size_t static const field_dimension = dimension_t::value;
97 typedef Eigen::Matrix<double, field_dimension, field_dimension> field_variance_t;
100 m_field_variance(variance), m_length(length)
104 static distance_t distance(location_t
const &x, location_t
const &y)
109 static distance_t distance_sph(location_t
const &x, location_t
const &y)
111 result_t v = x.normalized().dot(y.normalized());
112 v = std::min(v, 1.0);
113 v = std::max(v, -1.0);
117 result_t operator()(location_t
const &x, location_t
const &y)
const
119 auto r = distance(x, y);
120 return get_variance() * std::exp(-r / get_correlation_length());
124 test_input_t
const &x,
125 trial_input_t
const &y)
const
127 return (*
this)(x.get_x(), y.get_x());
130 field_variance_t
const &get_variance(
void)
const
132 return m_field_variance;
135 double get_correlation_length(
void)
const
141 field_variance_t m_field_variance;
147 template <
class Space,
class Dimension>
151 namespace kernel_traits_ns
153 template <
class Space,
class Dimension>
156 template<
class Space,
class Dimension>
159 typedef Eigen::Matrix<typename Space::scalar_t, Dimension::value, Dimension::value> type;
164 template<
class Space>
167 typedef typename Space::scalar_t type;
171 template <
class Space,
class Dimension>
174 template <
class Space,
class Dimension>
177 template <
class Space,
class Dimension>
183 template <
class Space,
class Dimension>
189 template <
class Space,
class Dimension>
193 template <
class Space,
class Dimension>
197 template <
class Space>
201 typedef Space space_t;
202 typedef typename space_t::scalar_t scalar_t;
203 static size_t const space_dimension = Space::dimension;
204 typedef typename space_t::location_t location_t;
206 typedef Eigen::Matrix<scalar_t, space_dimension, space_dimension> result_t;
209 : m_variance(variance)
213 result_t
const &get_variance(location_t
const&)
const
222 template <
class Space,
class Dimension>
224 public kernel_base<gaussian_covariance_kernel<Space, Dimension> >
231 typedef typename space_t::scalar_t distance_t;
234 typedef Dimension dimension_t;
235 size_t static const field_dimension = dimension_t::value;
236 size_t static const space_dimension = Space::dimension;
237 typedef Eigen::Matrix<double, space_dimension, space_dimension> space_variance_t;
238 typedef Eigen::Matrix<double, field_dimension, field_dimension> field_variance_t;
241 field_variance_t
const &field_variance,
242 space_variance_t
const &space_variance
244 : m_field_variance(field_variance)
245 , m_space_variance(space_variance)
246 , m_inv_space_variance(m_space_variance.inverse())
250 result_t operator()(location_t
const &x, location_t
const &y)
const
252 location_t d = x - y;
253 double Q = d.transpose() * m_inv_space_variance * d;
254 return get_field_variance() * std::exp(-Q);
258 test_input_t
const &x,
259 trial_input_t
const &y)
const
261 return (*
this)(x.get_x(), y.get_x());
264 field_variance_t
const &get_field_variance(
void)
const
266 return m_field_variance;
269 space_variance_t
const &get_space_variance(
void)
const
271 return m_space_variance;
275 field_variance_t m_field_variance;
276 space_variance_t m_space_variance;
277 space_variance_t m_inv_space_variance;
283 template <
class SpaceVariance,
class FieldVariance>
284 class gaussian_covariance_kernel;
287 namespace kernel_traits_ns
289 template <
class SpaceVariance,
class FieldVariance>
290 struct space<gaussian_covariance_kernel<SpaceVariance, FieldVariance> > : typename SpaceVariance::space_t {};
292 template<
class SpaceVariance,
class FieldVariance>
293 struct result<gaussian_covariance_kernel<SpaceVariance, FieldVariance> >
295 typedef FieldVariance::result_t type;
298 template <
class SpaceVariance,
class FieldVariance>
299 struct quadrature_family<gaussian_covariance_kernel<SpaceVariance, FieldVariance> > : gauss_family_tag {};
301 template <
class SpaceVariance,
class FieldVariance>
302 struct is_singular<gaussian_covariance_kernel<SpaceVariance, FieldVariance> > : std::false_type {};
304 template <
class SpaceVariance,
class FieldVariance>
305 struct test_input<gaussian_covariance_kernel<SpaceVariance, FieldVariance> >
307 typedef location_input<typename SpaceVariance::space_t> type;
310 template <
class SpaceVariance,
class FieldVariance>
311 struct trial_input<gaussian_covariance_kernel<SpaceVariance, FieldVariance> >
313 typedef location_input<typename SpaceVariance::space_t> type;
316 template <
class SpaceVariance,
class FieldVariance>
317 struct is_symmetric<gaussian_covariance_kernel<SpaceVariance, FieldVariance> > : std::true_type {};
320 template <
class SpaceVariance,
class FieldVariance>
321 struct far_field_behaviour<gaussian_covariance_kernel<SpaceVariance, FieldVariance> > : asymptotic::inverse<1> {};
324 template <
class SpaceVariance,
class FieldVariance>
325 class gaussian_covariance_kernel :
326 public kernel_base<gaussian_covariance_kernel<SpaceVariance, FieldVariance> >
328 typedef SpaceVariance space_variance_t;
329 typedef FieldVariance field_variance_t;
331 typedef kernel_base<gaussian_covariance_kernel<SpaceVariance, FieldVariance> > base_t;
335 typedef typename field_variance_t::result_t field_var_t;
336 typedef typename space_variance_t::result_t space_var_t;
338 gaussian_covariance_kernel(field_variance_t
const &fvar, space_variance_t
const &svar)
339 : m_field_variance(fvar)
340 , m_space_variance(svar)
344 result_t operator()(location_t
const &x, location_t
const &y)
const
346 location_t d = x - y;
347 space_variance_t svar_x = get_space_variance(x);
348 space_variance_t svar_y = get_space_variance(y);
349 field_variance_t fvar = get_field_variance(x);
351 space_variance_t svar = .5 * (svar_x + svar_y);
352 double Q = d.transpose() * svar.inverse() * d;
353 double scale = std::pow(svar_x.determinant(), .25) * std::pow(svar_y.determinant(), .25) / std::sqrt(svar.determinant());
355 return fvar * scale * std::exp(-Q);
358 field_var_t get_field_variance(location_t
const &x)
const
360 return m_field_variance.get_variance(x);
363 space_var_t get_space_variance(location_t
const &x)
const
365 return m_space_variance.get_variance(x);
369 test_input_t
const &x,
370 trial_input_t
const &y)
const
372 return (*
this)(x.get_x(), y.get_x());
376 space_variance_t m_space_variance;
377 field_variance_t m_field_variance;
384 #endif // COVARIANCE_KERNEL_HPP_INCLUDED