Loading...
Searching...
No Matches
NlmlHessianVector.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#ifndef GPR_ML_GP_NLMLHESSIANVECTOR_H_
5#define GPR_ML_GP_NLMLHESSIANVECTOR_H_
6
7#include <Eigen/Core>
8#include <limits>
9
10namespace gpr::detail {
11
24inline double nlmlHessianVectorComponent(
25 const Eigen::MatrixXd& inverse, const Eigen::VectorXd& alpha,
26 const Eigen::VectorXd& beta, const Eigen::MatrixXd& metric,
27 const Eigen::MatrixXd& kernel_derivative,
28 const Eigen::MatrixXd& mixed_second)
29{
30 const Eigen::Index n = inverse.rows();
31 if (n == 0 || inverse.cols() != n || alpha.size() != n ||
32 beta.size() != n || metric.rows() != n || metric.cols() != n ||
33 kernel_derivative.rows() != n || kernel_derivative.cols() != n ||
34 mixed_second.rows() != n || mixed_second.cols() != n) {
35 return std::numeric_limits<double>::quiet_NaN();
36 }
37 const double trace_kernel = metric.cwiseProduct(kernel_derivative).sum();
38 const double trace_second = inverse.cwiseProduct(mixed_second).sum();
39 const double quadratic = (kernel_derivative * alpha).dot(beta);
40 const double second_quadratic = alpha.dot(mixed_second * alpha);
41 return -0.5 * trace_kernel + 0.5 * trace_second + quadratic -
42 0.5 * second_quadratic;
43}
44
45} // namespace gpr::detail
46
47#endif // GPR_ML_GP_NLMLHESSIANVECTOR_H_