After doing the object stuff in the C++ course, I tried to make a polynomial object.
I also defined the +, -, * operators and I put in a static
method.
It’s almost complete … I’ll put it on GitHub when its finished.
I’d love some suggestions on how to improve it.
Here’s part of the code. (It’s not the whole thing.)
#include <iostream>
#include <string>
#include <sstream>
int const MAX_DEGREE = 15;
double pow(double num, int exp) { // does integer powers of num
bool negative_exponent = (exp < 0);
if (exp < 0) { exp = exp * -1; };
if (num == 0) { return 0.0; }
else if (exp == 0) { return 1.0; }
else if (exp == 1) { return num; }
double product = 1.0;
int i;
for (i = 1; i <= exp; i++) {
product = product * num;
}
if (negative_exponent)
{ return 1.0 / product; }
else { return product; }
}
double abs(double num) {
if (num < 0) { return -1 * num; }
else { return num; }
}
std::string to_string(double num) {
// better to_string for doubles
std::ostringstream oss;
oss << num;
return oss.str();
}
class Polynomial {
public:
double coefficient[MAX_DEGREE + 1];
int degree = 0;
private:
void initializeZeros() {
for (int i = 0; i <= MAX_DEGREE; i++)
{ coefficient[i] = 0.0; }
}
void copyArray(double array[]) {
for (int i = 0; i <= MAX_DEGREE; i++)
{ coefficient[i] = array[i]; }
}
void copyArray(double array[], int deg) {
if (deg > MAX_DEGREE)
{ deg = MAX_DEGREE; }
for (int i = 0; i <= deg; i++)
{ coefficient[i] = array[i]; }
}
protected:
void compute_degree() {
int deg = 0;
for (int i = 0; i <= MAX_DEGREE; i++) {
if (coefficient[i] != 0)
{ deg = i; }
}
// return deg;
degree = deg;
}
void compute_degree(int max) {
int deg = 0;
for (int i = 0; i <= max; i++) {
if (coefficient[i] != 0)
{ deg = i; }
}
// return deg;
degree = deg;
}
public: // constructors:
Polynomial(){
initializeZeros();
degree = 0;
}
Polynomial(double constant) {
initializeZeros();
degree = 0;
coefficient[0] = constant;
}
Polynomial(int constant) {
initializeZeros();
degree = 0;
coefficient[0] = constant;
}
Polynomial(double numArray[], int deg) {
degree = deg;
initializeZeros();
copyArray(numArray, deg);
}
Polynomial(double numArray[]) {
copyArray(numArray);
compute_degree();
}
public: // create polynomial
static Polynomial create(double nums[], int deg) {
Polynomial created;
if (deg > MAX_DEGREE)
{ deg = MAX_DEGREE; }
for (int i = 0; i <= deg; i++)
{ created.coefficient[i] = nums[deg-i]; }
created.compute_degree(deg);
return created;
}
public: // methods
// synthetic division
Polynomial synthetic_div_by(double divisor) {
Polynomial quotient(0.0);
if (degree == 0) { return quotient; }
quotient.degree = degree - 1;
int i = degree;
quotient.coefficient[i - 1] = coefficient[i];
i --;
while (i > 0) {
quotient.coefficient[i - 1] = coefficient[i] + (quotient.coefficient[i] * divisor);
i--;
}
double remainder = coefficient[0] + (quotient.coefficient[0] * divisor);
return quotient;
}
// the polynomial as a function using ()
double operator() (double x) {
double power = 1.0;
double total = coefficient[0];
int i = 0;
for (i = 1; i <= degree; i++) {
power *= x;
total += coefficient[i] * power;
}
return total;
}
double &operator[] (int index) {
return coefficient[index];
}
// leading coefficent
double leading_coefficient() {
return coefficient[degree];
}
void set_leading_coefficient(double &a) {
coefficient[degree] = a;
}
// constant term
double get_constant() {
return coefficient[0];
}
double constant() {
return coefficient[0];
}
void set_constant(double &c) {
coefficient[0] = c;
}
// toString method
std::string toString() {
int i = degree;
std::string text = to_string(coefficient[i]);
if (i > 0) { text += "x"; }
if (i > 1) { text += "^" + std::to_string(i); }
for (i = degree - 1; i >= 0; i--) {
if (coefficient[i] != 0) { // only do non-zero terms
if (coefficient[i] < 0) {
text += " - " + to_string(-1 * coefficient[i]);
}
else {
text += " + " + to_string(coefficient[i]);
}
if (i > 1) { text += "x^" + std::to_string(i); }
else if (i == 1) { text += "x"; }
}
}
return text;
}
// add polynomials
Polynomial operator+(Polynomial& g) {
Polynomial sum;
if (degree < g.degree) { sum.degree = g.degree; }
else { sum.degree = degree; }
for (int i = 0; i <= sum.degree; i++) {
sum.coefficient[i] = coefficient[i] + g.coefficient[i];
}
sum.compute_degree(sum.degree);
return sum;
}
// subtract polynomials
Polynomial operator-(Polynomial& g) {
Polynomial difference;
if (degree < g.degree) { difference.degree = g.degree; }
else { difference.degree = degree; }
for (int i = 0; i <= difference.degree; i++) {
difference.coefficient[i] = coefficient[i] - g.coefficient[i];
}
difference.compute_degree(difference.degree);
return difference;
}
// negative of polynomial
Polynomial operator-() {
Polynomial result(coefficient, degree);
for (int i = 0; i <= degree; i++) {
result.coefficient[i] = -coefficient[i];
}
return result;
}
// multiply polynomials
Polynomial operator*(Polynomial &g) {
Polynomial product(0.0);
if ((degree == 0) && (coefficient[0] == 0))
{ return product; } // zero polynomial
if ((g.degree == 0) && (g.coefficient[0] == 0))
{ return product; } // zero polynomial
product.degree = degree + g.degree;
for (int i = 0; i <= degree; i++) {
for (int j = 0; j <= g.degree; j++) {
product.coefficient[i+j] += (coefficient[i] * g.coefficient[j]);
}
}
return product;
}
// derivative of polynomial
Polynomial prime() {
Polynomial derivative;
if (degree > 0) { derivative.degree = degree - 1; }
else { derivative.degree = 0; }
for (int i = 1; i <= degree; i++) {
derivative.coefficient[i - 1] = i * coefficient[i];
}
return derivative;
}
double prime(double a) {
Polynomial derivative = prime();
return derivative(a);
}
void increase_degree(int n = 1) {
for (int i = degree; i >= 0; i--) {
coefficient[i + n] = coefficient[i];
}
degree = degree + n;
//coefficient[0] = 0.0;
for (int j = n - 1; j >= 0; j--) {
coefficient[j] = 0.0;
}
}
void decrease_degree(int n = 1) {
//int old_constant = coefficient[0];
for (int i = 1; i <= degree; i++) {
if ((i - n) >= 0) {
coefficient[i - n] = coefficient[i];
}
}
for (int j = 0; j < n; j++) {
coefficient[degree - j] = 0.0;
}
degree = degree - n;
//return coefficient[0];
}
// == operator
bool operator== (Polynomial g) {
//compute_degree();
//g.compute_degree();
bool isEqual = (degree == g.degree);
for (int i = 0; i <= degree && isEqual; i++) {
isEqual = (isEqual && (coefficient[i] == g.coefficient[i]));
}
return isEqual;
}
bool operator== (double num) {
//compute_degree();
//if ((degree == negInfinity) && (coefficient[0] == 0.0) && (num == 0.0)) { return true; }
return ((degree == 0) && (coefficient[0] == num));
}
double operator[] (char c) {
/* get coefficient for term using letter,
where
a gets coefficient of highest power,
b gets coefficient of next highest power,
and so on ...
*/
//note: 'a' is 97 and 'A' is 65 as an int
int i = (int)c;
if (i >= 97) { i = i - 97; }
else if (i >= 65) { i = i - 65; }
return coefficient[degree - i];
}
friend std::ostream& operator<<(std::ostream& os, Polynomial& f);
};
using namespace std; // so that I don't need to do std::
ostream& operator<<(ostream& os, Polynomial& f) {
char const var = 'x';
int i = f.degree;
os << f.coefficient[i];
if (i > 0) { os << var; }
if (i > 1) { os << "^" << i; }
for (i = f.degree - 1; i >= 0; i--) {
if (f.coefficient[i] < 0) {
os << " - " << (-1 * f.coefficient[i]);
}
else {
os << " + " << f.coefficient[i];
}
if (i > 1) { os << var << "^" << i; }
else if (i == 1) { os << var; }
}
return os;
}
int main() {
double f_coeffs[3] = {-5, -4, 1}; // degree is 2
//Polynomial f = Polynomial(f_coeffs, 2); //replaced
Polynomial f(f_coeffs, 2);
cout << "For the polynomial f(x) = " << f.toString() << " \n";
cout << "f(2) = " << f(2) << "\n";
int c = 2; // divisor for synthetic division
cout << "(" << f << ")" << " / (x - " << c << ")";
cout << "\n";
cout << " = ";
cout << f.synthetic_div_by(c).toString();
cout << " + ";
cout << f(c) << " / (x - " << c << ")" << " \n";
cout << "f(x) = " << f << "\n";
double x = -1.0;
cout << "f(" << x << ")" << " = " << f(x) << "\n";
Polynomial neg = -f;
cout << "-f(x) = " << neg.toString() << "\n";
cout << "In the quadratic formula: \n";
cout << "a = " << f['a'] << '\n';
cout << "b = " << f['b'] << '\n';
cout << "c = " << f['c'] << '\n';
cout << "from " << f << " = 0 " << '\n';
double g_coeffs[4] = {2, 5, 3, 1}; // degree is 3
Polynomial g(g_coeffs, 3);
cout << "g(x) = " << g << endl;
Polynomial dg = g.prime();
cout << "g'(x) = " << dg << endl;
Polynomial sum = f + g;
cout << "f + g = " << sum << endl;
Polynomial difference = f - g;
cout << "f - g = " << difference << endl;
Polynomial product = f * g;
cout << "f * g = " << product << endl;
return 0;
}
I’ve also done antiderivatives, polynomial long division, and composition of polynomials,
but the code for those is too long for this post.
If anyone finds a bug in the code, please let me know.