Anyone know a way to shorten my code.

Discussion in 'JavaScript' started by Frebreze, Feb 20, 2014.

  1. #1
    If you want to know, this code finds roots of polynomials.

    
    import java.util.*;
    
    class Roots {
    
          public static void main( String[] args ){
                double L, R;
                double resolution = 0.01;
                double tolerance = 0.0000001;
                double threshold = 0.001;
    
                int polyRootPointer = 0;
                int diffRootPointer = 0;
    
                boolean root = false;
    
                Scanner sc = new Scanner(System.in);
    
    
                System.out.print("Enter the degree: ");
                degree = sc.nextInt();
                coefNum = degree + 1;
                System.out.print("Enter " + coefNum + " coefficients: ");
                double[] coefficients = new double[coefNum];
                polyArray = new double[coefNum];
                double[] polyRootArray = new double[degree];
                diffArray = new double[coefNum];
                double[] diffRootArray = new double[degree];
                for(int i = 0; i < coefNum; i++) {
                      coefficients[i] = sc.nextDouble();
                }
                System.out.print("Enter the left and right endpoints: ");
                L = sc.nextDouble();
                R = sc.nextDouble();
    
                System.out.println();
    
                diff(coefficients);
    
                for (double i = L; i < R-resolution; i = i + resolution){
                      if (isPositive(diffArray, i) != isPositive(diffArray, i+resolution) || isPositive(diffArray, i) == 0) {
                            diffRootArray[diffRootPointer] = findRoot(diffArray, i, i+resolution, tolerance);
                            diffRootPointer++;
                      }
                }
    
                for (int i = 0; i < diffRootArray.length; i++) {
                      double tempValue;
                      tempValue = poly(coefficients, diffRootArray[i]);
                      tempValue = Math.abs(tempValue);
                      if (tempValue < threshold) {
                            polyRootArray[polyRootPointer] = diffRootArray[i];
                            polyRootPointer++;
                            root = true;
                      }
                }
    
                for (double i = L; i < R-resolution; i = i + resolution){
                      if (isPositive(coefficients, i) != isPositive(coefficients, i+resolution) || isPositive(coefficients, i) == 0) {
                            polyRootArray[polyRootPointer] = findRoot(coefficients, i, i+resolution, tolerance);
                            polyRootPointer++;
                            root = true;
                      }
                }
    
                Arrays.sort(polyRootArray);
    
                if (root == true) {
                      for (int i = 0; i < polyRootArray.length; i++) {
                            if (polyRootArray[i] != 0.0) {
                                  System.out.printf("Root found at %.5f%n", polyRootArray[i]);
                            }
                      }
                } else {
                      System.out.println("No roots were found.");
                }
          }
    
          public static int degree;
          public static double[] polyArray;
          public static double[] diffArray;
         public static int coefNum;
    
               static double poly(double[] C, double x){
                double Sum = 0;
                polyArray[0] = C[0];
                for (int i = 1; i < polyArray.length; i++){
                      polyArray[i] = C[i]*(Math.pow(x, i));
                }
                for (int i = 0; i < polyArray.length; i++){
                      Sum = Sum + polyArray[i];
                }
                return(Sum);
    
          }
    
          static double[] diff(double[] C){
                for (int i = 0; i < degree; i++){
                      diffArray[i] = (i+1)*C[i+1];
                }
                return(diffArray);
          }
    
          static double findRoot(double[] C, double a, double b, double tolerance){
                double root = 0.0 , residual;
                while ( Math.abs(b - a) > tolerance ) {
                      root = (a + b) / 2.0;
                      residual = poly(C, root);
                      if (poly(C, a) > 0 && poly(C, b) < 0) {
                            if (residual > 0)
                                  a = root;
                            else
                                  b = root;
                      } else if (poly(C, a) < 0 && poly(C, b) > 0) {
                            if (residual > 0)
                                  b = root;
                            else
                                  a = root;
                      }
                }
                return(root);
          }
          static int isPositive(double[] C, double a){
                double endpointTempA;
                endpointTempA = poly(C, a);
                if (endpointTempA < 0) {
                      return(1);
                } else if (endpointTempA > 0) {
                      return(2);
                } else {
                      return(0);
                }
          }
    }
    
    Code (markup):
    This code works so you guys know, so you don't have to help me with errors.
     
    Frebreze, Feb 20, 2014 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    This forum is about JAVASCRIPT not JAVA so you know, so you probably won't get help here.
     
    stephan2307, Feb 21, 2014 IP