00001 00029 #ifndef HELP_FUNCTIONS_H 00030 #define HELP_FUNCTIONS_H 00031 00032 #include <itpp/base/mat.h> 00033 00034 00035 namespace itpp 00036 { 00037 00040 00042 template<typename T> 00043 inline Vec<T> apply_function(T(*f)(T), const Vec<T>& v) 00044 { 00045 Vec<T> out(v.length()); 00046 for (int i = 0; i < v.length(); i++) { 00047 out(i) = f(v(i)); 00048 } 00049 return out; 00050 } 00051 00053 template<typename T> 00054 inline Vec<T> apply_function(T(*f)(const T&), const Vec<T>& v) 00055 { 00056 Vec<T> out(v.length()); 00057 for (int i = 0; i < v.length(); i++) { 00058 out(i) = f(v(i)); 00059 } 00060 return out; 00061 } 00062 00064 template<typename T> 00065 inline Mat<T> apply_function(T(*f)(T), const Mat<T>& m) 00066 { 00067 Mat<T> out(m.rows(), m.cols()); 00068 for (int i = 0; i < m.rows(); i++) { 00069 for (int j = 0; j < m.cols(); j++) { 00070 out(i, j) = f(m(i, j)); 00071 } 00072 } 00073 return out; 00074 } 00075 00077 template<typename T> 00078 inline Mat<T> apply_function(T(*f)(const T&), const Mat<T>& m) 00079 { 00080 Mat<T> out(m.rows(), m.cols()); 00081 for (int i = 0; i < m.rows(); i++) { 00082 for (int j = 0; j < m.cols(); j++) { 00083 out(i, j) = f(m(i, j)); 00084 } 00085 } 00086 return out; 00087 } 00088 00089 00091 template<typename T> 00092 inline Vec<T> apply_function(T(*f)(T, T), const T& x, const Vec<T>& v) 00093 { 00094 Vec<T> out(v.length()); 00095 for (int i = 0; i < v.length(); i++) { 00096 out(i) = f(x, v(i)); 00097 } 00098 return out; 00099 } 00100 00103 template<typename T> 00104 inline Vec<T> apply_function(T(*f)(const T&, const T&), const T& x, 00105 const Vec<T>& v) 00106 { 00107 Vec<T> out(v.length()); 00108 for (int i = 0; i < v.length(); i++) { 00109 out(i) = f(x, v(i)); 00110 } 00111 return out; 00112 } 00113 00115 template<typename T> 00116 inline Mat<T> apply_function(T(*f)(T, T), const T& x, const Mat<T>& m) 00117 { 00118 Mat<T> out(m.rows(), m.cols()); 00119 for (int i = 0; i < m.rows(); i++) { 00120 for (int j = 0; j < m.cols(); j++) { 00121 out(i, j) = f(x, m(i, j)); 00122 } 00123 } 00124 return out; 00125 } 00126 00129 template<typename T> 00130 inline Mat<T> apply_function(T(*f)(const T&, const T&), const T& x, 00131 const Mat<T>& m) 00132 { 00133 Mat<T> out(m.rows(), m.cols()); 00134 for (int i = 0; i < m.rows(); i++) { 00135 for (int j = 0; j < m.cols(); j++) { 00136 out(i, j) = f(x, m(i, j)); 00137 } 00138 } 00139 return out; 00140 } 00141 00143 template<typename T> 00144 inline Vec<T> apply_function(T(*f)(T, T), const Vec<T>& v, const T& x) 00145 { 00146 Vec<T> out(v.length()); 00147 for (int i = 0; i < v.length(); i++) { 00148 out(i) = f(v(i), x); 00149 } 00150 return out; 00151 } 00152 00155 template<typename T> 00156 inline Vec<T> apply_function(T(*f)(const T&, const T&), const Vec<T>& v, 00157 const T& x) 00158 { 00159 Vec<T> out(v.length()); 00160 for (int i = 0; i < v.length(); i++) { 00161 out(i) = f(v(i), x); 00162 } 00163 return out; 00164 } 00165 00167 template<typename T> 00168 inline Mat<T> apply_function(T(*f)(T, T), const Mat<T>& m, const T& x) 00169 { 00170 Mat<T> out(m.rows(), m.cols()); 00171 for (int i = 0; i < m.rows(); i++) { 00172 for (int j = 0; j < m.cols(); j++) { 00173 out(i, j) = f(m(i, j), x); 00174 } 00175 } 00176 return out; 00177 } 00178 00181 template<typename T> 00182 inline Mat<T> apply_function(T(*f)(const T&, const T&), const Mat<T>& m, 00183 const T& x) 00184 { 00185 Mat<T> out(m.rows(), m.cols()); 00186 for (int i = 0; i < m.rows(); i++) { 00187 for (int j = 0; j < m.cols(); j++) { 00188 out(i, j) = f(m(i, j), x); 00189 } 00190 } 00191 return out; 00192 } 00193 00195 00197 00198 // ---------------------------------------------------------------------- 00199 // Instantiations 00200 // ---------------------------------------------------------------------- 00201 00202 #ifndef _MSC_VER 00203 00204 extern template vec apply_function(double(*f)(double), const vec &v); 00205 extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &), 00206 const cvec &v); 00207 extern template svec apply_function(short(*f)(short), const svec &v); 00208 extern template ivec apply_function(int (*f)(int), const ivec &v); 00209 extern template bvec apply_function(bin(*f)(bin), const bvec &v); 00210 00211 extern template mat apply_function(double(*f)(double), const mat &m); 00212 extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &), 00213 const cmat &m); 00214 extern template smat apply_function(short(*f)(short), const smat &m); 00215 extern template imat apply_function(int (*f)(int), const imat &m); 00216 extern template bmat apply_function(bin(*f)(bin), const bmat &m); 00217 00218 extern template vec apply_function(double(*f)(double, double), const double& x, const vec &v); 00219 extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &, 00220 const std::complex<double> &), 00221 const std::complex<double>& x, const cvec &v); 00222 extern template svec apply_function(short(*f)(short, short), const short& x, const svec &v); 00223 extern template ivec apply_function(int (*f)(int, int), const int& x, const ivec &v); 00224 extern template bvec apply_function(bin(*f)(bin, bin), const bin& x, const bvec &v); 00225 00226 extern template mat apply_function(double(*f)(double, double), const double& x, const mat &m); 00227 extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &, 00228 const std::complex<double> &), 00229 const std::complex<double>& x, const cmat &m); 00230 extern template smat apply_function(short(*f)(short, short), const short& x, const smat &m); 00231 extern template imat apply_function(int (*f)(int, int), const int& x, const imat &m); 00232 extern template bmat apply_function(bin(*f)(bin, bin), const bin& x, const bmat &m); 00233 00234 extern template vec apply_function(double(*f)(double, double), const vec &v, const double& x); 00235 extern template cvec apply_function(std::complex<double> (*f)(const std::complex<double> &, 00236 const std::complex<double> &), 00237 const cvec &v, const std::complex<double>& x); 00238 extern template svec apply_function(short(*f)(short, short), const svec &v, const short& x); 00239 extern template ivec apply_function(int (*f)(int, int), const ivec &v, const int& x); 00240 extern template bvec apply_function(bin(*f)(bin, bin), const bvec &v, const bin& x); 00241 00242 extern template mat apply_function(double(*f)(double, double), const mat &m, const double& x); 00243 extern template cmat apply_function(std::complex<double> (*f)(const std::complex<double> &, 00244 const std::complex<double> &), 00245 const cmat &m, const std::complex<double>& x); 00246 extern template smat apply_function(short(*f)(short, short), const smat &m, const short& x); 00247 extern template imat apply_function(int (*f)(int, int), const imat &m, const int& x); 00248 extern template bmat apply_function(bin(*f)(bin, bin), const bmat &m, const bin& x); 00249 00250 #endif // _MSC_VER 00251 00253 00254 } // namespace itpp 00255 00256 #endif // #ifndef HELP_FUNCTIONS_H 00257
Generated on Wed Jul 27 2011 16:27:04 for IT++ by Doxygen 1.7.4