/*! * \file * \brief Help functions to make functions with vec and mat as arguments * \author Tony Ottosson and Adam Piatyszek * * ------------------------------------------------------------------------- * * IT++ - C++ library of mathematical, signal processing, speech processing, * and communications classes and functions * * Copyright (C) 1995-2007 (see AUTHORS file for a list of contributors) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * ------------------------------------------------------------------------- */ #ifndef HELP_FUNCTIONS_H #define HELP_FUNCTIONS_H #ifndef _MSC_VER # include #else # include #endif #include namespace itpp { //! \addtogroup miscfunc //!@{ //! Help function to call for a function: Vec function(Vec) template inline Vec apply_function(T (*f)(T), const Vec& v) { Vec out(v.length()); for (int i = 0; i < v.length(); i++) { out(i) = f(v(i)); } return out; } //! Help function to call for a function: Vec function(const Vec&) template inline Vec apply_function(T (*f)(const T&), const Vec& v) { Vec out(v.length()); for (int i = 0; i < v.length(); i++) { out(i) = f(v(i)); } return out; } //! Help function to call for a function: Mat function(Mat&) template inline Mat apply_function(T (*f)(T), const Mat& m) { Mat out(m.rows(), m.cols()); for (int i = 0; i < m.rows(); i++) { for (int j = 0; j < m.cols(); j++) { out(i, j) = f(m(i, j)); } } return out; } //! Help function to call for a function: Mat function(const Mat&) template inline Mat apply_function(T (*f)(const T&), const Mat& m) { Mat out(m.rows(), m.cols()); for (int i = 0; i < m.rows(); i++) { for (int j = 0; j < m.cols(); j++) { out(i, j) = f(m(i, j)); } } return out; } //! Help function to call for a function: Vec function(T, Vec) template inline Vec apply_function(T (*f)(T, T), const T& x, const Vec& v) { Vec out(v.length()); for (int i = 0; i < v.length(); i++) { out(i) = f(x, v(i)); } return out; } //! \brief Help function to call for a function: //! Vec function(const T&, const Vec&) template inline Vec apply_function(T (*f)(const T&, const T&), const T& x, const Vec& v) { Vec out(v.length()); for (int i = 0; i < v.length(); i++) { out(i) = f(x, v(i)); } return out; } //! Help function to call for a function: Mat function(T, Mat) template inline Mat apply_function(T (*f)(T, T), const T& x, const Mat& m) { Mat out(m.rows(), m.cols()); for (int i = 0; i < m.rows(); i++) { for (int j = 0; j < m.cols(); j++) { out(i, j) = f(x, m(i, j)); } } return out; } //! \brief Help function to call for a function: //! Mat function(const T&, const Mat&) template inline Mat apply_function(T (*f)(const T&, const T&), const T& x, const Mat& m) { Mat out(m.rows(), m.cols()); for (int i = 0; i < m.rows(); i++) { for (int j = 0; j < m.cols(); j++) { out(i, j) = f(x, m(i, j)); } } return out; } //! Help function to call for a function: Vec function(Vec, T) template inline Vec apply_function(T (*f)(T, T), const Vec& v, const T& x) { Vec out(v.length()); for (int i = 0; i < v.length(); i++) { out(i) = f(v(i), x); } return out; } //! \brief Help function to call for a function: //! Vec function(const Vec&, const T&) template inline Vec apply_function(T (*f)(const T&, const T&), const Vec& v, const T& x) { Vec out(v.length()); for (int i = 0; i < v.length(); i++) { out(i) = f(v(i), x); } return out; } //! Help function to call for a function: Mat function(Mat, T) template inline Mat apply_function(T (*f)(T, T), const Mat& m, const T& x) { Mat out(m.rows(), m.cols()); for (int i = 0; i < m.rows(); i++) { for (int j = 0; j < m.cols(); j++) { out(i, j) = f(m(i, j), x); } } return out; } //! \brief Help function to call for a function: //! Mat function(const Mat&, const T&) template inline Mat apply_function(T (*f)(const T&, const T&), const Mat& m, const T& x) { Mat out(m.rows(), m.cols()); for (int i = 0; i < m.rows(); i++) { for (int j = 0; j < m.cols(); j++) { out(i, j) = f(m(i, j), x); } } return out; } //!@} //! \cond // ---------------------------------------------------------------------- // Instantiations // ---------------------------------------------------------------------- #ifdef HAVE_EXTERN_TEMPLATE extern template vec apply_function(double (*f)(double), const vec &v); extern template cvec apply_function(std::complex (*f)(const std::complex &), const cvec &v); extern template svec apply_function(short (*f)(short), const svec &v); extern template ivec apply_function(int (*f)(int), const ivec &v); extern template bvec apply_function(bin (*f)(bin), const bvec &v); extern template mat apply_function(double (*f)(double), const mat &m); extern template cmat apply_function(std::complex (*f)(const std::complex &), const cmat &m); extern template smat apply_function(short (*f)(short), const smat &m); extern template imat apply_function(int (*f)(int), const imat &m); extern template bmat apply_function(bin (*f)(bin), const bmat &m); extern template vec apply_function(double (*f)(double, double), const double& x, const vec &v); extern template cvec apply_function(std::complex (*f)(const std::complex &, const std::complex &), const std::complex& x, const cvec &v); extern template svec apply_function(short (*f)(short, short), const short& x, const svec &v); extern template ivec apply_function(int (*f)(int, int), const int& x, const ivec &v); extern template bvec apply_function(bin (*f)(bin, bin), const bin& x, const bvec &v); extern template mat apply_function(double (*f)(double, double), const double& x, const mat &m); extern template cmat apply_function(std::complex (*f)(const std::complex &, const std::complex &), const std::complex& x, const cmat &m); extern template smat apply_function(short (*f)(short, short), const short& x, const smat &m); extern template imat apply_function(int (*f)(int, int), const int& x, const imat &m); extern template bmat apply_function(bin (*f)(bin, bin), const bin& x, const bmat &m); extern template vec apply_function(double (*f)(double, double), const vec &v, const double& x); extern template cvec apply_function(std::complex (*f)(const std::complex &, const std::complex &), const cvec &v, const std::complex& x); extern template svec apply_function(short (*f)(short, short), const svec &v, const short& x); extern template ivec apply_function(int (*f)(int, int), const ivec &v, const int& x); extern template bvec apply_function(bin (*f)(bin, bin), const bvec &v, const bin& x); extern template mat apply_function(double (*f)(double, double), const mat &m, const double& x); extern template cmat apply_function(std::complex (*f)(const std::complex &, const std::complex &), const cmat &m, const std::complex& x); extern template smat apply_function(short (*f)(short, short), const smat &m, const short& x); extern template imat apply_function(int (*f)(int, int), const imat &m, const int& x); extern template bmat apply_function(bin (*f)(bin, bin), const bmat &m, const bin& x); #endif // HAVE_EXTERN_TEMPLATE //! \endcond } // namespace itpp #endif // #ifndef HELP_FUNCTIONS_H