Polynomials in C++

B

Brian M. Dean

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.
 
V

Victor Bazarov

Brian M. Dean said:
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{ [...]
private:
Polynomial();

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.
double *coeff;
unsigned int degree;

Why can't you, instead of mucking with dynamic memory, just use
vector said:
};
[...]

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"
Really?

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

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).
I am using the compiler that comes with Fedora Core 3 and
using KDevelop as an interface.

That shouldn't matter much.

V
 
B

Brian M. Dean

I tried returning both "ans" and "Polynomial(ans)" but both
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).

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.
 
B

Brian M. Dean

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;
}
 
V

Victor Bazarov

Brian said:
Can anyone tell me why I get a segmentation fault in "eval"
with the following driver code?

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
 
B

Brian M. Dean

Can anyone tell me why I get a segmentation fault in "eval"
with the following driver code?

In the constructor, where you create the 'coeff', what is the top
index that you use to set the coeff to 0.0:



That was very stupid of me. Thanks.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top