实现数学函数的乘法运算符 C++

2024-05-08

我有以下抽象基类:

class Function {
 virtual double Eval(double x) const = 0;
};     

我希望能够使用 f * g 或 f->operator *(g) 等表达式,其中 f 和 g 是类 Function 的具体对象,在我的主文件中,例如当我想计算定积分时这样我就可以写:

AnIntegrationMethod(f*g);

我想出的一个相当简单的方法包括声明一个 Product 类(仅显示头文件,实现很明显):

class Product: public Function {
 public: Product(Function *g, Function *f); 
  ~Product(); 
  virtual double Eval(double x) const; //return _g->Eval(x)*_f->Eval(x)
 private: Function *_g; Function *_f;
 };

然后在我的任何函数中

#include "Product.h"

class ConcreteFunction: public Function {
 public: 
   ConcreteFunction(); 
  ~ConcreteFunction(); 
  virtual double Eval(double x) const;
 Function* operator*(Function *f) {return new Product(this, f);}
 };

这实际上适用于简单的东西,但问题是运算符 * 仅在基类的单个派生类中定义,而不是为每个可能的派生类定义。这意味着,例如,如果我有一个代表数学函数的具体对象 f,我可以调用 f->operator *g 但如果我想再次调用operator * 来获取对象 (f->operator * g)- >operator * f 我无法这样做,因为函数 f* g 没有将 * 运算符定义为 f。

我想我应该直接在我的基类中定义运算符 * ,但是我无法弄清楚如何实现该运算符,因为我真的不知道如何获得产品的正确 Eval 函数,因为我无法使用 Product 类现在,在 Function 类本身中使用从 Function 类派生的 Product 类是没有意义的。我想我也很困惑,写如下内容一般是否正确:

 Function* Function::operator*(Function *f) {
 Function *g;
 ...
 //operations that allow g to be have the right Eval function
 //which should of the sort this->Eval(x) * f->Eval(x)
 ...
 return g;
 }

任何有关如何继续的提示或建议都将受到赞赏。我的水平很基础,我已经编程两个月了。


只是一个草图,你可能会这样做:

#include <memory>

// Base Function: f(x) = x
class Function
{
    protected:
    struct Implementation
    {
        virtual ~Implementation() {}
        virtual double evaluate(double x) const { return x; }
    };

    public:
    Function()
    :   self(std::make_shared<Implementation>())
    {}

    double operator () (double x) const { return self->evaluate(x); }

    protected:
    Function(std::shared_ptr<Implementation> self)
    :   self(self)
    {}

    private:
    std::shared_ptr<Implementation> self;
};
typedef Function Identity;


// Unary Function: u(-f(x))
class UnaryMinus : public Function
{
    protected:
    struct Implementation : Function::Implementation
    {
        Function f;
        Implementation(Function f)
        :   f(f)
        {};

        virtual double evaluate(double x) const override { return -f(x); }
    };

    public:
    UnaryMinus(Function f)
    :   Function(std::make_shared<Implementation>(f))
    {}
};

// Binary Function: u(f(x) + g(x))
class BinaryAdd : public Function
{
    protected:
    struct Implementation : Function::Implementation
    {
        Function f;
        Function g;
        Implementation(Function f, Function g)
        :   f(f), g(g)
        {};

        virtual double evaluate(double x) const override { return f(x) + g(x); }
    };

    public:
    BinaryAdd(Function f, Function g)
    :   Function(std::make_shared<Implementation>(f, g))
    {}
};

// Binary Function: u(f(x) * g(x))
class BinaryMultiply : public Function
{
    protected:
    struct Implementation : Function::Implementation
    {
        Function f;
        Function g;
        Implementation(Function f, Function g)
        :   f(f), g(g)
        {};

        virtual double evaluate(double x) const override { return f(x) * g(x); }
    };

    public:
    BinaryMultiply(Function f, Function g)
    :   Function(std::make_shared<Implementation>(f, g))
    {}
};

inline UnaryMinus operator - (Function f) { return UnaryMinus(f); }
inline BinaryAdd operator + (Function f, Function g) { return BinaryAdd(f, g); }
inline BinaryMultiply operator * (Function f, Function g) { return BinaryMultiply(f, g); }

#include <iostream>
int main() {
    Identity x;
    Function result = -x * (x + x) + x;
    std::cout << result(2) << '\n';
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

实现数学函数的乘法运算符 C++ 的相关文章

随机推荐

  • OCMock - 尽管是预期的,但仍调用了意外的方法

    这是经过测试的代码 if MFMailComposeViewController canSendMail MFMailComposeViewController mailComposeController MFMailComposeView
  • RabbitMQ 管理插件窗口呈现为空白页面

    I have installed Erlang RabbitMQ and configured the management plugin as per the instructions on the website https www r
  • 在多个不同线程之间共享变量

    我想在多个线程之间共享一个变量 如下所示 boolean flag true T1 main new T1 T2 help new T2 main start help start 我想分享flag在主线程和帮助线程之间 这是我创建的两个不
  • 使用 store.findQuery 时捕获 404 错误

    我正在使用余烬findQuery方法并想知道如何在没有结果时捕获 404 错误 this store findQuery customer hasProjects true getArchivedProjects archived then
  • 如何在类组件中使用react-redux useSelector?

    我是 React 新手 正在尝试学习 Redux 我想访问类中的存储 但它给了我一个错误 我不能在类中使用钩子 当我在函数中使用此代码时 正如我在 YouTube 教程中看到的那样 它可以正常工作 我在这里进入商店的柜台 function
  • 在压缩、分块的 HTTP 流到达时高效地读取行

    我编写了一个 HTTP 服务器 它生成由 JSON 结构事件组成的无尽 HTTP 流 类似于 Twitter 的流 API 这些事件由 n 根据服务器发送的事件 http en wikipedia org wiki Server sent
  • 如何将逻辑应用程序出站 IP 列入白名单?

    我得到了一个使用大量逻辑应用程序和连接器 ftp sftp 到不同合作伙伴的解决方案 我的问题是出站逻辑应用 IP 的白名单 我知道有一个出站 IP 列表 但出于安全原因 大多数合作伙伴只会开放少数几个 IP 并且 IP 应该是静态的 因此
  • 如何修改erlang中的记录?

    我需要修改操作记录中的值 place 和 other place op action walk from place to other place preconds at place me on floor me other place p
  • 模拟 SQL Server 实例上的当前日期?

    是否可以更改 SQL Server 上特定数据库的日期时间 它与操作系统的日期 时间相关吗 我们希望模拟未来的日期时间以进行测试 即GETDATE 返回未来的日期 它必须处于半生产 暂存 环境中 因此不幸的是 我们无法选择更改操作系统日期
  • 使用csv文件-PHP创建表到mysql时添加反引号

    我有一个 php 代码 它将使用 csv 文件创建一个到 mysql 数据库的表 然而 某些列标题没有被 mysql 读取 mysql 唯一一次读取查询是当我添加反引号 您能帮助我在查询中的何处添加反引号吗 这是我的代码 file C Us
  • 如何在时序图上表示 if 条件 1 else if 条件 2?

    我想知道 如何在序列图上表示 if cond1 else if cond2 语句 if condition1 Do something else if condition2 Do something else if 我不确定是否有两个独立的
  • 如何在 1 个活动 (android) 中显示 2 个视图?

    假设我打开了一个网络视图 public void onCreate Bundle savedInstanceState super onCreate savedInstanceState setContentView R layout ma
  • 静态方法是否会立即编译(JIT)?

    根据我的理解 CLR 编译器对实例方法和静态方法的处理方式相同 并且每当首次调用该方法时 IL 代码都会进行 JIT 编译 今天我和同事讨论了 他告诉我静态方法与实例方法的处理方式不同 即 静态方法在程序集加载到应用程序域后立即进行 JIT
  • 在 Sphinx 中的引用中保留内联代码

    在 Sphinx 中 如果我有以下标题声明 somestuff this is code this is not 它呈现如下 this is code 这不是 这很好 但是 如果我使用参考 例如 Have a look at ref som
  • 完成后关闭线程

    完成后如何关闭线程 比如确保没有任何东西再打开或运行 到目前为止我知道如何打开它 但是 不知道如何正确关闭它 int iret1 pthread t thread1 char message1 void multithreading1 vo
  • 如何在opencv python中为图像添加边框

    如果我有如下图所示的图像 如何在图像周围添加边框 以便最终图像的整体高度和宽度增加 但原始图像的高度和宽度保持在中间 下面的代码添加了一个大小恒定的边框10像素到原始图像的所有四个边 对于颜色 我假设您想要使用背景的平均灰度值 这是我根据图
  • 来自 OpenCV 的外部参数

    我正在使用 OpenCV 来校准立体相机对 我拍摄了各种校准照片 并且使用 cv2 calibrateCamera 对内在参数进行了令人满意的拟合 然而 目前尚不清楚如何获取外部参数 该函数仅返回cameraMatrix 尽管它很有用 但实
  • 如何从办公文档迁移到基于现代网络技术的文档 - 欢迎建议

    目前 所有文档均基于 MS Office 如果您想集成某些功能 这将变得非常具有挑战性 然后您可以选择使用 VBA 或 VSTO 第一个不太舒服 第二个可能就像拿大锤敲碎坚果一样 简单的控件 隐藏文本或基本数学等简单的事情都可以通过 HTM
  • java:无法访问org.springframework.boot.SpringApplication错误的类文件

    java cannot access org springframework boot SpringApplication bad class file C Users xyz m2 repository org springframewo
  • 实现数学函数的乘法运算符 C++

    我有以下抽象基类 class Function virtual double Eval double x const 0 我希望能够使用 f g 或 f gt operator g 等表达式 其中 f 和 g 是类 Function 的具体