编写方法的不同方法

different way to write a method

本文关键字:方法      更新时间:2023-10-16

我有一个类来定义微分方程的右手侧术语:此类提供计算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[index]( t , u_valarray )而不是dfdt(index, t, u_valarray )更改方法DFDT我可以以哪种方式更改此方法?

edit 感谢您的回答,因此在我的情况下是:

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) ) ;
                                              ~~~~^~~~

我想以以下语法 dfdt[index]( t , u_valarray ) 来更改方法DFDT,而不是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);
     }
 };