Package sets

Source Code of sets.PointSet

package sets;

import java.awt.Color;
import java.util.ArrayList;

import statistics.Point;
import lipstone.joshua.parser.Parser;
import lipstone.joshua.parser.exceptions.ParserException;
import lipstone.joshua.parser.types.BigDec;

public class PointSet {
  public String name;
  public ArrayList<Point> points = new ArrayList<Point>();
  public ArrayList<Point> linePoints = new ArrayList<Point>();
  public String reExpression[] = {"x", "y"}, bestFit = "x";
  protected String equation = "x";
  public Color color = Color.black;
  public int pointDiameter = 8;
  public double accuracy = 2;
  public boolean isLine = false, refreshLine = true;
  private static Parser parser = new Parser();
  private static double previousScale[] = {0.0, 0.0};
 
  public PointSet(String name) {
    this.name = name;
  }
 
  public PointSet(String name, Color color) {
    this.name = name;
    this.color = color;
  }
 
  public PointSet(String name, Color color, boolean isLine, String equation) {
    this.name = name;
    this.color = color;
    this.isLine = isLine;
    if (isLine && !equation.equals("")) {
      this.equation = equation.replaceAll("[xyz]\\Q=\\E", "");
      this.equation = equation.replaceAll("[a-zA-Z]\\([a-zA-Z]\\)\\=", "");
    }
  }
 
  public PointSet(String name, Color color, boolean isLine, String equation, double accuracy) {
    this.name = name;
    this.color = color;
    this.isLine = isLine;
    if (isLine && !equation.equals("")) {
      this.equation = equation.replaceAll("[xyz]\\Q=\\E", "");
      this.equation = equation.replaceAll("[a-zA-Z]\\([a-zA-Z]\\)\\=", "");
    }
    this.accuracy = accuracy;
  }
 
  public void clearPoints() {
    if (isLine)
      linePoints = new ArrayList<Point>();
    else
      points = new ArrayList<Point>();
  }
 
  public ArrayList<Point> getPoints() throws ParserException {
    if (isLine)
      return linePoints;
    else
      return reExpression(points);
  }
 
  public ArrayList<Point> getPoints(boolean reExpress) throws ParserException {
    if (isLine)
      return linePoints;
    else if (reExpress)
      return reExpression(points);
    else
      return points;
  }
 
  public void setPoints(ArrayList<Point> points) {
    if (isLine)
      linePoints = points;
    else
      this.points = points;
  }
 
  public void setReExpression(String reExpression[]) {
    this.reExpression = reExpression;
  }
 
  public void setReExpression(String x, String y) {
    reExpression[0] = x;
    reExpression[1] = y;
  }
 
  public ArrayList<Point> reExpression(ArrayList<Point> points) throws ParserException {
    if (reExpression[0].equals("x") && reExpression[1].equals("y"))
      return points;
    ArrayList<Point> reExpressed = new ArrayList<Point>();
    for (int i = 0; i < reExpression.length; i++)
      reExpression[i] = parser.preProcess(reExpression[i]);
    System.out.println(reExpression[0] + " " + reExpression[1]);
    for (Point p : points) {
      BigDec pos[] = p.getPosition();
      for (int i = 0; i < reExpression.length; i++) {
        pos[i] = new BigDec(parser.run(reExpression[i].replaceAll("[xyz]", pos[i].toString())));
      }
      Point point = new Point(p);
      System.out.println(point.getPosition()[0] + " " + point.getPosition()[1]);
      point.setPosition(pos);
      reExpressed.add(point);
    }
    return reExpressed;
  }
 
  public void constructLine() throws ParserException {
    double scale[] = {1, 1};
    if (parser.containsVariable(equation, parser.getVariables(equation)) && (previousScale != scale || refreshLine)) {
      linePoints = new ArrayList<Point>();
      equation = parser.preProcess(equation);
      int numPoints = 0;
      for (int i = 0; i <= 1; i++) {
        int iterations = 0;
        numPoints = 0;
        while (numPoints <= 1.3 * 20 * (Double.parseDouble(parser.run("10^(" + accuracy + "-1)")))) {
          Double deltax = (scale[0]) / (Double.parseDouble(parser.run("10^(" + accuracy + "-1)")));
          BigDec x = new BigDec((iterations * deltax) * (1 - 2 * i));
          BigDec y = new BigDec(parser.run(equation.replaceAll("[xyz]", "(" + x.toString() + ")")));
          linePoints.add(new Point(x, y, name, color, 5));
          iterations++;
          numPoints++;
        }
        refreshLine = false;
        previousScale[0] = scale[0];
        previousScale[1] = scale[1];
      }
      sortPoints("locationX");
    }
  }
 
  public ArrayList<Double> getResiduals() throws NumberFormatException, ParserException {
    ArrayList<Double> residuals = new ArrayList<Double>();
    for (Point p : getPoints())
      residuals.add(p.getPosition()[1].doubleValue() - Double.parseDouble(parser.run(bestFit.replaceAll("[xyz]", p.getPosition()[0].toString()))));
    return residuals;
  }
 
  public Double getSumResiduals() throws NumberFormatException, ParserException {
    ArrayList<Double> residuals = getResiduals();
    Double total = 0.0;
    for (Double d : residuals)
      total = total + Math.abs(d.doubleValue());
    return total;
  }
 
  public void sortPoints(String method) throws ParserException {
    if (getPoints().size() >= 2) {
      if (method.equalsIgnoreCase("set")) {
        ArrayList<Point> points = getPoints();
        for (int a = 0; a < points.size(); a++) {
          for (int b = a + 1; b < points.size(); b++) {
            if (points.get(b).set.compareTo(points.get(a).set) < 0) {
              Point temp = points.get(a);
              points.set(a, points.get(b));
              points.set(b, temp);
            }
          }
        }
        setPoints(points);
      }
      if (method.equalsIgnoreCase("locationX")) {
        ArrayList<Point> points = getPoints();
        for (int a = 0; a < points.size(); a++) {
          for (int b = a + 1; b < points.size(); b++) {
            if (points.get(a).getPosition()[0].doubleValue() > points.get(b).getPosition()[0].doubleValue()) {
              Point temp = points.get(a);
              points.set(a, points.get(b));
              points.set(b, temp);
            }
          }
        }
        setPoints(points);
      }
      if (method.equalsIgnoreCase("locationY")) {
        ArrayList<Point> points = getPoints();
        for (int a = 0; a < points.size(); a++) {
          for (int b = a + 1; b < points.size(); b++) {
            if (points.get(a).getPosition()[1].doubleValue() > points.get(b).getPosition()[1].doubleValue()) {
              Point temp = points.get(a);
              points.set(a, points.get(b));
              points.set(b, temp);
            }
          }
        }
        setPoints(points);
      }
    }
  }
}
TOP

Related Classes of sets.PointSet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.