17#ifndef __math_gaussian_h__
18#define __math_gaussian_h__
21#include "math/vector.h"
27 template <
typename T>
inline T
lnP (
const T measured,
const T actual,
const T one_over_noise_squared)
29 return (0.5 * (one_over_noise_squared *
pow2 (actual - measured) - log (one_over_noise_squared)));
35 template <
typename T>
inline T
lnP (
const T measured,
const T actual,
const T one_over_noise_squared, T& dP_dactual, T& dP_dN)
37 assert (one_over_noise_squared > 0.0);
38 dP_dN =
pow2 (actual - measured);
39 dP_dactual = one_over_noise_squared * dP_dN;
40 dP_dN = 0.5 * (dP_dN - 1.0/one_over_noise_squared);
41 return (0.5 * (dP_dactual - log(one_over_noise_squared)));
48 template <
typename T>
inline T
lnP (
const int N,
const T* measured,
const T* actual,
const T one_over_noise_squared)
50 assert (one_over_noise_squared > 0.0);
53 for (
int i = 0; i < N; i++)
54 lnP +=
pow2 (actual[i] - measured[i]);
55 lnP *= one_over_noise_squared;
56 lnP -= N * log (one_over_noise_squared);
62 template <
typename T>
inline T
lnP (
const int N,
const T* measured,
const T* actual,
const T one_over_noise_squared, T* dP_dactual, T& dP_dN)
64 assert (one_over_noise_squared > 0.0);
68 for (
int i = 0; i < N; i++) {
69 T diff = actual[i] - measured[i];
70 dP_dactual[i] = one_over_noise_squared * diff;
73 dP_dN = 0.5 * (
lnP - T(N)/one_over_noise_squared);
74 lnP *= one_over_noise_squared;
75 lnP -= N * log (one_over_noise_squared);
82 template <
typename T>
inline T
lnP (
const Vector<T>& measured,
const Vector<T>& actual,
const T one_over_noise_squared)
84 assert (one_over_noise_squared > 0.0);
85 assert (measured.size() == actual.size());
88 for (
size_t i = 0; i < measured.size(); i++)
89 lnP +=
pow2 (actual[i] - measured[i]);
90 lnP *= one_over_noise_squared;
91 lnP -= measured.size() * log (one_over_noise_squared);
98 template <
typename T>
inline T
lnP (
const Vector<T>& measured,
const Vector<T>& actual,
const T one_over_noise_squared, Vector<T>& dP_dactual, T& dP_dN)
100 assert (one_over_noise_squared > 0.0);
101 assert (measured.size() == actual.size());
102 assert (measured.size() == dP_dactual.size());
106 for (
size_t i = 0; i < measured.size(); i++) {
107 T diff = actual[i] - measured[i];
108 dP_dactual[i] = one_over_noise_squared * diff;
111 dP_dN = 0.5 * (
lnP - T(measured.size())/one_over_noise_squared);
112 lnP *= one_over_noise_squared;
113 lnP -= measured.size() * log (one_over_noise_squared);
constexpr T pow2(const T &v)
T lnP(const T measured, const T actual, const T one_over_noise_squared)