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: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:stream& operator << (std:stream& out, const Polynomial& p);
~Polynomial();
private:
Polynomial();
double *coeff;
unsigned int degree;
};
___________________________________________
Polynomial.cpp
___________________________________________
Polynomial:olynomial(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: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.
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: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:stream& operator << (std:stream& out, const Polynomial& p);
~Polynomial();
private:
Polynomial();
double *coeff;
unsigned int degree;
};
___________________________________________
Polynomial.cpp
___________________________________________
Polynomial:olynomial(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: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.