17#ifndef __math_one_over_cosh_h__
18#define __math_one_over_cosh_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 T n = sqrt (one_over_noise_squared);
30 T
e = n * (actual - measured);
31 if (
e < -20.0)
e = -
e;
32 else if (
e <= 20.0) {
e = exp(
e);
e = log (
e + 1.0/
e); }
33 return (
e - 0.5*log (one_over_noise_squared));
39 template <
typename T>
inline T
lnP (
const T measured,
const T actual,
const T one_over_noise_squared, T& dP_dactual, T& dP_dN)
41 assert (one_over_noise_squared > 0.0);
42 T n = sqrt(one_over_noise_squared);
43 T
e = n * (actual - measured);
45 if (
e < -20.0) {
lnP = -
e;
e = -1.0; }
47 else {
lnP =
e;
e = 1.0; }
50 dP_dN = 0.5 * ((actual - measured) *
e / n - 1.0/one_over_noise_squared);
51 return (
lnP - 0.5*log (one_over_noise_squared));
58 template <
typename T>
inline T
lnP (
const int N,
const T* measured,
const T* actual,
const T one_over_noise_squared)
60 assert (one_over_noise_squared > 0.0);
62 T n = sqrt (one_over_noise_squared);
64 for (
int i = 0; i < N; i++) {
65 T
e = n * (actual[i] - measured[i]);
66 if (
e < -20.0)
e = -
e;
67 else if (
e <= 20.0) {
e = exp (
e);
e = log (
e + 1.0/
e); }
70 return (
lnP - 0.5*N*log (one_over_noise_squared));
76 template <
typename T>
inline T
lnP (
const Math::Vector<T>& measured,
const Math::Vector<T>& actual,
const T one_over_noise_squared)
78 assert (one_over_noise_squared > 0.0);
79 assert (measured.size() == actual.size());
81 T n = sqrt (one_over_noise_squared);
83 for (
size_t i = 0; i < actual.size(); i++) {
84 T
e = n * (actual[i] - measured[i]);
85 if (
e < -20.0)
e = -
e;
86 else if (
e <= 20.0) {
e = exp (
e);
e = log (
e + 1.0/
e); }
89 return (
lnP - 0.5*actual.size()*log (one_over_noise_squared));
94 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)
96 assert (one_over_noise_squared > 0.0);
98 T n = sqrt (one_over_noise_squared);
101 for (
int i = 0; i < N; i++) {
102 T
e = n * (actual[i] - measured[i]);
104 if (
e < -20.0) { t = -
e;
e = -1.0; }
105 else if (
e <= 20.0) {
e = exp (
e); t =
e + 1.0/
e;
e = (
e - 1.0/
e) / t; t = log (t); }
106 else { t =
e;
e = 1.0; }
108 dP_dactual[i] = n *
e;
109 dP_dN += (actual[i] - measured[i]) *
e;
111 dP_dN = 0.5 * (dP_dN / n - N / one_over_noise_squared);
112 return (
lnP - 0.5*N*log (one_over_noise_squared));
119 template <
typename T>
inline T
lnP (
const Math::Vector<T>& measured,
const Math::Vector<T>& actual,
const T one_over_noise_squared, Math::Vector<T>& dP_dactual, T& dP_dN)
121 assert (one_over_noise_squared > 0.0);
122 assert (measured.size() == actual.size());
124 T n = sqrt (one_over_noise_squared);
127 for (
size_t i = 0; i < actual.size(); i++) {
128 T
e = n * (actual[i] - measured[i]);
130 if (
e < -20.0) { t = -
e;
e = -1.0; }
131 else if (
e <= 20.0) {
e = exp (
e); t =
e + 1.0/
e;
e = (
e - 1.0/
e) / t; t = log (t); }
132 else { t =
e;
e = 1.0; }
134 dP_dactual[i] = n *
e;
135 dP_dN += (actual[i] - measured[i]) *
e;
137 dP_dN = 0.5 * (dP_dN / n - actual.size() / one_over_noise_squared);
138 return (
lnP - 0.5*actual.size()*log (one_over_noise_squared));
T lnP(const T measured, const T actual, const T one_over_noise_squared)