Polynomials in C++

Discussion in 'C++' started by Brian M. Dean, May 7, 2005.

  1. I am trying to write a polynomial class in C++.
    I admit that so far what I have is a bit sloppy.
    This is not for a class, but for my own personal
    research and enjoyment.

    Polynomial.h
    __________________________________________
    class Polynomial{
    public:
    Polynomial(const unsigned int exponent = 0);
    Polynomial(double c = 0.0, unsigned int exponent = 0);

    // MODIFICATION MEMBER FUNCTIONS
    void add_to_coeff(double amount, unsigned int exponent);
    void set_coeff(double coefficient, unsigned int exponent);

    // CONSTANT MEMBER FUNCTIONS
    double get_coeff(unsigned int exponent) const;
    unsigned int get_degree( ) const { return degree; }
    Polynomial derivative( ) const;
    double eval(double x) const;
    Polynomial eval(Polynomial p) const;
    unsigned int next_term(unsigned int e) const;
    unsigned int previous_term(unsigned int e) const;

    // CONSTANT OPERATORS
    double operator( ) (double x) const { return eval(x); }

    // Assignment and copy constructors
    const Polynomial &Polynomial::eek:perator= (const Polynomial &rhs);

    Polynomial(const Polynomial &poly)
    {
    degree = poly.degree;
    coeff = new double [degree+1];
    for (int i=0;i<=degree;i++)
    coeff=poly.coeff;

    // unsigned int tmpdegree = poly.get_degree();
    // degree = tmpdegree;
    // coeff = new double[tmpdegree];
    // for (int i=0; i<=tmpdegree; i++)
    // coeff = poly.coeff;
    }

    // BINARY OPERATORS
    const Polynomial operator+( const Polynomial& poly)
    {
    unsigned int largedegree, i;

    if (this->degree > poly.degree) largedegree = degree;
    else largedegree = poly.degree;
    Polynomial ans(largedegree);
    for (i=0; i<=largedegree; i++)
    {
    if (i <= degree) ans.coeff += coeff;
    if (i <= poly.degree) ans.coeff += poly.coeff;
    }
    return ans;
    }

    const Polynomial operator-( const Polynomial& poly)
    {
    unsigned int largedegree, i;

    if (degree > poly.degree) largedegree = degree;
    else largedegree = poly.degree;
    Polynomial ans(largedegree);
    for (i=0; i<=largedegree; i++)
    {
    if (i <= degree) ans.coeff += coeff;
    if (i <= poly.degree) ans.coeff -= poly.coeff;
    }
    return ans;
    }

    const Polynomial operator*( const Polynomial& poly)
    {
    unsigned int largedegree, i, j, tmpint;

    largedegree = degree + poly.degree;
    Polynomial ans(largedegree);
    for (i=0; i <= degree; i++)
    {
    for (j=0; j <= poly.degree; j++)
    {
    tmpint = i + j;
    ans.coeff[tmpint] += (coeff * poly.coeff[j]);
    }
    }
    printf("Times Function:\n");
    for (i=0; i<=largedegree; i++)
    printf("%0.12lf\t", ans.coeff);
    printf("\n");
    return Polynomial(ans);
    }

    // OUTPUT FUNCTION
    // std::eek:stream& operator << (std::eek:stream& out, const Polynomial& p);
    ~Polynomial();
    private:
    Polynomial();
    double *coeff;
    unsigned int degree;
    };
    ___________________________________________

    Polynomial.cpp
    ___________________________________________
    Polynomial::polynomial(const unsigned int n)
    {
    unsigned int i;
    coeff = new double[n+1];
    for (i=0; (i < n+2); i++)
    coeff = 0.0;
    degree = n;
    }

    void Polynomial::add_to_coeff(double amount, unsigned int exponent)
    {
    coeff[exponent] += amount;
    }



    double Polynomial::eval(double x) const
    {
    double ans;
    int i;

    for (i=degree; i>0; i--)
    {
    ans += coeff;
    ans *= x;
    }
    ans += coeff[0];
    return ans;
    }


    const Polynomial &Polynomial::eek:perator= (const Polynomial &rhs)
    {
    if (this==&rhs)
    return *this;

    if (degree!=rhs.degree)
    {
    delete [] coeff;
    degree=rhs.degree;
    coeff=new double [degree+1];
    }
    for (int i=0;i<=degree;i++)
    coeff = rhs.coeff;

    return *this;
    }


    Polynomial Polynomial::eval(Polynomial poly) const
    {
    Polynomial ans(degree);
    int i;

    for (i=degree; i>0; i--)
    {
    ans.coeff[0] += coeff;
    ans = ans * poly;
    }
    ans.coeff[0] += coeff[0];
    return ans;
    }

    void Polynomial::set_coeff(double coefficient, unsigned int exponent)
    {
    coeff[exponent] = coefficient;
    }

    double Polynomial::get_coeff(unsigned int exponent) const
    {
    return coeff[exponent];
    }

    Polynomial::~Polynomial()
    {
    delete [] coeff;
    }
    _____________________________________

    The problem I am having is that when I call the * operator
    it destroys ans before returning it and so I get the wrong
    answer if I do "poly1 = poly2 * poly2"

    I tried returning both "ans" and "Polynomial(ans)" but both
    have the same problem.

    I am using the compiler that comes with Fedora Core 3 and
    using KDevelop as an interface.
     
    Brian M. Dean, May 7, 2005
    #1
    1. Advertisements

  2. There is no need to declare the default c-tor. The one you have with
    an argument (unsigned) will tell the compiler not to create the default
    one.
    Why can't you, instead of mucking with dynamic memory, just use
    Well, you didn't post any "driver" code to complete the picture
    you're painting. So, there is no way for us to verify your claims
    or to help you fix your code (which can be broken elsewhere, in
    the parts you didn't post).
    That shouldn't matter much.

    V
     
    Victor Bazarov, May 7, 2005
    #2
    1. Advertisements

  3. I tried returning both "ans" and "Polynomial(ans)" but both
    Sorry, after I posted I noticed that I made a stupid
    mistake in the driver code. I had
    "ans = test * test"
    but then I printed out the variables to "test" instead
    of "ans" and was assuming that the assignment operator
    was deleting the variable instead of returning it.
     
    Brian M. Dean, May 8, 2005
    #3
  4. Can anyone tell me why I get a segmentation fault in "eval"
    with the following driver code?

    int main(int argc, char *argv[])
    {
    int tmpint = 2;
    Polynomial test((unsigned int) tmpint);

    test.set_coeff(0.2, 2);
    test.set_coeff(1.0, 1);
    test.set_coeff(0.5, 0);
    Polynomial ans = test * test;
    printf("Times function:\n");
    for (tmpint = 4; tmpint >=0 ; tmpint--)
    {
    printf("%0.12lf\t", ans.get_coeff((unsigned int) tmpint));
    }
    printf("\n");
    // segmentation fault in the next line of code
    ans = test.eval(test);
    printf("Eval function:\n");
    for (tmpint = 4; tmpint >=0 ; tmpint--)
    {
    printf("%0.12lf\t", ans.get_coeff((unsigned int) tmpint));
    }
    printf("\n");
    cout << endl;
    return EXIT_SUCCESS;
    }
     
    Brian M. Dean, May 8, 2005
    #4
  5. In the constructor, where you create the 'coeff', what is the top
    index that you use to set the coeff to 0.0:

    Polynomial::polynomial(const unsigned int n)
    {
    unsigned int i;
    coeff = new double[n+1];
    for (i=0; (i < n+2); i++)
    coeff = 0.0;
    degree = n;
    }

    ? And how many elements does the 'coeff' actually have?

    V
     
    Victor Bazarov, May 9, 2005
    #5
  6. Can anyone tell me why I get a segmentation fault in "eval"



    That was very stupid of me. Thanks.
     
    Brian M. Dean, May 9, 2005
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.