1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*!
* \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-2008 (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 <itpp/config.h>
#else
# include <itpp/config_msvc.h>
#endif
#include <itpp/base/mat.h>
namespace itpp {
//! \addtogroup miscfunc
//!@{
//! Help function to call for a function: Vec<T> function(Vec<T>)
template<typename T>
inline Vec<T> apply_function(T (*f)(T), const Vec<T>& v)
{
Vec<T> 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<T> function(const Vec<T>&)
template<typename T>
inline Vec<T> apply_function(T (*f)(const T&), const Vec<T>& v)
{
Vec<T> 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<T> function(Mat<T>&)
template<typename T>
inline Mat<T> apply_function(T (*f)(T), const Mat<T>& m)
{
Mat<T> 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<T> function(const Mat<T>&)
template<typename T>
inline Mat<T> apply_function(T (*f)(const T&), const Mat<T>& m)
{
Mat<T> 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<T> function(T, Vec<T>)
template<typename T>
inline Vec<T> apply_function(T (*f)(T, T), const T& x, const Vec<T>& v)
{
Vec<T> 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<T> function(const T&, const Vec<T>&)
template<typename T>
inline Vec<T> apply_function(T (*f)(const T&, const T&), const T& x,
const Vec<T>& v)
{
Vec<T> 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<T> function(T, Mat<T>)
template<typename T>
inline Mat<T> apply_function(T (*f)(T, T), const T& x, const Mat<T>& m)
{
Mat<T> 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<T> function(const T&, const Mat<T>&)
template<typename T>
inline Mat<T> apply_function(T (*f)(const T&, const T&), const T& x,
const Mat<T>& m)
{
Mat<T> 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<T> function(Vec<T>, T)
template<typename T>
inline Vec<T> apply_function(T (*f)(T, T), const Vec<T>& v, const T& x)
{
Vec<T> 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<T> function(const Vec<T>&, const T&)
template<typename T>
inline Vec<T> apply_function(T (*f)(const T&, const T&), const Vec<T>& v,
const T& x)
{
Vec<T> 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<T> function(Mat<T>, T)
template<typename T>
inline Mat<T> apply_function(T (*f)(T, T), const Mat<T>& m, const T& x)
{
Mat<T> 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<T> function(const Mat<T>&, const T&)
template<typename T>
inline Mat<T> apply_function(T (*f)(const T&, const T&), const Mat<T>& m,
const T& x)
{
Mat<T> 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<double> (*f)(const std::complex<double> &),
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<double> (*f)(const std::complex<double> &),
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<double> (*f)(const std::complex<double> &,
const std::complex<double> &),
const std::complex<double>& 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<double> (*f)(const std::complex<double> &,
const std::complex<double> &),
const std::complex<double>& 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<double> (*f)(const std::complex<double> &,
const std::complex<double> &),
const cvec &v, const std::complex<double>& 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<double> (*f)(const std::complex<double> &,
const std::complex<double> &),
const cmat &m, const std::complex<double>& 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
|