• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List

/export/development/ViennaMath/viennamath/forwards.h

Go to the documentation of this file.
00001 #ifndef VIENNAMATH_FORWARDS_H
00002 #define VIENNAMATH_FORWARDS_H
00003 
00004 /* =======================================================================
00005    Copyright (c) 2012, Institute for Microelectronics,
00006                        Institute for Analysis and Scientific Computing,
00007                        TU Wien.
00008                              -----------------
00009                ViennaMath - Symbolic and Numerical Math in C++
00010                              -----------------
00011 
00012    Author:     Karl Rupp                          rupp@iue.tuwien.ac.at
00013 
00014    License:    MIT (X11), see file LICENSE in the ViennaMath base directory
00015 ======================================================================= */
00016 
00028 #include <string>
00029 #include <vector>
00030 #include <exception>
00031 #include <sstream>
00032 #include <memory>
00033 
00039 namespace viennamath
00040 {
00041   //basic numeric type (customize to your needs)
00043   typedef double              default_numeric_type;
00044   
00046   typedef unsigned long       id_type;
00047   
00049 
00050   template <bool b, class T = void> 
00051   struct enable_if
00052   {
00053     typedef T   type;
00054   };
00055 
00056   template <class T> 
00057   struct enable_if<false, T> {};
00058 
00063   template <id_type dim>
00064   struct cartesian {};
00065   
00066 
00077   template <typename NumericT>
00078   class numeric_type_proxy
00079   {
00080     public:
00081       numeric_type_proxy(NumericT value) : value_(value) {}
00082       
00083       NumericT get() const { return value_; }
00084     private:
00085       NumericT value_;
00086   };
00087 
00088   //  
00090   //
00091   
00098   template <typename LHS,
00099             typename OP,
00100             typename RHS>
00101   class ct_binary_expr;
00102 
00108   template <typename LHS,
00109             typename OP>
00110   class ct_unary_expr;
00111   
00116   template <long value_>
00117   class ct_constant;
00118 
00123   template <id_type id = 0>
00124   struct ct_variable;
00125 
00130   template <typename Tag>
00131   class ct_function_symbol;
00132 
00138   template <typename LHS, typename RHS>
00139   class ct_equation;
00140   
00141 
00142   
00147   template <long i>
00148   struct ct_index {};
00149   
00154   template <typename T0>
00155   class ct_vector_1;
00156   
00162   template <typename T0, typename T1>
00163   class ct_vector_2;
00164   
00171   template <typename T0, typename T1, typename T2>
00172   class ct_vector_3;
00173  
00174   //
00176   //
00177 
00179   template <typename NumericT>
00180   class rt_expression_interface;
00181   
00183   typedef rt_expression_interface<default_numeric_type>     default_interface_type;
00184   
00189   template <typename InterfaceType = default_interface_type>
00190   class rt_variable;
00191   typedef rt_variable<>                variable;
00192 
00197   template <typename ScalarType = default_numeric_type, typename InterfaceType = default_interface_type>
00198   class rt_constant;
00199   typedef rt_constant<>                constant;
00200 
00205   template <typename InterfaceType = default_interface_type>
00206   class rt_binary_expr;  
00207   typedef rt_binary_expr<>             binary_expr;
00208   
00213   template <typename InterfaceType = default_interface_type>
00214   class rt_unary_expr;
00215   typedef rt_unary_expr<>              unary_expr;
00216   
00223   template <typename InterfaceType = default_interface_type>
00224   class rt_expr;
00225   typedef rt_expr<>                    expr;
00226   
00231   template <typename InterfaceType = default_interface_type>
00232   class rt_vector_expr; 
00233   typedef rt_vector_expr<>             vector_expr;
00234   
00239   template <typename InterfaceType = default_interface_type>
00240   class rt_function_symbol;
00241   typedef rt_function_symbol<>         function_symbol;
00242 
00247   template <typename InterfaceType = default_interface_type>
00248   class rt_equation;
00249   typedef rt_equation<>                equation;
00250 
00255   template <typename InterfaceType = default_interface_type>
00256   class rt_interval;
00257   typedef rt_interval<>                interval;
00258 
00263   template <typename InterfaceType = default_interface_type>
00264   class rt_symbolic_interval;
00265   typedef rt_symbolic_interval<>       symbolic_interval;
00266   
00267   
00272   template <typename InterfaceType = default_interface_type>   //Note: No convenience typedef needed here
00273   class rt_manipulation_interface;
00274 
00279   template <typename InterfaceType = default_interface_type>   //Note: No convenience typedef needed here
00280   class rt_manipulation_wrapper;
00281   
00282   
00287   template <typename InterfaceType = default_interface_type>   //Note: No convenience typedef needed here
00288   class rt_traversal_interface;
00289   
00294   template <typename InterfaceType = default_interface_type>   //Note: No convenience typedef needed here
00295   class rt_traversal_wrapper;
00296   
00297   //
00299   //
00300   
00305   template <typename InterfaceType = default_interface_type>
00306   class rt_op_interface;
00307 
00308   //binary operator tags:  
00314   template <typename BinaryOperation, typename InterfaceType = default_interface_type>
00315   class op_binary;
00316   
00318   template <typename NumericT>
00319   struct op_plus;
00320   
00322   template <typename NumericT>
00323   struct op_minus;
00324   
00326   template <typename NumericT>
00327   struct op_mult;
00328   
00330   template <typename NumericT>
00331   struct op_div;
00332   
00334   template <typename NumericT>
00335   struct op_pow;
00336 
00337   
00338   
00339   //
00341   //
00342   
00348   template <typename UnaryOperation, typename InterfaceType = default_interface_type>
00349   class op_unary;
00350   
00351 
00353   template <typename NumericT>
00354   struct op_id;
00355   
00357   template <typename NumericT>
00358   struct op_exp;
00359   
00361   template <typename NumericT>
00362   struct op_sin;
00363   
00365   template <typename NumericT>
00366   struct op_cos;
00367   
00369   template <typename NumericT>
00370   struct op_tan;
00371   
00373   template <typename NumericT>
00374   struct op_fabs;
00375   
00377   template <typename NumericT>
00378   struct op_sqrt;
00379   
00381   template <typename NumericT>
00382   struct op_log; //natural logarithm
00383   
00385   template <typename NumericT>
00386   struct op_log10;
00387 
00389   template <typename NumericT>
00390   class op_partial_deriv;
00391   
00393   template <typename InterfaceType>
00394   class op_rt_integral;
00395 
00396   
00401   template <typename InterfaceType>
00402   class op_rt_symbolic_integral;
00403   
00404 
00405 
00407   namespace result_of
00408   {
00415     template <typename T>
00416     struct is_viennamath
00417     {
00418       enum { value = false };
00419     };
00420     
00422     template <long val>
00423     struct is_viennamath<ct_constant<val> >
00424     {
00425       enum { value = true };
00426     };
00427     
00429     template <id_type id>
00430     struct is_viennamath<ct_variable<id> >
00431     {
00432       enum { value = true };
00433     };
00434 
00436     template <typename TAG>
00437     struct is_viennamath<ct_function_symbol<TAG> >
00438     {
00439       enum { value = true };
00440     };
00441     
00443     template <typename LHS, typename OP, typename RHS>
00444     struct is_viennamath<ct_binary_expr<LHS, OP, RHS> >
00445     {
00446       enum { value = true };
00447     };
00448 
00450     template <typename LHS, typename OP>
00451     struct is_viennamath<ct_unary_expr<LHS, OP> >
00452     {
00453       enum { value = true };
00454     };
00455     
00456     
00457     
00459     template <typename NumericType, typename InterfaceType>
00460     struct is_viennamath<rt_constant<NumericType, InterfaceType> >
00461     {
00462       enum { value = true };
00463     };
00464     
00466     template <typename InterfaceType>
00467     struct is_viennamath<rt_variable<InterfaceType> >
00468     {
00469       enum { value = true };
00470     };
00471 
00473     template <typename InterfaceType>
00474     struct is_viennamath<rt_function_symbol<InterfaceType> >
00475     {
00476       enum { value = true };
00477     };
00478     
00480     template <typename InterfaceType>
00481     struct is_viennamath<rt_unary_expr<InterfaceType> >
00482     {
00483       enum { value = true };
00484     };
00485     
00487     template <typename InterfaceType>
00488     struct is_viennamath<rt_binary_expr<InterfaceType> >
00489     {
00490       enum { value = true };
00491     };
00492     
00494     template <typename InterfaceType>
00495     struct is_viennamath<rt_expr<InterfaceType> >
00496     {
00497       enum { value = true };
00498     };
00499     
00501     
00508     template <typename T>
00509     struct is_compiletime
00510     {
00511       enum { value = false };
00512     };
00513     
00515     template <long val>
00516     struct is_compiletime<ct_constant<val> >
00517     {
00518       enum { value = true };
00519     };
00520     
00522     template <id_type id>
00523     struct is_compiletime<ct_variable<id> >
00524     {
00525       enum { value = true };
00526     };
00527 
00529     template <typename TAG>
00530     struct is_compiletime<ct_function_symbol<TAG> >
00531     {
00532       enum { value = true };
00533     };
00534     
00536     template <typename LHS, typename OP, typename RHS>
00537     struct is_compiletime<ct_binary_expr<LHS, OP, RHS> >
00538     {
00539       enum { value = true };
00540     };
00541 
00543     template <typename LHS, typename OP>
00544     struct is_compiletime<ct_unary_expr<LHS, OP> >
00545     {
00546       enum { value = true };
00547     };
00548 
00550     
00555     template <typename LHS, typename RHS>
00556     struct interface
00557     {
00558       typedef default_interface_type  type; //for compatibility with enable_if
00559     };
00560 
00562     template <typename T, typename U, typename RHS>
00563     struct interface< rt_constant<T, U>, RHS >
00564     {
00565       typedef U    type;
00566     };
00567 
00569     template <typename T, typename RHS>
00570     struct interface< rt_variable<T>, RHS >
00571     {
00572       typedef T    type;
00573     };
00574 
00576     template <typename T>
00577     struct interface< default_numeric_type, rt_variable<T> >
00578     {
00579       typedef T    type;
00580     };
00581 
00583     template <typename T, typename RHS>
00584     struct interface< rt_binary_expr<T>, RHS >
00585     {
00586       typedef T    type;
00587     };
00588 
00590     template <typename T>
00591     struct interface< default_numeric_type, rt_binary_expr<T> >
00592     {
00593       typedef T    type;
00594     };
00595 
00597     template <typename T, typename RHS>
00598     struct interface< rt_unary_expr<T>, RHS >
00599     {
00600       typedef T    type;
00601     };
00602     
00604     template <typename T, typename RHS>
00605     struct interface< rt_expr<T>, RHS >
00606     {
00607       typedef T    type;
00608     };
00609 
00611     template <typename T>
00612     struct interface< double,  rt_expr<T> >
00613     {
00614       typedef T    type;
00615     };
00616     
00620     template <typename LHS,
00621               typename RHS,
00622               bool lhs_is_ct = is_compiletime<LHS>::value,
00623               bool rhs_is_ct = is_compiletime<RHS>::value>
00624     struct add;
00625     
00629     template <typename LHS,
00630               typename RHS,
00631               bool lhs_is_ct = is_compiletime<LHS>::value,
00632               bool rhs_is_ct = is_compiletime<RHS>::value>
00633     struct subtract;
00634     
00638     template <typename LHS,
00639               typename RHS,
00640               bool lhs_is_ct = is_compiletime<LHS>::value,
00641               bool rhs_is_ct = is_compiletime<RHS>::value>
00642     struct mult;
00643 
00647     template <typename LHS,
00648               typename RHS,
00649               bool lhs_is_ct = is_compiletime<LHS>::value,
00650               bool rhs_is_ct = is_compiletime<RHS>::value>
00651     struct div;
00652     
00653     
00655     
00657     template <long a, long b>
00658     struct gcd
00659     {
00660       enum { value = gcd<b, a % b>::value };
00661     };
00662     
00664     template <long a>
00665     struct gcd <a, 0>
00666     {
00667       enum { value = a};
00668     };
00669 
00671     template <>
00672     struct gcd <0, 0>
00673     {
00674       enum { error = 1};
00675     };
00676     
00677     
00678   } //namespace result_of
00679   
00680   
00681     
00683   template <typename T>
00684   struct expression_traits
00685   {
00686     typedef T   const_reference_type; 
00687   };
00688   
00689   //for compile time constants, we have to copy in order to circumvent problems with temporaries
00696   template <long value>
00697   struct expression_traits < ct_constant<value> >
00698   {
00699      typedef ct_constant<value>    const_reference_type;
00700   };
00701 
00708   template <typename T, typename InterfaceType>
00709   struct expression_traits < rt_constant<T, InterfaceType> >
00710   {
00711      typedef rt_constant<T, InterfaceType>    const_reference_type;
00712   };
00713   
00714 
00715   
00716   
00720   template <typename LHS, typename RHS>
00721   typename enable_if< result_of::is_viennamath<LHS>::value || result_of::is_viennamath<RHS>::value,
00722                       typename result_of::add<LHS, RHS>::type >::type
00723   operator+(LHS const & lhs, RHS const & rhs)
00724   {
00725     return result_of::add<LHS, RHS>::instance(lhs, rhs); 
00726   }
00727   
00731   template <typename LHS, typename RHS>
00732   typename enable_if< result_of::is_viennamath<LHS>::value || result_of::is_viennamath<RHS>::value,
00733                       typename result_of::subtract<LHS, RHS>::type >::type
00734   operator-(LHS const & lhs, RHS const & rhs)
00735   {
00736     return result_of::subtract<LHS, RHS>::instance(lhs, rhs); 
00737   }
00738 
00742   template <typename LHS, typename RHS>
00743   typename enable_if< result_of::is_viennamath<LHS>::value || result_of::is_viennamath<RHS>::value,
00744                       typename result_of::mult<LHS, RHS>::type >::type
00745   operator*(LHS const & lhs, RHS const & rhs)
00746   {
00747     return result_of::mult<LHS, RHS>::instance(lhs, rhs); 
00748   }
00749   
00753   template <typename LHS, typename RHS>
00754   typename enable_if< result_of::is_viennamath<LHS>::value || result_of::is_viennamath<RHS>::value,
00755                       typename result_of::div<LHS, RHS>::type >::type
00756   operator/(LHS const & lhs, RHS const & rhs)
00757   {
00758     return result_of::div<LHS, RHS>::instance(lhs, rhs); 
00759   }
00760   
00761 }
00762 
00763 #endif

Generated on Wed Feb 29 2012 21:50:43 for ViennaMath by  doxygen 1.7.1