25 #include <queso/ScalarFunction.h>
26 #include <queso/VectorSet.h>
27 #include <queso/VectorSpace.h>
28 #include <queso/GslVector.h>
29 #include <queso/GslMatrix.h>
31 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
32 #include <queso/BoostInputOptionsParser.h>
34 #include <queso/getpot.h>
39 #define QUESO_BASESCALARFN_FD_STEPSIZE_ODV "1e-6"
44 template<
class V,
class M>
47 : m_env(domainSet.env()),
48 m_prefix((std::string)(prefix) +
"func_"),
49 m_domainSet(domainSet),
50 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
53 m_fdStepSize(1, std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV))
58 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
60 QUESO_BASESCALARFN_FD_STEPSIZE_ODV,
61 "step size for finite difference");
63 m_parser->getOption<std::vector<double> >(m_prefix +
"fdStepSize",
68 "Finite difference vector is not the correct size");
79 unsigned int size =
m_env.
input().vector_variable_size(m_prefix +
"fdStepSize");
82 m_fdStepSize.resize(dim, std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV));
85 double value =
m_env.
input()(m_prefix +
"fdStepSize",
86 std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV),
91 else if (size == dim) {
92 for (
unsigned int i = 0; i <
size; i++) {
94 std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV),
101 queso_error_msg(
"Finite difference vector must be a scalar or a vector of length parameter dimension");
106 for (
unsigned int i = 0; i <
dim; i++) {
109 "Finite difference step sizes must be positive");
114 template<
class V,
class M>
120 template<
class V,
class M>
126 template <
class V,
class M>
129 const V * domainDirection,
132 V * hessianEffect)
const
136 msg +=
"Implementation of all lnValue methods is missing. Please implement";
137 msg +=
" at least lnValue(const V &).";
139 queso_error_msg(msg);
142 template <
class V,
class M>
146 return this->lnValue(domainVector, NULL, NULL, NULL, NULL);
149 template <
class V,
class M>
153 double value = this->lnValue(domainVector);
156 V perturbedVector(domainVector);
159 for (
unsigned int i = 0; i < domainVector.sizeLocal(); ++i) {
161 double tmp = perturbedVector[i];
163 perturbedVector[i] += m_fdStepSize[i];
164 gradVector[i] = (this->lnValue(perturbedVector) - value) / m_fdStepSize[i];
167 perturbedVector[i] = tmp;
173 template <
class V,
class M>
177 const V & domainDirection,
178 V & hessianEffect)
const
182 msg +=
"QUESO asked for Hessian information from an lnValue method, but the";
183 msg +=
" implementation of is missing. Please implement";
184 msg +=
" lnValue(const V &, V &, const V &, V &).";
186 queso_error_msg(msg);
189 template <
class V,
class M>
193 queso_require_greater_msg(fdStepSize, 0.0,
194 "Must provide a finite difference step > 0");
196 for (
unsigned int i = 0; i < this->m_fdStepSize.size(); i++) {
197 this->m_fdStepSize[i] = fdStepSize;
201 template <
class V,
class M>
206 queso_require_greater_msg(fdStepSize, 0.0,
207 "Must provide a finite difference step > 0");
209 queso_require_greater_equal_msg(i, 0,
"Must provide a nonnegative index");
211 unsigned int size = this->m_fdStepSize.size();
212 queso_require_less_msg(i, size,
"Must provide an index less than size of parameter dimension");
214 this->m_fdStepSize[i] = fdStepSize;
void setFiniteDifferenceStepSize(double fdStepSize)
Sets the step size for finite differencing gradients.
const VectorSet< V, M > & m_domainSet
Domain set of the scalar function.
const BaseEnvironment & m_env
const VectorSet< V, M > & domainSet() const
Access to the protected attribute m_domainSet: domain set of the scalar function. ...
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.
virtual ~BaseScalarFunction()
Destructor.
ScopedPtr< BoostInputOptionsParser >::Type m_parser
Input parser.
A templated class for handling sets.
const GetPot & input() const
The GetPot input file parser.
BaseScalarFunction(const char *prefix, const VectorSet< V, M > &domainSet)
Default constructor.