libDAI
fo.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * libDAI is licensed under the terms of the GNU General Public License version
4  * 2, or (at your option) any later version. libDAI is distributed without any
5  * warranty. See the file COPYING for more details.
6  *
7  * Copyright (C) 2009-2010 Joris Mooij [joris dot mooij at libdai dot org]
8  */
9 
10 
13 
14 
15 #ifndef __defined_libdai_fo_h
16 #define __defined_libdai_fo_h
17 
18 
19 namespace dai {
20 
21 
23 template<typename T> struct fo_id : public std::unary_function<T, T> {
25  T operator()( const T &x ) const {
26  return x;
27  }
28 };
29 
30 
32 template<typename T> struct fo_abs : public std::unary_function<T, T> {
34  T operator()( const T &x ) const {
35  if( x < (T)0 )
36  return -x;
37  else
38  return x;
39  }
40 };
41 
42 
44 template<typename T> struct fo_exp : public std::unary_function<T, T> {
46  T operator()( const T &x ) const {
47  return exp( x );
48  }
49 };
50 
51 
53 template<typename T> struct fo_log : public std::unary_function<T, T> {
55  T operator()( const T &x ) const {
56  return log( x );
57  }
58 };
59 
60 
62 template<typename T> struct fo_log0 : public std::unary_function<T, T> {
64  T operator()( const T &x ) const {
65  if( x )
66  return log( x );
67  else
68  return 0;
69  }
70 };
71 
72 
74 template<typename T> struct fo_inv : public std::unary_function<T, T> {
76  T operator()( const T &x ) const {
77  return 1 / x;
78  }
79 };
80 
81 
83 template<typename T> struct fo_inv0 : public std::unary_function<T, T> {
85  T operator()( const T &x ) const {
86  if( x )
87  return 1 / x;
88  else
89  return 0;
90  }
91 };
92 
93 
95 template<typename T> struct fo_plog0p : public std::unary_function<T, T> {
97  T operator()( const T &p ) const {
98  return p * dai::log0(p);
99  }
100 };
101 
102 
104 template<typename T> struct fo_divides0 : public std::binary_function<T, T, T> {
106  T operator()( const T &x, const T &y ) const {
107  if( y == (T)0 )
108  return (T)0;
109  else
110  return x / y;
111  }
112 };
113 
114 
116 template<typename T> struct fo_KL : public std::binary_function<T, T, T> {
118  T operator()( const T &p, const T &q ) const {
119  if( p == (T)0 )
120  return (T)0;
121  else
122  return p * (log(p) - log(q));
123  }
124 };
125 
126 
128 template<typename T> struct fo_Hellinger : public std::binary_function<T, T, T> {
130  T operator()( const T &p, const T &q ) const {
131  T x = sqrt(p) - sqrt(q);
132  return x * x;
133  }
134 };
135 
136 
138 template<typename T> struct fo_pow : public std::binary_function<T, T, T> {
140  T operator()( const T &x, const T &y ) const {
141  if( y != 1 )
142  return pow( x, y );
143  else
144  return x;
145  }
146 };
147 
148 
150 template<typename T> struct fo_max : public std::binary_function<T, T, T> {
152  T operator()( const T &x, const T &y ) const {
153  return (x > y) ? x : y;
154  }
155 };
156 
157 
159 template<typename T> struct fo_min : public std::binary_function<T, T, T> {
161  T operator()( const T &x, const T &y ) const {
162  return (x > y) ? y : x;
163  }
164 };
165 
166 
168 template<typename T> struct fo_absdiff : public std::binary_function<T, T, T> {
170  T operator()( const T &x, const T &y ) const {
171  return dai::abs( x - y );
172  }
173 };
174 
175 
176 } // end of namespace dai
177 
178 
179 #endif