00001
00029 #ifndef HELP_FUNCTIONS_H
00030 #define HELP_FUNCTIONS_H
00031
00032 #ifndef _MSC_VER
00033 # include <itpp/config.h>
00034 #else
00035 # include <itpp/config_msvc.h>
00036 #endif
00037
00038 #include <itpp/base/mat.h>
00039
00040
00041 namespace itpp
00042 {
00043
00046
00048 template<typename T>
00049 inline Vec<T> apply_function(T(*f)(T), const Vec<T>& v)
00050 {
00051 Vec<T> out(v.length());
00052 for (int i = 0; i < v.length(); i++) {
00053 out(i) = f(v(i));
00054 }
00055 return out;
00056 }
00057
00059 template<typename T>
00060 inline Vec<T> apply_function(T(*f)(const T&), const Vec<T>& v)
00061 {
00062 Vec<T> out(v.length());
00063 for (int i = 0; i < v.length(); i++) {
00064 out(i) = f(v(i));
00065 }
00066 return out;
00067 }
00068
00070 template<typename T>
00071 inline Mat<T> apply_function(T(*f)(T), const Mat<T>& m)
00072 {
00073 Mat<T> out(m.rows(), m.cols());
00074 for (int i = 0; i < m.rows(); i++) {
00075 for (int j = 0; j < m.cols(); j++) {
00076 out(i, j) = f(m(i, j));
00077 }
00078 }
00079 return out;
00080 }
00081
00083 template<typename T>
00084 inline Mat<T> apply_function(T(*f)(const T&), const Mat<T>& m)
00085 {
00086 Mat<T> out(m.rows(), m.cols());
00087 for (int i = 0; i < m.rows(); i++) {
00088 for (int j = 0; j < m.cols(); j++) {
00089 out(i, j) = f(m(i, j));
00090 }
00091 }
00092 return out;
00093 }
00094
00095
00097 template<typename T>
00098 inline Vec<T> apply_function(T(*f)(T, T), const T& x, const Vec<T>& v)
00099 {
00100 Vec<T> out(v.length());
00101 for (int i = 0; i < v.length(); i++) {
00102 out(i) = f(x, v(i));
00103 }
00104 return out;
00105 }
00106
00109 template<typename T>
00110 inline Vec<T> apply_function(T(*f)(const T&, const T&), const T& x,
00111 const Vec<T>& v)
00112 {
00113 Vec<T> out(v.length());
00114 for (int i = 0; i < v.length(); i++) {
00115 out(i) = f(x, v(i));
00116 }
00117 return out;
00118 }
00119
00121 template<typename T>
00122 inline Mat<T> apply_function(T(*f)(T, T), const T& x, const Mat<T>& m)
00123 {
00124 Mat<T> out(m.rows(), m.cols());
00125 for (int i = 0; i < m.rows(); i++) {
00126 for (int j = 0; j < m.cols(); j++) {
00127 out(i, j) = f(x, m(i, j));
00128 }
00129 }
00130 return out;
00131 }
00132
00135 template<typename T>
00136 inline Mat<T> apply_function(T(*f)(const T&, const T&), const T& x,
00137 const Mat<T>& m)
00138 {
00139 Mat<T> out(m.rows(), m.cols());
00140 for (int i = 0; i < m.rows(); i++) {
00141 for (int j = 0; j < m.cols(); j++) {
00142 out(i, j) = f(x, m(i, j));
00143 }
00144 }
00145 return out;
00146 }
00147
00149 template<typename T>
00150 inline Vec<T> apply_function(T(*f)(T, T), const Vec<T>& v, const T& x)
00151 {
00152 Vec<T> out(v.length());
00153 for (int i = 0; i < v.length(); i++) {
00154 out(i) = f(v(i), x);
00155 }
00156 return out;
00157 }
00158
00161 template<typename T>
00162 inline Vec<T> apply_function(T(*f)(const T&, const T&), const Vec<T>& v,
00163 const T& x)
00164 {
00165 Vec<T> out(v.length());
00166 for (int i = 0; i < v.length(); i++) {
00167 out(i) = f(v(i), x);
00168 }
00169 return out;
00170 }
00171
00173 template<typename T>
00174 inline Mat<T> apply_function(T(*f)(T, T), const Mat<T>& m, const T& x)
00175 {
00176 Mat<T> out(m.rows(), m.cols());
00177 for (int i = 0; i < m.rows(); i++) {
00178 for (int j = 0; j < m.cols(); j++) {
00179 out(i, j) = f(m(i, j), x);
00180 }
00181 }
00182 return out;
00183 }
00184
00187 template<typename T>
00188 inline Mat<T> apply_function(T(*f)(const T&, const T&), const Mat<T>& m,
00189 const T& x)
00190 {
00191 Mat<T> out(m.rows(), m.cols());
00192 for (int i = 0; i < m.rows(); i++) {
00193 for (int j = 0; j < m.cols(); j++) {
00194 out(i, j) = f(m(i, j), x);
00195 }
00196 }
00197 return out;
00198 }
00199
00201
00203
00204
00205
00206
00207
00208 #ifdef HAVE_EXTERN_TEMPLATE
00209
00210 extern template vec apply_function(double(*f)(double), const vec &v);
00211 extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &),
00212 const cvec &v);
00213 extern template svec apply_function(short(*f)(short), const svec &v);
00214 extern template ivec apply_function(int (*f)(int), const ivec &v);
00215 extern template bvec apply_function(bin(*f)(bin), const bvec &v);
00216
00217 extern template mat apply_function(double(*f)(double), const mat &m);
00218 extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &),
00219 const cmat &m);
00220 extern template smat apply_function(short(*f)(short), const smat &m);
00221 extern template imat apply_function(int (*f)(int), const imat &m);
00222 extern template bmat apply_function(bin(*f)(bin), const bmat &m);
00223
00224 extern template vec apply_function(double(*f)(double, double), const double& x, const vec &v);
00225 extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &,
00226 const std::complex<double> &),
00227 const std::complex<double>& x, const cvec &v);
00228 extern template svec apply_function(short(*f)(short, short), const short& x, const svec &v);
00229 extern template ivec apply_function(int (*f)(int, int), const int& x, const ivec &v);
00230 extern template bvec apply_function(bin(*f)(bin, bin), const bin& x, const bvec &v);
00231
00232 extern template mat apply_function(double(*f)(double, double), const double& x, const mat &m);
00233 extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &,
00234 const std::complex<double> &),
00235 const std::complex<double>& x, const cmat &m);
00236 extern template smat apply_function(short(*f)(short, short), const short& x, const smat &m);
00237 extern template imat apply_function(int (*f)(int, int), const int& x, const imat &m);
00238 extern template bmat apply_function(bin(*f)(bin, bin), const bin& x, const bmat &m);
00239
00240 extern template vec apply_function(double(*f)(double, double), const vec &v, const double& x);
00241 extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &,
00242 const std::complex<double> &),
00243 const cvec &v, const std::complex<double>& x);
00244 extern template svec apply_function(short(*f)(short, short), const svec &v, const short& x);
00245 extern template ivec apply_function(int (*f)(int, int), const ivec &v, const int& x);
00246 extern template bvec apply_function(bin(*f)(bin, bin), const bvec &v, const bin& x);
00247
00248 extern template mat apply_function(double(*f)(double, double), const mat &m, const double& x);
00249 extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &,
00250 const std::complex<double> &),
00251 const cmat &m, const std::complex<double>& x);
00252 extern template smat apply_function(short(*f)(short, short), const smat &m, const short& x);
00253 extern template imat apply_function(int (*f)(int, int), const imat &m, const int& x);
00254 extern template bmat apply_function(bin(*f)(bin, bin), const bmat &m, const bin& x);
00255
00256 #endif // HAVE_EXTERN_TEMPLATE
00257
00259
00260 }
00261
00262 #endif // #ifndef HELP_FUNCTIONS_H
00263