IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
34 
35 namespace itpp
36 {
37 
40 
42 template<typename T>
43 inline Vec<T> apply_function(T(*f)(T), const Vec<T>& v)
44 {
45  Vec<T> out(v.length());
46  for (int i = 0; i < v.length(); i++) {
47  out(i) = f(v(i));
48  }
49  return out;
50 }
51 
53 template<typename T>
54 inline Vec<T> apply_function(T(*f)(const T&), const Vec<T>& v)
55 {
56  Vec<T> out(v.length());
57  for (int i = 0; i < v.length(); i++) {
58  out(i) = f(v(i));
59  }
60  return out;
61 }
62 
64 template<typename T>
65 inline Mat<T> apply_function(T(*f)(T), const Mat<T>& m)
66 {
67  Mat<T> out(m.rows(), m.cols());
68  for (int i = 0; i < m.rows(); i++) {
69  for (int j = 0; j < m.cols(); j++) {
70  out(i, j) = f(m(i, j));
71  }
72  }
73  return out;
74 }
75 
77 template<typename T>
78 inline Mat<T> apply_function(T(*f)(const T&), const Mat<T>& m)
79 {
80  Mat<T> out(m.rows(), m.cols());
81  for (int i = 0; i < m.rows(); i++) {
82  for (int j = 0; j < m.cols(); j++) {
83  out(i, j) = f(m(i, j));
84  }
85  }
86  return out;
87 }
88 
89 
91 template<typename T>
92 inline Vec<T> apply_function(T(*f)(T, T), const T& x, const Vec<T>& v)
93 {
94  Vec<T> out(v.length());
95  for (int i = 0; i < v.length(); i++) {
96  out(i) = f(x, v(i));
97  }
98  return out;
99 }
100 
103 template<typename T>
104 inline Vec<T> apply_function(T(*f)(const T&, const T&), const T& x,
105  const Vec<T>& v)
106 {
107  Vec<T> out(v.length());
108  for (int i = 0; i < v.length(); i++) {
109  out(i) = f(x, v(i));
110  }
111  return out;
112 }
113 
115 template<typename T>
116 inline Mat<T> apply_function(T(*f)(T, T), const T& x, const Mat<T>& m)
117 {
118  Mat<T> out(m.rows(), m.cols());
119  for (int i = 0; i < m.rows(); i++) {
120  for (int j = 0; j < m.cols(); j++) {
121  out(i, j) = f(x, m(i, j));
122  }
123  }
124  return out;
125 }
126 
129 template<typename T>
130 inline Mat<T> apply_function(T(*f)(const T&, const T&), const T& x,
131  const Mat<T>& m)
132 {
133  Mat<T> out(m.rows(), m.cols());
134  for (int i = 0; i < m.rows(); i++) {
135  for (int j = 0; j < m.cols(); j++) {
136  out(i, j) = f(x, m(i, j));
137  }
138  }
139  return out;
140 }
141 
143 template<typename T>
144 inline Vec<T> apply_function(T(*f)(T, T), const Vec<T>& v, const T& x)
145 {
146  Vec<T> out(v.length());
147  for (int i = 0; i < v.length(); i++) {
148  out(i) = f(v(i), x);
149  }
150  return out;
151 }
152 
155 template<typename T>
156 inline Vec<T> apply_function(T(*f)(const T&, const T&), const Vec<T>& v,
157  const T& x)
158 {
159  Vec<T> out(v.length());
160  for (int i = 0; i < v.length(); i++) {
161  out(i) = f(v(i), x);
162  }
163  return out;
164 }
165 
167 template<typename T>
168 inline Mat<T> apply_function(T(*f)(T, T), const Mat<T>& m, const T& x)
169 {
170  Mat<T> out(m.rows(), m.cols());
171  for (int i = 0; i < m.rows(); i++) {
172  for (int j = 0; j < m.cols(); j++) {
173  out(i, j) = f(m(i, j), x);
174  }
175  }
176  return out;
177 }
178 
181 template<typename T>
182 inline Mat<T> apply_function(T(*f)(const T&, const T&), const Mat<T>& m,
183  const T& x)
184 {
185  Mat<T> out(m.rows(), m.cols());
186  for (int i = 0; i < m.rows(); i++) {
187  for (int j = 0; j < m.cols(); j++) {
188  out(i, j) = f(m(i, j), x);
189  }
190  }
191  return out;
192 }
193 
195 
197 
198 // ----------------------------------------------------------------------
199 // Instantiations
200 // ----------------------------------------------------------------------
201 
202 #ifndef _MSC_VER
203 
204 extern template vec apply_function(double(*f)(double), const vec &v);
205 extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &),
206  const cvec &v);
207 extern template svec apply_function(short(*f)(short), const svec &v);
208 extern template ivec apply_function(int (*f)(int), const ivec &v);
209 extern template bvec apply_function(bin(*f)(bin), const bvec &v);
210 
211 extern template mat apply_function(double(*f)(double), const mat &m);
212 extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &),
213  const cmat &m);
214 extern template smat apply_function(short(*f)(short), const smat &m);
215 extern template imat apply_function(int (*f)(int), const imat &m);
216 extern template bmat apply_function(bin(*f)(bin), const bmat &m);
217 
218 extern template vec apply_function(double(*f)(double, double), const double& x, const vec &v);
219 extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &,
220  const std::complex<double> &),
221  const std::complex<double>& x, const cvec &v);
222 extern template svec apply_function(short(*f)(short, short), const short& x, const svec &v);
223 extern template ivec apply_function(int (*f)(int, int), const int& x, const ivec &v);
224 extern template bvec apply_function(bin(*f)(bin, bin), const bin& x, const bvec &v);
225 
226 extern template mat apply_function(double(*f)(double, double), const double& x, const mat &m);
227 extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &,
228  const std::complex<double> &),
229  const std::complex<double>& x, const cmat &m);
230 extern template smat apply_function(short(*f)(short, short), const short& x, const smat &m);
231 extern template imat apply_function(int (*f)(int, int), const int& x, const imat &m);
232 extern template bmat apply_function(bin(*f)(bin, bin), const bin& x, const bmat &m);
233 
234 extern template vec apply_function(double(*f)(double, double), const vec &v, const double& x);
235 extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &,
236  const std::complex<double> &),
237  const cvec &v, const std::complex<double>& x);
238 extern template svec apply_function(short(*f)(short, short), const svec &v, const short& x);
239 extern template ivec apply_function(int (*f)(int, int), const ivec &v, const int& x);
240 extern template bvec apply_function(bin(*f)(bin, bin), const bvec &v, const bin& x);
241 
242 extern template mat apply_function(double(*f)(double, double), const mat &m, const double& x);
243 extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &,
244  const std::complex<double> &),
245  const cmat &m, const std::complex<double>& x);
246 extern template smat apply_function(short(*f)(short, short), const smat &m, const short& x);
247 extern template imat apply_function(int (*f)(int, int), const imat &m, const int& x);
248 extern template bmat apply_function(bin(*f)(bin, bin), const bmat &m, const bin& x);
249 
250 #endif // _MSC_VER
251 
253 
254 } // namespace itpp
255 
256 #endif // #ifndef HELP_FUNCTIONS_H
257 
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:12 for IT++ by Doxygen 1.8.1.2