Go to the documentation of this file.00001 namespace internal {
00002
00003 template<typename FunctorType, typename Scalar>
00004 DenseIndex fdjac1(
00005 const FunctorType &Functor,
00006 Matrix< Scalar, Dynamic, 1 > &x,
00007 Matrix< Scalar, Dynamic, 1 > &fvec,
00008 Matrix< Scalar, Dynamic, Dynamic > &fjac,
00009 DenseIndex ml, DenseIndex mu,
00010 Scalar epsfcn)
00011 {
00012 typedef DenseIndex Index;
00013
00014
00015 Scalar h;
00016 Index j, k;
00017 Scalar eps, temp;
00018 Index msum;
00019 int iflag;
00020 Index start, length;
00021
00022
00023 const Scalar epsmch = NumTraits<Scalar>::epsilon();
00024 const Index n = x.size();
00025 assert(fvec.size()==n);
00026 Matrix< Scalar, Dynamic, 1 > wa1(n);
00027 Matrix< Scalar, Dynamic, 1 > wa2(n);
00028
00029 eps = sqrt(std::max(epsfcn,epsmch));
00030 msum = ml + mu + 1;
00031 if (msum >= n) {
00032
00033 for (j = 0; j < n; ++j) {
00034 temp = x[j];
00035 h = eps * abs(temp);
00036 if (h == 0.)
00037 h = eps;
00038 x[j] = temp + h;
00039 iflag = Functor(x, wa1);
00040 if (iflag < 0)
00041 return iflag;
00042 x[j] = temp;
00043 fjac.col(j) = (wa1-fvec)/h;
00044 }
00045
00046 }else {
00047
00048 for (k = 0; k < msum; ++k) {
00049 for (j = k; (msum<0) ? (j>n): (j<n); j += msum) {
00050 wa2[j] = x[j];
00051 h = eps * abs(wa2[j]);
00052 if (h == 0.) h = eps;
00053 x[j] = wa2[j] + h;
00054 }
00055 iflag = Functor(x, wa1);
00056 if (iflag < 0)
00057 return iflag;
00058 for (j = k; (msum<0) ? (j>n): (j<n); j += msum) {
00059 x[j] = wa2[j];
00060 h = eps * abs(wa2[j]);
00061 if (h == 0.) h = eps;
00062 fjac.col(j).setZero();
00063 start = std::max<Index>(0,j-mu);
00064 length = std::min(n-1, j+ml) - start + 1;
00065 fjac.col(j).segment(start, length) = ( wa1.segment(start, length)-fvec.segment(start, length))/h;
00066 }
00067 }
00068 }
00069 return 0;
00070 }
00071
00072 }