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