181 {
182 require(coordinate.size() == d_ && coordinate.allFinite(),
183 "Finite matching Gaussian query required");
184 const V displacement = coordinate - anchor_,
185 r = basis_.transpose() * displacement,
186 z = normal_ * displacement;
187 V gradient = V::Zero(r_), h = V::Zero(d_),
188 cross = V::Zero(n_ * (r_ + 1)), kernels(n_);
189 M hessian = M::Zero(r_, r_), hDerivative = M::Zero(r_, d_),
190 hContraction = M::Zero(r_, r_);
191 M crossDerivative = M::Zero(r_, cross.size()), kernelDerivative(n_, d_);
192 double mean = 0;
193 for (Eigen::Index i = 0; i < n_; ++i) {
194
195 const V delta = r_ == 0 ?
V() :
V(r - sites_.row(i).transpose());
196 const double k =
197 variance_ * std::exp(-delta.squaredNorm() / (2 * length2_));
198 kernels[i] = k;
199 const V b = r_ == 0 ?
V() :
V(alpha_.segment(i * (r_ + 1) + 1, r_));
200 const double value = alpha_[i * (r_ + 1)] + delta.dot(b) / length2_;
201 const M second = delta * delta.transpose() / (length2_ * length2_) -
202 M::Identity(r_, r_) / length2_;
203 mean += k * value;
204 gradient += k * (b - delta * value) / length2_;
205 hessian += k * (second * value -
206 (delta * b.transpose() + b * delta.transpose()) /
207 (length2_ * length2_));
208 h += k * beta_.row(i).transpose() / length2_;
209 hDerivative -= k * delta * beta_.row(i) / (length2_ * length2_);
210 hContraction += k * second * beta_.row(i).dot(
z) / length2_;
211 cross[i * (r_ + 1)] = k;
212 if (r_ > 0) {
213 cross.segment(i * (r_ + 1) + 1, r_) = k * delta / length2_;
214 crossDerivative.col(i * (r_ + 1)) = -k * delta / length2_;
215 crossDerivative.block(0, i * (r_ + 1) + 1, r_, r_) =
216 -k * second;
217 }
218 kernelDerivative.row(i) =
219 -k * (r_ == 0 ?
z :
V(basis_ * delta +
z)).transpose() /
220 length2_;
221 }
222 const double value = mean +
z.dot(h),
223 attenuation = std::exp(-
z.squaredNorm() / (2 * length2_));
224 const V fullGradient = basis_ * (gradient + hDerivative *
z) + h,
225 logGradient = -
z / length2_;
226 const M fullHessian =
227 basis_ * (hessian + hContraction) * basis_.transpose() +
228 basis_ * hDerivative + hDerivative.transpose() * basis_.transpose();
230 result.
energy = attenuation * value;
231 result.gradient = attenuation * (fullGradient + value * logGradient);
232 result.hessian = attenuation *
233 (fullHessian + logGradient * fullGradient.transpose() +
234 fullGradient * logGradient.transpose() +
235 value * (logGradient * logGradient.transpose() -
236 normal_ / length2_));
237 Eigen::Index owner = -1;
238 if (uncertainty)
239 for (Eigen::Index i = 0; i < n_; ++i)
240 if ((coordinate.array() ==
241 physicalSites_.row(i).transpose().array())
242 .all()) {
243 owner = i;
244 break;
245 }
246 if (uncertainty && owner >= 0 && energyNoise_ <= variance_ &&
247 gradientNoise_ <= variance_ / length2_) {
248
249
250
251 const auto offset = owner * (r_ + 1);
252 M selection = M::Zero(n_ * (r_ + 1), r_ + 1);
253 selection.block(offset, 0, r_ + 1, r_ + 1).setIdentity();
254 const M response = tangentFactor_.solve(selection);
255 V normalSelection = V::Zero(n_);
256 normalSelection[owner] = 1.;
257 const M normalResponse = normalFactor_.solve(normalSelection);
258 result.energyVariance =
259 energyNoise_ * (1 - energyNoise_ * response(offset, 0));
260 result.gradientVariance.resize(d_);
261 for (Eigen::Index j = 0; j < d_; ++j) {
262
263
264 double tangent = 0;
265 if (r_ > 0) {
266 const M inverseGradients =
267 response.block(offset + 1, 1, r_, r_);
268 tangent = basis_.row(j).dot(
269 (inverseGradients * basis_.row(j).transpose()).eval());
270 }
271 const double inverse =
272 tangent + normal_(j, j) * normalResponse(owner, 0);
273 result.gradientVariance[j] =
274 gradientNoise_ * (1 - gradientNoise_ * inverse);
275 }
276 } else if (uncertainty) {
277 const V normalSolved = normalFactor_.solve(kernels);
278 const double normalQuadratic = kernels.dot(normalSolved),
279 l4 = length2_ * length2_;
280 const V tangentSolved = tangentFactor_.solve(cross);
281 result.energyVariance =
282 variance_ - attenuation * attenuation *
283 (cross.dot(tangentSolved) +
284 z.squaredNorm() * normalQuadratic / l4);
285 const M fullCross = attenuation * (basis_ * crossDerivative +
286 logGradient * cross.transpose());
287 const M tangentResponse =
288 tangentFactor_.solve(fullCross.transpose());
289 const M normalResponse = normalFactor_.solve(kernelDerivative);
290 result.gradientVariance.resize(d_);
291 for (Eigen::Index j = 0; j < d_; ++j) {
292 const double reduction =
293 normal_(j, j) * normalQuadratic +
294 2 *
z[j] * kernels.dot(normalResponse.col(j)) +
296 kernelDerivative.col(j).dot(normalResponse.col(j));
297 result.gradientVariance[j] =
298 variance_ / length2_ -
299 fullCross.row(j).dot(tangentResponse.col(j)) -
300 attenuation * attenuation * reduction / l4;
301 }
302 }
303 require(std::isfinite(result.energy) && result.gradient.allFinite() &&
304 result.hessian.allFinite(),
305 "Gaussian jet prediction is nonfinite");
306 require(!uncertainty || (std::isfinite(result.energyVariance) &&
307 result.gradientVariance.allFinite()),
308 "Gaussian jet covariance is nonfinite");
309 return result;
310 }