sparseLM.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2012 Desire Nuentsa <desire.nuentsa_wakam@inria.fr>
5 // Copyright (C) 2012 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <iostream>
11 #include <fstream>
12 #include <iomanip>
13 
14 #include "main.h"
15 #include <Eigen/LevenbergMarquardt>
16 
17 using namespace std;
18 using namespace Eigen;
19 
20 template <typename Scalar>
21 struct sparseGaussianTest : SparseFunctor<Scalar, int>
22 {
25  typedef typename Base::JacobianType JacobianType;
27  { }
28 
30  {
31  VectorType y; //Change this to use expression template
32  int m = Base::values();
33  int n = Base::inputs();
34  eigen_assert(uv.size()%2 == 0);
35  eigen_assert(uv.size() == n);
36  eigen_assert(x.size() == m);
37  y.setZero(m);
38  int half = n/2;
41  Scalar coeff;
42  for (int j = 0; j < m; j++)
43  {
44  for (int i = 0; i < half; i++)
45  {
46  coeff = (x(j)-i)/v(i);
47  coeff *= coeff;
48  if (coeff < 1. && coeff > 0.)
49  y(j) += u(i)*std::pow((1-coeff), 2);
50  }
51  }
52  return y;
53  }
55  {
56  m_x = x;
57  m_y = this->model(uv_ref,x);
58  }
59  int operator()(const VectorType& uv, VectorType& fvec)
60  {
61  int m = Base::values();
62  int n = Base::inputs();
63  eigen_assert(uv.size()%2 == 0);
64  eigen_assert(uv.size() == n);
65  int half = n/2;
68  fvec = m_y;
69  Scalar coeff;
70  for (int j = 0; j < m; j++)
71  {
72  for (int i = 0; i < half; i++)
73  {
74  coeff = (m_x(j)-i)/v(i);
75  coeff *= coeff;
76  if (coeff < 1. && coeff > 0.)
77  fvec(j) -= u(i)*std::pow((1-coeff), 2);
78  }
79  }
80  return 0;
81  }
82 
83  int df(const VectorType& uv, JacobianType& fjac)
84  {
85  int m = Base::values();
86  int n = Base::inputs();
87  eigen_assert(n == uv.size());
88  eigen_assert(fjac.rows() == m);
89  eigen_assert(fjac.cols() == n);
90  int half = n/2;
93  Scalar coeff;
94 
95  //Derivatives with respect to u
96  for (int col = 0; col < half; col++)
97  {
98  for (int row = 0; row < m; row++)
99  {
100  coeff = (m_x(row)-col)/v(col);
101  coeff = coeff*coeff;
102  if(coeff < 1. && coeff > 0.)
103  {
104  fjac.coeffRef(row,col) = -(1-coeff)*(1-coeff);
105  }
106  }
107  }
108  //Derivatives with respect to v
109  for (int col = 0; col < half; col++)
110  {
111  for (int row = 0; row < m; row++)
112  {
113  coeff = (m_x(row)-col)/v(col);
114  coeff = coeff*coeff;
115  if(coeff < 1. && coeff > 0.)
116  {
117  fjac.coeffRef(row,col+half) = -4 * (u(col)/v(col))*coeff*(1-coeff);
118  }
119  }
120  }
121  return 0;
122  }
123 
124  VectorType m_x, m_y; //Data points
125 };
126 
127 
128 template<typename T>
130 {
132 
133  int inputs = 10;
134  int values = 2000;
135  sparseGaussianTest<T> sparse_gaussian(inputs, values);
136  VectorType uv(inputs),uv_ref(inputs);
138  // Generate the reference solution
139  uv_ref << -2, 1, 4 ,8, 6, 1.8, 1.2, 1.1, 1.9 , 3;
140  //Generate the reference data points
141  x.setRandom();
142  x = 10*x;
143  x.array() += 10;
144  sparse_gaussian.initPoints(uv_ref, x);
145 
146 
147  // Generate the initial parameters
148  VectorBlock<VectorType> u(uv, 0, inputs/2);
149  VectorBlock<VectorType> v(uv, inputs/2, inputs/2);
150  v.setOnes();
151  //Generate u or Solve for u from v
152  u.setOnes();
153 
154  // Solve the optimization problem
155  LevenbergMarquardt<sparseGaussianTest<T> > lm(sparse_gaussian);
156  int info;
157 // info = lm.minimize(uv);
158 
160  // Do a step by step solution and save the residual
161  int maxiter = 200;
162  int iter = 0;
163  MatrixXd Err(values, maxiter);
164  MatrixXd Mod(values, maxiter);
166  status = lm.minimizeInit(uv);
168  return ;
169 
170 }
172 {
173  CALL_SUBTEST_1(test_sparseLM_T<double>());
174 
175  // CALL_SUBTEST_2(test_sparseLM_T<std::complex<double>());
176 }
Eigen::SparseMatrix::cols
Index cols() const
Definition: SparseMatrix.h:140
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::SparseMatrix
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
col
m col(1)
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::SparseFunctor< Scalar, int >::Scalar
Scalar Scalar
Definition: LevenbergMarquardt/LevenbergMarquardt.h:71
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(sparseLM)
Definition: sparseLM.cpp:171
sparseGaussianTest
Definition: sparseLM.cpp:21
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
Eigen::LevenbergMarquardt
Performs non linear optimization over a non-linear function, using a variant of the Levenberg Marquar...
Definition: LevenbergMarquardt/LevenbergMarquardt.h:110
sparseGaussianTest::JacobianType
Base::JacobianType JacobianType
Definition: sparseLM.cpp:25
n
int n
Definition: BiCGSTAB_simple.cpp:1
sparseGaussianTest::m_y
VectorType m_y
Definition: sparseLM.cpp:124
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
sparseGaussianTest::VectorType
Matrix< Scalar, Dynamic, 1 > VectorType
Definition: sparseLM.cpp:23
Eigen::LevenbergMarquardtSpace::Status
Status
Definition: LevenbergMarquardt/LevenbergMarquardt.h:25
Eigen::LevenbergMarquardt::minimizeInit
LevenbergMarquardtSpace::Status minimizeInit(FVectorType &x)
Definition: LevenbergMarquardt/LevenbergMarquardt.h:294
Eigen::LevenbergMarquardtSpace::ImproperInputParameters
@ ImproperInputParameters
Definition: LevenbergMarquardt/LevenbergMarquardt.h:28
info
else if n * info
Definition: 3rdparty/Eigen/lapack/cholesky.cpp:18
sparseGaussianTest::operator()
int operator()(const VectorType &uv, VectorType &fvec)
Definition: sparseLM.cpp:59
sparseGaussianTest::model
VectorType model(const VectorType &uv, VectorType &x)
Definition: sparseLM.cpp:29
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
ceres::pow
Jet< T, N > pow(const Jet< T, N > &f, double g)
Definition: jet.h:570
model
noiseModel::Diagonal::shared_ptr model
Definition: doc/Code/Pose2SLAMExample.cpp:7
sparseGaussianTest::sparseGaussianTest
sparseGaussianTest(int inputs, int values)
Definition: sparseLM.cpp:26
y
Scalar * y
Definition: level1_cplx_impl.h:124
sparseGaussianTest::initPoints
void initPoints(VectorType &uv_ref, VectorType &x)
Definition: sparseLM.cpp:54
sparseGaussianTest::df
int df(const VectorType &uv, JacobianType &fjac)
Definition: sparseLM.cpp:83
test_sparseLM_T
void test_sparseLM_T()
Definition: sparseLM.cpp:129
main.h
iter
iterator iter(handle obj)
Definition: pytypes.h:2475
return
if n return
Definition: level1_cplx_impl.h:33
row
m row(1)
std
Definition: BFloat16.h:88
Eigen::VectorBlock
Expression of a fixed-size or dynamic-size sub-vector.
Definition: ForwardDeclarations.h:85
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Eigen::Matrix< Scalar, Dynamic, 1 >
VectorType
Definition: FFTW.cpp:65
Eigen::SparseMatrix::rows
Index rows() const
Definition: SparseMatrix.h:138
Eigen::half
Definition: Half.h:142
Eigen::SparseMatrix::coeffRef
Scalar & coeffRef(Index row, Index col)
Definition: SparseMatrix.h:208
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::SparseFunctor
Definition: LevenbergMarquardt/LevenbergMarquardt.h:69
sparseGaussianTest::Base
SparseFunctor< Scalar, int > Base
Definition: sparseLM.cpp:24


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:30