有没有办法在C ++中复合函数?

时间:2018-06-06 11:33:31

标签: c++ inheritance statistics operator-overloading

一般问题:

如果有两个对象AB,其功能分别为f_A(arg list)f_B(arg list)

使用由f_A(...)和f_B(...)组合的函数创建对象C的最佳方法是什么? 例如:f_C() = f_A() + f_B() or f_C() = f_A(f_B())

是否可以重载“+”运算符,以便我们可以创建对象C做类似的事情?

auto object_c = object_a + object_b

以下是我的代码示例:

class GaussianKernel : public Kernel {
        public:
            GaussianKernel(double sigma) : m_sigma(sigma), m_scale(1) {}

            double covarianceFunction(
                double   X,
                double   Y
            )
            {
                double result;

                result = m_scale  *  exp(-norm(X - Y) / (m_sigma*m_sigma));

                return result;      
            }


            GaussianKernel operator+(const GaussianKernel& b) {
            /*Here I would like to overload the + operator such that 
            I can create a kernel from two others kernels, 
            I mean with a covariance function compound of the previous ones 
            */
            }
        private:
            double m_sigma;
            double m_scale;
        };

谢谢你。

3 个答案:

答案 0 :(得分:3)

给定两种方法f_Af_B,您可以通过使用lambda来获取f_C返回其他方法的总和:

auto f_C = [](/*param*/){ return f_A(/*param*/) + f_B(/*param*/); };
auto sum_result = f_C(param);

要获得复合方法,就是这样:

auto f_C = [](/*param*/){ return f_B( f_A(/*param*/)); };
auto compound_result = f_C(param);

PS:我知道这不是直接适用于你的例子,仍然试图找出你想要做什么。

答案 1 :(得分:2)

我会从这样的原型解决方案开始:

//TextField with border
textField.doGlowAnimation(withColor: UIColor.red, withEffect: .big)

//Label
label.doGlowAnimation(withColor: label.textColor, withEffect: .small)

不再使用lambda,但现在使用你想要的功能。

稍后我会尝试删除class FooKernel : public Kernel { public: FooKernel (std::function<double(double, double)> fun) : fun_(fun) {} double covarianceFunction( double X, double Y ) const { return fun_(X, Y); } template<class T> auto operator+(const T &b) const { return FooKernel([b, this](double X, double Y){ return this->covarianceFunction(X, Y) + b.covarianceFunction(X, Y); }); } private: std::function<double(double, double)> fun_; }; class GaussianKernel : public Kernel { public: GaussianKernel(double sigma) : m_sigma(sigma), m_scale(1) {} double covarianceFunction( double X, double Y ) const { double result; result = m_scale * exp(-norm(X - Y) / (m_sigma*m_sigma)); return result; } template<class T> auto operator+(const T &b) const { return FooKernel([b, this](double X, double Y){ return this->covarianceFunction(X, Y) + b.covarianceFunction(X, Y); }); } private: double m_sigma; double m_scale; }; ,因为它可能会对性能产生很大的影响。相反,我会使std::function成为一个类模板,它存储可按值调用。

答案 2 :(得分:2)

我建议另一个Kernel的子类:

class CompoundGaussianKernel : public Kernel {
    public:
        CompoundGaussianKernel(GaussianKernel const& kernel1, GaussianKernel const& kernel2) 
            : m_kernel1(kernel1), m_kernel2(kernel2) 
        {}

        double covarianceFunction(double X, double Y)
        {
            return m_kernel1.covarianceFunction(X, Y) + m_kernel2.covarianceFunction(X, Y);
            // or any other composition than "+"
        }

    private:
        GaussianKernel m_kernel1;
        GaussianKernel m_kernel2;
    };

我建议不要在类中定义operator+,而应将其定义为自由函数。

CompoundGaussianKernel operator+(GaussianKernel const& kernel1, GaussianKernel const& kernel2)
{
    return CompoundGaussianKernel(kernel1, kernel2);
}
相关问题