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

/export/development/ViennaMath/viennamath/runtime/constant.hpp

Go to the documentation of this file.
00001 #ifndef VIENNAMATH_RUNTIME_CONSTANT_HPP
00002 #define VIENNAMATH_RUNTIME_CONSTANT_HPP
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 
00017 
00018 
00019 
00020 #include <ostream>
00021 #include "viennamath/forwards.h"
00022 //#include "viennamath/expression_compile_time.hpp"
00023 #include "viennamath/runtime/expression_interface.hpp"
00024 //#include "viennamath/expression_run_time.hpp"
00025 
00026 
00031 namespace viennamath
00032 {
00033   
00034   //per default, we assume floating point constants, which cannot be tackled with template arguments
00039   template <typename ScalarType, /* see forwards.h for default argument */
00040             typename InterfaceType /* see forwards.h for default argument */>
00041   class rt_constant : public InterfaceType
00042   {
00043       typedef rt_constant<ScalarType, InterfaceType>     self_type;
00044     public:
00045       typedef typename InterfaceType::numeric_type    numeric_type;
00046       
00047       explicit rt_constant(ScalarType s_) : s(s_) {};
00048 
00050       self_type operator() () const
00051       {
00052         return *this;
00053       }
00054 
00056       template <typename VectorType>
00057       self_type operator() (const VectorType & p) const
00058       {
00059         return *this;
00060       }
00061       
00063       operator ScalarType() const { return s; }
00064       
00065       //interface requirements:
00067       InterfaceType * clone() const { return new self_type(s); }
00068       
00070       numeric_type eval(std::vector<numeric_type> const & v) const { return s; }
00072       numeric_type eval(numeric_type v) const { return s; }
00073       
00075       bool is_constant() const { return true; }
00076       
00078       std::string deep_str() const
00079       {
00080         std::stringstream ss;
00081         ss << "constant(" << s << ")";
00082         return ss.str();      
00083       }
00084       
00086       numeric_type unwrap() const { return s; }
00087       
00089       InterfaceType * substitute(const InterfaceType * e,
00090                                  const InterfaceType * repl) const
00091       {
00092         if (deep_equal(e))
00093           return repl->clone();
00094         
00095         //std::cout << "FALSE" << std::endl;
00096         return clone();
00097       };    
00098       
00100       InterfaceType * substitute(std::vector<const InterfaceType *> const &  e,
00101                                  std::vector<const InterfaceType *> const &  repl) const
00102       {
00103         //std::cout << "Comparing variable<" << id << "> with " << e->str() << ", result: ";
00104         for (size_t i=0; i<e.size(); ++i)
00105           if (deep_equal(e[i]))
00106             return repl[i]->clone();
00107         
00108         //std::cout << "FALSE" << std::endl;
00109         return clone();
00110       };    
00111       
00113       bool deep_equal(const InterfaceType * other) const
00114       {
00115         const self_type * ptr = dynamic_cast< const self_type *>(other);
00116         if (ptr != NULL)
00117           return ptr->s == s;
00118         
00119         return false;
00120       }
00121       
00123       bool shallow_equal(const InterfaceType * other) const
00124       {
00125         return dynamic_cast< const self_type * >(other) != NULL;
00126       }
00127       
00129       InterfaceType * diff(const InterfaceType * diff_var) const
00130       {
00131         return new self_type(0);
00132       }
00133       
00134     private:
00135       ScalarType s;
00136   };
00137 
00139   template <typename ScalarType, typename InterfaceType>
00140   std::ostream& operator<<(std::ostream & stream,
00141                            rt_constant<ScalarType, InterfaceType> const & c)
00142   {
00143     stream << c.deep_str();
00144     return stream;
00145   }
00146 
00147   
00148   
00149   
00150 }
00151 
00152 #endif

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