25 #include <queso/config_queso.h>
26 #include <queso/ScalarFunction.h>
27 #include <queso/VectorSet.h>
28 #include <queso/VectorSpace.h>
29 #include <queso/GslVector.h>
30 #include <queso/GslMatrix.h>
32 #ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
33 #include <queso/BoostInputOptionsParser.h>
35 #include <queso/getpot.h>
40 #define QUESO_BASESCALARFN_FD_STEPSIZE_ODV "1e-6"
45 template<
class V,
class M>
48 : m_env(domainSet.env()),
49 m_prefix((std::string)(prefix) +
"func_"),
50 m_domainSet(domainSet),
51 #ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
54 m_fdStepSize(1, std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV))
59 #ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
61 QUESO_BASESCALARFN_FD_STEPSIZE_ODV,
62 "step size for finite difference");
64 m_parser->getOption<std::vector<double> >(m_prefix +
"fdStepSize",
69 "Finite difference vector is not the correct size");
80 unsigned int size =
m_env.
input().vector_variable_size(m_prefix +
"fdStepSize");
83 m_fdStepSize.resize(dim, std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV));
86 double value =
m_env.
input()(m_prefix +
"fdStepSize",
87 std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV),
92 else if (size == dim) {
93 for (
unsigned int i = 0; i <
size; i++) {
95 std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV),
102 queso_error_msg(
"Finite difference vector must be a scalar or a vector of length parameter dimension");
107 for (
unsigned int i = 0; i <
dim; i++) {
110 "Finite difference step sizes must be positive");
115 template<
class V,
class M>
121 template<
class V,
class M>
127 template <
class V,
class M>
130 const V * domainDirection,
133 V * hessianEffect)
const
137 msg +=
"Implementation of all lnValue methods is missing. Please implement";
138 msg +=
" at least lnValue(const V &).";
140 queso_error_msg(msg);
143 template <
class V,
class M>
147 return this->lnValue(domainVector, NULL, NULL, NULL, NULL);
150 template <
class V,
class M>
154 double value = this->lnValue(domainVector);
157 V perturbedVector(domainVector);
160 for (
unsigned int i = 0; i < domainVector.sizeLocal(); ++i) {
162 double tmp = perturbedVector[i];
164 perturbedVector[i] += m_fdStepSize[i];
165 gradVector[i] = (this->lnValue(perturbedVector) - value) / m_fdStepSize[i];
168 perturbedVector[i] = tmp;
174 template <
class V,
class M>
178 const V & domainDirection,
179 V & hessianEffect)
const
183 msg +=
"QUESO asked for Hessian information from an lnValue method, but the";
184 msg +=
" implementation of is missing. Please implement";
185 msg +=
" lnValue(const V &, V &, const V &, V &).";
187 queso_error_msg(msg);
190 template <
class V,
class M>
194 queso_require_greater_msg(fdStepSize, 0.0,
195 "Must provide a finite difference step > 0");
197 for (
unsigned int i = 0; i < this->m_fdStepSize.size(); i++) {
198 this->m_fdStepSize[i] = fdStepSize;
202 template <
class V,
class M>
207 queso_require_greater_msg(fdStepSize, 0.0,
208 "Must provide a finite difference step > 0");
210 queso_require_greater_equal_msg(i, 0,
"Must provide a nonnegative index");
212 unsigned int size = this->m_fdStepSize.size();
213 queso_require_less_msg(i, size,
"Must provide an index less than size of parameter dimension");
215 this->m_fdStepSize[i] = fdStepSize;
BaseScalarFunction(const char *prefix, const VectorSet< V, M > &domainSet)
Default constructor.
const GetPot & input() const
The GetPot input file parser.
virtual ~BaseScalarFunction()
Destructor.
void setFiniteDifferenceStepSize(double fdStepSize)
Sets the step size for finite differencing gradients.
A templated class for handling sets.
ScopedPtr< BoostInputOptionsParser >::Type m_parser
Input parser.
const VectorSet< V, M > & domainSet() const
Access to the protected attribute m_domainSet: domain set of the scalar function. ...
const BaseEnvironment & m_env
virtual double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Logarithm of the value of the scalar function. Deprecated.
std::vector< double > m_fdStepSize
Finite different step size.
const VectorSet< V, M > & m_domainSet
Domain set of the scalar function.