IT++ Logo
help_functions.h
Go to the documentation of this file.
1 
29 #ifndef HELP_FUNCTIONS_H
30 #define HELP_FUNCTIONS_H
31 
32 #include <itpp/base/mat.h>
33 #include <itpp/itexports.h>
34 
35 namespace itpp
36 {
38 namespace details
39 {
40 //it would be nice to use binders and functors from <functional> header, but
41 //bind1st/bind2nd are broken since they can not be used with functions
42 //getting arguments by reference, so we introduce our own simple functors and
43 //binders here
44 
45 //functor made from unary function Ret Ftn(Arg)
46 template<typename Ret, typename Arg>
47 class Unary_Function_Wrapper
48 {
49  typedef Ret(*Ftn)(Arg);
50  Ftn _f;
51 public:
52  explicit Unary_Function_Wrapper(Ftn f): _f(f) {}
53  Ret operator()(Arg v) const { return _f(v);}
54 };
55 
56 //functor made from binary function Ret Ftn(Arg, Arg) with 1st arg bound
57 template<typename Ret, typename Arg>
58 class Binary_Function_With_Bound_1st
59 {
60  typedef Ret(*Ftn)(Arg, Arg);
61  Arg _b;
62  Ftn _f;
63 public:
64  Binary_Function_With_Bound_1st(Ftn f, const Arg b): _f(f), _b(b) {}
65  Ret operator()(Arg v) const { return _f(_b, v);}
66 };
67 
68 //functor made from binary function Ret Ftn(const Arg&, const Arg&) with 1st arg bound
69 //(partially specialize previous template)
70 template<typename Ret, typename Arg>
71 class Binary_Function_With_Bound_1st<Ret, const Arg&>
72 {
73  typedef Ret(*Ftn)(const Arg&, const Arg&);
74  const Arg& _b;
75  Ftn _f;
76 public:
77  Binary_Function_With_Bound_1st(Ftn f, const Arg& b): _f(f), _b(b) {}
78  Ret operator()(const Arg& v) const { return _f(_b, v);}
79 };
80 
81 //functor made from binary function Ret Ftn(Arg, Arg) with 2nd arg bound
82 template<typename Ret, typename Arg>
83 class Binary_Function_With_Bound_2nd
84 {
85  typedef Ret(*Ftn)(Arg, Arg);
86  Arg _b;
87  Ftn _f;
88 public:
89  Binary_Function_With_Bound_2nd(Ftn f, Arg b): _f(f), _b(b) {}
90  Ret operator()(Arg v) const { return _f(v, _b);}
91 };
92 
93 //functor made from binary function Ret Ftn(const Arg&, const Arg&) with 2nd arg bound
94 //(partially specialize previous template)
95 template<typename Ret, typename Arg>
96 class Binary_Function_With_Bound_2nd<Ret, const Arg&>
97 {
98  typedef Ret(*Ftn)(const Arg&, const Arg&);
99  const Arg& _b;
100  Ftn _f;
101 public:
102  Binary_Function_With_Bound_2nd(Ftn f, const Arg& b): _f(f), _b(b) {}
103  Ret operator()(const Arg& v) const { return _f(v, _b);}
104 };
105 }
107 
110 
112 template<typename T, typename Ftn>
113 inline Vec<T> apply_functor(Ftn f, const Vec<T>& v)
114 {
115  Vec<T> out(v.length());
116  for(int i = 0; i < v.length(); i++) {
117  out(i) = f(v(i));
118  }
119  return out;
120 }
121 
123 template<typename T>
124 inline Vec<T> apply_function(T(*f)(T), const Vec<T>& v)
125 {
126  return apply_functor(details::Unary_Function_Wrapper<T, T>(f), v);
127 }
128 
130 template<typename T>
131 inline Vec<T> apply_function(T(*f)(const T&), const Vec<T>& v)
132 {
133  return apply_functor(details::Unary_Function_Wrapper<T, const T&>(f), v);
134 }
135 
137 template<typename T, typename Ftn>
138 inline Mat<T> apply_functor(Ftn f, const Mat<T>& m)
139 {
140  Mat<T> out(m.rows(), m.cols());
141  for(int i = 0; i < m.rows(); i++) {
142  for(int j = 0; j < m.cols(); j++) {
143  out(i, j) = f(m(i, j));
144  }
145  }
146  return out;
147 }
149 template<typename T>
150 inline Mat<T> apply_function(T(*f)(T), const Mat<T>& m)
151 {
152  return apply_functor(details::Unary_Function_Wrapper<T, T>(f), m);
153 }
154 
156 template<typename T>
157 inline Mat<T> apply_function(T(*f)(const T&), const Mat<T>& m)
158 {
159  return apply_functor(details::Unary_Function_Wrapper<T, const T&>(f), m);
160 }
161 
163 template<typename T>
164 inline Vec<T> apply_function(T(*f)(T, T), const T& x, const Vec<T>& v)
165 {
166  return apply_functor(details::Binary_Function_With_Bound_1st<T, T>(f, x), v);
167 }
168 
171 template<typename T>
172 inline Vec<T> apply_function(T(*f)(const T&, const T&), const T& x,
173  const Vec<T>& v)
174 {
175  return apply_functor(details::Binary_Function_With_Bound_1st<T, const T&>(f, x), v);
176 }
177 
179 template<typename T>
180 inline Mat<T> apply_function(T(*f)(T, T), const T& x, const Mat<T>& m)
181 {
182  return apply_functor(details::Binary_Function_With_Bound_1st<T, T>(f, x), m);
183 }
184 
187 template<typename T>
188 inline Mat<T> apply_function(T(*f)(const T&, const T&), const T& x,
189  const Mat<T>& m)
190 {
191  return apply_functor(details::Binary_Function_With_Bound_1st<T, const T&>(f, x), m);
192 }
193 
195 template<typename T>
196 inline Vec<T> apply_function(T(*f)(T, T), const Vec<T>& v, const T& x)
197 {
198  return apply_functor(details::Binary_Function_With_Bound_2nd<T, T>(f, x), v);
199 }
200 
203 template<typename T>
204 inline Vec<T> apply_function(T(*f)(const T&, const T&), const Vec<T>& v,
205  const T& x)
206 {
207  return apply_functor(details::Binary_Function_With_Bound_2nd<T, const T&>(f, x), v);
208 }
209 
211 template<typename T>
212 inline Mat<T> apply_function(T(*f)(T, T), const Mat<T>& m, const T& x)
213 {
214  return apply_functor(details::Binary_Function_With_Bound_2nd<T, T>(f, x), m);
215 }
216 
219 template<typename T>
220 inline Mat<T> apply_function(T(*f)(const T&, const T&), const Mat<T>& m,
221  const T& x)
222 {
223  return apply_functor(details::Binary_Function_With_Bound_2nd<T, const T&>(f, x), m);
224 }
225 
227 
229 
230 // ----------------------------------------------------------------------
231 // Instantiations
232 // ----------------------------------------------------------------------
233 
234 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec apply_function(double(*f)(double), const vec &v);
235 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec apply_function(std::complex<double> (*f)(const std::complex<double> &),
236  const cvec &v);
237 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec apply_function(short(*f)(short), const svec &v);
238 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec apply_function(int (*f)(int), const ivec &v);
239 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec apply_function(bin(*f)(bin), const bvec &v);
240 
241 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat apply_function(double(*f)(double), const mat &m);
242 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat apply_function(std::complex<double> (*f)(const std::complex<double> &),
243  const cmat &m);
244 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat apply_function(short(*f)(short), const smat &m);
245 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat apply_function(int (*f)(int), const imat &m);
246 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat apply_function(bin(*f)(bin), const bmat &m);
247 
248 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec apply_function(double(*f)(double, double), const double& x, const vec &v);
249 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec apply_function(std::complex<double> (*f)(const std::complex<double> &,
250  const std::complex<double> &),
251  const std::complex<double>& x, const cvec &v);
252 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec apply_function(short(*f)(short, short), const short& x, const svec &v);
253 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec apply_function(int (*f)(int, int), const int& x, const ivec &v);
254 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec apply_function(bin(*f)(bin, bin), const bin& x, const bvec &v);
255 
256 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat apply_function(double(*f)(double, double), const double& x, const mat &m);
257 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat apply_function(std::complex<double> (*f)(const std::complex<double> &,
258  const std::complex<double> &),
259  const std::complex<double>& x, const cmat &m);
260 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat apply_function(short(*f)(short, short), const short& x, const smat &m);
261 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat apply_function(int (*f)(int, int), const int& x, const imat &m);
262 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat apply_function(bin(*f)(bin, bin), const bin& x, const bmat &m);
263 
264 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec apply_function(double(*f)(double, double), const vec &v, const double& x);
265 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec apply_function(std::complex<double> (*f)(const std::complex<double> &,
266  const std::complex<double> &),
267  const cvec &v, const std::complex<double>& x);
268 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec apply_function(short(*f)(short, short), const svec &v, const short& x);
269 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec apply_function(int (*f)(int, int), const ivec &v, const int& x);
270 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec apply_function(bin(*f)(bin, bin), const bvec &v, const bin& x);
271 
272 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat apply_function(double(*f)(double, double), const mat &m, const double& x);
273 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat apply_function(std::complex<double> (*f)(const std::complex<double> &,
274  const std::complex<double> &),
275  const cmat &m, const std::complex<double>& x);
276 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat apply_function(short(*f)(short, short), const smat &m, const short& x);
277 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat apply_function(int (*f)(int, int), const imat &m, const int& x);
278 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat apply_function(bin(*f)(bin, bin), const bmat &m, const bin& x);
279 
281 
282 } // namespace itpp
283 
284 #endif // #ifndef HELP_FUNCTIONS_H
285 
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:20 for IT++ by Doxygen 1.8.2