Back to index

texmacs  1.0.7.15
function.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : function.hpp
00004 * DESCRIPTION: Mathematical functions from F into T
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 FUNCTION_HPP
00013 #define FUNCTION_HPP
00014 #include "ball.hpp"
00015 #include "vector.hpp"
00016 #include "polynomial.hpp"
00017 #define TMPL template<typename F, typename T>
00018 #define V typename properties<F>::index_type
00019 #define C typename properties<F>::scalar_type
00020 
00021 TMPL class function;
00022 TMPL bool is_nil (function<F,T> f);
00023 
00024 /******************************************************************************
00025 * Abstract function class
00026 ******************************************************************************/
00027 
00028 TMPL
00029 class function_rep: public abstract_struct {
00030 public:
00031   inline function_rep () {}
00032   inline virtual ~function_rep () {}
00033   virtual T apply (F x) = 0;
00034   virtual ball<T> apply (ball<F> x) = 0;
00035   virtual function<F,T> derive (V var) = 0;
00036   virtual tree expression () = 0;
00037 };
00038 
00039 TMPL
00040 class function {
00041 public:
00042 ABSTRACT_NULL_TEMPLATE_2(function,F,T);
00043   inline function (T x);
00044   inline T operator () (F x);
00045   inline ball<T> operator () (ball<F> x);
00046 };
00047 ABSTRACT_NULL_TEMPLATE_2_CODE(function,typename,F,typename,T);
00048 
00049 TMPL inline T function<F,T>::operator () (F x) {
00050   return rep->apply (x); }
00051 TMPL inline ball<T> function<F,T>::operator () (ball<F> x) {
00052   return rep->apply (x); }
00053 TMPL inline function<F,T> derive (function<F,T> f, V var) {
00054   return f->derive (var); }
00055 TMPL inline tree as_tree (function<F,T> f) {
00056   return f->expression (); }
00057 TMPL inline tm_ostream& operator << (tm_ostream& out, function<F,T> f) {
00058   return out << as_math_string (as_tree (f)); }
00059 
00060 TMPL
00061 class properties<function<F,T> > {
00062 public:
00063   typedef function<F,typename properties<T>::scalar_type> scalar_type;
00064   typedef function<F,typename properties<T>::norm_type> norm_type;
00065   typedef function<F,typename properties<T>::index_type> index_type;
00066 };
00067 
00068 /******************************************************************************
00069 * Basic functions
00070 ******************************************************************************/
00071 
00072 TMPL
00073 class constant_function_rep: public function_rep<F,T> {
00074   T c;
00075 public:
00076   inline constant_function_rep (T c2): c (c2) {}
00077   inline T apply (F x) { return c; }
00078   inline ball<T> apply (ball<F> x) { return ball<T> (c, 0); }
00079   inline function<F,T> derive (V var) { return C(0) * c; }
00080   inline tree expression () { return as_tree (c); }
00081 };
00082 
00083 TMPL inline function<F,T>::function (T c):
00084   rep (tm_new<constant_function_rep<F,T> > (c)) { rep->ref_count++; }
00085 
00086 TMPL
00087 class coordinate_function_rep: public function_rep<F,T> {
00088   V var;
00089   T c;
00090 public:
00091   inline coordinate_function_rep (V var2, T c2): var (var2), c (c2) {}
00092   inline T apply (F x) { return properties<F>::access (x, var) * c; }
00093   inline ball<T> apply (ball<F> x) {
00094     return properties<ball<F> >::access (x, var) * ball<T> (c, 0); }
00095   inline function<F,T> derive (V var2) { return var2==var? c: C(0) * c; }
00096   inline tree expression () {
00097     return mul (as_tree (c), properties<F>::index_name (var)); }
00098 };
00099 
00100 TMPL inline function<F,T>
00101 coordinate_function (V var, T c=1) {
00102   return tm_new<coordinate_function_rep<F,T> > (var, c); }
00103 
00104 /******************************************************************************
00105 * Operators on functions
00106 ******************************************************************************/
00107 
00108 template<typename F, typename T, typename Op>
00109 class unary_function_rep: public function_rep<F,T> {
00110   function<F,T> f;
00111 public:
00112   inline unary_function_rep (function<F,T> f2): f (f2) {}
00113   inline T apply (F x) { return Op::eval (f (x)); }
00114   inline ball<T> apply (ball<F> x) { return Op::eval (f (x)); }
00115   inline function<F,T> derive (V var) { return Op::diff (f, var); }
00116   inline tree expression () { return Op::eval (as_tree (f)); }
00117 };
00118 
00119 template<typename F, typename T, typename Op> inline function<F,T>
00120 unary_function (function<F,T> f) {
00121   return tm_new<unary_function_rep<F,T,Op> > (f); }
00122 
00123 template<typename F, typename T, typename Op>
00124 class binary_function_rep: public function_rep<F,T> {
00125   function<F,T> f, g;
00126 public:
00127   inline binary_function_rep (function<F,T> f2, function<F,T> g2):
00128     f (f2), g (g2) {}
00129   inline T apply (F x) { return Op::eval (f (x), g (x)); }
00130   inline ball<T> apply (ball<F> x) { return Op::eval (f (x), g (x)); }
00131   inline function<F,T> derive (V var) { return Op::diff (f, g, var); }
00132   inline tree expression () { return Op::eval (as_tree (f), as_tree (g)); }
00133 };
00134 
00135 template<typename F, typename T, typename Op> inline function<F,T>
00136 binary_function (function<F,T> f, function<F,T> g) {
00137   return tm_new<binary_function_rep<F,T,Op> > (f, g); }
00138 
00139 /******************************************************************************
00140 * Standard functions
00141 ******************************************************************************/
00142 
00143 TMPL inline function<F,T>
00144 operator - (function<F,T> f) {
00145   return unary_function<F,T,neg_op> (f); }
00146 
00147 TMPL inline function<F,T>
00148 operator + (function<F,T> f, function<F,T> g) {
00149   return binary_function<F,T,add_op> (f, g); }
00150 
00151 TMPL inline function<F,T>
00152 operator - (function<F,T> f, function<F,T> g) {
00153   return binary_function<F,T,sub_op> (f, g); }
00154 
00155 TMPL inline function<F,T>
00156 operator * (function<F,T> f, function<F,T> g) {
00157   return binary_function<F,T,mul_op> (f, g); }
00158 
00159 TMPL inline function<F,T>
00160 operator / (function<F,T> f, function<F,T> g) {
00161   return binary_function<F,T,div_op> (f, g); }
00162 
00163 TMPL inline function<F,T>
00164 sqrt (function<F,T> f) {
00165   return unary_function<F,T,sqrt_op> (f); }
00166 
00167 TMPL inline function<F,T>
00168 exp (function<F,T> f) {
00169   return unary_function<F,T,exp_op> (f); }
00170 
00171 TMPL inline function<F,T>
00172 log (function<F,T> f) {
00173   return unary_function<F,T,log_op> (f); }
00174 
00175 TMPL inline function<F,T>
00176 pow (function<F,T> f, function<F,T> g) {
00177   return binary_function<F,T,pow_op> (f, g); }
00178 
00179 TMPL inline function<F,T>
00180 cos (function<F,T> f) {
00181   return unary_function<F,T,cos_op> (f); }
00182 
00183 TMPL inline function<F,T>
00184 sin (function<F,T> f) {
00185   return unary_function<F,T,sin_op> (f); }
00186 
00187 TMPL inline function<F,T>
00188 tan (function<F,T> f) {
00189   return unary_function<F,T,tan_op> (f); }
00190 
00191 #undef TMPL
00192 #undef V
00193 #undef C
00194 #endif // FUNCTION_H