c++ template for polynomial and deviation

Viewed 94

I am trying to understand c++ templates. At first I tried to make C++ templates to represent integer polynomial expression. as I am new I am afraid I might have missed something and that there might be a bug and I will be happy for comments on my code:

class Literal {
    public:
        Literal(const double v) : _val(v) {}
        double eval() const { return _val; }
    private:
        const double _val;
    };
class Variable {
public:
    Variable(double& v) : _val(v) {}
    double eval() const { return _val; }
private:
    double& _val;
};

template <class ExprT1, class ExprT2, class BinOp>
class BinaryExpr {
public:
    BinaryExpr(ExprT1 e1, ExprT2 e2, BinOp op = BinOp())
        : _expr1(e1), _expr2(e2), _op(op) {}
    double eval() const
    {
        return _op(_expr1.eval(), _expr2.eval());
    }
private:
    ExprT1 _expr1;
    ExprT2 _expr2;
    BinOp  _op;
};

template  <class ExprT1, class ExprT2>
BinaryExpr<ExprT1, ExprT2, std::plus<double>>
makeSum(ExprT1 e1, ExprT2 e2)
{
    return BinaryExpr<ExprT1, ExprT2, std::plus<double>>(e1, e2);
}


template  <class ExprT1, class ExprT2>
BinaryExpr<ExprT1, ExprT2, std::minus<double>>
makeMinus(ExprT1 e1, ExprT2 e2)
{
    return BinaryExpr<ExprT1, ExprT2, std::minus<double>>(e1, e2);
}

template  <class ExprT1, class ExprT2>
BinaryExpr <ExprT1, ExprT2, std::multiplies<double> >
makeProd(ExprT1 e1, ExprT2 e2)
{
    return
        BinaryExpr<ExprT1, ExprT2, std::multiplies<double> >(e1, e2);
};


template<class T>
struct pows
{
    constexpr T operator()(const T& base, const T& exponent) const
    {               
        return (exponent == 0) ? 1 : (base * pow(base, exponent - 1));
    }
};



template  <class ExprT1, class ExprT2>
BinaryExpr <ExprT1, ExprT2, pows<double>>  
makePow(ExprT1 e1, ExprT2 e2)
{
    return
        BinaryExpr<ExprT1, ExprT2, pows<double> >(e1, e2);
};

double someFunction(double x)
{
    //return  makeProd(makeSum(Variable(x), Literal(2)), Literal(3)).eval();
    return makeSum(makeSum(makePow(Variable(x), Literal(4)), makePow(makeMinus(Variable(x), Literal(2)), Literal(2))), makeSum(makeProd(Variable(x), Literal(3)), Literal(5))).eval();

}
void main()
{
    cout << someFunction(4) << endl;
    system("pause");    

}
  1. Also I want to make template that takes an expression from Part 1 and returns an expression representing the derivative of the input expression. Honesty the way I have implemented my first template I have no idea if I am on the right track.

AT first I try to represent the two simplest rules:

e + f )’ = ( e’ ) + ( f’ )

( en )’ = n * en-1 * ( e )’

Does it mean that for example for the deviation of the pow function I can create

template<BinaryExpr <ExprT1, ExprT2, pows<double>>  
DeviatePow(ExprT1 e1, ExprT2 e2)
{
    return
        BinaryExpr<ExprT1, ExprT2, pows<double>(makeProd(e2, MakePow(e1,makeMinus(e2, Literal(1))));
};

I will be happy for any suggestions how to start! Thank you

0 Answers
Related