-
Notifications
You must be signed in to change notification settings - Fork 6
/
pctEnergyAdaptiveMLPFunction.txx
130 lines (108 loc) · 4.35 KB
/
pctEnergyAdaptiveMLPFunction.txx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
namespace pct
{
EnergyAdaptiveMLPFunction
::EnergyAdaptiveMLPFunction()
{
// We operate a change of origin, u0 is always 0
m_u0=0.;
m_ScalarTest = -1.;
m_CanBeVectorised = true;
}
void
EnergyAdaptiveMLPFunction
::Init(const VectorType posIn, const VectorType posOut, const VectorType dirIn, const VectorType dirOut, double eIn, double eOut)
{
m_uOrigin = posIn[2];
m_u2 = posOut[2]-m_uOrigin;
// Parameters vectors
m_x0[0] = posIn[0];
m_x0[1] = std::atan(dirIn[0]); //dirIn[2] is implicitely 1.
m_x2[0] = posOut[0];
m_x2[1] = std::atan(dirOut[0]); //dirOut[2] is implicitely 1.
m_y0[0] = posIn[1];
m_y0[1] = std::atan(dirIn[1]); //dirIn[2] is implicitely 1.
m_y2[0] = posOut[1];
m_y2[1] = std::atan(dirOut[1]); //dirOut[2] is implicitely 1.
Functor::FlexibleMLP::GetLinearCoefficients( m_ab, eIn, eOut, m_u2 );
const double A = Functor::FlexibleMLP::FactorsABCD::GetA(m_u2, m_ab);
const double B = Functor::FlexibleMLP::FactorsABCD::GetB(m_u2, m_ab);
const double C = Functor::FlexibleMLP::FactorsABCD::GetC(m_u2, m_ab);
const double D = Functor::FlexibleMLP::FactorsABCD::GetD(m_u2, m_ab);
Functor::FlexibleMLP::CoefficientsC::GetValue(m_c_x, m_u2, m_x0, m_x2, A, B, C, D);
Functor::FlexibleMLP::CoefficientsC::GetValue(m_c_y, m_u2, m_y0, m_y2, A, B, C, D);
double bOvera = m_ab[1]/m_ab[0];
double bOvera2 = m_ab[1]/m_ab[0]/m_ab[0];
double b2Overa3 = bOvera2*m_ab[1]/m_ab[0];
m_dm_x[0] = m_x0[0];
m_dm_x[3] = m_c_x[0]*bOvera2 - m_c_x[1]*b2Overa3;
m_dm_x[1] = m_x0[1]*bOvera - m_dm_x[3];
m_dm_x[2] = 0.5*m_c_x[1]*b2Overa3;
m_dm_y[0] = m_y0[0];
m_dm_y[3] = m_c_y[0]*bOvera2 - m_c_y[1]*b2Overa3;
m_dm_y[1] = m_y0[1]*bOvera - m_dm_y[3];
m_dm_y[2] = 0.5*m_c_y[1]*b2Overa3;
}
// vectorised version
void
EnergyAdaptiveMLPFunction
::Evaluate( std::vector<double> u, std::vector<double> &x, std::vector<double> &y )
{
// shift so u starts at 0 and scale by a/b to get u_tilde
for(auto& element : u)
{
element -= m_uOrigin;
element *= (m_ab[0]/m_ab[1]);
}
// uLog = log(u_tilde + 1)
std::vector<double> uLog;
uLog.reserve(u.size());
std::copy(u.begin(),u.end(),std::back_inserter(uLog));
for(auto& element : uLog)
{
element += 1;
element = std::log(element);
}
#ifdef MLP_TIMING
m_EvaluateProbe1.Start();
#endif
/* terms order 0, 1, 2 */
std::fill(x.begin(), x.end(), m_dm_x[2]);
std::transform(x.begin(), x.end(), u.begin(), x.begin(), std::multiplies<double>() );
std::transform(x.begin(), x.end(), x.begin(), bind2nd(std::plus<double>(), m_dm_x[1]));
std::transform(x.begin(), x.end(), u.begin(), x.begin(), std::multiplies<double>() );
std::transform(x.begin(), x.end(), x.begin(), bind2nd(std::plus<double>(), m_dm_x[0]));
std::fill(y.begin(), y.end(), m_dm_y[2]);
std::transform(y.begin(), y.end(), u.begin(), y.begin(), std::multiplies<double>() );
std::transform(y.begin(), y.end(), y.begin(), bind2nd(std::plus<double>(), m_dm_y[1]));
std::transform(y.begin(), y.end(), u.begin(), y.begin(), std::multiplies<double>() );
std::transform(y.begin(), y.end(), y.begin(), bind2nd(std::plus<double>(), m_dm_y[0]));
/* logarithmic term */
std::transform(u.begin(), u.end(), u.begin(), bind2nd(std::plus<double>(), 1) ); // u => u+1
std::transform(u.begin(), u.end(), uLog.begin(), uLog.begin(), std::multiplies<double>() ); // uLog = (u+1)*log(u+1)
std::transform(uLog.begin(), uLog.end(), uLog.begin(), bind2nd(std::multiplies<double>(), m_dm_x[3]));
std::transform(x.begin(), x.end(), uLog.begin(), x.begin(), std::plus<double>() );
std::transform(uLog.begin(), uLog.end(), uLog.begin(), bind2nd(std::multiplies<double>(), m_dm_y[3]/m_dm_x[3]));
std::transform(y.begin(), y.end(), uLog.begin(), y.begin(), std::plus<double>() );
#ifdef MLP_TIMING
m_EvaluateProbe1.Stop();
#endif
}
void
EnergyAdaptiveMLPFunction
::EvaluateError( const double u, itk::Matrix<double, 2, 2> &error )
{
itkGenericExceptionMacro("The method PolynomialMLPFunction::EvaluateError is not implemented at the moment");
}
#ifdef MLP_TIMING
void
EnergyAdaptiveMLPFunction
::PrintTiming(std::ostream& os)
{
os << "PolynomialMLPFunction timing:" << std::endl;
os << " EvaluateProbe1: " << m_EvaluateProbe1.GetTotal()
<< ' ' << m_EvaluateProbe1.GetUnit() << std::endl;
// os << " EvaluateProbe2: " << m_EvaluateProbe2.GetTotal()
// << ' ' << m_EvaluateProbe2.GetUnit() << std::endl;
}
#endif
}