What I have here works only for multiplying a one term polynomial by a multiple term polynomial... and even then it's not formatted correctly. When I perform multiplication with two multiple term polynomials the array answer[] contains the correct products yet they're not lined up. Hopefully an example will make it clearer:
As it is now:
(5)(2x^3 + 4x^2 + 6x + 3)
produces:
which is very nnnnice, but this is not enough.
The problem:
(2x + 3)(2x^3 + 3x^2 + 4x + 5)
produces:
4x^7 + 6x^6 + 8x^5 + 10x^4 + 6x^3 + 9x^2 + 12x + 15
and of course what I wish is:
4x^4 + 12x^3 + 9x^2 + 12x + 15
Now I realize this code may be long to some, but my goal in writing is usually to get the functions to properly work then I usually focus on cleanup and code shortening, so please don't flame the length.
Code:
.....
int total; // total in size of the final output
int larger, smaller; // determine the larger - smaller of two polynomials
int i, j, k; // index advancers
double *temp, answer[ 1024 ]; //[ (smaller - 1) + larger ]; // final storage
if ( One.getSize() > Two.getSize() ) {
larger = One.getSize(); // first poly has more terms
smaller = Two.getSize(); // second poly has less terms
double largeArr[ larger ], smallArr[ smaller ];
for ( k = 0; k < larger; k++ ) {
largeArr[ k ] = One.getOneCoeff( k );
}
for ( k = 0; k < smaller; k++ ) {
smallArr[ k ] = Two.getOneCoeff( k );
}
// perform (1 or 0)*(x^n-.. + x + C)
if ( smaller < 2 ) {
for ( j = 0; j < larger; j++ ) {
answer[ j ] = smallArr * largeArr[ j ];
}
} else { // otherwise perform multiple term multiplication
k = 0; // set outside because it can't be reset inside
for ( i = 0; i < smaller; i++ ) {
for ( j = 0; j < larger; j++, k++ ) {
answer[ k ] = largeArr[ j ] * smallArr[ i ];
}
}
}
} else if ( One.getSize() < Two.getSize() ) {
larger = Two.getSize(); // second poly has more terms
smaller = One.getSize(); // first poly has less terms
double largeArr[ larger ], smallArr[ smaller ];
for ( int k = 0; k < larger; k++ ) {
largeArr[ k ] = Two.getOneCoeff( k );
}
for ( int k = 0; k < smaller; k++ ) {
smallArr[ k ] = One.getOneCoeff( k );
}
// perform (1 or 0)*(x^n-.. + x + C)
if ( smaller < 2 ) {
for ( j = 0; j < larger; j++ ) {
answer[ j ] = smallArr * largeArr[ j ];
}
} else { // otherwise perform multiple term multiplication
k = 0; // set outside because it can't be reset inside
for ( i = 0; i < smaller; i++ ) {
for ( j = 0; j < larger; j++, k++ ) {
answer[ k ] = largeArr[ j ] * smallArr[ i ];
}
}
}
} else {
larger = One.getSize(); // first poly has more terms
smaller = Two.getSize(); // second poly has less terms
double largeArr[ larger ], smallArr[ smaller ];
for ( int k = 0; k < larger; k++ ) {
largeArr[ k ] = One.getOneCoeff( k );
}
for ( int k = 0; k < smaller; k++ ) {
smallArr[ k ] = Two.getOneCoeff( k );
}
// perform (1 or 0)*(x^n-.. + x + C)
if ( smaller < 2 ) {
for ( j = 0; j < larger; j++ ) {
answer[ j ] = smallArr * largeArr[ j ];
}
} else { // otherwise perform multiple term multiplication
k = 0; // set outside because it can't be reset inside
for ( i = 0; i < smaller; i++ ) {
for ( j = 0; j < larger; j++ ) {
answer[ k ] = largeArr[ j ] * smallArr[ i ];
}
}
}
}
.....
I was able to perform the multiplications above without much thought... but for multiple term polynomials I'm at a loss... my best guess is the need for some dynamic allocation of some sort because if you have for example, Poly1 = 4 terms, and Poly2 = 2 terms, well then you have (size1 - 1) * size2 total terms in the end. Obviously this number grows as there are more terms input, so I can't just do a nested structure of for loops up to a finite count(say 2 or up to 5) of terms. Any pointers or suggestions are highly welcomed.
Thank you.