GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GModelData.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GModelData.cpp - Abstract virtual data model class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2011-2025 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GModelData.cpp
23 * @brief GModelData class implementation.
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "GTools.hpp"
32#include "GException.hpp"
33#include "GModelData.hpp"
34#include "GVector.hpp"
35#include "GMatrixSparse.hpp"
36#include "GObservation.hpp"
37
38/* __ Method name definitions ____________________________________________ */
39#define G_EVAL "GModel::eval(GObservation&, GMatrixSparse*)"
40
41/* __ Macros _____________________________________________________________ */
42
43/* __ Coding definitions _________________________________________________ */
44
45/* __ Debug definitions __________________________________________________ */
46
47
48/*==========================================================================
49 = =
50 = Constructors/destructors =
51 = =
52 ==========================================================================*/
53
54/***********************************************************************//**
55 * @brief Void constructor
56 ***************************************************************************/
58{
59 // Initialise members
61
62 // Return
63 return;
64}
65
66
67/***********************************************************************//**
68 * @brief XML constructor
69 *
70 * @param[in] xml XML element.
71 ***************************************************************************/
73{
74 // Initialise members
76
77 // Return
78 return;
79}
80
81
82/***********************************************************************//**
83 * @brief Copy constructor
84 *
85 * @param[in] model Data model.
86 ***************************************************************************/
88{
89 // Initialise members
91
92 // Copy members
93 copy_members(model);
94
95 // Return
96 return;
97}
98
99
100/***********************************************************************//**
101 * @brief Destructor
102 ***************************************************************************/
104{
105 // Free members
106 free_members();
107
108 // Return
109 return;
110}
111
112
113/*==========================================================================
114 = =
115 = Operators =
116 = =
117 ==========================================================================*/
118
119/***********************************************************************//**
120 * @brief Assignment operator
121 *
122 * @param[in] model Data model.
123 * @return Data model.
124 ***************************************************************************/
126{
127 // Execute only if object is not identical
128 if (this != &model) {
129
130 // Copy base class members
131 this->GModel::operator=(model);
132
133 // Free members
134 free_members();
135
136 // Initialise members
137 init_members();
138
139 // Copy members
140 copy_members(model);
141
142 } // endif: object was not identical
143
144 // Return
145 return *this;
146}
147
148
149/*==========================================================================
150 = =
151 = Public methods =
152 = =
153 ==========================================================================*/
154
155/***********************************************************************//**
156 * @brief Return model values and gradients
157 *
158 * @param[in] obs Observation.
159 * @param[out] gradients Pointer to matrix of gradients.
160 * @param[in] offset Column index of first matrix gradient (not used).
161 * @return Model values.
162 *
163 * @exception GException::invalid_argument
164 * Gradient matrix has wrong number of rows or columns
165 *
166 * Evaluates the model values and parameter gradients for all events in an
167 * observation. Gradients are only returned if the @p gradients pointer is
168 * not NULL.
169 *
170 * The matrix of gradients is a sparse matrix where the number of rows
171 * corresponds to the number of events and the number of columns corresponds
172 * to the number of model parameters (see GObservation::model() method).
173 *
174 * An exception is thrown if the dimension of the @p gradients matrix is not
175 * compatible with the model and the observations.
176 ***************************************************************************/
178 GMatrixSparse* gradients,
179 const int& offset) const
180{
181 // Get number of model parameters and number of events
182 int npars = size();
183 int nevents = obs.events()->size();
184
185 // Initialise gradients flag
186 bool grad = ((gradients != NULL) && (npars > 0));
187
188 // Check matrix consistency
189 if (grad) {
190 if (gradients->columns() != npars) {
191 std::string msg = "Number of "+gammalib::str(gradients->columns())+
192 " columns in gradient matrix differs from number "
193 "of "+gammalib::str(npars)+" parameters "
194 "in model. Please provide a compatible gradient "
195 "matrix.";
197 }
198 if (gradients->rows() != nevents) {
199 std::string msg = "Number of "+gammalib::str(gradients->rows())+
200 " rows in gradient matrix differs from number "
201 "of "+gammalib::str(nevents)+" events in "
202 "observation. Please provide a compatible "
203 "gradient matrix.";
205 }
206 }
207
208 // Initialise values
209 GVector values(nevents);
210
211 // Initialise temporary vectors to hold gradients
212 GVector* tmp_gradients = NULL;
213 if (grad) {
214
215 // Allocate temporary vectors to hold the gradients
216 tmp_gradients = new GVector[npars];
217 for (int i = 0; i < npars; ++i) {
218 tmp_gradients[i] = GVector(nevents);
219 }
220
221 // Signal all parameters that will have analytical gradients. These
222 // are all parameters that are free and for which the model provides
223 // analytical gradients.
224 for (int i = 0; i < npars; ++i) {
225 const GModelPar& par = (*this)[i];
226 if (par.is_free() && par.has_grad()) {
227 obs.computed_gradient(*this, par);
228 }
229 }
230
231 } // endif: gradients were requested
232
233 // Loop over events
234 for (int k = 0; k < nevents; ++k) {
235
236 // Get reference to event
237 const GEvent& event = *((*obs.events())[k]);
238
239 // Evaluate probability
240 values[k] = eval(event, obs, grad);
241
242 // Set gradients
243 if (grad) {
244
245 // Extract relevant parameter gradients
246 for (int i = 0; i < npars; ++i) {
247 const GModelPar& par = (*this)[i];
248 if (par.is_free() && par.has_grad()) {
249 tmp_gradients[i][k] = par.factor_gradient();
250 }
251 }
252
253 } // endif: looped over gradients
254
255 } // endfor: looped over events
256
257 // Post-process gradients
258 if (grad) {
259
260 // Fill gradients into matrix
261 for (int i = 0; i < npars; ++i) {
262 gradients->column(i, tmp_gradients[i]);
263 }
264
265 // Delete temporal gradients
266 delete [] tmp_gradients;
267
268 } // endif: post-processed gradients
269
270 // Return values
271 return values;
272}
273
274
275/*==========================================================================
276 = =
277 = Private methods =
278 = =
279 ==========================================================================*/
280
281/***********************************************************************//**
282 * @brief Initialise class members
283 ***************************************************************************/
285{
286 // Return
287 return;
288}
289
290
291/***********************************************************************//**
292 * @brief Copy class members
293 *
294 * @param[in] model Data model.
295 ***************************************************************************/
297{
298 // Return
299 return;
300}
301
302
303/***********************************************************************//**
304 * @brief Delete class members
305 ***************************************************************************/
307{
308 // Return
309 return;
310}
Exception handler interface definition.
Sparse matrix class definition.
#define G_EVAL
Abstract data model base class interface definition.
Abstract observation base class interface definition.
Gammalib tools definition.
Vector class interface definition.
Abstract interface for the event classes.
Definition GEvent.hpp:71
virtual int size(void) const =0
const int & rows(void) const
Return number of matrix rows.
const int & columns(void) const
Return number of matrix columns.
Sparse matrix class interface definition.
virtual GVector column(const int &column) const
Extract column as vector from matrix.
Abstract data model class.
virtual ~GModelData(void)
Destructor.
void init_members(void)
Initialise class members.
virtual double eval(const GEvent &event, const GObservation &obs, const bool &gradients=false) const =0
GModelData(void)
Void constructor.
void copy_members(const GModelData &model)
Copy class members.
void free_members(void)
Delete class members.
virtual GModelData & operator=(const GModelData &model)
Assignment operator.
Model parameter class.
Definition GModelPar.hpp:87
Abstract model class.
Definition GModel.hpp:100
int size(void) const
Return number of parameters in model.
Definition GModel.hpp:237
virtual GModel & operator=(const GModel &model)
Assignment operator.
Definition GModel.cpp:132
Abstract observation base class.
virtual GEvents * events(void)
Return events.
void computed_gradient(const GModel &model, const GModelPar &par) const
Signals that an analytical gradient was computed for a model parameter.
bool is_free(void) const
Signal if parameter is free.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
const double & factor_gradient(void) const
Return parameter factor gradient.
Vector class.
Definition GVector.hpp:46
XML element node class.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:508