Connecting Tech Pros Worldwide Help | Site Map

Derivative method for class polynomial

Newbie
 
Join Date: Sep 2008
Posts: 3
#1: Oct 8 '08
Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial
in which i need a derivative method I have everything, just dont know how to start the derivative method. Any help will be appriciated here is the code:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class Polynomial extends UnorderedArrayList
  5. {
  6.         Scanner sc = new Scanner(System.in);
  7.         //Default constructor
  8.         //Postcondition: An array of the size 100, and
  9.         //             length and maxSize are set to 100
  10.     public Polynomial()
  11.     {
  12.          super();
  13.          length = 100;
  14.     }
  15.  
  16.         //Constructor with parameter
  17.         //Postcondition: An array of the size specified by
  18.         //      the parameter size is created, length and maxSize
  19.         //      are initialized to size
  20.     public Polynomial(int size)
  21.     {
  22.          super(size);
  23.          length = size;
  24.     }
  25.  
  26.         //copy constructor
  27.     public Polynomial(Polynomial right)
  28.     {
  29.          super(right);
  30.     }
  31.  
  32.  
  33.         //Method to copy the coefficients of the polynomial
  34.         //specified by the parameter right
  35.         //Postcondition: The coefficients of the polynomial
  36.         //               specified by right are copied.
  37.     public void copy(Polynomial right)
  38.     {
  39.         super.copyList(right);
  40.     }
  41.  
  42.         //Method to evaluate a polynomial at a given value
  43.         //Postcondition: The polynomial is evaluated at x and
  44.         //               the value is returned
  45.     public double evaluate(double x)
  46.     {
  47.         double value = 0.0;
  48.  
  49.         DoubleElement coeff = new DoubleElement();
  50.  
  51.         for(int i = 0; i < length; i++)
  52.         {
  53.             coeff.makeCopy((DoubleElement) list[i]);
  54.  
  55.             if(coeff.getNum() != 0.0)
  56.                value = value + coeff.getNum() * Math.pow(x,i);
  57.         }
  58.  
  59.         return value;
  60.     }
  61.  
  62.  
  63.         //Method to add two polynomials
  64.         //Postcondition: This polynomial is added with the polynomial
  65.         //          specified by the parameter right. A reference of
  66.         //          the result is returned.
  67.     public Polynomial add(Polynomial right)
  68.     {
  69.         int size = max(length, right.length);
  70.         int i;
  71.         double sumCoeff;
  72.  
  73.         DoubleElement coeffP = new DoubleElement();
  74.         DoubleElement coeffQ = new DoubleElement();
  75.  
  76.         DoubleElement z;
  77.  
  78.         Polynomial temp = new Polynomial(size);
  79.  
  80.         for(i = 0; i < min(length, right.length); i++)
  81.         {
  82.             coeffP.makeCopy((DoubleElement) list[i]);
  83.             coeffQ.makeCopy((DoubleElement) right.list[i]);
  84.  
  85.             sumCoeff = coeffP.getNum() + coeffQ.getNum();
  86.             z = new DoubleElement(sumCoeff);
  87.             temp.list[i] = z;
  88.         }
  89.  
  90.         if(size == length)
  91.              for(i = min(length, right.length); i < length; i++)
  92.                  temp.list[i] = list[i].getCopy();
  93.         else
  94.            for(i = min(length, right.length); i < right.length; i++)
  95.                temp.list[i] = right.list[i].getCopy();
  96.  
  97.         return temp;
  98.     }
  99.  
  100.         //Method to subtract two polynomials
  101.         //Postcondition: The polynomial specified by the
  102.         //          parameter right is subtracted from this
  103.         //          polynomial. A reference of the result is returned.
  104.     public Polynomial subtract(Polynomial right)
  105.     {
  106.         int size = max(length, right.length);
  107.         int i;
  108.         double diffCoeff;
  109.         double coeff;
  110.  
  111.         DoubleElement coeffP = new DoubleElement();
  112.         DoubleElement coeffQ = new DoubleElement();
  113.  
  114.         DoubleElement z;
  115.  
  116.         Polynomial temp = new Polynomial(size);
  117.  
  118.         for(i = 0; i < min(length, right.length); i++)
  119.         {
  120.             coeffP.makeCopy((DoubleElement) list[i]);
  121.             coeffQ.makeCopy((DoubleElement) right.list[i]);
  122.  
  123.             diffCoeff = coeffP.getNum() - coeffQ.getNum();
  124.             z = new DoubleElement(diffCoeff);
  125.             temp.list[i] = z;
  126.         }
  127.  
  128.         if(size == length)
  129.            for(i = min(length, right.length); i < length; i++)
  130.                temp.list[i] = list[i].getCopy();
  131.         else
  132.            for(i = min(length, right.length); i < right.length; i++)
  133.            {
  134.                z = new DoubleElement();
  135.                z.makeCopy((DoubleElement) right.list[i]);
  136.                coeff = z.getNum();
  137.                z.setNum(-coeff);
  138.                temp.list[i] = z;
  139.            }
  140.  
  141.         return temp;
  142.     }
  143.  
  144.         //Method to multiply two polynomials
  145.         //Postcondition: This polynomial is multiplied with the
  146.         //          polynomial specified by the parameter right. A
  147.         //          reference of the result is returned.
  148.     public Polynomial multiply(Polynomial right)
  149.     {
  150.  
  151.         Polynomial temp = new Polynomial();
  152.  
  153.         System.out.println("See Programming Exercise 5 at the end of the chapter.");
  154.  
  155.         return temp;
  156.     }
  157.  
  158.         //Method to read the coefficients of a polynomial
  159.     public void read() throws IOException
  160.     {
  161.         DoubleElement x = new DoubleElement();
  162.  
  163.         System.out.println("The degree of this polynomial is: "
  164.                          + (length - 1));
  165.         for(int i = 0; i < length; i++)
  166.         {
  167.             System.out.print("Enter the coefficient of x^"
  168.                              + i + ": ");
  169.  
  170.             double y;
  171.             x.setNum(y = sc.nextDouble());
  172.  
  173.             list[i] = x.getCopy();
  174.         }
  175.     }
  176.  
  177.           //Method to return the string containing the polynomial
  178.     public String toString()
  179.     {
  180.         int i;
  181.         int firstNonzeroCoeff = 0;
  182.         DoubleElement x = new DoubleElement();
  183.         String str = "";
  184.  
  185.         for(i = 0; i < length; i++)
  186.         {
  187.             x.makeCopy((DoubleElement) list[i]);
  188.  
  189.             if(x.getNum() != 0.0)
  190.             {
  191.                firstNonzeroCoeff = i;
  192.                break;
  193.             }
  194.         }
  195.  
  196.         if(firstNonzeroCoeff < length)
  197.         {
  198.            if(firstNonzeroCoeff == 0)
  199.               str = list[firstNonzeroCoeff] + " ";
  200.            else
  201.               str = list[firstNonzeroCoeff] + "x^"
  202.                        + firstNonzeroCoeff + " ";
  203.  
  204.            for(i = firstNonzeroCoeff + 1; i < length; i++)
  205.            {
  206.                x.makeCopy((DoubleElement) list[i]);
  207.  
  208.                if(x.getNum() != 0.0)
  209.                   if(x.getNum() > 0.0)
  210.                      str += "+ " + x.getNum() + "x^" + i + " ";
  211.                   else
  212.                      str += "- " + -x.getNum() + "x^" + i + " ";
  213.            }
  214.         }
  215.  
  216.         return str;
  217.     }
  218.  
  219.         //Method to determine the smaller of x and y
  220.         //Postcondition: The smaller of x and y is returned
  221.     public int min(int x, int y)
  222.     {
  223.         if(x <= y)
  224.            return x;
  225.         else
  226.            return y;
  227.     }
  228.  
  229.         //Method to determine the larger of x and y
  230.         //Postcondition: The larger of x and y is returned
  231.     public int max(int x, int y)
  232.     {
  233.         if(x >= y)
  234.            return x;
  235.         else
  236.            return y;
  237.     }
  238. }
  239.  
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Oct 8 '08

re: Derivative method for class polynomial


What is an UnorderedArrayList? Did you write your Polynomial code yourself?

kind regards,

Jos
Reply