vnl_matrix_exp.hxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_matrix_exp.hxx
2 #ifndef vnl_matrix_exp_hxx_
3 #define vnl_matrix_exp_hxx_
4 //:
5 // \file
6 // \author fsm
7 #include <iostream>
8 #include "vnl_matrix_exp.h"
9 #include <cassert>
10 #ifdef DEBUG
11 #ifdef _MSC_VER
12 # include <vcl_msvc_warnings.h>
13 #endif
14 #endif
15 
16 template <class Matrix>
17 bool vnl_matrix_exp(Matrix const &X, Matrix &expX, double max_err)
18 {
19  assert(X.rows() == X.cols());
20  assert(X.rows() == expX.rows());
21  assert(X.cols() == expX.cols());
22  assert(max_err > 0);
23 
24  double norm_X = X.operator_inf_norm();
25 #ifdef DEBUG
26  std::cerr << "norm_X = " << norm_X << std::endl;
27 #endif
28 
29  // exponential series
30  expX.set_identity();
31  Matrix acc(X);
32  double norm_acc_bound = norm_X;
33  for (unsigned n=1; true; ++n) {
34  expX += acc;
35 #ifdef DEBUG
36  std::cerr << "n=" << n << std::endl;
37 #endif
38 
39  if (norm_X < n) {
40  double err_bound = norm_acc_bound / (1 - norm_X/n);
41 #ifdef DEBUG
42  std::cerr << "err_bound = " << err_bound << std::endl;
43 #endif
44  if (err_bound < max_err)
45  break;
46  }
47 
48  acc = acc * X;
49  acc /= n+1;
50 
51  norm_acc_bound *= norm_X/(n+1);
52  }
53 
54  return true;
55 }
56 
57 
58 template <class Matrix>
59 Matrix vnl_matrix_exp(Matrix const &X)
60 {
61  Matrix expX(X.rows(), X.cols());
62 #ifndef NDEBUG
63  bool retval =
64 #endif
65  vnl_matrix_exp(X, expX, 1e-10);
66 
67  assert(retval);
68  return expX;
69 }
70 
71 //------------------------------------------------------------------------------
72 
73 #undef VNL_MATRIX_EXP_INSTANTIATE
74 #define VNL_MATRIX_EXP_INSTANTIATE(Matrix) \
75 template VNL_EXPORT bool vnl_matrix_exp(Matrix const&, Matrix &, double); \
76 template VNL_EXPORT Matrix vnl_matrix_exp(Matrix const&)
77 
78 #endif // vnl_matrix_exp_hxx_
VNL_EXPORT bool vnl_matrix_exp(SquareMatrix const &X, SquareMatrix &expX, double max_err)
Compute the exponential of a square matrix - fiddly form.
Compute the exponential of a square matrix.