vnl_discrete_diff.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <cassert>
3 #include "vnl_discrete_diff.h"
5 
6 /*
7  fsm
8 */
9 
11  double h_,
12  vnl_vector<double> const &x,
14 {
16  lsf->f(x,y);
17  if (lsf->failure)
18  return false;
20  h.fill(h_);
21  return vnl_discrete_diff_fwd(lsf,h,x,y,J);
22 }
23 
25  vnl_vector<double> const &h,
26  vnl_vector<double> const &x,
28 {
30  lsf->f(x,y);
31  if (lsf->failure)
32  return false;
33  return vnl_discrete_diff_fwd(lsf,h,x,y,J);
34 }
35 
37  vnl_vector<double> const &h,
38  vnl_vector<double> const &x,
39  vnl_vector<double> const &y,
41 {
42  unsigned m=J.rows();
43  unsigned n=J.columns();
44  assert(m==lsf->get_number_of_residuals());
45  assert(m==y.size());
46  assert(n==lsf->get_number_of_unknowns());
47  assert(n==h.size());
48  assert(n==x.size());
49 
50  vnl_vector<double> tx(n);
52 
53  for (unsigned j=0;j<n;j++) {
54  tx=x; tx(j) += h(j);
55  lsf->f(tx,ty);
56  if (lsf->failure)
57  return false;
58  for (unsigned i=0;i<m;i++)
59  J(i,j) = (ty(i)-y(i))/h(j);
60  }
61  return true;
62 }
63 
65  double h_,
66  vnl_vector<double> const &x,
68 {
70  h.fill(h_);
71  return vnl_discrete_diff_sym(lsf,h,x,J);
72 }
73 
75  vnl_vector<double> const &h,
76  vnl_vector<double> const &x,
78 {
79  unsigned m=J.rows();
80  unsigned n=J.columns();
81  assert(m==lsf->get_number_of_residuals());
82  assert(n==lsf->get_number_of_unknowns());
83  assert(n==h.size());
84  assert(n==x.size());
85 
86  vnl_vector<double> xp(n),xm(n);
87  vnl_vector<double> yp(m),ym(m);
88 
89  for (unsigned j=0;j<n;j++) {
90  xp=x; xp(j) += h(j);
91  lsf->f(xp,yp);
92  if (lsf->failure)
93  return false;
94 
95  xm=x; xm(j) -= h(j);
96  lsf->f(xm,ym);
97  if (lsf->failure)
98  return false;
99 
100  for (unsigned i=0;i<m;i++)
101  J(i,j) = (yp(i)-ym(i))/(2*h(j));
102  }
103  return true;
104 }
105 
106 //----------------------------------------------------------------------
107 
109 {
110  unsigned int m = lsf->get_number_of_residuals();
111  unsigned int n = lsf->get_number_of_unknowns ();
112  assert(x.size() == n);
113 
114  vnl_matrix<double> J1(m, n);
115  lsf->gradf(x, J1);
116 
117  vnl_matrix<double> J2(m, n);
118  vnl_discrete_diff_sym(lsf, 0.0001, x, J2);
119 
120  double e = (J1 - J2).fro_norm();
121  //assert(e <= 1e-3);
122  double t = cos_angle(J1, J2);
123  //assert(t >= 0.99);
124 
125  std::cerr << __FILE__ ": e = " << e << std::endl
126  << __FILE__ ": t = " << t << std::endl;
127 }
void vnl_discrete_diff_test_lsf(vnl_least_squares_function *lsf, vnl_vector< double > const &x)
bool VNL_ALGO_EXPORT vnl_discrete_diff_fwd(vnl_least_squares_function *lsf, double h, vnl_vector< double > const &x, vnl_matrix< double > &J)
forward differences.
Abstract base for minimising functions.
Functions to compute jacobians of vnl_least_squares_functions.
#define m
Definition: vnl_vector.h:43
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
unsigned int get_number_of_unknowns() const
Return the number of unknowns.
Abstract base for minimising functions.
virtual void gradf(vnl_vector< double > const &x, vnl_matrix< double > &jacobian)
Calculate the Jacobian, given the parameter vector x.
bool VNL_ALGO_EXPORT vnl_discrete_diff_sym(vnl_least_squares_function *lsf, double h, vnl_vector< double > const &x, vnl_matrix< double > &J)
symmetric differences.
vnl_vector & fill(T const &v)
Set all values to v.
Definition: vnl_vector.hxx:314
unsigned int get_number_of_residuals() const
Return the number of residuals.
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
VNL_EXPORT T cos_angle(m const &, m const &)
virtual void f(vnl_vector< double > const &x, vnl_vector< double > &fx)=0
The main function.
unsigned int columns() const
Return the number of columns.
Definition: vnl_matrix.h:187