ivl 679
ivl/details/core/math/binary_functions.hpp
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00024 #ifndef IVL_CORE_DETAILS_MATH_BINARY_FUNCTIONS_HPP
00025 #define IVL_CORE_DETAILS_MATH_BINARY_FUNCTIONS_HPP
00026 
00027 namespace ivl {
00028 namespace math {
00029 
00031 template <class T, class S>
00032 inline
00033 T max(const T& t, const S& s)
00034 {
00035         if(t > s)
00036                 return t;
00037         else
00038                 return s;
00039 }
00040 
00042 template <class T, class S>
00043 inline
00044 T min(const T& t, const S& s)
00045 {
00046         if(t < s)
00047                 return t;
00048         else
00049                 return s;
00050 }
00051 
00053 template <class T, class S>
00054 T mod(const T& s, S length)
00055 {
00056         T rem_result = s % length;
00057         return (rem_result >= 0) ? rem_result : (rem_result + length);
00058 }
00059 
00060 inline
00061 double mod(const double& s, double length)
00062 {
00063         return s - floor(s / length) * length;
00064 }
00065 
00066 inline
00067 float mod(const float& s, float length)
00068 {
00069         return s - floor(s / length) * length;
00070 }
00071 
00072 template <class T>
00073 inline
00074 std::complex<T> pow(const std::complex<T> &x, int y)
00075 {
00076         // kimon: todo: should define element cast above and use that.......
00077         return std::complex<T>(std::pow(
00078                 std::complex<T>(types::to_floating<T>::type(x.real()),
00079                         types::to_floating<T>::type(x.image()), y)));
00080 }
00081 
00082 template <class T>
00083 inline
00084 T pow(const T &x, int y)
00085 {
00086         return T(std::pow(typename types::to_floating<T>::type(x), y));
00087 }
00088 
00089 template <class T>
00090 inline
00091 std::complex<T> pow(const std::complex<T> &x, double y)
00092 {
00093         // kimon: todo: should define element cast above and use that.......
00094         return std::complex<T>(std::pow(
00095                 std::complex<T>(types::to_floating<T>::type(x.real()),
00096                         types::to_floating<T>::type(x.image()), y)));
00097 }
00098 
00099 template <class T>
00100 inline
00101 T pow(const T &x, double y)
00102 {
00103         return T(std::pow(typename types::to_floating<T>::type(x), y));
00104 }
00105 
00106 } /* namespace math */
00107 } /* namespace ivl */
00108 
00109 #endif // IVL_CORE_DETAILS_MATH_BINARY_FUNCTIONS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations