1 #ifndef math_methods_H__
2 #define math_methods_H__
57 template<
class Data,
class WeightingClass>
58 inline std::vector<Data>
ransac(std::vector<Data>
const& data,
59 WeightingClass
const& w,
61 double p0,
double P) {
62 if (data.size() < N) {
63 return std::vector<Data>();
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();
75 for (
size_t j = 0; ok && j < i; j++)
76 if (index[i] == index[j])
79 sample.push_back(data[index[i]]);
81 double w_now = w(sample, data);
162 template<
class Scalar,
class Function>
167 for ( ; counter != 0 && !f.accept(residure_v = f.residure(ans)); --counter) {
171 for (
int i = 1; M.
valid() ==
false; i++) {
172 M = (m + f.diagonal(ans, i)).inverse();
174 ans = ans - M * m_jt * residure_v;
182 template<
class Scalar,
class Function>
185 Scalar
const& init_mu,
186 Scalar
const& mu_pow,
187 Scalar
const& er_max,
190 if (retry_number == 0) retry_number = 1;
193 Scalar er_now(rv_now.length2());
196 Scalar er_best ( er_now);
199 for ( ; counter != 0 && er_now > er_max; --counter) {
204 for (
int i = 0; i != retry_number; ++i, mu = mu * mu_pow) {
205 ans_nxt = ans_now + (m + iden * mu).inverse() * m_jt * rv_now.matrix();
206 rv_nxt = f.residure(ans_nxt);
208 if (er_nxt <= er_now) {
216 mu =
inRange(0.0000001, 100.0, mu);
220 if (er_now < er_best) {
230 #endif // math_methods_H__
Vector< Scalar > levenbergMarquardtTraining(Function &f, Vector< Scalar > const &init, Scalar const &init_mu, Scalar const &mu_pow, Scalar const &er_max, int retry_number, int counter)
Scalar length2() const
same as (*this).dot(*this)
Vector< Scalar > levenbergMarquardt(Function const &f, Vector< Scalar > const &init, int counter=-1)
bool valid() const
Return whether it is a valid matrix.
T inRange(T const &mn, T const &mx, T const &v)
std::min(mx,std::max(mn,v))
std::vector< Data > ransac(std::vector< Data > const &data, WeightingClass const &w, size_t N, double p0, double P)
Run the RANSAC method to approach the best solution.
Matrix transpose() const
return itself's transpose matrix