编写方法的不同方式

时间:2017-12-16 10:40:50

标签: c++ differential-equations

我有一个类来定义微分方程的右边项: 这个类提供了计算rhs函数及其派生的方法 该函数以这种方式存储在向量容器中,该类也适用于微分方程组。

这里是我要更改的方法的界面

 template <typename Type = double>
class rhsODEProblem {

       using analysisFunction = std::function<const Type(const Type, const std::valarray<Type>)>;


   public:



   rhsODEProblem(const std::vector<std::function<const Type(const Type,const std::valarray<Type>)>> numfun ,
                  const std::vector<std::function<const Type(const Type,const Type)>> exactfun ,
                  const Type,const Type,const Type,const std::valarray<Type>,const std::string ) noexcept ;


   rhsODEProblem(const std::vector<std::function<const Type(const Type,const std::valarray<Type>)>> numfun,
                 const Type, const Type, const Type, const std::valarray<Type> ) noexcept ;


      virtual ~rhsODEProblem() = default ;

      rhsODEProblem(const rhsODEProblem &) = default ;
      rhsODEProblem(rhsODEProblem&& ) = default ;
      rhsODEProblem& operator=(const rhsODEProblem&) = default ;
      rhsODEProblem& operator=(rhsODEProblem&& ) = default ;


      const std::vector<std::function<const Type(const Type,const std::valarray<Type>)>> numericalFunction ;

      const std::vector<std::function<const Type(const Type,const Type)>> analiticalFunction ;


      const std::vector<analysisFunction>&  f = numericalFunction ;   


      const auto dfdt(std::size_t indx , const Type t , const std::valarray<Type> u) {
            return (f[indx](t, u+eps )-f[indx](t,u))/eps ;
      }   


      auto setRhs (const std::vector<
                   std::function<const Type(const Type,const std::valarray<Type>)>> numfun) noexcept
      {     

        for(auto i=0 ; i < numfun.size() ; i++)
        {
          numericalFunction.push_back(numfun.at(i)) ; 
        }  
      } 

      auto setExact(const std::vector<std::function<const Type(const Type,const Type)>> exactfun) noexcept
      {
          for(auto i=0 ; i < exactfun.size(); i++)
          {
            analiticalFunction.push_back(exactfun.at(i));
          }  
      }

      auto solveExact() noexcept ;

      const Type t0() const noexcept { return _t0 ;} 
      const Type tf() const noexcept { return _tf ;}
      const Type dt() const noexcept { return _dt ;}
      const std::valarray<Type> u0() const noexcept { return _u0 ;}
      const std::string fname ()     const noexcept { return filename ;}


//---
   private:

                   Type  _t0 ;  // start time
                   Type  _tf ;  // final time
                   Type  _dt ;  // time-step
     std::valarray<Type> _u0 ;  // iv

     std::string filename ; 

     Type eps  = 1e-12  ; 


};

我想更改方法dfdt,我可以使用以下语法dfdt[index]( t , u_valarray )而不是dfdt(index, t, u_valarray )来调用它 我可以通过哪种方式更改此方法?

编辑感谢您的回答,所以在我的情况下会是:

foo_helper(foo &, int index);
          operator()(int n, Type t, std::valarray<Type> u );

对吧?

编辑不,我没有说明问题。我写道:

class dfdx {
        public:
            dfdx( rhsODEProblem<Type> &r , int index_ ) : index{index_ } {}
            void operator()(Type t, std::valarray<Type> u){
                    return (f[index](t, u + eps )-f[index](t,u))/eps ;
            }
        int index ;
  };

  dfdx operator[](std::size_t index) {
        return dfdx(*this, index);
  }

然后我用这种方式称呼它:

rhs.dfdx[j](t , uOld) )

但是我收到了一个错误:

BackwardEulerSolver.H:114:50: error: invalid use of ‘class mg::numeric::odesystem::rhsODEProblem<double>::dfdx’
                                  ( 1- dt() * rhs.dfdx[j](t , uOld) ) ;
                                              ~~~~^~~~

1 个答案:

答案 0 :(得分:1)

  

我想改变方法dfdt的方式我可以使用以下语法 dfdt[index]( t , u_valarray ) 而不是dfdt(index, t, u_valarray )来调用它。我可以通过哪种方式更改此方法方法 ?

你可以重载索引操作符([])并返回一个内部帮助器类型,它会重载调用操作符(())。

这是草图:

 class foo {
     class foo_helper;
     friend class foo_helper;
 public:
     class foo_helper {
     public:
          foo_helper(foo &, int index);
          void operator()(int n, double y);
     };
     foo_helper operator[](int index) {
           return foo_helper(*this, index);
     }
 };