Templates -- Meow  1.1.2
不能,也不應該先編譯成obj-file的templates
methods.h
Go to the documentation of this file.
1 #ifndef math_methods_H__
2 #define math_methods_H__
3 
4 #include "Matrix.h"
5 #include "Vector.h"
6 #include "utility.h"
7 
8 #include <cstdlib>
9 #include <vector>
10 
11 namespace meow {
12 
57 template<class Data, class WeightingClass>
58 inline std::vector<Data> ransac(std::vector<Data> const& data,
59  WeightingClass const& w,
60  size_t N,
61  double p0, double P) {
62  if (data.size() < N) {
63  return std::vector<Data>();
64  }
65  double ww = -1.0;
66  std::vector<Data> ret;
67  for (double count = ceil(log(1.0 - P) / log(1.0 - pow(p0, N)));
68  count > 0.0; count -= 1.0) {
69  std::vector<Data> sample;
70  std::vector<int> index(N);
71  for (size_t i = 0; i < N; i++) {
72  for (bool ok = false; !ok; ) {
73  index[i] = rand() % data.size();
74  ok = true;
75  for (size_t j = 0; ok && j < i; j++)
76  if (index[i] == index[j])
77  ok = false;
78  }
79  sample.push_back(data[index[i]]);
80  }
81  double w_now = w(sample, data);
82  if (w_now < 0) {
83  count += 0.5;
84  continue;
85  }
86  if (ww < w_now) {
87  ret = sample;
88  ww = w_now;
89  }
90  }
91  return ret;
92 }
93 
94 
162 template<class Scalar, class F, class J, class I, class Stop>
163 inline Vector<Scalar> levenbergMarquardt(F const& func,
164  J const& jaco,
165  I const& iden,
166  Vector<Scalar> const& init,
167  Stop const& stop,
168  int counter = -1) {
169  Vector<Scalar> ans(init);
170  for(Vector<Scalar> rv;
171  !stop((rv = func(ans)).length2()) && counter != 0; counter--) {
172  Matrix<Scalar> j(jaco(ans)), jt(j.transpose());
173  Matrix<Scalar> i(iden(ans));
174  ans = ans - Vector<Scalar>((jt * j + i).inverse() * jt * rv.matrix());
175  }
176  return ans;
177 }
178 
179 }
180 
181 #endif // math_methods_H__