Developer documentation
Version 3.0.3-105-gd3941f44
sech.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __math_one_over_cosh_h__
18#define __math_one_over_cosh_h__
19
20#include "math/math.h"
21#include "math/vector.h"
22
23namespace MR {
24 namespace Math {
25 namespace Sech {
26
27 template <typename T> inline T lnP (const T measured, const T actual, const T one_over_noise_squared)
28 {
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));
34 }
35
36
37
38
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)
40 {
41 assert (one_over_noise_squared > 0.0);
42 T n = sqrt(one_over_noise_squared);
43 T e = n * (actual - measured);
44 T lnP;
45 if (e < -20.0) { lnP = -e; e = -1.0; }
46 else if (e <= 20.0) { e = exp (e); lnP = e + 1.0/e; e = (e - 1.0/e) / lnP; lnP = log (lnP); }
47 else { lnP = e; e = 1.0; }
48
49 dP_dactual = n * e;
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));
52 }
53
54
55
56
57
58 template <typename T> inline T lnP (const int N, const T* measured, const T* actual, const T one_over_noise_squared)
59 {
60 assert (one_over_noise_squared > 0.0);
61
62 T n = sqrt (one_over_noise_squared);
63 T lnP = 0.0;
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); }
68 lnP += e;
69 }
70 return (lnP - 0.5*N*log (one_over_noise_squared));
71 }
72
73
74
75
76 template <typename T> inline T lnP (const Math::Vector<T>& measured, const Math::Vector<T>& actual, const T one_over_noise_squared)
77 {
78 assert (one_over_noise_squared > 0.0);
79 assert (measured.size() == actual.size());
80
81 T n = sqrt (one_over_noise_squared);
82 T lnP = 0.0;
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); }
87 lnP += e;
88 }
89 return (lnP - 0.5*actual.size()*log (one_over_noise_squared));
90 }
91
92
93
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)
95 {
96 assert (one_over_noise_squared > 0.0);
97
98 T n = sqrt (one_over_noise_squared);
99 T lnP = 0.0;
100 dP_dN = 0.0;
101 for (int i = 0; i < N; i++) {
102 T e = n * (actual[i] - measured[i]);
103 T t;
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; }
107 lnP += t;
108 dP_dactual[i] = n * e;
109 dP_dN += (actual[i] - measured[i]) * e;
110 }
111 dP_dN = 0.5 * (dP_dN / n - N / one_over_noise_squared);
112 return (lnP - 0.5*N*log (one_over_noise_squared));
113 }
114
115
116
117
118
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)
120 {
121 assert (one_over_noise_squared > 0.0);
122 assert (measured.size() == actual.size());
123
124 T n = sqrt (one_over_noise_squared);
125 T lnP = 0.0;
126 dP_dN = 0.0;
127 for (size_t i = 0; i < actual.size(); i++) {
128 T e = n * (actual[i] - measured[i]);
129 T t;
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; }
133 lnP += t;
134 dP_dactual[i] = n * e;
135 dP_dN += (actual[i] - measured[i]) * e;
136 }
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));
139 }
140
141
142 }
143 }
144}
145
146#endif
constexpr double e
Definition: math.h:39
T lnP(const T measured, const T actual, const T one_over_noise_squared)
Definition: sech.h:27
Definition: base.h:24