KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
diff_util.h
Go to the documentation of this file.
1 /* =========================================================================== *
2  |
3  | Copyright (c) 1994-2011 by Kobus Barnard (author)
4  |
5  | Personal and educational use of this code is granted, provided that this
6  | header is kept intact, and that the authorship is not misrepresented, that
7  | its use is acknowledged in publications, and relevant papers are cited.
8  |
9  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
10  |
11  | Please note that the code in this file has not necessarily been adequately
12  | tested. Naturally, there is no guarantee of performance, support, or fitness
13  | for any particular task. Nonetheless, I am interested in hearing about
14  | problems that you encounter.
15  |
16  | Author: Ernesto Brau
17  * =========================================================================== */
18 
19 /* $Id$ */
20 
21 #ifndef DIFF_ADAPTER_H
22 #define DIFF_ADAPTER_H
23 
24 #include <l_cpp/l_exception.h>
25 #include <iostream>
26 #include <vector>
27 #include <algorithm>
28 #include <utility>
29 
30 namespace kjb {
31 
41 template<class Vec>
43 {
44 public:
45  typedef Vec Model_type;
46 
47 public:
49  double get(const Vec* x, size_t i) const
50  {
51  return (*x)[i];
52  }
53 
55  void set(Vec* x, size_t i, double v) const
56  {
57  (*x)[i] = v;
58  }
59 
61  void set(Vec* x, size_t i, size_t j, double v, double w) const
62  {
63  if(i == j)
64  {
65  std::cerr << "WARNING: setting same dimension to two "
66  << "different values."
67  << std::endl;
68  }
69 
70  set(x, i, v);
71  set(x, j, w);
72  }
73 
75  void set(Vec* x, const Vec& v) const;
76 
78  size_t size(const Vec* x) const
79  {
80  return x->size();
81  }
82 };
83 
84 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
85 
86 template<class Vec>
87 void Vector_adapter<Vec>::set(Vec* x, const Vec& v) const
88 {
89  const size_t D = x->size();
90  IFT(D == v.size(), Illegal_argument,
91  "cannot set model to vector: dimension must match.");
92 
93  for(size_t i = 0; i < D; i++)
94  {
95  set(x, i, v[i]);
96  }
97 }
98 
100 template<class Model, class Adapter>
101 inline
102 void move_param(Model& x, size_t i, double dv, const Adapter& aptr)
103 {
104  aptr.set(&x, i, aptr.get(&x, i) + dv);
105 }
106 
108 template<class Model, class Adapter>
109 inline
110 void move_params
111 (
112  Model& x,
113  size_t i,
114  size_t j,
115  double dv,
116  double dw,
117  const Adapter& aptr
118 )
119 {
120  aptr.set(&x, i, j, aptr.get(&x, i) + dv, aptr.get(&x, j) + dw);
121 }
122 
124 template<class Model, class Vec, class Adapter>
125 void move_params(Model& x, const Vec& dx, const Adapter& aptr)
126 {
127  const size_t D = aptr.size(&x);
128  IFT(D == dx.size(), Illegal_argument,
129  "cannot move model by vector: dimension must match.");
130 
131  Vec vx(D);
132  for(size_t i = 0; i < D; i++)
133  {
134  vx[i] = aptr.get(&x, i) + dx[i];
135  }
136 
137  aptr.set(&x, vx);
138 }
139 
143 template<class M, class A>
144 bool next_point
145 (
146  const std::vector<std::pair<double, double> >& bounds,
147  const std::vector<double>& widths,
148  size_t nbins,
149  std::vector<size_t>& indices,
150  M& x,
151  const A& adapter
152 )
153 {
154  using namespace std;
155 
156  size_t D = bounds.size();
157 
158  // first time called
159  if(indices.empty())
160  {
161  indices.resize(D, 0);
162  for(size_t d = 0; d < D; d++)
163  {
164  //x[d] = bounds[d].first + widths[d]/2;
165  adapter.set(&x, d, bounds[d].first + widths[d]/2);
166  }
167 
168  return true;
169  }
170 
171  // find element to change
172  vector<size_t>::reverse_iterator s_p
173  = find_if(indices.rbegin(), indices.rend(),
174  bind2nd(not_equal_to<size_t>(), nbins - 1));
175 
176  if(s_p == indices.rend())
177  {
178  return false;
179  }
180 
181  // increment and reset everything after
182  (*s_p)++;
183  fill(indices.rbegin(), s_p, 0);
184 
185  // find corresponding point index
186  size_t i = D - 1 - std::distance(indices.rbegin(), s_p);
187  //x[i] += widths[i];
188  move_param(x, i, widths[i], adapter);
189  for(size_t d = i + 1; d < D; d++)
190  {
191  //x[d] = bounds[d].first + widths[d]/2;
192  adapter.set(&x, d, bounds[d].first + widths[d]/2);
193  }
194 
195  return true;
196 }
197 
198 } //namespace kjb
199 
200 #endif /*DIFF_ADAPTER_H */
201 
202 
void move_param(Model &x, size_t i, double dv, const Adapter &aptr)
Helper function that moves a parameter by an amount.
Definition: diff_util.h:102
void set(Vec *x, size_t i, size_t j, double v, double w) const
Sets the ith element of x; i.e., x[i] = v.
Definition: diff_util.h:61
#define IFT(a, ex, msg)
Definition: l_exception.h:101
bool next_point(const std::vector< std::pair< double, double > > &bounds, const std::vector< double > &widths, size_t nbins, std::vector< size_t > &indices, M &x, const A &adapter)
Gets the next point in a N-dimensional grid.
Definition: diff_util.h:145
void move_params(Model &x, size_t i, size_t j, double dv, double dw, const Adapter &aptr)
Helper function that moves a pair of parameters by an amount.
Definition: diff_util.h:111
x
Definition: APPgetLargeConnectedEdges.m:100
Default adapter for the hessian function.
Definition: diff_util.h:42
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
size_t size(const Vec *x) const
Returns the size of x; i.e., x.size().
Definition: diff_util.h:78
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
D
Definition: APPgetLargeConnectedEdges.m:106
Support for error handling exception classes in libKJB.
Vec Model_type
Definition: diff_util.h:45
void set(Vec *x, size_t i, double v) const
Sets the ith element of x; i.e., x[i] = v.
Definition: diff_util.h:55