Hey so I did this real quick, didn't test it or anything, but the logic is pretty simple:
Polynomial A is greater than polynomial B if and only if, for the term of highest degree with unequal coefficients between the two polynomials, A's coefficient for this term is greater than B's.
I.e:
2x^2 + 3x + 1 > 2x^2 + 2x + 1;
3x^6 + 1 > 3x^5 + 1;
-7x^2 < x + 5;
Here's the code:
Code:
public class Polynom implements Comparable<Polynom> {
double[] polynom;
double[] der;
/**
polynom contains the coefficient of the term i such as the exponent is also i
* example : 4x^7 +3x^2 is representend by
*[0,0,3,0,0,0,0,4]
*/
public Polynom(double[] coeff) {
polynom = new double[coeff.length];
for (int i = 0; i < coeff.length; i++) {
polynom[i] = coeff[i];
}
der = new double[coeff.length - 1];
getDeri();
}
private double[] getDeri() {
for (int i = 1; i < polynom.length; i++) {
der[i - 1] = polynom[i] * i;
}
return der;
}
public double[] getDerivative() {
return der;
}
public double calculatePolynom(double x) {
double summa = 0;
for (int i = 0; i < polynom.length; i++) {
summa = summa + polynom[i] * (Math.pow(x, i));
}
return summa;
}
public double calculateDerivative(double x) {
double summa = 0;
for (int i = 0; i < der.length; i++) {
summa = summa + der[i] * (Math.pow(x, i));
}
return summa;
}
public int compareTo( Polynom p ) {
if( !p ) return 0;
if( this.polynom == p.polynom )
return 0;//The two are equal.
double a,b;
for( int iter = Math.max(polynom.length,p.polynom.length); iter > 0; iter-- )
{
a = (iter > polynom.length) ? 0 : polynom[iter-1];
b = (iter > p.polynom.length) ? 0 : p.polynom[iter-1];
if( a > b )
return 1;
if( b > a )
return -1;
}
return 0;//Sanity check.
}
public String toString() {
String p = "" + polynom[0];
if (polynom.length > 1) {
p = p + " + " + polynom[1] + "x";
}
int i = 2;
while (i < polynom.length) {
if (polynom[i] != 0) {
p = p + " + " + polynom[i] + "x^" + i;
}
i++;
}
return p;
}
}
(p.s. sorry if it doesn't work, I don't have a java VM installed on my comp atm to test it.)