Poly.h

``````#include <iostream>
using namespace std;

#ifndef POLY101
#define POLY101

class Poly
{
[B]private:[/B]
int order;
double *a;   // Coefficient vector -- a, a, a, ..., a[order]
// to represent a + a*x + a*x^2 + ...

public:
Poly(int = 0);       // Default constructor
Poly(const Poly &p);  // Copy constructor
~Poly();             // Destructor (must free memory)

// Function to change polynomial size/order (dynamic memory allocation)
void Resize(int);     // Resize polynomial based on new order value

// Math operators
Poly operator=(const Poly &p);
Poly operator+(const Poly &p);
Poly operator-(const Poly &p);
Poly operator*(const Poly &p);
double Evaluate(double x);  // Compute and return the polynomial value at x

// Support functions
void SetCoeff(int o, double x);  // Set coefficient of order o to value x
friend istream &operator>>(istream &is, Poly &p);
friend ostream &operator<<(ostream &os, Poly &p);

// Note:  The Divide() function was added for convenience.  It can be used
// in both the / and % functions to minimize total amount of code.
Poly operator/(const Poly &);  // Returns quotient from polynomial division
Poly operator%(const Poly &);  // Returns remainder from polynomial division
void Divide(const Poly &d, Poly &q, Poly &r); // d=divisor; q=quotient; r=remainder
};

#endif``````

//the alogrithm for my poly.cpp is as follows
main_test.cpp

``````#include "Poly.h"
int main()
{
Poly f, g, h, i, j;
Poly q, r;
double y;

cin >> f;
cout << "f = " << f << endl;

cin >> g;
cout << "g = " << g << endl;

h=f+g;
cout << "\n h=f+g=" << h << endl;

i=f-g;
cout << "\n i=f-g=" << i << endl;

j=f*g;
cout << "\n j=f*g=" << j << endl;

y=h.Evaluate(3);
cout << "\n h(3) =  " << y << endl;

cout << "\n\n Division and remainder:\n\n";
q=f/g;
cout << "\n f/g=" << q << endl;
r=f%g;
cout << "\n f%g=" << r << endl;

return 0;
}``````

//and now when I debug my output screen should look like
test_output.cpp
Enter polynomial coefficients (decreasing power order):
1
2
1
f = 1 * x^2 + 2 * x^1 + 1

Enter polynomial order:
1
Enter polynomial coefficients (decreasing power order):
1
1
g = 1 * x^1 + 1

h=f+g=1 * x^2 + 3 * x^1 + 2

i=f-g=1 * x^2 + 1 * x^1 + 0

j=f*g=1 * x^3 + 3 * x^2 + 3 * x^1 + 1

h(3) = 20

I just need to make a code for poly.cpp looking like the main_test.cpp(it should consist of the codeed form) and as I debug poly.cpp along side poly.h the output screen should look like the

test_output.cpp

## All 22 Replies

Any Kind of help would be greatly appreciated I know we have to use the resize function but am still having trouble.Also the poly.h does look good to me hopefully someone can decipher it.I know what my screen should look like as I debug poly.h and poly.cpp(once I am done with it)as I illustrated in the test_output.cpp.

But the issue is in making my main_test.cpp into a code form of poly.cpp as in the main_test.cpp is the alogrithm or logic I want to use as I code poly.cpp

You need to use some C++ code tags so you get the line numbers, do some formatting so it's readable, and be more clear with your question.

[code=cplusplus] // paste your code here

[/code]

I think you need at least one more constructor:

``Poly (double coeff[], int polyOrder);``

The above constructor will set aside the memory for your polynomial using the "new" command, set order to polyOrder and set the a array values to the coeff values.

For a test program, I guess a simple snippet would be this:

``````double pc1[] = {5.0, 4.0, 3.0};
double pc2[] = {4.0, 3.0, 3.0};
double pc3[] = {0.0, 0.0, 0.0};
Poly p1 (pc1, 2);
Poly p2 (pc2, 2);
Poly p3 (pc3, 2);

p3 = p1 + p2;
cout << p1 << endl;
cout << p2 << endl;
cout << p3 << endl;``````

p3 should contain {9.0, 7.0, 6.0}.

commented: Yes, code tags, gotta keep mentioning the code tags, it's all we ever do around this place. +15

``````Poly.h
#include <iostream>
using namespace std;

#ifndef POLY101
#define POLY101

class Poly
{
[B]private:[/B]
int order;
double *a;   // Coefficient vector -- a, a, a, ..., a[order]
// to represent a + a*x + a*x^2 + ...

[B] public:[/B]
Poly(int = 0);       // Default constructor
Poly(const Poly &p);  // Copy constructor
~Poly();             // Destructor (must free memory)

// Function to change polynomial size/order (dynamic memory allocation)
void Resize(int);     // Resize polynomial based on new order value

// Math operators
[U]  Poly operator=(const Poly &p);
Poly operator+(const Poly &p);
Poly operator-(const Poly &p);
Poly operator*(const Poly &p);[/U]
double Evaluate(double x);  // Compute and return the polynomial value at x

// Support functions
void SetCoeff(int o, double x);  // Set coefficient of order o to value x
friend istream &operator>>(istream &is, Poly &p);
friend ostream &operator<<(ostream &os, Poly &p);

// Note:  The Divide() function was added for convenience.  It can be used
// in both the / and % functions to minimize total amount of code.
Poly operator/(const Poly &);  // Returns quotient from polynomial division
Poly operator%(const Poly &);  // Returns remainder from polynomial division
void Divide(const Poly &d, Poly &q, Poly &r); // d=divisor; q=quotient; r=remainder
};

#endif

//the alogrithm for my poly.cpp is as follows
main_test.cpp
#include "Poly.h"

int main()
{
Poly f, g, h, i, j;
Poly q, r;
double y;

cin >> f;
cout << "f = " << f << endl;

cin >> g;
cout << "g = " << g << endl;

h=f+g;
cout << "\n h=f+g=" << h << endl;

i=f-g;
cout << "\n i=f-g=" << i << endl;

j=f*g;
cout << "\n j=f*g=" << j << endl;

y=h.Evaluate(3);
cout << "\n h(3) =  " << y << endl;

cout << "\n\n Division and remainder:\n\n";
q=f/g;
cout << "\n f/g=" << q << endl;
r=f%g;
cout << "\n f%g=" << r << endl;

return 0;
}

//and now when I debug my output screen should look like
test_output.cpp
Enter polynomial coefficients (decreasing power order):
1
2
1
f = 1 * x^2 + 2 * x^1 + 1

Enter polynomial order:
1
Enter polynomial coefficients (decreasing power order):
1
1
g = 1 * x^1 + 1

h=f+g=1 * x^2 + 3 * x^1 + 2

i=f-g=1 * x^2 + 1 * x^1 + 0

j=f*g=1 * x^3 + 3 * x^2 + 3 * x^1 + 1

h(3) =  20``````

I just need to make a code for poly.cpplooking like the main_test.cpp(it should consist of the codeed form) and as I debug poly.cppalong side poly.h the output screen should look like the

so main_test.cpp has an easier alternative is that what you are telling me?

``````poly.cpp
#include <cmath>
#include "poly.h"

poly::poly(int size)
{	int i;
length = size;
x = new double [length];
for (i=0;i<length;i++) x[i] = 0.0;
}
poly::poly(const poly &p)
{
int i;
length = p.length;
x = new double [length];
for (i=0;i<length;i++) x[i] = p.x[i];
}

poly::~poly()
{
delete [] x;
}

void poly::Resize(int l)
{
if (length != l) {
if (length > 0) delete [] x;
x = new double [l];
length = l;
}
}

poly poly::operator=(const poly &p)
{
int i;

Resize(p.length);
for (i=0; i<length; i++) x[i] = p.x[i];
return(*this);

}

poly poly::operator+(const poly &p)
{
int i;
poly temp(*this);

if (length != p.length) {
cout << "+:  Illegal operation -- polynomial lengths differ.\n";
return(*this);
}

for (i=0; i<length; i++) temp.x[i] += p.x[i];
return temp;

}

istream &operator>>(istream &is, poly &p)
{
char ichar='\0';
int size=0, i;
poly temp;

while (ichar != '[') is >> ichar;

if (p.length != 0) p.Resize(0);

while (is.peek() != ']') {
size++;
p.Resize(size);
for (i=0;i<(size-1);i++) p.x[i] = temp.x[i];
is >> p.x[size-1];
temp = p;
}

is >> ichar;
return is;
}

ostream &operator<<(ostream &os, poly p)
{
int i;
os << '[';
for (i=0;i<p.length;i++) os << '\t' << p.x[i];
os << "]\n";
return os;
}

bool poly::operator==(const poly &p)
{
int i;

for (i=0; i<length; i++) {
if (fabs(x[i] - p.x[i]) > 0.0001) return(0);
}
return(1);
}

poly poly::operator-(const poly &p)
{
int i;

if (length != p.length) {
cout << "-:  Illegal operation -- polynomial lengths differ.\n";
return(*this);
}

poly temp(*this);
for (i=0; i<length; i++) temp.x[i] -= p.x[i];

return(temp);

}

poly poly::operator+(double a) {
poly temp(*this);
for (int i=0;i<length;i++) temp.x[i] = x[i]+a;
return(temp);
}

poly poly::operator-(double a) {
poly temp(*this);
for (int i=0;i<length;i++) temp.x[i] = x[i]-a;
return(temp);
}

poly poly::operator-(void) {
poly temp(*this);
for (int i=0;i<length;i++) temp.x[i] = -x[i];
return(temp);
};

poly poly::operator*(double a) {
poly temp(*this);
for (int i=0;i<length;i++) temp.x[i] = a*x[i];
return(temp);
}

poly poly::operator/(double a) {
poly temp(*this);
for (int i=0;i<length;i++) temp.x[i] = x[i]/a;
return(temp);
}

double poly::mag()
{
int i;
double m=0.0;

for (i=0;i<length;i++) {
m += x[i]*x[i];
}

return(sqrt(m));

}

double poly::dot(const poly &p)
{
int i;
double dot=0.0;

if (length != p.length) {
cout << "dot:  Illegal operation -- polynomial lengths differ.\n";
return(0.0);
}

for (i=0; i<length; i++) dot += x[i]*p.x[i];

return(dot);

}

poly poly::cross(const poly &p)
{
poly temp(3);
if (length != p.length) {
cout << "Illegal operation - polynomial lengths differ.\n";
return(temp);
}
temp = x * p.x - x * p.x;
temp = x * p.x - x * p.x;
temp = x * p.x - x * p.x;

return(temp);
}

poly poly::stack(poly w)
{
int i;
int sz = length+w.length;
poly temp(sz);

for (i=0;i<length;i++) temp.x[i] = x[i];
for (i=length;i<sz;i++) temp.x[i] = w.x[i-length];

return(temp);

}

poly poly::extract(int i, int j, int base)
{
int sz,ist,k;
poly temp;

if ( (j<i) || ( (base==0) && ( (i<0) || (j>=length) ) ) ||
( (base!=0) && ( (i<1) || (j>length))) )
{
cout << "Invalid range for subpolynomial ("<<i<<","<<j<<")" << endl;
return(temp);
}
else {
sz = j-i+1;
temp.Resize(sz);
if (base!=0) ist = i-1;
else ist = i;
for (k=ist;k<ist+sz;k++) temp.x[k-ist]=x[k];
return(temp);
}

}``````

Does this code look right at all,I tried it around a little bit but I know I am way far off from gettting the test_output.cpp as I debug it.

I don't see any set or get functions in your class that would set the coefficients of the polynomial, nor do I see a constructor in your class that sets coefficients. I would get those working and your << operator working first, and ignore your mathematical operator functions for now. If you have no way of setting a polynomial up with coefficients and order/degree, how can you add, multiply, divide, etc.? So make sure you get that part right first and can display the polynomials in the way you want because you'll need to display in order to test. Unless I missed that part of your code. I just looked through it again and I don't see it. I guess you are calling "order" "length" now?

``````poly poly::operator+(const poly &p)
{
double * temp

if (order>p.order) {
temp=new double[order];
for (int i=0;i<= order = p.a[i];i++)
{
if(i<= p.order)
temp[i]
else
temp[i]=0.0;
temp.a[i]=p.a[i]+a[i]
}

cout << "\n temp.a[i]=p.a[i]+a[i]" << temp.a[i] << endl;

}

}``````

does this part of my poly.cpp code do the addition part??I think its right but suggestions please

Don't have cout statements in your addition procedure unless it is for debugging purposes. Let the << operator do the displaying. The + operator should add, nothing else. Make sure to take the cout statements out when you are done debugging. This is a function that is supposed to return a polynomial:

``poly poly::operator+(const poly &p)``

Your function doesn't return anything. Also, you need to pick a name for the degree of the polynomial. It's either length or order, but it can't be both.

I'm not sure what you are trying to do in this line but I'd break it up into more than one line to avoid confusion:

``for (int i=0;i<= order = p.a[i];i++)``

Why are you assigning order to be equal to p.a in this loop?

``````poly poly::operator+(const poly &p)
{
Poly ptemp

if (order>p.order) {
temp=new double[order];
for (int i=0;i<= order;i++)
{
if(i<= p.order)
ptemp[i]
else
ptemp[i]=0.0;

ptemp.a[i]=p.a[i]+a[i]
}
}
return << ptemp << endl;

}``````

the poly poly::operator+(const poly &p) is a math operator,ya I took your suggestion and removed the cout statements.
The 1.
for (int i=0;i<= order = p.a;i++)
was a typo sorry I changed it by removing
"= p.a ".The problem with this code is that it add polynomials of the same order,I want it to add polynomials of different orders like order=1 and order=2,I know I do this by setting both polynomials to the order of the greater polynomial between the two.The same goes with by subtraction part of the code :(

Subtraction part

``````poly poly::operator-(const Poly &p)
{	Poly my_temp

if (order>p.order) {
my_temp=new double[order];
for (int i=0;i<= order = p.a[i];i++)
{
if(i<= p.order)
my_temp[i]
else
my_temp[i]=0.0;
{	if(p.a[i]>a[i])
{
my_temp.a[i]=p.a[i]-a[i]
}
else if(p.a[i]<a[i])
{
my_temp.a[i]=a[i]-p.a[i]
}
else
{
my_temp.a[i]=0.0
}
}
}
}
return <<  my_temp << endl;

}``````

Subtraction part

``````poly poly::operator-(const Poly &p)
{	Poly my_temp

if (order>p.order) {
my_temp=new double[order];
for (int i=0;i<= order = p.a[i];i++)
{
if(i<= p.order)
my_temp[i]
else
my_temp[i]=0.0;
{	if(p.a[i]>a[i])
{
my_temp.a[i]=p.a[i]-a[i]
}
else if(p.a[i]<a[i])
{
my_temp.a[i]=a[i]-p.a[i]
}
else
{
my_temp.a[i]=0.0
}
}
}
}
return <<  my_temp << endl;

}``````

It wouldn't hurt to stick a default constructor in there:

``````Poly::Poly ()
{
order = 0;
}``````

This line isn't going to work:

``my_temp=new double[order];``

One, my_temp isn't an array. my_temp.a can be an array of doubles, but my_temp isn't an array of anything. Two, order is not a type. If you want an array of doubles, replace "order" with "double". order is a variable. Putting it in the brackets doesn't make any sense.

``return <<  my_temp << endl;``

This line doesn't work. If you want to display it using the << operator, use cout instead of return. If you want to return my_temp, just do this:

``return my_temp;``

If you want to do both, do it in two separate lines.

temp=new double[order];

is a new array of doubles of a size-order

so now thats clear,I used the return my_temp; and return temp; for my subtraction and addition operators respectively.
Now can you guide my on to how I can modify these two codes(from the previous page)so that I can add and subtract polynomials of different order.I know that we do this by setting the order of the both polynomials to the higher order between the two polynomials.
Thank you so much for your help,you seem to be the only one that's around and tries to make codes better

There are several ways to do it, but you may have to redo some members of the Polynomial class. Here's one possible way to approach it.

If the coeffiecent vector contains zero at appropriate element at the power of the variable, then just extend the coefficient vector by adding the appropriate number of zeros to the polynomial of the lower degree so it has the same degree as the other one.

For example, say you want to add these two polynomials:

2x^2 + 1;
3x - 2;

the coeffient vectors could start out as

1, 0, 2
-2, 3

If so, then adjust the coefficient vector of polynomial of lower degree to be the same size as larger one.
1, 0, 2
-2, 3, 0

now add coefficients in each vector if they have the same index and make that value the coefficient of resulting polynomial

-1, 3, 2

which when printed may will look like:

2x^2 + 3x^2 + -1x^0

or maybe it will look like:

2x^2 + 3x - 1

if the output statement is sophisticated enough.

temp=new double[order];

is a new array of doubles of a size-order

My bad. Yes, you are correct. It's fine to have "order" in the brackets. That does make sense. I'd blame it on dyslexia but I don't have dyslexia. I just got it backwards. "double" should not be inside the parentheses, order can be, though it should be order + 1. my_temp is type Poly though, right? So you still need to change it a little to something like this:

``````Poly temp;
temp.a = new double[order + 1];``````

Isn't the polynomial structure kinda similar to a vector??

What's wrong with defining Poly as derived from std::vector<double>?? It makes all the new/delete stuff go away, and looks neater and easier to me at least.

``````class Poly : vector<double>{
public:
Poly(int order){
resize(order);
}

Poly(double *coeffs, int order){
resize(order);
for(int i = 0; i < order; i++){
vector[i] = coeffs[i]
}
}

Poly &operator=(Poly &p){
*this = p;
}

Poly operator+(Poly &p){
int order = max(p.size(), size();
Poly res(order);

for(int i = 0; i < order; i++){
res = operator[](i) + p[i];
}

return res;
}

Poly operator-(Poly &p){
int order = max(p.size(), size();
Poly res(order);

for(int i = 0; i < order; i++){
res = operator[](i) - p[i];
}

return res;
}

Poly operator*(Poly &p){
int order = p.size() + size() - 1;   // heaps bigger
Poly res(order);

// need some tricky stuff here...
return res;
}

};``````

Also, you're multiplication and division aren't correct. For example, multiplying (a + bx).(a + bx) is not (a^2 + (b^2).(x^2)) but (a^2 + 2abx + (b^2).(x^2)).

You should also note that the order in the case of multiplying is the sum of the orders of the multiplicands.

``````#include <cmath>
#include "poly.h"

Poly Poly::(int = 0)
{
order = 0.0;
}

Poly Poly::(const Poly &p)
{	int i;
for(i=0; i<=n+1; i++)
a[i]*new_a[i]= newa[i]
}

void SetCoeff(int o, double x)
{	int i
for(1)
{	size = inSize;
data = new double[size];
}
{
throw ();
}
{
for (i=0; i<size; i++)
data[i] = values[size-1-i];
}
if (data != NULL)
a[i]*new_a[i] = newa[i](o, x);
new_a[i]->next = a[i];
a[i] = new_a[i];
if (a[i]==NULL)
a[i]=0.0

order++;
}
Poly Poly::operator+(const Poly &p)
{
Poly ptemp;

if (order>p.order) {
ptemp=new double[order];
for (int i=0;i<=order;i++)
{
if(i<= p.order)
ptemp[i]
else
ptemp[i]=0.0;

ptemp.a[i]=p.a[i]+a[i]
}
}
return  ptemp;

Poly temp_poly;
int psize = temp_poly.Resize(psize);
for(int i=0; i<=temp_poly.order; i++){
temp_poly.a[i] = 0.0;
for(int i=0; i<=short_poly.order; i++){
temp_poly.a[i] = short_poly.a[i];
for(int i=0; i<=temp_poly.order; i++){
return_poly.a[i] = temp_poly.a[i]  + long_poly.a[i];

}
}
}return temp_poly;
}

Poly Poly::operator-(const Poly &p)
{	Poly my_temp

if (order>p.order) {
my_temp=new double[order];
for (int i=0;i<= order = p.a[i];i++)
{
if(i<= p.order)
my_temp[i]
else
my_temp[i]=0.0;
{	if(p.a[i]>a[i])
{
my_temp.a[i]=p.a[i]-a[i]
}
else if(p.a[i]<a[i])
{
my_temp.a[i]=a[i]-p.a[i]
}
else
{
my_temp.a[i]=0.0
}
}
}
}
return  my_temp;

Poly mypoly;
int psize = mypoly.Resize(psize);
for(int i=0; i<=mypoly.order; i++){
temp_poly.a[i] = 0.0;
for(int i=0; i<=short_poly.order; i++){
temp_poly.a[i] = short_poly.a[i];
for(int i=0; i<=mypoly.order; i++){
return_my_poly.a[i] = mypoly.a[i]  + long_poly.a[i];

}
}
}return mypoly;
}``````

heres my poly.cpp so far I am confident about the addition and substraction.Lerner I used your pseudo code as you can see it,thanks buddy.
But still I have multiplication,and
1) void Divide(const Poly &d, Poly &q, Poly &r); // d=divisor; q=quotient; r=remainder
this does the division and remainder for me.But most Importantly I still dont know how to make the user enter coeffetients into the respective orders.What I mean is if the user when asked for an order enters "4"
now, how do I make my code in a way that the user entered coeffecients are substituted in the decreasing power order of the polynomial?
Please check my code when you guys get a chance

assume we're multiplying polynomials X and Y
let Z be a 0 polynomial
for i := degree of Y downto 0
if there is a coefficient of Y (call it Yco) with exponent i
for j := degree of X downto 0
if there is a coefficient of X (call it Xco) with exponent j
if there is a coefficient of Z (call it Zco) with exponent i*j
set the coefficient of Z whose exponent is i*j to
Zco+Xco*Yco
else
set the coefficient of Z whose exponent is i*j to Xco*Yco

Z is now the product of X and Y

here is my pseudocode for mutiplication now how do I translate it to code is an issue.And as dougy83 was saying "order in the case of multiplying is the sum of the orders of the multiplicands".Someone help my deduce this

Regarding the order for multiplication, an example:
m1 = 1.x^2 + 1.x^1 + 2, order 2
m2 = 1.x^3 + 1.x^2 + 1.x^1 + 2, order 3
r = 1.x^5 + 2.x^4 + 4.x^3 + 5.x^2 + 4.x + 4, order 5

notice how the order of r is the sum of the order of m1 and the order of m2

Genius,but what about the coefficients?I dont find any connection as to the way they are multiplied.And also as to how the coffecients go into their resceptive orders.
t4 + t + 1 10011 (reduction polynomial)
t3 + t2 + 1 1101
t2 + t + 1 0111
1101
0111 *
------
1101
11010
------
100011
10011 - (t * reduction_polynomial)
------
101
And thus (t3 + t2 + 1) (t2 + t + 1) = t2 + 1.
I know this is the logic but here I am only using 1 as the cofficient so hence its easy

And thus (t3 + t2 + 1) (t2 + t + 1) = t2 + 1.

How is this right??

You'll probably want to use what they call 'vertical' multiplication, as it lends itself to being done with 2 nested for loops.

ok thanks I am working on my remainder and part I have a pseudo code or the logic :
R=Remainder polynomial, Z=temporary polynomial. So initially I have Poly R w/order= the numerators order and I have Poly Z w/order = the numerators. I do my math to calculate the remainder and it should return something like (R=0x^2+0x^1+6x+1). To eliminate these 0 terms I set Z=R. I then resized R to be the correct order of the Remainder minus the 0 coefficient terms (in this case order =1). I then looped over Z from (i=order;i>=0; with a decreasing loop) setting R=Z. In this case Z technically has more memory allocated for it that I never refer to and R once had more spaces but was resized (which should delete and re-allocate the correct memory based on my resize function I believe)...

After my for loop do I make my set my remainder = to a temp poly z. I then resize the remainder to the size of the actual remainder (because when calculated the remainder has alot of zero terms that I want to eliminate ie: 0x^4+0x^3+0x^2+6x+6). So in this case I resize my remainder to order=1 and I loop in the coefficients of z into my new r from order to 0

but coding this seems impossible

What are you doing now? Division? Is that why you're talking about a remainder?

Coding polynomial addition/subtraction/multiplication/division is certainly not impossible.

Addition/Subtraction/Multiplication are simple and take only a few lines each.

The division is simple, but may be a little hard to represent if the result is not a polynomial. Normally the resultant of division is represented as a valid polynomial quotient and remainder (which a single instance of your poly class obviously won't hold ).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.