queso-0.50.1
GenericScalarFunction.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008,2009,2010,2011,2012,2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <cmath>
26 
27 #include <queso/Defines.h>
28 #include <queso/VectorSet.h>
29 #include <queso/VectorSubset.h>
30 #include <queso/Environment.h>
31 #include <queso/GenericScalarFunction.h>
32 #include <queso/GslVector.h>
33 #include <queso/GslMatrix.h>
34 
35 namespace QUESO {
36 
37 // Default constructor
38 template<class V,class M>
40  const VectorSet<V,M>& domainSet,
41  double (*valueRoutinePtr)(const V& domainVector, const V* domainDirection, const void* routinesDataPtr, V* gradVector, M* hessianMatrix, V* hessianEffect),
42  const void* routinesDataPtr,
43  bool routineIsForLn)
44  : BaseScalarFunction<V,M>(((std::string)(prefix)+"gen").c_str(), domainSet),
45  m_valueRoutinePtr (valueRoutinePtr),
46  m_routinesDataPtr (routinesDataPtr),
47  m_routineIsForLn (routineIsForLn)
48 {
49 }
50 
51 // Destructor
52 template<class V,class M>
54 {
55 }
56 
57 // Math methods
58 template<class V,class M>
59 double GenericScalarFunction<V,M>::actualValue(const V& domainVector,
60  const V* domainDirection,
61  V* gradVector,
62  M* hessianMatrix,
63  V* hessianEffect) const
64 {
65  UQ_FATAL_TEST_MACRO(m_valueRoutinePtr == NULL,
66  m_env.worldRank(),
67  "GenericScalarFunction<V,M>::actualValue()",
68  "m_valueRoutinePtr = NULL");
69 
70  double value = m_valueRoutinePtr(domainVector, domainDirection, m_routinesDataPtr, gradVector, hessianMatrix, hessianEffect);
71  if (m_routineIsForLn) {
72 #ifdef QUESO_EXPECTS_LN_LIKELIHOOD_INSTEAD_OF_MINUS_2_LN
73  value = std::exp(value);
74 #else
75  value = std::exp(-.5*value);
76 #endif
77  UQ_FATAL_TEST_MACRO((domainDirection != NULL) ||
78  (gradVector != NULL) ||
79  (hessianMatrix != NULL) ||
80  (hessianEffect != NULL),
81  m_env.worldRank(),
82  "GenericScalarFunction<V,M>::gradOfActual()",
83  "INCOMPLETE CODE");
84  }
85  return value;
86 }
87 
88 
89 template<class V,class M>
90 double GenericScalarFunction<V,M>::lnValue(const V& domainVector,
91  const V* domainDirection,
92  V* gradVector,
93  M* hessianMatrix,
94  V* hessianEffect) const
95 {
96  UQ_FATAL_TEST_MACRO(m_valueRoutinePtr == NULL,
97  m_env.worldRank(),
98  "GenericScalarFunction<V,M>::lnValue()",
99  "m_valueRoutinePtr = NULL");
100 
101  double value = m_valueRoutinePtr(domainVector, domainDirection, m_routinesDataPtr, gradVector, hessianMatrix, hessianEffect);
102  if (m_routineIsForLn == false) {
103 #ifdef QUESO_EXPECTS_LN_LIKELIHOOD_INSTEAD_OF_MINUS_2_LN
104  value = log(value);
105 #else
106  value = -2.*log(value);
107 #endif
108  UQ_FATAL_TEST_MACRO((domainDirection != NULL) ||
109  (gradVector != NULL) ||
110  (hessianMatrix != NULL) ||
111  (hessianEffect != NULL),
112  m_env.worldRank(),
113  "GenericScalarFunction<V,M>::gradOfLn()",
114  "INCOMPLETE CODE");
115  }
116  return value;
117 }
118 
119 } // End namespace QUESO
120 
A templated (base) class for handling scalar functions.
double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Calculates the logarithm of value of this scalar function.
virtual ~GenericScalarFunction()
Virtual destructor.
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:222
A templated class for handling sets.
Definition: VectorSet.h:49
double actualValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Calculates the actual value of this scalar function.
A class for handling generic scalar functions.
GenericScalarFunction(const char *prefix, const VectorSet< V, M > &domainSet, double(*valueRoutinePtr)(const V &domainVector, const V *domainDirection, const void *routinesDataPtr, V *gradVector, M *hessianMatrix, V *hessianEffect), const void *routinesDataPtr, bool routineIsForLn)
Default constructor.

Generated on Thu Apr 23 2015 19:18:33 for queso-0.50.1 by  doxygen 1.8.5