root/win32/itpp-4.0.1/itpp/base/help_functions.h @ 109

Revision 35, 9.1 kB (checked in by mido, 16 years ago)

zasadni zmeny ve /win32

Line 
1/*!
2 * \file
3 * \brief Help functions to make functions with vec and mat as arguments
4 * \author Tony Ottosson and Adam Piatyszek
5 *
6 * -------------------------------------------------------------------------
7 *
8 * IT++ - C++ library of mathematical, signal processing, speech processing,
9 *        and communications classes and functions
10 *
11 * Copyright (C) 1995-2007  (see AUTHORS file for a list of contributors)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * -------------------------------------------------------------------------
28 */
29
30#ifndef HELP_FUNCTIONS_H
31#define HELP_FUNCTIONS_H
32
33#ifndef _MSC_VER
34#  include <itpp/config.h>
35#else
36#  include <itpp/config_msvc.h>
37#endif
38
39#include <itpp/base/mat.h>
40
41
42namespace itpp {
43
44  //! \addtogroup miscfunc
45  //!@{
46
47  //! Help function to call for a function: Vec<T> function(Vec<T>)
48  template<typename T>
49  inline Vec<T> apply_function(T (*f)(T), const Vec<T>& v)
50  {
51    Vec<T> out(v.length());
52    for (int i = 0; i < v.length(); i++) {
53      out(i) = f(v(i));
54    }
55    return out;
56  }
57
58  //! Help function to call for a function: Vec<T> function(const Vec<T>&)
59  template<typename T>
60  inline Vec<T> apply_function(T (*f)(const T&), const Vec<T>& v)
61  {
62    Vec<T> out(v.length());
63    for (int i = 0; i < v.length(); i++) {
64      out(i) = f(v(i));
65    }
66    return out;
67  }
68
69  //! Help function to call for a function: Mat<T> function(Mat<T>&)
70  template<typename T>
71  inline Mat<T> apply_function(T (*f)(T), const Mat<T>& m)
72  {
73    Mat<T> out(m.rows(), m.cols());
74    for (int i = 0; i < m.rows(); i++) {
75      for (int j = 0; j < m.cols(); j++) {
76        out(i, j) = f(m(i, j));
77      }
78    }
79    return out;
80  }
81
82  //! Help function to call for a function: Mat<T> function(const Mat<T>&)
83  template<typename T>
84  inline Mat<T> apply_function(T (*f)(const T&), const Mat<T>& m)
85  {
86    Mat<T> out(m.rows(), m.cols());
87    for (int i = 0; i < m.rows(); i++) {
88      for (int j = 0; j < m.cols(); j++) {
89        out(i, j) = f(m(i, j));
90      }
91    }
92    return out;
93  }
94
95
96  //! Help function to call for a function: Vec<T> function(T, Vec<T>)
97  template<typename T>
98  inline Vec<T> apply_function(T (*f)(T, T), const T& x, const Vec<T>& v)
99  {
100    Vec<T> out(v.length());
101    for (int i = 0; i < v.length(); i++) {
102      out(i) = f(x, v(i));
103    }
104    return out;
105  }
106
107  //! \brief Help function to call for a function:
108  //!        Vec<T> function(const T&, const Vec<T>&)
109  template<typename T>
110  inline Vec<T> apply_function(T (*f)(const T&, const T&), const T& x,
111                               const Vec<T>& v)
112  {
113    Vec<T> out(v.length());
114    for (int i = 0; i < v.length(); i++) {
115      out(i) = f(x, v(i));
116    }
117    return out;
118  }
119
120  //! Help function to call for a function: Mat<T> function(T, Mat<T>)
121  template<typename T>
122  inline Mat<T> apply_function(T (*f)(T, T), const T& x, const Mat<T>& m)
123  {
124    Mat<T> out(m.rows(), m.cols());
125    for (int i = 0; i < m.rows(); i++) {
126      for (int j = 0; j < m.cols(); j++) {
127        out(i, j) = f(x, m(i, j));
128      }
129    }
130    return out;
131  }
132
133  //! \brief Help function to call for a function:
134  //!        Mat<T> function(const T&, const Mat<T>&)
135  template<typename T>
136  inline Mat<T> apply_function(T (*f)(const T&, const T&), const T& x,
137                               const Mat<T>& m)
138  {
139    Mat<T> out(m.rows(), m.cols());
140    for (int i = 0; i < m.rows(); i++) {
141      for (int j = 0; j < m.cols(); j++) {
142        out(i, j) = f(x, m(i, j));
143      }
144    }
145    return out;
146  }
147
148  //! Help function to call for a function: Vec<T> function(Vec<T>, T)
149  template<typename T>
150  inline Vec<T> apply_function(T (*f)(T, T), const Vec<T>& v, const T& x)
151  {
152    Vec<T> out(v.length());
153    for (int i = 0; i < v.length(); i++) {
154      out(i) = f(v(i), x);
155    }
156    return out;
157  }
158
159  //! \brief Help function to call for a function:
160  //!        Vec<T> function(const Vec<T>&, const T&)
161  template<typename T>
162  inline Vec<T> apply_function(T (*f)(const T&, const T&), const Vec<T>& v,
163                               const T& x)
164  {
165    Vec<T> out(v.length());
166    for (int i = 0; i < v.length(); i++) {
167      out(i) = f(v(i), x);
168    }
169    return out;
170  }
171
172  //! Help function to call for a function: Mat<T> function(Mat<T>, T)
173  template<typename T>
174  inline Mat<T> apply_function(T (*f)(T, T), const Mat<T>& m, const T& x)
175  {
176    Mat<T> out(m.rows(), m.cols());
177    for (int i = 0; i < m.rows(); i++) {
178      for (int j = 0; j < m.cols(); j++) {
179        out(i, j) = f(m(i, j), x);
180      }
181    }
182    return out;
183  }
184
185  //! \brief Help function to call for a function:
186  //!        Mat<T> function(const Mat<T>&, const T&)
187  template<typename T>
188  inline Mat<T> apply_function(T (*f)(const T&, const T&), const Mat<T>& m,
189                               const T& x)
190  {
191    Mat<T> out(m.rows(), m.cols());
192    for (int i = 0; i < m.rows(); i++) {
193      for (int j = 0; j < m.cols(); j++) {
194        out(i, j) = f(m(i, j), x);
195      }
196    }
197    return out;
198  }
199
200  //!@}
201
202  //! \cond
203
204  // ----------------------------------------------------------------------
205  // Instantiations
206  // ----------------------------------------------------------------------
207
208#ifdef HAVE_EXTERN_TEMPLATE
209
210  extern template vec apply_function(double (*f)(double), const vec &v);
211  extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &),
212                                      const cvec &v);
213  extern template svec apply_function(short (*f)(short), const svec &v);
214  extern template ivec apply_function(int (*f)(int), const ivec &v);
215  extern template bvec apply_function(bin (*f)(bin), const bvec &v);
216
217  extern template mat apply_function(double (*f)(double), const mat &m);
218  extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &),
219                                      const cmat &m);
220  extern template smat apply_function(short (*f)(short), const smat &m);
221  extern template imat apply_function(int (*f)(int), const imat &m);
222  extern template bmat apply_function(bin (*f)(bin), const bmat &m);
223
224  extern template vec apply_function(double (*f)(double, double), const double& x, const vec &v);
225  extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &,
226                                                                const std::complex<double> &),
227                                      const std::complex<double>& x, const cvec &v);
228  extern template svec apply_function(short (*f)(short, short), const short& x, const svec &v);
229  extern template ivec apply_function(int (*f)(int, int), const int& x, const ivec &v);
230  extern template bvec apply_function(bin (*f)(bin, bin), const bin& x, const bvec &v);
231
232  extern template mat apply_function(double (*f)(double, double), const double& x, const mat &m);
233  extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &,
234                                                                const std::complex<double> &),
235                                      const std::complex<double>& x, const cmat &m);
236  extern template smat apply_function(short (*f)(short, short), const short& x, const smat &m);
237  extern template imat apply_function(int (*f)(int, int), const int& x, const imat &m);
238  extern template bmat apply_function(bin (*f)(bin, bin), const bin& x, const bmat &m);
239
240  extern template vec apply_function(double (*f)(double, double), const vec &v, const double& x);
241  extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &,
242                                                                const std::complex<double> &),
243                                      const cvec &v, const std::complex<double>& x);
244  extern template svec apply_function(short (*f)(short, short), const svec &v, const short& x);
245  extern template ivec apply_function(int (*f)(int, int), const ivec &v, const int& x);
246  extern template bvec apply_function(bin (*f)(bin, bin), const bvec &v, const bin& x);
247
248  extern template mat apply_function(double (*f)(double, double), const mat &m, const double& x);
249  extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &,
250                                                                const std::complex<double> &),
251                                      const cmat &m, const std::complex<double>& x);
252  extern template smat apply_function(short (*f)(short, short), const smat &m, const short& x);
253  extern template imat apply_function(int (*f)(int, int), const imat &m, const int& x);
254  extern template bmat apply_function(bin (*f)(bin, bin), const bmat &m, const bin& x);
255
256#endif // HAVE_EXTERN_TEMPLATE
257
258  //! \endcond
259
260} // namespace itpp
261
262#endif // #ifndef HELP_FUNCTIONS_H
Note: See TracBrowser for help on using the browser.