ProjectGradient.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2004 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/More/MEAL/MEAL/ProjectGradient.h
10
11#ifndef __ProjectProductGradient_H
12#define __ProjectProductGradient_H
13
14#include "MEAL/Projection.h"
15
16namespace MEAL {
17
18 template <class Function, class Grad>
19 void ProjectGradient (const Project<Function>& model, unsigned& igrad,
20 const std::vector<Grad>& input,
21 std::vector<Grad>& output)
22 {
23 unsigned nparam = model->get_nparam();
24
25 for (unsigned iparam = 0; iparam < nparam; iparam++)
26 {
27 unsigned imap = model.get_map()->get_imap (iparam);
28
30 std::cerr << "ProjectGradient iparam=" << iparam << " imap=" << imap
31 << " igrad=" << igrad << std::endl;
32
33 if (imap >= output.size())
34 throw Error (InvalidRange, "MEAL::ProjectGradient",
35 "iparam=%u -> imap=%u >= composite.nparam=%u",
36 iparam, imap, output.size());
37
38 output[imap] += input[igrad];
39
40 igrad ++;
41 }
42 }
43
44 template <class Function, class Grad>
45 void ProjectGradient (const Project<Function>& model,
46 const std::vector<Grad>& input,
47 std::vector<Grad>& output)
48 {
49 unsigned igrad = 0;
50 ProjectGradient (model, igrad, input, output);
51 }
52
53 template <class Function, class Grad>
54 void ProjectGradient (const std::vector<Project<Function> >& model,
55 const std::vector<Grad>& input,
56 std::vector<Grad>& output)
57 {
58 unsigned nparam = output.size();
59 unsigned nmodel = model.size();
60
61 // set each element of the gradient to zero
62 for (unsigned iparam=0; iparam<nparam; iparam++)
63 output[iparam] = 0.0;
64
65 unsigned igrad = 0;
66
67 for (unsigned imodel=0; imodel<nmodel; imodel++)
68 {
70 std::cerr << "ProjectGradient imodel=" << imodel
71 << " igrad=" << igrad << std::endl;
72
73 ProjectGradient (model[imodel], igrad, input, output);
74 }
75
76 // sanity check, ensure that all elements have been set
77 if (igrad != input.size())
78 throw Error (InvalidState, "MEAL::ProjectGradient",
79 "on completion igrad=%d != ngrad=%d",
80 igrad, input.size());
81 }
82}
83
84#endif
static bool verbose
Verbosity flag.
Definition Function.h:54
Template combines a reference to a Component and its Projection.
Definition Projection.h:60
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16

Generated using doxygen 1.14.0