regressionpanel.java

来自「一个一元曲线多项式数值演示例子」· Java 代码 · 共 130 行

JAVA
130
字号
package numbercruncher.program10_2;

import numbercruncher.graphutils.*;
import numbercruncher.mathutils.*;
import numbercruncher.pointutils.*;

/**
 * The demo panel for the Polynomial Regression program and applet.
 */
public class RegressionPanel
    extends InterRegressPanel {
  private static final int MAX_POINTS = 100;

  /** regression polynomial degree */
  int degree = 1;
  /** regression polynomial function */
  RegressionPolynomial poly;

  /**
   * Constructor.
   */
  RegressionPanel() {
    super(MAX_POINTS, "Regression poly", "Reset", true);
    poly = new RegressionPolynomial(degree, MAX_POINTS);
  }

  /**
   * The user has added a data point.
   * @param r the dot's row
   * @param c the dot's column
   */
  protected void doDotAction(int r, int c) {
    if (n > degree) {
      actionButton1.setEnabled(true);

    }
    PlotProperties props = getPlotProperties();

    float x = props.getXMin() + c * props.getXDelta();
    float y = props.getYMax() - r * props.getYDelta();

    poly.addDataPoint(new DataPoint(x, y));
  }

  /**
   * Button 1 action: Construct and plot the regression polynomial.
   */
  protected void doButton1Action() {
    drawDots();
    plotOK = true;

    try {
      poly.computeCoefficients();
      plotFunction();

      String label = "Regression polynomial of degree " + degree;
      String message = poly.getWarningMessage();
      if (message != null) {
        label += "  (WARNING: " + message;

      }
      setHeaderLabel(label);
    }
    catch (Exception ex) {
      setHeaderLabel("Could not generate polynomial:  " +
                     ex.getMessage(), MAROON);
    }
  }

  /**
   * Button 2 action: Reset.
   */
  protected void doButton2Action() {
    reset();
    draw();

    setHeaderLabel("");
    actionButton1.setEnabled(false);
  }

  //------------------//
  // Method overrides //
  //------------------//

  /**
   * Return the value of the regression poly function at x.
   * @param x the value of x
   * @return the value of the function
   */
  public float valueAt(float x) {
    return poly.at(x);
  }

  /**
   * Notification that the plot bounds changed.
   * Redraw the panel.
   */
  public void plotBoundsChanged() {
    n = 0;
    draw();
  }

  /**
   * The degree has changed.
   * @param degree the new degree
   */
  protected void degreeChanged(int degree) {
    this.degree = degree;
    plotOK = false;

    DataPoint data[] = poly.getDataPoints();

    poly = new RegressionPolynomial(degree, MAX_POINTS);
    for (int i = 0; i < n; ++i) {
      poly.addDataPoint(data[i]);

    }
    actionButton1.setEnabled(n > degree);
  }

  /**
   * Reset.
   */
  protected void reset() {
    super.reset();
    plotOK = false;
    poly.reset();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?