aimstil  5.0.5
math_functions.h
Go to the documentation of this file.
1 #ifndef MATH_FUNCTIONS_H_
2 #define MATH_FUNCTIONS_H_
3 
4 
5 #include <functional>
6 
7 // TODO: I guess all this crap should rather go into functor? I mean, why Exp in functor and Gaussian in Math?
8 
9 namespace til { namespace math {
10 
11  //---------------------------------------------------------------------------
12 
13  //---------//
14  // huber //
15  //---------//
16 
20  template < typename T >
21  inline
22  T huber(T x, T K);
23 
24  //---------------------------------------------------------------------------
25 
26  //-----------//
27  // d_huber //
28  //-----------//
29 
33  template < typename T >
34  inline
35  T d_huber(T x, T K);
36 
37  //---------------------------------------------------------------------------
38 
39  //----------//
40  // shrink //
41  //----------//
42 
46  template < typename T >
47  inline
48  T shrink(T x, T K);
49 
50  //---------------------------------------------------------------------------
51 
52  //------------//
53  // Gaussian //
54  //------------//
55 
59  template < typename TPrec >
60  class Gaussian
61  : public std::unary_function<TPrec, TPrec>
62  {
63  public: // typedefs
64 
65  typedef TPrec prec_type;
66 
67  public: // exceptions
68 
69  class StandardDeviationTooSmall : public std::exception {};
70 
71  public: // constructors
72 
74  explicit Gaussian(prec_type sigma);
75 
76  public: // set & get
77 
78  prec_type getSigma() const { return std::sqrt(m_2var / 2); }
79 
80  inline void setSigma(prec_type sigma);
81 
82  public: // operators
83 
85  prec_type operator()(prec_type x) const;
86 
88  prec_type squared_input(prec_type x2) const;
89 
90  private: // data
91  prec_type m_2var;
92  };
93 
94  //---------------------------------------------------------------------------
95 
96  //---------------------------//
97  // IsotropicGaussianKernel //
98  //---------------------------//
99 
101  // NB: as a reminder to myself: should be labeled 'kernel' a function of two (spatial) parameters.
102  // So the 'gaussian kernel' really is exp(- ||x - y||^2)
103  // as opposed to the standard scalar gaussian function exp (-x^2)
104  // TODO: There should be a 'scalar matrix' class, with just a number. Then the GaussianKernel
105  // would be templated over the matrix type. When this matrix type is a scalar matrix, we get
106  // the fast isotropic gaussian kernel for free.
107  template < typename TArray, typename TPrec >
109  : public std::binary_function<const TArray &, const TArray &, TPrec>
110  {
111  public: // typedefs
112  //typedef typename TArray::value_type prec_type;
113  typedef TPrec prec_type;
114  public: // constructors
115  explicit IsotropicGaussianKernel(prec_type sigma) : m_gaussian(sigma) {}
116  public: // set & get
117  void setSigma(prec_type sigma) { m_gaussian.setSigma(sigma); }
118  prec_type getSigma() const { return m_gaussian.getSigma(); }
119  public: // functions
120  prec_type operator()(const TArray & x, const TArray & y) const
121  {
122  return m_gaussian.squared_input(dist2(x, y, prec<TPrec>()));
123  }
124  private: // data
125  Gaussian<prec_type> m_gaussian;
126  };
127 
128  //---------------------------------------------------------------------------
129 
130 }} // namespace til::math
131 
132 // package include
133 #include "math_functions.tpp"
134 
135 #endif /*MATH_FUNCTIONS_H_*/
Gaussian(prec_type sigma)
Constructor with Gaussian standard deviation.
T d_huber(T x, T K)
Derivative of Huber&#39;s penalty function.
void sqrt(const TImage &in, TImage &out)
Definition: imageArith.h:326
Unnormalized Gaussian.
T huber(T x, T K)
Huber&#39;s penalty function.
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
void setSigma(prec_type sigma)
Unnormalized isotropic Gaussian kernel.
T shrink(T x, T K)
The shrink operator if |x| > K then x +/- K else 0.
prec_type getSigma() const
prec_type squared_input(prec_type x2) const
Return the value of the Gaussian kernel at x, the input being x^2.
prec_type operator()(prec_type x) const
Return the value of the Gaussian kernel at x.
prec_type operator()(const TArray &x, const TArray &y) const
A dummy class used to pass a precision type for computations to some functions.
TPrec dist2(const numeric_array< T1, D > &v1, const numeric_array< T2, D > &v2, prec< TPrec >)
Return the squared Euclidean distance between two vectors, computed with a precision given as the fir...