ivl 679
|
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_OPERATORS_HPP 00025 #define IVL_CORE_DETAILS_MATH_BINARY_OPERATORS_HPP 00026 00027 namespace ivl { 00028 namespace math { 00029 00030 00031 // ------------------------------------------------------------------ 00032 // FUNCTIONS definition of functions that correspond to each operator 00033 00034 //---------------------- Relational Operators 00035 00036 using std::operator <; 00037 using std::operator >; 00038 using std::operator <=; 00039 using std::operator >=; 00040 using std::operator ==; 00041 using std::operator !=; 00042 00043 00044 //less than 00045 template <class T, class S> 00046 inline bool lt(const T& a, const S& b) 00047 { 00048 return a < b; 00049 } 00050 00051 template <class T, class S> 00052 inline bool lt(const std::complex<T> &a, const std::complex<S> &b) 00053 { 00054 return a.real() < b.real(); 00055 } 00056 00057 //less than or equal 00058 template <class T, class S> 00059 inline bool le(const T& a, const S& b) 00060 { 00061 return a <= b; 00062 } 00063 00064 template <class T, class S> 00065 inline bool le(const std::complex<T> &a, const std::complex<S> &b) 00066 { 00067 return a.real() <= b.real(); 00068 } 00069 00070 //greater than 00071 template <class T, class S> 00072 inline bool gt(const T& a, const S& b) 00073 { 00074 return a > b; 00075 } 00076 00077 template <class T, class S> 00078 inline bool gt(const std::complex<T> &a, const std::complex<S> &b) 00079 { 00080 return a.real() > b.real(); 00081 } 00082 00083 //greater than or equal 00084 template <class T, class S> 00085 inline bool ge(const T& a, const S& b) 00086 { 00087 return a >= b; 00088 } 00089 00090 template <class T, class S> 00091 inline bool ge(const std::complex<T> &a, const std::complex<S> &b) 00092 { 00093 return a.real() >= b.real(); 00094 } 00095 00096 //test for equality 00097 template <class T, class S> 00098 inline bool eq(const T& a, const S& b) 00099 { 00100 return a == b; 00101 } 00102 00103 template <class T, class S> 00104 inline bool eq(const std::complex<T> &a, const std::complex<S> &b) 00105 { 00106 return (a.real() == b.real()) && (a.imag() == b.imag()); 00107 } 00108 00109 //test for unequality 00110 template <class T, class S> 00111 inline bool ne(const T& a, const S& b) 00112 { 00113 return a != b; 00114 } 00115 00116 template <class T, class S> 00117 inline bool ne(const std::complex<T> &a, const std::complex<S> &b) 00118 { 00119 return (a.real() != b.real()) || (a.imag() != b.imag()); 00120 } 00121 00122 //---------------------- Logical Operators 00123 00124 template <class T, class S> 00125 inline bool logical_or(const T& a, const S& b) 00126 { 00127 return a || b; 00128 } 00129 00130 template <class T, class S> 00131 inline bool logical_and(const T& a, const S& b) 00132 { 00133 return a && b; 00134 } 00135 00136 //---------------------- Bitwise Operators 00137 00138 template <class T> 00139 inline T bitlshift(const T& a, int b) 00140 { 00141 return a << b; 00142 } 00143 00144 template <class T> 00145 inline T bitrshift(const T& a, int b) 00146 { 00147 return a >> b; 00148 } 00149 00150 template <class T, class S> 00151 inline T cbitor(const T& a, const S& b) 00152 { 00153 return a | b; 00154 } 00155 00156 template <class T, class S> 00157 inline T cbitand(const T& a, const S& b) 00158 { 00159 return a & b; 00160 } 00161 00162 //---------------------- Mathematical Operators 00163 00164 using std::operator+; 00165 using std::operator-; 00166 using std::operator/; 00167 using std::operator*; 00168 00169 template <class T, class S> 00170 inline T plus(const T& a, const S& b) 00171 { 00172 return a + b; 00173 } 00174 00175 template <class T, class S> 00176 inline T minus(const T& a, const S& b) 00177 { 00178 return a - b; 00179 } 00180 00181 template <class T, class S> 00182 inline T times(const T& a, const S& b) 00183 { 00184 return a * b; 00185 } 00186 00187 template <class T, class S> 00188 inline T rdivide(const T& a, const S& b) 00189 { 00190 return a / b; 00191 } 00192 00193 template <class T, class S> 00194 T rem(const T& s, S length) 00195 { 00196 return s % length; 00197 } 00198 00199 template <class T> 00200 double rem(const T& s, double length) 00201 { 00202 // todo: find a better implementation 00203 double md = s - floor(s / length) * length; 00204 if(md < 0) md = -md; 00205 return (s < 0) ? -md : md; 00206 } 00207 00208 template <class T> 00209 float rem(const T& s, float length) 00210 { 00211 // todo: find a better implementation 00212 float md = s - floor(s / length) * length; 00213 if(md < 0) md = -md; 00214 return (s < 0) ? -md : md; 00215 } 00216 /* 00217 template <class T, class S> 00218 inline T power(const T& x, const S& y) 00219 { 00220 using std::pow; // TODO: definition of pow in right place. This line may not be needed if this is made. 00221 return pow(x, y); 00222 } 00223 00224 inline int power(int x, int y) 00225 { 00226 // TODO: fix this well, for all types. Also add integer power with different algorithm. 00227 return std::pow((double)x, y); 00228 } 00229 00230 */ 00231 } /* namespace math */ 00232 00233 // ------------------------------------------------------------------ 00234 // OPERATORS definition of operators based on the actual functions 00235 00236 //---------------------- Relational Operators 00237 00238 // according operators for complex 00239 template <class T, class S> 00240 bool operator < (const std::complex<T> &a, const std::complex<S> &b) 00241 { 00242 return lt(a, b); 00243 } 00244 00245 template <class T, class S> 00246 bool operator <= (const std::complex<T> &a, const std::complex<S> &b) 00247 { 00248 return le(a, b); 00249 } 00250 00251 template <class T, class S> 00252 bool operator > (const std::complex<T> &a, const std::complex<S> &b) 00253 { 00254 return gt(a, b); 00255 } 00256 00257 template <class T, class S> 00258 bool operator >= (const std::complex<T> &a, const std::complex<S> &b) 00259 { 00260 return ge(a, b); 00261 } 00262 00263 } /* namespace ivl */ 00264 00265 #endif // IVL_CORE_DETAILS_MATH_BINARY_OPERATORS_HPP