vnl_power.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_power.h
2 #ifndef vnl_power_h_
3 #define vnl_power_h_
4 //:
5 // \file
6 // \brief Calculates nth power of a small vnl_matrix_fixed (not using svd)
7 // \author Peter Vanroose
8 // \date 21 July 2009
9 //
10 // \verbatim
11 // Modifications
12 // <none yet>
13 // \endverbatim
14 
15 #include <vnl/vnl_matrix_fixed.h>
16 #include <vnl/vnl_matrix.h>
17 #include <vnl/vnl_inverse.h> // used for negative powers
18 #include <cassert>
19 #ifdef _MSC_VER
20 # include <vcl_msvc_warnings.h>
21 #endif
22 #include "vnl/vnl_export.h"
23 
24 //: Calculates nth power of a vnl_matrix_fixed (not using svd)
25 // This allows you to write e.g.
26 //
27 // x = vnl_power(A,7) * vnl_power(B,-4) * b;
28 //
29 // Note that this function is inlined (except for the call to vnl_inverse()),
30 // which makes it much faster than a full-fledged square matrix power
31 // implementation using svd, which belongs in vnl/algo.
32 //
33 // \relatesalso vnl_matrix_fixed
34 
35 template <class T, unsigned int d>
37 {
38  assert(n >= 0 || d <= 4); // to allow the use of vnl_inverse()
39  if (n == 0)
41  else if (n == 1 || m.is_identity())
42  return m;
43  else if (n < 0)
44  return vnl_inverse(vnl_power(m, -n));
45  else {
47  return n%2==0 ? r * r : r * r * m;
48  }
49 }
50 
51 //: Calculates nth power of a square vnl_matrix (not using svd)
52 // This allows you to write e.g.
53 //
54 // x = vnl_power(A,7) * vnl_power(B,-4) * b;
55 //
56 // Note that this function is inlined (except for the call to vnl_inverse()),
57 // which makes it much faster than a full-fledged square matrix power
58 // implementation using svd, which belongs in vnl/algo.
59 //
60 // \relatesalso vnl_matrix
61 
62 template <class T>
64 {
65  assert(m.rows() == m.columns());
66  assert(n >= 0 || m.rows() <= 4);
67  if (n == 0)
68  return vnl_matrix<T>(m.rows(),m.columns()).set_identity();
69  else if (n == 1 || m.is_identity())
70  return m;
71  else if (n < 0 && m.rows() == 1)
72  return vnl_power(vnl_matrix_fixed<T,1,1>(m),n).as_ref();
73  else if (n < 0 && m.rows() == 2)
74  return vnl_power(vnl_matrix_fixed<T,2,2>(m),n).as_ref();
75  else if (n < 0 && m.rows() == 3)
76  return vnl_power(vnl_matrix_fixed<T,3,3>(m),n).as_ref();
77  else if (n < 0 && m.rows() == 4)
78  return vnl_power(vnl_matrix_fixed<T,4,4>(m),n).as_ref();
79  else {
80  vnl_matrix<T> r = vnl_power(m, n/2);
81  return n%2==0 ? r * r : r * r * m;
82  }
83 }
84 
85 #endif // vnl_power_h_
vnl_matrix_fixed< T, 1, 1 > vnl_inverse(vnl_matrix_fixed< T, 1, 1 > const &m)
Calculates inverse of a small vnl_matrix_fixed (not using svd).
Definition: vnl_inverse.h:39
An ordinary mathematical matrix.
#define m
Definition: vnl_vector.h:43
Fixed size, stack-stored, space-efficient matrix.
Definition: vnl_fwd.h:23
vnl_matrix_fixed & set_identity()
Sets this matrix to an identity matrix, then returns "*this".
Calculates inverse of a small vnl_matrix_fixed (not using svd)
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
vnl_matrix_fixed< T, d, d > vnl_power(vnl_matrix_fixed< T, d, d > const &m, int n)
Calculates nth power of a vnl_matrix_fixed (not using svd).
Definition: vnl_power.h:36
fixed size matrix