// YOUR NAMES HERE!!!!
public class Polynomial {
double[] a; // array of coefficients
// default Constructor creates the zero polynomial
public Polynomial(){
a = new double[1];
a[0] = 0.0D;
}
// Constructor based on an array of coefficents
public Polynomial(double[] A){
a = new double[1]; // REPLACE
a[0] = 0.0D; // REPLACE
}
// Copy Constructor
public Polynomial(Polynomial p){
a = new double[1]; // REPLACE
a[0] = 0.0D; // REPLACE
}
// return ith coefficient (0 returns constant term)
public double coeff(int i){
return 0.0; // REPLACE
}
// returns the degree of this polynomial based on array length
public int degree() {
return 0; // REPLACE
}
// toString method - works as is!
public String toString(){
String s="";
for(int i=a.length-1; i>=0; i--){
s+=a[i];
if (i!=0) s+="x^"+i+" + ";
}
return s;
}
// Evaluate polynomial at x
// consider using Horner's method:
// f(x) = a[0] + x(a[1] + x(a[2] + x(... + x(a[n-1]+ xa[n])...)))
public double f(double x){
return 0.0; // REPLACE
}
// add the constant b to each term of p
public static Polynomial add(Polynomial p, double b){
return new Polynomial(); // REPLACE
}
// multiply each term of p by the constant b
public static Polynomial multiply(Polynomial p, double b){
return new Polynomial(); // REPLACE
}
// add two polynomials
public static Polynomial add(Polynomial p1, Polynomial p2){
return new Polynomial(); // REPLACE
}
// multiply two polynomials
public static Polynomial multiply(Polynomial p1, Polynomial p2){
return new Polynomial(); // REPLACE
}
// Use the power rule to differentiate p(x)
// be careful when degree is zero
public Polynomial derivative(){
return new Polynomial(); // REPLACE
}
// Use the power rule to integrate p(x), assume constant C is zero
public Polynomial integrate(){
return new Polynomial(); // REPLACE
}
// Integrate this polynomial from x0 to x1
public double integral(double x0, double x1) {
Polynomial p = integrate(); // REPLACE
return 0.0; // REPLACE
}
public static void main(String args[]){
Polynomial p[] = new Polynomial[10];
int n = 0;
p[n++] = new Polynomial(); // p(x) = 0;
p[n++] = new Polynomial(new double[] {1.0}); // p(x) = 1;
p[n++] = new Polynomial(new double[] {2.0, -1.0}); // p(x) = -x + 2;
p[n++] = new Polynomial(new double[] {6.0, 5.0, 1.0}); // p(x) = x^2 + 5x + 6;
p[n++] = new Polynomial(new double[] {1.0, 5.0, 7.0, 3.0}); // p(x) = 3x^3 + 7x^2 + 5x + 1
for (int i = 0; i < n; i++) {
System.out.println("p(x) = " + p[i]);
System.out.println("degree: " + p[i].degree());
Polynomial r = new Polynomial(p[i]);
System.out.println("r(x) = " + r);
System.out.println("degree: " + r.degree());
System.out.printf("p(%f) = %f = %f\n", 0.0, p[i].f(0.0), p[i].coeff(0));
System.out.printf("p(%f) = %f\n", 1.0, p[i].f(1.0));
System.out.printf("p(%f) = %f\n", -1.0, p[i].f(-1.0));
Polynomial q = p[i].derivative();
System.out.println("p'(x) = " + q);
System.out.println("p' degree: " + q.degree());
q = p[i].integrate();
System.out.println("P(x) = " + q);
System.out.println("P degree: " + q.degree());
double v = p[i].integral(1.0, 2.0);
System.out.println("integral from 1.0 to 2.0 = " + v);
r = Polynomial.add(p[i],1.0);
System.out.println("p(x) + 1 = " + r);
System.out.println("degree: " + r.degree());
r = Polynomial.multiply(p[i],2.0);
System.out.println("p(x) * 2 = " + r);
System.out.println("degree: " + r.degree());
r = Polynomial.multiply(p[i],p[(i-1+n)%n]);
System.out.println("(" + p[i] + ") * (" + p[(i-1+n)%n] + ") = " + r);
System.out.println("degree: " + r.degree());
r = Polynomial.add(p[i],p[(i-1+n)%n]);
System.out.println("(" + p[i] + ") + (" + p[(i-1+n)%n] + ") = " + r);
System.out.println("degree: " + r.degree());
System.out.println("--------------------------------------------------------");
}
}
}
|