PICCANTE  0.4
The hottest HDR imaging library!
nelder_mead_opt_positive_polynomial.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_COMPUTER_VISION_NELDER_MEAD_OPT_POSITIVE_POLYNOMIAL_HPP
19 #define PIC_COMPUTER_VISION_NELDER_MEAD_OPT_POSITIVE_POLYNOMIAL_HPP
20 
21 #include <random>
22 
23 #include "../util/nelder_mead_opt_base.hpp"
24 #include "../util/math.hpp"
25 #include "../util/polynomial.hpp"
26 
27 namespace pic {
28 
30 {
31 protected:
32  std::vector< float > px, py;
33 
34 public:
35  NelderMeadOptPositivePolynomial(std::vector< float > &px, std::vector< float > &py) : NelderMeadOptBase()
36  {
37  if(px.size() == py.size()) {
38  this->px.assign(px.begin(), px.end());
39  this->py.assign(py.begin(), py.end());
40  }
41  }
42 
43  float function(float *x, unsigned int n)
44  {
45 
46  Polynomial poly(x, n);
47 
48  float err = 0.0f;
49  for(auto i = 0; i < px.size(); i++) {
50  float py_i = poly.eval(px[i]);
51  if(py_i > 0.0f) {
52  float delta_y = py_i - py[i];
53  err += (delta_y * delta_y);
54  } else {
55  err += 1e6f;
56  }
57  }
58 
59  return err;
60  }
61 
62 #ifndef PIC_DISABLE_EIGEN
63 
64  static void test()
65  {
66  std::mt19937 m(1);
67 
68  std::vector< float > x, y;
69 
70  for(int i = 0; i < 100; i++) {
71  float tx = float(i) / 100.0f;
72  float ty = tx + (getRandom(m()) * 0.01f - 0.05f); //noise
73  float ty_sq = ty * ty;
74  x.push_back(tx);
75  y.push_back(ty_sq);
76  }
77 
79 
80  Polynomial poly;
81  poly.fit(x, y, 2);
82 
83  float *in = poly.getArray(NULL);
84 
85  float *out = test.run(in, 3, 1e-12f, 100000);
86 
87  printf("In: [%f %f %f]\nOut: [%f %f %f]\n", in[2], in[0], in[1], out[2], out[1], out[0]);
88  }
89 
90 #endif
91 };
92 
93 }
94 
95 #endif // PIC_COMPUTER_VISION_NELDER_MEAD_OPT_POSITIVE_POLYNOMIAL_HPP
float eval(float x)
eval
Definition: polynomial.hpp:166
std::vector< float > py
Definition: nelder_mead_opt_positive_polynomial.hpp:32
float * getArray(float *ret)
getArray
Definition: polynomial.hpp:148
Definition: polynomial.hpp:37
static void test()
Definition: nelder_mead_opt_positive_polynomial.hpp:64
The NelderMeadOptBase class.
Definition: nelder_mead_opt_base.hpp:31
std::vector< float > px
Definition: nelder_mead_opt_positive_polynomial.hpp:32
Definition: bilateral_separation.hpp:25
PIC_INLINE float getRandom(unsigned int n)
Random returns a number in [0, 2^32 - 1] to a float in [0, 1].
Definition: math.hpp:138
NelderMeadOptPositivePolynomial(std::vector< float > &px, std::vector< float > &py)
Definition: nelder_mead_opt_positive_polynomial.hpp:35
Definition: nelder_mead_opt_positive_polynomial.hpp:29
void fit(std::vector< float > &x, std::vector< float > &y, int n)
fit
Definition: polynomial.hpp:204