Back to index

texmacs  1.0.7.15
operators.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : operators.hpp
00004 * DESCRIPTION: Templates and default implementations of several operations
00005 * COPYRIGHT  : (C) 2006  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef OPERATORS_HPP
00013 #define OPERATORS_HPP
00014 #include "math_tree.hpp"
00015 
00016 /******************************************************************************
00017 * Helper functions
00018 ******************************************************************************/
00019 
00020 template<typename C> inline C
00021 invert (C x) {
00022   return 1/x;
00023 }
00024 
00025 template<typename C> inline C
00026 square (C x) {
00027   return x*x;
00028 }
00029 
00030 template<typename C> inline C
00031 norm (C x) {
00032   return x >= 0? x: -x;
00033 }
00034 
00035 template<typename C> inline C
00036 square_norm (C x) {
00037   return x*x;
00038 }
00039 
00040 /******************************************************************************
00041 * Arithmetic operators
00042 ******************************************************************************/
00043 
00044 struct copy_op {
00045   template<typename C> static inline C
00046   eval (C x) { return x; }
00047   static inline tree
00048   eval (tree x) { return x; }
00049   template<typename C, typename V> static inline C
00050   diff (C x, V v) { return derive (x, v); }
00051 };
00052 
00053 struct neg_op {
00054   template<typename C> static inline C
00055   eval (C x) { return -x; }
00056   static inline tree
00057   eval (tree x) { return neg (x); }
00058   template<typename C, typename V> static inline C
00059   diff (C x, V v) { return -derive (x, v); }
00060 };
00061 
00062 struct add_op {
00063   template<typename C> static inline C
00064   neutral () { return 0; }
00065   template<typename C> static inline C
00066   eval (C x, C y) { return x + y; }
00067   static inline tree
00068   eval (tree x, tree y) { return add (x, y); }
00069   template<typename C, typename V> static inline C
00070   diff (C x, C y, V v) { return derive (x, v) + derive (y, v); }
00071 };
00072 
00073 struct sub_op {
00074   template<typename C> static inline C
00075   eval (C x, C y) { return x - y; }
00076   static inline tree
00077   eval (tree x, tree y) { return sub (x, y); }
00078   template<typename C, typename V> static inline C
00079   diff (C x, C y, V v) { return derive (x, v) - derive (y, v); }
00080 };
00081 
00082 struct mul_op {
00083   template<typename C> static inline C
00084   neutral () { return 1; }
00085   template<typename C> static inline C
00086   eval (C x, C y) { return x * y; }
00087   static inline tree
00088   eval (tree x, tree y) { return mul (x, y); }
00089   template<typename C, typename V> static inline C
00090   diff (C x, C y, V v) { return derive (x, v) * y + x * derive (y, v); }
00091 };
00092 
00093 struct div_op {
00094   template<typename C> static inline C
00095   eval (C x, C y) { return x / y; }
00096   static inline tree
00097   eval (tree x, tree y) { return div (x, y); }
00098   template<typename C, typename V> static inline C
00099   diff (C x, C y, V v) {
00100     return (derive (x, v) * y - x * derive (y, v)) / square (y); }
00101 };
00102 
00103 /******************************************************************************
00104 * Special functions
00105 ******************************************************************************/
00106 
00107 struct sqrt_op {
00108   template<typename C> static inline C
00109   eval (C x) { return sqrt (x); }
00110   template<typename C, typename V> static inline C
00111   diff (C x, V v) { return derive (x, v) / (2 * sqrt (x)); }
00112 };
00113 
00114 struct exp_op {
00115   template<typename C> static inline C
00116   eval (C x) { return exp (x); }
00117   template<typename C, typename V> static inline C
00118   diff (C x, V v) { return derive (x, v) * exp (x); }
00119 };
00120 
00121 struct log_op {
00122   template<typename C> static inline C
00123   eval (C x) { return log (x); }
00124   template<typename C, typename V> static inline C
00125   diff (C x, V v) { return derive (x, v) / x; }
00126 };
00127 
00128 struct pow_op {
00129   template<typename C> static inline C
00130   eval (C x, C y) { return pow (x, y); }
00131   template<typename C, typename V> static inline C
00132   diff (C x, C y, V v) {
00133     return (derive (x, v) * y / x + log (x) * derive (y, v)) * pow (x, y); }
00134 };
00135 
00136 struct cst_pow_op {
00137   template<typename C, typename T> static inline C
00138   eval (C x, T c) { return pow (x, c); }
00139   template<typename C, typename T, typename V> static inline C
00140   diff (C x, T c, V v) { return derive (x, v) * pow (x, c - 1); }
00141 };
00142 
00143 struct cos_op {
00144   template<typename C> static inline C
00145   eval (C x) { return cos (x); }
00146   template<typename C, typename V> static inline C
00147   diff (C x, V v) { return -derive (x, v) * sin (x); }
00148 };
00149 
00150 struct sin_op {
00151   template<typename C> static inline C
00152   eval (C x) { return sin (x); }
00153   template<typename C, typename V> static inline C
00154   diff (C x, V v) { return derive (x, v) * cos (x); }
00155 };
00156 
00157 struct tan_op {
00158   template<typename C> static inline C
00159   eval (C x) { return tan (x); }
00160   template<typename C, typename V> static inline C
00161   diff (C x, V v) { return derive (x, v) / square (cos (x)); }
00162 };
00163 
00164 /******************************************************************************
00165 * Other operators
00166 ******************************************************************************/
00167 
00168 struct derive_op {
00169   template<typename C> static inline C
00170   eval (C x) { return derive (x); }
00171 };
00172 
00173 #endif // OPERATORS_H