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
35namespace itpp
36{
38namespace 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)
46template<typename Ret, typename Arg>
47class Unary_Function_Wrapper
48{
49 typedef Ret(*Ftn)(Arg);
50 Ftn _f;
51public:
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
57template<typename Ret, typename Arg>
58class Binary_Function_With_Bound_1st
59{
60 typedef Ret(*Ftn)(Arg, Arg);
61 Arg _b;
62 Ftn _f;
63public:
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)
70template<typename Ret, typename Arg>
71class Binary_Function_With_Bound_1st<Ret, const Arg&>
72{
73 typedef Ret(*Ftn)(const Arg&, const Arg&);
74 const Arg& _b;
75 Ftn _f;
76public:
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
82template<typename Ret, typename Arg>
83class Binary_Function_With_Bound_2nd
84{
85 typedef Ret(*Ftn)(Arg, Arg);
86 Arg _b;
87 Ftn _f;
88public:
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)
95template<typename Ret, typename Arg>
96class Binary_Function_With_Bound_2nd<Ret, const Arg&>
97{
98 typedef Ret(*Ftn)(const Arg&, const Arg&);
99 const Arg& _b;
100 Ftn _f;
101public:
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
112template<typename T, typename Ftn>
113inline 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
123template<typename T>
124inline 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
130template<typename T>
131inline 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
137template<typename T, typename Ftn>
138inline 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}
149template<typename T>
150inline 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
156template<typename T>
157inline 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
163template<typename T>
164inline 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
171template<typename T>
172inline 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
179template<typename T>
180inline 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
187template<typename T>
188inline 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
195template<typename T>
196inline 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
203template<typename T>
204inline 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
211template<typename T>
212inline 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
219template<typename T>
220inline 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
234ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec apply_function(double(*f)(double), const vec &v);
235ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec apply_function(std::complex<double> (*f)(const std::complex<double> &),
236 const cvec &v);
237ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec apply_function(short(*f)(short), const svec &v);
238ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec apply_function(int (*f)(int), const ivec &v);
239ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec apply_function(bin(*f)(bin), const bvec &v);
240
241ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat apply_function(double(*f)(double), const mat &m);
242ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat apply_function(std::complex<double> (*f)(const std::complex<double> &),
243 const cmat &m);
244ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat apply_function(short(*f)(short), const smat &m);
245ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat apply_function(int (*f)(int), const imat &m);
246ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat apply_function(bin(*f)(bin), const bmat &m);
247
248ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec apply_function(double(*f)(double, double), const double& x, const vec &v);
249ITPP_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);
252ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec apply_function(short(*f)(short, short), const short& x, const svec &v);
253ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec apply_function(int (*f)(int, int), const int& x, const ivec &v);
254ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec apply_function(bin(*f)(bin, bin), const bin& x, const bvec &v);
255
256ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat apply_function(double(*f)(double, double), const double& x, const mat &m);
257ITPP_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);
260ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat apply_function(short(*f)(short, short), const short& x, const smat &m);
261ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat apply_function(int (*f)(int, int), const int& x, const imat &m);
262ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat apply_function(bin(*f)(bin, bin), const bin& x, const bmat &m);
263
264ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec apply_function(double(*f)(double, double), const vec &v, const double& x);
265ITPP_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);
268ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec apply_function(short(*f)(short, short), const svec &v, const short& x);
269ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec apply_function(int (*f)(int, int), const ivec &v, const int& x);
270ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec apply_function(bin(*f)(bin, bin), const bvec &v, const bin& x);
271
272ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat apply_function(double(*f)(double, double), const mat &m, const double& x);
273ITPP_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);
276ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat apply_function(short(*f)(short, short), const smat &m, const short& x);
277ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat apply_function(int (*f)(int, int), const imat &m, const int& x);
278ITPP_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
General array class.
Definition array.h:105
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
Vec< T > apply_function(T(*f)(T), const Vec< T > &v)
Help function to call for a function: Vec<T> function(Vec<T>)
Vec< T > apply_functor(Ftn f, const Vec< T > &v)
Help function to apply function object to Vec<T>
Matrix Class Definitions.
Mat< bin > bmat
bin matrix
Definition mat.h:508
itpp namespace
Definition itmex.h:37

Generated on Tue Mar 26 2024 19:08:31 for IT++ by Doxygen 1.9.8