예제 #1
0
 private void getintbright() {
   weights = new float[ncurves][xpts][ypts];
   for (int i = 0; i < ncurves; i++) {
     nmeas[i] = 0;
     for (int j = 0; j < xpts; j++) {
       for (int k = 0; k < ypts; k++) {
         nmeas[i] += (int) pch[i][j][k];
       }
     }
     double tempavg = 0.0;
     double tempavg2 = 0.0;
     double temp2avg = 0.0;
     double temp2avg2 = 0.0;
     double tempccavg = 0.0;
     for (int j = 0; j < xpts; j++) {
       for (int k = 0; k < ypts; k++) {
         double normed = (double) pch[i][j][k] / (double) nmeas[i];
         if (pch[i][j][k] > 0.0f) {
           weights[i][j][k] = (float) ((double) nmeas[i] / (normed * (1.0f - normed)));
         } else {
           weights[i][j][k] = 1.0f;
         }
         tempavg += normed * (double) j;
         tempavg2 += normed * (double) j * (double) j;
         temp2avg += normed * (double) k;
         temp2avg2 += normed * (double) k * (double) k;
         tempccavg += normed * (double) k * (double) j;
       }
     }
     tempccavg -= tempavg * temp2avg;
     brightcc[i] = tempccavg / Math.sqrt(tempavg * temp2avg);
     tempavg2 -= tempavg * tempavg;
     tempavg2 /= tempavg;
     bright1[i] = (tempavg2 - 1.0);
     temp2avg2 -= temp2avg * temp2avg;
     temp2avg2 /= temp2avg;
     bright2[i] = (temp2avg2 - 1.0);
     intensity1[i] = tempavg;
     intensity2[i] = temp2avg;
     if (psfflag == 0) {
       bright1[i] /= 0.3536;
       bright2[i] /= 0.3536;
       brightcc[i] /= 0.3536;
     } else {
       if (psfflag == 1) {
         bright1[i] /= 0.078;
         bright2[i] /= 0.078;
         brightcc[i] /= 0.078;
       } else {
         bright1[i] /= 0.5;
         bright2[i] /= 0.5;
         brightcc[i] /= 0.5;
       }
     }
     number1[i] = intensity1[i] / bright1[i];
     number2[i] = intensity2[i] / bright2[i];
     brightmincc[i] = (bright1[i] * beta) * Math.sqrt(intensity1[i] / intensity2[i]);
   }
 }
예제 #2
0
 protected void plotScatterDiagram() {
   // plot sample as one dimensional scatter plot and Gaussian
   double xmax = 5.;
   double xmin = -5.;
   DatanGraphics.openWorkstation(getClass().getName(), "E3Min_1.ps");
   DatanGraphics.setFormat(0., 0.);
   DatanGraphics.setWindowInComputingCoordinates(xmin, xmax, 0., .5);
   DatanGraphics.setViewportInWorldCoordinates(-.15, .9, .16, .86);
   DatanGraphics.setWindowInWorldCoordinates(-.414, 1., 0., 1.);
   DatanGraphics.setBigClippingWindow();
   DatanGraphics.chooseColor(2);
   DatanGraphics.drawFrame();
   DatanGraphics.drawScaleX("y");
   DatanGraphics.drawScaleY("f(y)");
   DatanGraphics.drawBoundary();
   double xpl[] = new double[2];
   double ypl[] = new double[2];
   // plot scatter diagram
   DatanGraphics.chooseColor(1);
   for (int i = 0; i < y.length; i++) {
     xpl[0] = y[i];
     xpl[1] = y[i];
     ypl[0] = 0.;
     ypl[0] = .1;
     DatanGraphics.drawPolyline(xpl, ypl);
   }
   // draw Gaussian corresponding to solution
   int npl = 100;
   xpl = new double[npl];
   ypl = new double[npl];
   double fact = 1. / (Math.sqrt(2. * Math.PI) * x.getElement(1));
   double dpl = (xmax - xmin) / (double) (npl - 1);
   for (int i = 0; i < npl; i++) {
     xpl[i] = xmin + (double) i * dpl;
     ypl[i] = fact * Math.exp(-.5 * Math.pow((xpl[i] - x.getElement(0)) / x.getElement(1), 2.));
   }
   DatanGraphics.chooseColor(5);
   DatanGraphics.drawPolyline(xpl, ypl);
   // draw caption
   String sn = "N = " + nny;
   numForm.setMaximumFractionDigits(3);
   numForm.setMinimumFractionDigits(3);
   String sx1 = ", x_1# = " + numForm.format(x.getElement(0));
   String sx2 = ", x_2# = " + numForm.format(x.getElement(1));
   String sdx1 = ", &D@x_1# = " + numForm.format(Math.sqrt(cx.getElement(0, 0)));
   String sdx2 = ", &D@x_2# = " + numForm.format(Math.sqrt(cx.getElement(1, 1)));
   caption = sn + sx1 + sx2 + sdx1 + sdx2;
   DatanGraphics.setBigClippingWindow();
   DatanGraphics.chooseColor(2);
   DatanGraphics.drawCaption(1., caption);
   DatanGraphics.closeWorkstation();
 }
예제 #3
0
파일: LJ3MDApp.java 프로젝트: eskilj/mvp
 static { // data[] is a bitmap image of the ball of radius R
   data = new byte[R * 2 * R * 2];
   for (int Y = -R; Y < R; Y++) {
     int x0 = (int) (Math.sqrt(R * R - Y * Y) + 0.5);
     for (int X = -x0; X < x0; X++) {
       // sqrt(x^2 + y^2) gives distance from the spot light
       int x = X + hx, y = Y + hy;
       int r = (int) (Math.sqrt(x * x + y * y) + 0.5);
       // set the maximal intensity to the maximal distance
       // (in pixels) from the spot light
       if (r > maxr) maxr = r;
       data[(Y + R) * (R * 2) + (X + R)] = (r <= 0) ? 1 : (byte) r;
     }
   }
 }
예제 #4
0
  public void advance(double dt) {
    double dx, dy, dz, distance, mag;

    for (int i = 0; i < bodies.length; ++i) {
      for (int j = i + 1; j < bodies.length; ++j) {
        dx = bodies[i].x - bodies[j].x;
        dy = bodies[i].y - bodies[j].y;
        dz = bodies[i].z - bodies[j].z;

        distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
        mag = dt / (distance * distance * distance);

        bodies[i].vx -= dx * bodies[j].mass * mag;
        bodies[i].vy -= dy * bodies[j].mass * mag;
        bodies[i].vz -= dz * bodies[j].mass * mag;

        bodies[j].vx += dx * bodies[i].mass * mag;
        bodies[j].vy += dy * bodies[i].mass * mag;
        bodies[j].vz += dz * bodies[i].mass * mag;
      }
    }

    for (int i = 0; i < bodies.length; ++i) {
      bodies[i].x += dt * bodies[i].vx;
      bodies[i].y += dt * bodies[i].vy;
      bodies[i].z += dt * bodies[i].vz;
    }
  }
  /** Low-quality topics often have lots of unusually short words. */
  public TopicScores getWordLengthStandardDeviation() {

    TopicScores scores = new TopicScores("word-length-sd", numTopics, numTopWords);
    scores.wordScoresDefined = true;

    // Get the mean length

    double meanLength = 0.0;
    int totalWords = 0;

    for (int topic = 0; topic < numTopics; topic++) {
      for (int position = 0; position < topicTopWords[topic].length; position++) {
        // Some topics may not have all N words
        if (topicTopWords[topic][position] == null) {
          break;
        }
        meanLength += topicTopWords[topic][position].length();
        totalWords++;
      }
    }

    meanLength /= totalWords;

    // Now calculate the standard deviation

    double lengthVariance = 0.0;

    for (int topic = 0; topic < numTopics; topic++) {
      for (int position = 0; position < topicTopWords[topic].length; position++) {
        if (topicTopWords[topic][position] == null) {
          break;
        }

        int length = topicTopWords[topic][position].length();

        lengthVariance += (length - meanLength) * (length - meanLength);
      }
    }
    lengthVariance /= (totalWords - 1);

    // Finally produce an overall topic score

    double lengthSD = Math.sqrt(lengthVariance);
    for (int topic = 0; topic < numTopics; topic++) {
      for (int position = 0; position < topicTopWords[topic].length; position++) {
        if (topicTopWords[topic][position] == null) {
          break;
        }

        int length = topicTopWords[topic][position].length();

        scores.addToTopicScore(topic, (length - meanLength) / lengthSD);
        scores.setTopicWordScore(topic, position, (length - meanLength) / lengthSD);
      }
    }

    return scores;
  }
예제 #6
0
  //
  // Normalize the vector (X,Y,Z) so that X*X + Y*Y + Z*Z = 1.
  //
  // The normalization divisor is returned.  If the divisor is zero, no
  // normalization occurs.
  //
  double normalize_vector(double cvec[]) {
    double divisor;

    divisor = Math.sqrt((double) DOT_PRODUCT(cvec, cvec));
    if (divisor > 0.0) {
      cvec[X] /= divisor;
      cvec[Y] /= divisor;
      cvec[Z] /= divisor;
    }
    return divisor;
  }
 /**
  * Calculates statistical values for a data array.
  *
  * @param data the data array
  * @return the max, min, mean, SD, SE and non-NaN data count
  */
 private double[] getStatistics(double[] data) {
   double max = -Double.MAX_VALUE;
   double min = Double.MAX_VALUE;
   double sum = 0.0;
   double squareSum = 0.0;
   int count = 0;
   for (int i = 0; i < data.length; i++) {
     if (Double.isNaN(data[i])) {
       continue;
     }
     count++;
     max = Math.max(max, data[i]);
     min = Math.min(min, data[i]);
     sum += data[i];
     squareSum += data[i] * data[i];
   }
   double mean = sum / count;
   double sd = count < 2 ? Double.NaN : Math.sqrt((squareSum - count * mean * mean) / (count - 1));
   if (max == -Double.MAX_VALUE) max = Double.NaN;
   if (min == Double.MAX_VALUE) min = Double.NaN;
   return new double[] {max, min, mean, sd, sd / Math.sqrt(count), count};
 }
예제 #8
0
  public void solve1245(int kaseno) throws Exception {
    int N = iread();
    int sqrtN = (int) Math.sqrt((double) N);
    long sum1 = 0;
    for (int i = 1; i <= sqrtN; ++i) {
      sum1 += (int) (N / i);
    }
    sum1 <<= 1;

    long sum2 = sqrtN * sqrtN;
    long ret = sum1 - sum2;
    out.write("Case " + kaseno + ": " + ret + "\n");
  }
예제 #9
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.next();

    int l = s.length();
    Double sqrl = Math.sqrt(l);
    Double low = Math.floor(sqrl);
    Double high = Math.ceil(sqrl);
    int r = 0;
    int c = 0;

    if (low == high) {
      r = low.intValue();
      c = low.intValue();
    } else if (low * high >= l) {
      r = low.intValue();
      c = high.intValue();
    } else {
      r = high.intValue();
      c = high.intValue();
    }

    //        System.out.println("Row: " + r);
    //        System.out.println("Col: " + c);

    StringBuilder sb = new StringBuilder();
    char en[] = s.toCharArray();
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < r; j++) {
        if (j * c + i < l) {
          sb.append(en[j * c + i]);
        }
      }
      sb.append(" ");
    }

    System.out.println(sb.toString());
  }
예제 #10
0
  public double energy() {
    double dx, dy, dz, distance;
    double e = 0.0;

    for (int i = 0; i < bodies.length; ++i) {
      e +=
          0.5
              * bodies[i].mass
              * (bodies[i].vx * bodies[i].vx
                  + bodies[i].vy * bodies[i].vy
                  + bodies[i].vz * bodies[i].vz);

      for (int j = i + 1; j < bodies.length; ++j) {
        dx = bodies[i].x - bodies[j].x;
        dy = bodies[i].y - bodies[j].y;
        dz = bodies[i].z - bodies[j].z;

        distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
        e -= (bodies[i].mass * bodies[j].mass) / distance;
      }
    }
    return e;
  }
예제 #11
0
  /* CALCULATE VALUES QUADRANTS: Calculate x-y values where direction is not
  parallel to eith x or y axis. */
  public static void calcValuesQuad(int x1, int y1, int x2, int y2) {
    double arrowAng = Math.toDegrees(Math.atan((double) haw / (double) al));
    double dist = Math.sqrt(al * al + aw);
    double lineAng =
        Math.toDegrees(Math.atan(((double) Math.abs(x1 - x2)) / ((double) Math.abs(y1 - y2))));

    // Adjust line angle for quadrant
    if (x1 > x2) {
      // South East
      if (y1 > y2) lineAng = 180.0 - lineAng;
    } else {
      // South West
      if (y1 > y2) lineAng = 180.0 + lineAng;
      // North West
      else lineAng = 360.0 - lineAng;
    }

    // Calculate coords
    xValues[0] = x2;
    yValues[0] = y2;
    calcCoords(1, x2, y2, dist, lineAng - arrowAng);
    calcCoords(2, x2, y2, dist, lineAng + arrowAng);
  }
예제 #12
0
  private class MinLogLikeGauss extends DatanUserFunction {
    double[] y;
    double sqrt2pi = Math.sqrt(2. * Math.PI);
    double big = 1.E10, small = 1.E-10;
    double f;

    MinLogLikeGauss(double[] y) {
      this.y = y;
    }

    public double getValue(DatanVector x) {
      double result;
      double a = sqrt2pi * x.getElement(1);
      if (a < small) f = big;
      else f = Math.log(a);
      result = (double) y.length * f;
      for (int i = 0; i < y.length; i++) {
        f = Math.pow((y[i] - x.getElement(0)), 2.) / (2. * x.getElement(1) * x.getElement(1));
        result += f;
      }
      return result;
    }
  }
예제 #13
0
  //
  // Interface resized method.
  //
  public void resized(int width, int height) {
    double dx = eyeX - centerX;
    double dy = eyeY - centerY;
    double dz = eyeZ - centerZ;
    double d = Math.sqrt(dx * dx + dy * dy + dz * dz);

    double fovy = 2.0 * 180.0 / Math.PI * Math.atan(radius / d);
    double near = d - radius;
    double far = d + radius;

    double xUnits, yUnits;

    if (width >= height) {
      xUnits = 2 * radius * width / height;
      yUnits = 2 * radius;
    } else {
      xUnits = 2 * radius;
      yUnits = 2 * radius * height / width;
    }

    // setup projection matrix
    go.matrixMode(Go.PROJECTION);
    go.identity();
    if (perspective) {
      if (width >= height) {
        go.perspective(fovy, xUnits / yUnits, near, far);
      } else {
        double r = radius * yUnits / xUnits;
        double fovyModified = Math.atan(r / d) * 180.0 / Math.PI * 2.0;
        go.perspective(fovyModified, xUnits / yUnits, near, far);
      }
    } else {
      go.ortho(-xUnits / 2.0, xUnits / 2.0, -yUnits / 2.0, yUnits / 2.0, near, far);
    }
    go.matrixMode(Go.MODELVIEW);
  }
예제 #14
0
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.WHITE);
    // Draws a white arrow and the principal axis
    g.drawLine(0, 200, 700, 200);
    g.drawLine(arrow_x, 200, arrow_x, arrow_y2);

    // Show coordinates of arrow tip
    arrowCoordinate_x = arrow_x - startingPosition;
    arrowCoordinate_x /= 10;
    arrowCoordinate_y = 200 - arrow_y2;
    arrowCoordinate_y /= 10;

    // Coordinates
    Optics.lbl_arrowCoordinates.setText(
        "<html>(d<sub>o</sub>, h<sub>o</sub>) = ("
            + arrowCoordinate_x
            + ", "
            + arrowCoordinate_y
            + ")</html>");

    if (arrow_y2 < 200) // if arrow is above principal axis
    {
      g.drawLine(arrow_x, arrow_y2, arrow_x - 7, arrow_y2 + 7);
      g.drawLine(arrow_x, arrow_y2, arrow_x + 7, arrow_y2 + 7);
    } else if (arrow_y2 > 200) // if arrow is below principal axis
    {
      g.drawLine(arrow_x, arrow_y2, arrow_x - 7, arrow_y2 - 7);
      g.drawLine(arrow_x, arrow_y2, arrow_x + 7, arrow_y2 - 7);
    }
    // Draws lines for the grid
    if (lenses) startingPosition = 350;
    else {
      radiusOfCurvature = 20 * focalLength;
      if (type == 0) startingPosition = 500;
      else startingPosition = 350;
    }
    {
      for (int i = startingPosition; i <= 700; i += 10) {
        if ((i - startingPosition) % (10 * focalLength) == 0) {
          g.setColor(Color.ORANGE);
          g.drawLine(i, 195, i, 205);
        } else {
          g.setColor(Color.WHITE);
          g.drawLine(i, 197, i, 203);
        }
      }
      for (int i = startingPosition; i >= 0; i -= 10) {
        if ((i - startingPosition) % (10 * focalLength) == 0 && i != 0) {
          g.setColor(Color.ORANGE);
          g.drawLine(i, 195, i, 205);
        } else {
          g.setColor(Color.WHITE);
          g.drawLine(i, 197, i, 203);
        }
      }
    }
    g.setColor(Color.WHITE);

    if (lenses) {
      if (type == 0) // If Converging
      {
        // Draws a converging lens
        g.drawArc(340, 50, 40, 300, 120, 120);
        g.drawArc(320, 50, 40, 300, 60, -120);
        // draws horizontal line from the tip of the arrow to the lens (line 1/3)
        g.setColor(Color.RED);
        g.drawLine(arrow_x, arrow_y2, 350, arrow_y2);
        // calculates necessary information to form equation of line from lens to focal point (line
        // 2/3)

        dy_1 = 200 - arrow_y2;

        if (arrow_x > 350) dx_1 = -10 * focalLength;
        else dx_1 = 10 * focalLength;
        slope_1 = dy_1 / dx_1;

        if (arrow_x > 350) y_intercept_1 = 200 - slope_1 * (350 - 10 * focalLength);
        else y_intercept_1 = 200 - slope_1 * (10 * focalLength + 350);
        // Calculates coordinates of points on the edge of screen (endpoints)
        if (arrow_x <= 350)
          y_screenIntersection_1 = (int) (Math.round(slope_1 * 700 + y_intercept_1));
        else y_screenIntersection_1 = (int) (Math.round(y_intercept_1));
        if (slope_1 != 0)
          if (arrow_y2 <= 200)
            x_screenIntersection_1 = (int) (Math.round((400 - y_intercept_1) / slope_1));
          else x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1));
        if (x_screenIntersection_1 >= 0
            && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge
        if (arrow_y2 <= 200) g.drawLine(350, arrow_y2, x_screenIntersection_1, 400);
          else g.drawLine(350, arrow_y2, x_screenIntersection_1, 0);
        else if (arrow_x > 350) g.drawLine(350, arrow_y2, 0, y_screenIntersection_1);
        else
          g.drawLine(350, arrow_y2, 700, y_screenIntersection_1); // Else: endpoint is on the y-edge
      } else // Else: Diverging
      {
        // Draws a diverging lens
        g.drawArc(360, 50, 40, 300, 120, 120);
        g.drawArc(300, 50, 40, 300, 60, -120);
        g.drawLine(330, 68, 370, 68);
        g.drawLine(330, 330, 370, 330);

        // draws horizontal line from the tip of the arrow to the lens (line 1/3)
        g.setColor(Color.RED);
        g.drawLine(arrow_x, arrow_y2, 350, arrow_y2);

        // calculates necessary information to form equation of line from lens to focal point (line
        // 2/3)

        dy_1 = arrow_y2 - 200;

        if (arrow_x > 350) dx_1 = -10 * focalLength;
        else dx_1 = 10 * focalLength;
        slope_1 = dy_1 / dx_1;

        if (arrow_x > 350) y_intercept_1 = 200 - slope_1 * (10 * focalLength + 350);
        else y_intercept_1 = 200 - slope_1 * (350 - 10 * focalLength);
        // Calculates coordinates of points on the edge of screen (endpoints)
        if (arrow_x <= 350)
          y_screenIntersection_1 = (int) (Math.round(slope_1 * 700 + y_intercept_1));
        else y_screenIntersection_1 = (int) (Math.round(y_intercept_1));
        if (slope_1 != 0)
          if (arrow_y2 <= 200)
            x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1));
          else x_screenIntersection_1 = (int) (Math.round((400 - y_intercept_1) / slope_1));
        if (x_screenIntersection_1 >= 0
            && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge
        if (arrow_y2 <= 200) g.drawLine(350, arrow_y2, x_screenIntersection_1, 0);
          else g.drawLine(350, arrow_y2, x_screenIntersection_1, 400);
        else // Else: endpoint is on the y-edge
        if (arrow_x > 350) g.drawLine(350, arrow_y2, 0, y_screenIntersection_1);
        else g.drawLine(350, arrow_y2, 700, y_screenIntersection_1);
      }
      // Line 3/3
      dy_2 = 200 - arrow_y2;
      dx_2 = 350 - arrow_x;
      slope_2 = dy_2 / dx_2;
      y_intercept_2 = 200 - slope_2 * 350;
      if (arrow_x <= 350)
        y_screenIntersection_2 = (int) (Math.round(slope_2 * 700 + y_intercept_2));
      else y_screenIntersection_2 = (int) (Math.round(y_intercept_2));
      if (slope_2 != 0)
        if (arrow_y2 <= 200)
          x_screenIntersection_2 = (int) (Math.round((400 - y_intercept_2) / slope_2));
        else x_screenIntersection_2 = (int) (Math.round(-y_intercept_2 / slope_2));

      if (x_screenIntersection_2 >= 0
          && x_screenIntersection_2 <= 700) // If endpoint is on the x-edge
      if (arrow_y2 <= 200) g.drawLine(arrow_x, arrow_y2, x_screenIntersection_2, 400);
        else g.drawLine(arrow_x, arrow_y2, x_screenIntersection_2, 0);
      else if (arrow_x <= 350)
        g.drawLine(
            arrow_x, arrow_y2, 700, y_screenIntersection_2); // Else: endpoint is on the y-edge
      else g.drawLine(arrow_x, arrow_y2, 0, y_screenIntersection_2);

      // POI between Line 2 & Line 3
      x_pointOfIntersection = (int) ((y_intercept_2 - y_intercept_1) / (slope_1 - slope_2));
      y_pointOfIntersection = (int) (slope_1 * x_pointOfIntersection + y_intercept_1);
      // Draw image
      g.setColor(Color.ORANGE);
      g.drawLine(x_pointOfIntersection, 200, x_pointOfIntersection, y_pointOfIntersection);
      if (y_pointOfIntersection < 200) {
        g.drawLine(
            x_pointOfIntersection,
            y_pointOfIntersection,
            x_pointOfIntersection - 7,
            y_pointOfIntersection + 7);
        g.drawLine(
            x_pointOfIntersection,
            y_pointOfIntersection,
            x_pointOfIntersection + 7,
            y_pointOfIntersection + 7);
      } else {
        g.drawLine(
            x_pointOfIntersection,
            y_pointOfIntersection,
            x_pointOfIntersection - 7,
            y_pointOfIntersection - 7);
        g.drawLine(
            x_pointOfIntersection,
            y_pointOfIntersection,
            x_pointOfIntersection + 7,
            y_pointOfIntersection - 7);
      }
      // Same side image line continuation
      if (((x_pointOfIntersection > 350 && arrow_x > 350)
              || (x_pointOfIntersection < 350 && arrow_x < 350))
          && (arrow_x != 350 - 10 * focalLength && arrow_x != 350 + 10 * focalLength
              || type == 1)) {
        g.setColor(Color.YELLOW);
        g.drawLine(x_pointOfIntersection, y_pointOfIntersection, 350, arrow_y2);
        if (type == 0) g.drawLine(x_pointOfIntersection, y_pointOfIntersection, arrow_x, arrow_y2);
      }

      // Mag calculations
      height_image = 200 - y_pointOfIntersection;
      height_object = 200 - arrow_y2;
      if (height_object != 0) magnification = height_image / height_object;

      if (magnification <= 9999 && magnification >= -9999)
        Optics.txt_magnification.setText("" + roundTwoDecimals(magnification));
      else if (magnification > 9999) {
        magnification = Double.POSITIVE_INFINITY;
        Optics.txt_magnification.setText("N/A");
      } else {
        magnification = Double.NEGATIVE_INFINITY;
        Optics.txt_magnification.setText("N/A");
      }
      // Characteristics
      g.setColor(Color.ORANGE);
      g.drawString("Image Characteristics:", 20, 300);
      if (type == 0) {
        if ((Math.abs(magnification) > 1 && Math.abs(magnification) < 9999))
          g.drawString("Magnification:  Enlarged", 20, 320);
        else if (arrow_x == 350 - 20 * focalLength
            || arrow_x == 350 + 20 * focalLength
            || (int) (Math.abs(magnification)) == 1) g.drawString("Magnification:  None", 20, 320);
        else if (Math.abs(magnification) < 1 && Math.abs(magnification) > 0)
          g.drawString("Magnification:  Diminished", 20, 320);
        else g.drawString("Magnification:  N/A", 20, 320);
        if (arrow_x == 350 - 10 * focalLength || arrow_x == 350 + 10 * focalLength)
          g.drawString("Orientation:      N/A", 20, 335);
        else if ((arrow_y2 < 200 && y_pointOfIntersection < 200)
            || (arrow_y2 > 200 && y_pointOfIntersection > 200))
          g.drawString("Orientation:      Upright", 20, 335);
        else g.drawString("Orientation:      Inverted", 20, 335);
        if (arrow_x == 350 - 10 * focalLength || arrow_x == 350 + 10 * focalLength)
          g.drawString("Type:                 N/A", 20, 350);
        else if ((x_pointOfIntersection < 350 && arrow_x < 350)
            || (x_pointOfIntersection > 350 && arrow_x > 350))
          g.drawString("Type:                 Virtual", 20, 350);
        else g.drawString("Type:                 Real", 20, 350);
      } else {
        g.drawString("Magnification:  Diminished", 20, 320);
        g.drawString("Orientation:      Upright", 20, 335);
        g.drawString("Type:                 Virtual", 20, 350);
      }

      height_image /= 10;

      if (height_image > 9999 || height_image < -9999)
        Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= N/A</html>");
      else Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= " + height_image + "</html>");

      distance_image = x_pointOfIntersection - 350;
      distance_image /= 10;
      if (distance_image > 9999 || distance_image < -9999)
        Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= N/A</html>");
      else Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= " + distance_image + "</html>");
    } else // Else: mirrors
    {

      if (type == 0) // If converging
      {
        // draws converging mirror
        g.drawArc(
            500 - 2 * radiusOfCurvature,
            200 - radiusOfCurvature,
            2 * radiusOfCurvature,
            2 * radiusOfCurvature,
            60,
            -120);
        // draws horizontal line from the tip of the arrow to the lens (line 1/4)
        g.setColor(Color.RED);
        x_arcIntersection_1 =
            (int)
                ((Math.sqrt(Math.abs(Math.pow(radiusOfCurvature, 2) - Math.pow(arrow_y2 - 200, 2))))
                    + (500 - radiusOfCurvature));
        g.drawLine(arrow_x, arrow_y2, x_arcIntersection_1, arrow_y2);

        // line 2/4
        dy_1 = arrow_y2 - 200;
        dx_1 = x_arcIntersection_1 - (500 - focalLength * 10);
        slope_1 = dy_1 / dx_1;
        y_intercept_1 = 200 - slope_1 * (500 - focalLength * 10);

        // Calculates coordinates of points on the edge of screen (endpoints)
        y_screenIntersection_1 = (int) (Math.round(y_intercept_1));
        if (slope_1 != 0)
          if (arrow_y2 <= 200)
            x_screenIntersection_1 = (int) (Math.round((400 - y_intercept_1) / slope_1));
          else x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1));
        if (x_screenIntersection_1 >= 0
            && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge
        if (arrow_y2 <= 200) g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 400);
          else g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 0);
        else
          g.drawLine(
              x_arcIntersection_1,
              arrow_y2,
              0,
              y_screenIntersection_1); // Else: endpoint is on the y-edge
        // line 3/4
        if (!(arrow_x > 495 - focalLength * 10 && arrow_x < 505 - focalLength * 10)) {
          dy_2 = 200 - arrow_y2;
          dx_2 = (500 - 10 * focalLength) - arrow_x;
          slope_2 = dy_2 / dx_2;
          y_intercept_2 = arrow_y2 - slope_2 * arrow_x;
          quadratic_a = (float) (Math.pow(slope_2, 2) + 1);
          quadratic_b =
              (float)
                  (((2 * slope_2 * y_intercept_2)
                      - (400 * slope_2)
                      + ((radiusOfCurvature - 500) * 2)));
          quadratic_c =
              (float)
                  ((Math.pow(y_intercept_2, 2)
                      - Math.pow(radiusOfCurvature, 2)
                      - (400 * y_intercept_2)
                      + 40000
                      + Math.pow((radiusOfCurvature - 500), 2)));
          discriminant = (float) (Math.pow(quadratic_b, 2) - (4 * quadratic_a * quadratic_c));
          if (discriminant >= 0)
            x_arcIntersection_2 =
                (int)
                    (Math.max(
                        ((-quadratic_b + Math.sqrt(discriminant)) / (2 * quadratic_a)),
                        ((-quadratic_b - Math.sqrt(discriminant)) / (2 * quadratic_a))));
          else System.out.println("Error, imaginary root!");
          y_arcIntersection_2 = (int) (slope_2 * x_arcIntersection_2 + y_intercept_2);
          g.drawLine(arrow_x, arrow_y2, x_arcIntersection_2, y_arcIntersection_2);
          // System.out.println ("slope: " + slope_2 + "\n yintercept: " + y_intercept_2 + "\n
          // quadratic-a: " + quadratic_a + "\n quadratic-b: " + quadratic_b + "\n quadratic_c: " +
          // quadratic_c + "\n discriminant: " + discriminant + "\n xarcintersection2: " +
          // x_arcIntersection_2 + "\n yarcintersection2: " + y_arcIntersection_2);
          // line 4/4
          g.drawLine(x_arcIntersection_2, y_arcIntersection_2, 0, y_arcIntersection_2);

          // POI between line 2 and line 4
          x_pointOfIntersection = (int) ((y_arcIntersection_2 - y_intercept_1) / slope_1);
          y_pointOfIntersection = y_arcIntersection_2;
          g.setColor(Color.ORANGE);
          g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection, 200);

          if (y_pointOfIntersection < 200) {
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_pointOfIntersection - 7,
                y_pointOfIntersection + 7);
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_pointOfIntersection + 7,
                y_pointOfIntersection + 7);
          } else {
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_pointOfIntersection - 7,
                y_pointOfIntersection - 7);
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_pointOfIntersection + 7,
                y_pointOfIntersection - 7);
          }
          // Same side image line continuation
          if (arrow_x > 500 - 10 * focalLength) {
            g.setColor(Color.YELLOW);
            g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_1, arrow_y2);
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_arcIntersection_2,
                y_arcIntersection_2);
          }
        }
      } else // Diverging
      {
        // draws converging mirror
        g.drawArc(
            350, 200 - radiusOfCurvature, 2 * radiusOfCurvature, 2 * radiusOfCurvature, 120, 120);
        // draws horizontal line from the tip of the arrow to the lens (line 1/4)
        g.setColor(Color.RED);
        x_arcIntersection_1 =
            (int)
                (-(Math.sqrt(Math.pow(radiusOfCurvature, 2) - Math.pow(arrow_y2 - 200, 2)))
                    + (350 + radiusOfCurvature));
        g.drawLine(arrow_x, arrow_y2, x_arcIntersection_1, arrow_y2);

        // line 2/4
        dy_1 = arrow_y2 - 200;
        dx_1 = x_arcIntersection_1 - (350 + focalLength * 10);
        slope_1 = dy_1 / dx_1;
        y_intercept_1 = 200 - slope_1 * (350 + focalLength * 10);

        // Calculates coordinates of points on the edge of screen (endpoints)
        y_screenIntersection_1 = (int) (Math.round(y_intercept_1));
        if (slope_1 != 0)
          if (arrow_y2 <= 200)
            x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1));
          else if (arrow_y2 > 200)
            x_screenIntersection_1 = (int) (Math.round(400 - y_intercept_1 / slope_1));
        if (x_screenIntersection_1 >= 0
            && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge
        if (arrow_y2 <= 200) g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 0);
          else g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 400);
        else
          g.drawLine(
              x_arcIntersection_1,
              arrow_y2,
              0,
              y_screenIntersection_1); // Else: endpoint is on the y-edge
        // line 3/4

        dy_2 = 200 - arrow_y2;
        dx_2 = (350 + 10 * focalLength) - arrow_x;
        slope_2 = dy_2 / dx_2;
        y_intercept_2 = arrow_y2 - slope_2 * arrow_x;
        quadratic_a = (float) (Math.pow(slope_2, 2) + 1);
        quadratic_b =
            (float)
                ((2 * slope_2 * y_intercept_2) - (400 * slope_2) - (2 * radiusOfCurvature + 700));
        quadratic_c =
            (float)
                ((Math.pow(y_intercept_2, 2)
                    - Math.pow(radiusOfCurvature, 2)
                    - (400 * y_intercept_2)
                    + 40000
                    + Math.pow((radiusOfCurvature + 350), 2)));
        discriminant = (float) (Math.pow(quadratic_b, 2) - (4 * quadratic_a * quadratic_c));
        if (discriminant >= 0)
          x_arcIntersection_2 =
              (int)
                  (Math.min(
                      ((-quadratic_b + Math.sqrt(discriminant)) / (2 * quadratic_a)),
                      ((-quadratic_b - Math.sqrt(discriminant)) / (2 * quadratic_a))));
        else System.out.println("Error, imaginary root!");
        y_arcIntersection_2 = (int) (slope_2 * x_arcIntersection_2 + y_intercept_2);
        g.drawLine(arrow_x, arrow_y2, x_arcIntersection_2, y_arcIntersection_2);
        // System.out.println ("slope: " + slope_2 + "\n yintercept: " + y_intercept_2 + "\n
        // quadratic-a: " + quadratic_a + "\n quadratic-b: " + quadratic_b + "\n quadratic_c: " +
        // quadratic_c + "\n discriminant: " + discriminant + "\n xarcintersection2: " +
        // x_arcIntersection_2 + "\n yarcintersection2: " + y_arcIntersection_2);
        // line 4/4
        g.drawLine(x_arcIntersection_2, y_arcIntersection_2, 0, y_arcIntersection_2);

        // POI between line 2 and line 4
        x_pointOfIntersection = (int) ((y_arcIntersection_2 - y_intercept_1) / slope_1);
        y_pointOfIntersection = y_arcIntersection_2;
        g.setColor(Color.ORANGE);
        g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection, 200);

        if (y_pointOfIntersection < 200) {
          g.drawLine(
              x_pointOfIntersection,
              y_pointOfIntersection,
              x_pointOfIntersection - 7,
              y_pointOfIntersection + 7);
          g.drawLine(
              x_pointOfIntersection,
              y_pointOfIntersection,
              x_pointOfIntersection + 7,
              y_pointOfIntersection + 7);
        } else {
          g.drawLine(
              x_pointOfIntersection,
              y_pointOfIntersection,
              x_pointOfIntersection - 7,
              y_pointOfIntersection - 7);
          g.drawLine(
              x_pointOfIntersection,
              y_pointOfIntersection,
              x_pointOfIntersection + 7,
              y_pointOfIntersection - 7);
        }
        // Same side image line continuation
        g.setColor(Color.YELLOW);
        g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_1, arrow_y2);
        g.drawLine(
            x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_2, y_arcIntersection_2);
      }

      // Mag calculations
      height_image = 200 - y_pointOfIntersection;
      height_object = 200 - arrow_y2;
      if (height_object != 0) magnification = height_image / height_object;

      if (magnification <= 9999 && magnification >= -9999)
        Optics.txt_magnification.setText("" + roundTwoDecimals(magnification));
      else if (magnification > 9999) {
        magnification = Double.POSITIVE_INFINITY;
        Optics.txt_magnification.setText("N/A");
      } else {
        magnification = Double.NEGATIVE_INFINITY;
        Optics.txt_magnification.setText("N/A");
      }
      // Characteristics
      g.setColor(Color.ORANGE);
      g.drawString("Image Characteristics:", 20, 300);
      if (type == 0) {

        if ((Math.abs(magnification) > 1 && Math.abs(magnification) < 9999)
            && arrow_x != 500 - 10 * focalLength) g.drawString("Magnification:  Enlarged", 20, 320);
        else if ((int) (Math.abs(magnification)) == 1)
          g.drawString("Magnification:  None", 20, 320);
        else if (Math.abs(magnification) < 1 && Math.abs(magnification) > 0)
          g.drawString("Magnification:  Diminished", 20, 320);
        else {
          g.drawString("Magnification:  N/A", 20, 320);
          Optics.txt_magnification.setText("N/A");
          Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= N/A</html>");
          Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= N/A</html>");
        }
        if (arrow_x == 500 - 10 * focalLength) g.drawString("Orientation:      N/A", 20, 335);
        else if ((arrow_y2 < 200 && y_pointOfIntersection < 200)
            || (arrow_y2 > 200 && y_pointOfIntersection > 200))
          g.drawString("Orientation:      Upright", 20, 335);
        else g.drawString("Orientation:      Inverted", 20, 335);
        if (arrow_x == 500 - 10 * focalLength) g.drawString("Type:                 N/A", 20, 350);
        else if (x_pointOfIntersection < 500 && arrow_x < 500)
          g.drawString("Type:                 Real", 20, 350);
        else if (x_pointOfIntersection > 500 && arrow_x < 500)
          g.drawString("Type:                 Virtual", 20, 350);
      } else {
        g.drawString("Magnification:  Diminished", 20, 320);
        g.drawString("Orientation:      Upright", 20, 335);
        g.drawString("Type:                 Virtual", 20, 350);
      }

      height_image /= 10;

      if (height_image > 9999 || height_image < -9999 || arrow_x == 500 - 10 * focalLength)
        Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= N/A</html>");
      else Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= " + height_image + "</html>");
      if (type == 0) distance_image = x_pointOfIntersection - 500;
      else distance_image = x_pointOfIntersection - 350;
      distance_image /= 10;
      if (distance_image > 9999 || distance_image < -9999 || arrow_x == 500 - 10 * focalLength)
        Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= N/A</html>");
      else Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= " + distance_image + "</html>");
    }
  }
예제 #15
0
  protected void plotParameterPlane() {
    double x1 = x.getElement(0);
    double x2 = x.getElement(1);
    double dx1 = Math.sqrt(cx.getElement(0, 0));
    double dx2 = Math.sqrt(cx.getElement(1, 1));
    double rho = (cx.getElement(1, 0)) / (dx1 * dx2);
    // prepare size of plot
    double xmin = x1 - 2. * dx1;
    double xmax = x1 + 2. * dx1;
    double ymin = x2 - 2. * dx2;
    double ymax = x2 + 2. * dx2;
    DatanGraphics.openWorkstation(getClass().getName(), "E3Min_2.ps");
    DatanGraphics.setFormat(0., 0.);
    DatanGraphics.setWindowInComputingCoordinates(xmin, xmax, ymin, ymax);
    DatanGraphics.setViewportInWorldCoordinates(.2, .9, .16, .86);
    DatanGraphics.setWindowInWorldCoordinates(-.414, 1., 0., 1.);
    DatanGraphics.setBigClippingWindow();
    DatanGraphics.chooseColor(2);
    DatanGraphics.drawFrame();
    DatanGraphics.drawScaleX("x_1");
    DatanGraphics.drawScaleY("x_2");
    DatanGraphics.drawBoundary();
    DatanGraphics.chooseColor(5);
    // draw data point with errors (and correlation)
    DatanGraphics.drawDatapoint(1, 1., x1, x2, dx1, dx2, rho);
    DatanGraphics.chooseColor(2);
    DatanGraphics.drawCaption(1., caption);
    // draw confidence contour
    double fcont = mllg.getValue(x) + .5;
    int nx = 100;
    int ny = 100;
    double dx = (xmax - xmin) / (int) nx;
    double dy = (ymax - ymin) / (int) ny;
    MinLogLikeGaussCont mllgc = new MinLogLikeGaussCont();
    //      System.out.println(" x = " + x.toString() + ", mllgc.getValue(x) = " +
    // mllgc.getValue(x.getElement(0), x.getElement(1)));
    DatanGraphics.setBigClippingWindow();
    DatanGraphics.chooseColor(1);
    DatanGraphics.drawContour(xmin, ymin, dx, dy, nx, ny, fcont, mllgc);
    // draw asymmetric errors as horiyontal and vertical bars
    DatanGraphics.chooseColor(3);
    double[] xpl = new double[2];
    double[] ypl = new double[2];
    for (int i = 0; i < 2; i++) {
      if (i == 0) xpl[0] = x1 - dxasy[0][0];
      else xpl[0] = x1 + dxasy[0][1];
      xpl[1] = xpl[0];
      ypl[0] = ymin;
      ypl[1] = ymax;
      DatanGraphics.drawPolyline(xpl, ypl);
    }
    for (int i = 0; i < 2; i++) {
      if (i == 0) ypl[0] = x2 - dxasy[1][0];
      else ypl[0] = x2 + dxasy[1][1];
      ypl[1] = ypl[0];
      xpl[0] = xmin;
      xpl[1] = xmax;
      DatanGraphics.drawPolyline(xpl, ypl);
    }

    DatanGraphics.closeWorkstation();
  }
예제 #16
0
 public void updatebeta() {
   for (int i = 0; i <= ncurves; i++) {
     brightmincc[i] = (bright1[i] * beta) / Math.sqrt(intensity1[i] / intensity2[i]);
     eminccarray[i].setText("" + (float) brightmincc[i]);
   }
 }
예제 #17
0
 private void updateavg() {
   nmeas[ncurves] = 0;
   avg = new float[xpts][ypts];
   avgweights = new float[xpts][ypts];
   for (int i = 0; i < ncurves; i++) {
     if (include[i]) {
       for (int j = 0; j < xpts; j++) {
         for (int k = 0; k < ypts; k++) {
           avg[j][k] += pch[i][j][k];
           nmeas[ncurves] += (int) pch[i][j][k];
         }
       }
     }
   }
   double tempavg = 0.0;
   double tempavg2 = 0.0;
   double temp2avg = 0.0;
   double temp2avg2 = 0.0;
   double tempccavg = 0.0;
   for (int i = 0; i < xpts; i++) {
     for (int j = 0; j < ypts; j++) {
       double normed = (double) avg[i][j] / (double) nmeas[ncurves];
       avgweights[i][j] = (float) ((double) nmeas[ncurves] / (normed * (1.0f - normed)));
       if (avg[i][j] > 0.0f) {
         avgweights[i][j] = (float) ((double) nmeas[ncurves] / (normed * (1.0f - normed)));
       } else {
         avgweights[i][j] = 1.0f;
       }
       tempavg += (double) i * normed;
       tempavg2 += (double) i * (double) i * normed;
       temp2avg += (double) j * normed;
       temp2avg2 += (double) j * (double) j * normed;
       tempccavg += (double) i * (double) j * normed;
     }
   }
   tempccavg -= tempavg * temp2avg;
   brightcc[ncurves] = tempccavg / Math.sqrt(tempavg * temp2avg);
   tempavg2 -= tempavg * tempavg;
   tempavg2 /= tempavg;
   bright1[ncurves] = (tempavg2 - 1.0);
   temp2avg2 -= temp2avg * temp2avg;
   temp2avg2 /= temp2avg;
   bright2[ncurves] = (temp2avg2 - 1.0);
   intensity1[ncurves] = tempavg;
   intensity2[ncurves] = temp2avg;
   if (psfflag == 0) {
     bright1[ncurves] /= 0.3536;
     bright2[ncurves] /= 0.3536;
     brightcc[ncurves] /= 0.3536;
   } else {
     if (psfflag == 1) {
       bright1[ncurves] /= 0.078;
       bright2[ncurves] /= 0.078;
       brightcc[ncurves] /= 0.078;
     } else {
       bright1[ncurves] /= 0.5;
       bright2[ncurves] /= 0.5;
       brightcc[ncurves] /= 0.5;
     }
   }
   number1[ncurves] = intensity1[ncurves] / bright1[ncurves];
   number2[ncurves] = intensity2[ncurves] / bright2[ncurves];
   brightmincc[ncurves] =
       (bright1[ncurves] * beta) * Math.sqrt(intensity1[ncurves] / intensity2[ncurves]);
 }
예제 #18
0
파일: LJ3MDApp.java 프로젝트: eskilj/mvp
 public double getStd() {
   return Math.sqrt(getVar());
 }
예제 #19
0
 public void actionPerformed(ActionEvent ae) {
   String s = ae.getActionCommand();
   if (s.equals("Dodawanie")) {
     tekst = "";
     tekst2 = "";
     wynik = "";
     tekst += textField1.getText();
     wynik += "1";
     textField1.setText("");
   } else if (s.equals("Odejmowanie")) {
     tekst = "";
     tekst2 = "";
     wynik = "";
     tekst += textField1.getText();
     wynik += "2";
     textField1.setText("");
   } else if (s.equals("Mnozenie")) {
     tekst = "";
     tekst2 = "";
     wynik = "";
     tekst += textField1.getText();
     wynik += "3";
     textField1.setText("");
   } else if (s.equals("Dzielenie")) {
     tekst = "";
     tekst2 = "";
     wynik = "";
     tekst += textField1.getText();
     wynik += "4";
     textField1.setText("");
   } else if (s.equals("Potegowanie")) {
     tekst = "";
     tekst2 = "";
     wynik = "";
     tekst += textField1.getText();
     wynik += "5";
     textField1.setText("");
   } else if (s.equals("Pierwiastek")) {
     tekst = "";
     tekst2 = "";
     wynik = "";
     tekst += textField1.getText();
     int a = Integer.parseInt(tekst);
     f = (double) Math.sqrt(a);
     textField1.setText("" + zao.format(f));
   } else if (s.equals("Wynik")) {
     tekst2 += textField1.getText();
     int a = Integer.parseInt(tekst);
     int b = Integer.parseInt(tekst2);
     int w = Integer.parseInt(wynik);
     if (w == 1) {
       c = a + b;
       textField1.setText("" + c);
     }
     if (w == 2) {
       c = a - b;
       textField1.setText("" + c);
     }
     if (w == 3) {
       c = a * b;
       textField1.setText("" + c);
     }
     if (w == 4) {
       c = a / b;
       textField1.setText("" + c);
     }
     if (w == 5) {
       c = (int) Math.pow(a, b);
       textField1.setText("" + c);
     }
   } else if (s.equals("Czysc")) {
     tekst = "";
     tekst2 = "";
     wynik = "";
     textField1.setText("");
   }
 }
 /** Gets volatility for given instrument. */
 public double calculate(final Instrument instrument) {
   final double[] values = this.getValues(instrument);
   return Math.sqrt(this.variance.evaluate(values));
 }
예제 #21
0
 protected void compute() {
   n = (int) ni[0].parseInput();
   t0 = ni[1].parseInput();
   deltat = ni[2].parseInput();
   x1 = ni[3].parseInput();
   x2 = ni[4].parseInput();
   sigma = ni[5].parseInput();
   // generate data points
   t = new DatanVector(n);
   y = new DatanVector(n);
   dy = new DatanVector(n);
   rand = DatanRandom.standardNormal(n);
   for (int i = 0; i < n; i++) {
     t.setElement(i, t0 + (double) i * deltat);
     y.setElement(i, x1 * Math.pow(t.getElement(i), x2) + sigma * rand[i]);
     dy.setElement(i, sigma);
   }
   // find 1st approximation of unknowns by method of log-log plot
   tlog = new double[n];
   ylog = new double[n];
   dellog = new double[n];
   int npos = 0;
   for (int i = 0; i < n; i++) {
     if (t.getElement(i) > 0. && y.getElement(i) > 0.) {
       tlog[npos] = Math.log(t.getElement(i));
       ylog[npos] = Math.log(y.getElement(i));
       dellog[npos] = 1.;
       npos++;
     }
   }
   DatanVector vtlog = new DatanVector(npos);
   DatanVector vylog = new DatanVector(npos);
   DatanVector vdellog = new DatanVector(npos);
   for (int j = 0; j < npos; j++) {
     vtlog.setElement(j, tlog[j]);
     vylog.setElement(j, ylog[j]);
     vdellog.setElement(j, dellog[j]);
   }
   LsqPol lp = new LsqPol(vtlog, vylog, vdellog, 2);
   x = lp.getResult();
   x.setElement(0, Math.exp(x.getElement(0)));
   df.writeLine(" x = " + x.toString());
   // perform fit
   int[] list = {1, 1};
   powerlaw = new Powerlaw();
   LsqNon ln = new LsqNon(t, y, dy, x, list, powerlaw);
   x = ln.getResult();
   x1 = x.getElement(0);
   x2 = x.getElement(1);
   cov = ln.getCovarianceMatrix();
   delx1 = Math.sqrt(cov.getElement(0, 0));
   delx2 = Math.sqrt(cov.getElement(1, 1));
   rho = cov.getElement(1, 0) / (delx1 * delx2);
   m = ln.getChiSquare();
   p = 1. - StatFunct.cumulativeChiSquared(m, n - 1);
   // curve of fitted exponential
   xpl = new double[1001];
   ypl = new double[1001];
   dpl = (double) (n - 1) * deltat / 1000.;
   for (int i = 0; i < 1001; i++) {
     xpl[i] = t0 + (double) i * dpl;
     ypl[i] = x1 * Math.pow(xpl[i], x2);
   }
   // prepare data points for plotting
   datx = new double[n];
   daty = new double[n];
   datsx = new double[n];
   datsy = new double[n];
   datrho = new double[n];
   for (int i = 0; i < n; i++) {
     datx[i] = t.getElement(i);
     daty[i] = y.getElement(i);
     datsx[i] = 0.;
     datsy[i] = dy.getElement(i);
     datrho[i] = 0.;
   }
   // display data and fitted curve
   caption =
       "x_1#="
           + String.format(Locale.US, "%5.2f", x1)
           + ", x_2#="
           + String.format(Locale.US, "%5.2f", x2)
           + ", &D@x_1#="
           + String.format(Locale.US, "%5.2f", delx1)
           + ", &D@x_2#="
           + String.format(Locale.US, "%5.2f", delx2)
           + ", &r@="
           + String.format(Locale.US, "%5.2f", rho)
           + ", M="
           + String.format(Locale.US, "%5.2f", m)
           + ", P="
           + String.format(Locale.US, "%6.4f", p);
   new GraphicsWithDataPointsAndPolyline(
       getClass().getName(),
       "",
       xpl,
       ypl,
       1,
       .3,
       datx,
       daty,
       datsx,
       datsy,
       datrho,
       "t",
       "y",
       caption);
 }
  private double test(List baseline, List preferred, List gold) {
    double bs = 0, ps = 0;
    if (stat.toUpperCase().equals("F1")) {
      bs = new Evaluator(gold, baseline).getF1();
      ps = new Evaluator(gold, preferred).getF1();
    } else if (stat.toUpperCase().equals("P")) {
      bs = new Evaluator(gold, baseline).getPrecision();
      ps = new Evaluator(gold, preferred).getPrecision();
    } else if (stat.toUpperCase().equals("R")) {
      bs = new Evaluator(gold, baseline).getRecall();
      ps = new Evaluator(gold, preferred).getRecall();
    }
    double d = Math.abs(ps - bs);
    double mean = 0;
    double variance = 0;
    double sum = 0;
    double ssum = 0;
    logger.info(
        stat
            + ": original score bs, ps,d: "
            + formatter.format(bs * 100)
            + "%, "
            + formatter.format(ps * 100)
            + "%, "
            + formatter.format(d * 100)
            + "%");

    // p - p-value. In general, the lowest the p-value,
    // the less probable it is that that the null
    // hypothesis holds. That is, the two systems are
    // are significantly different.

    double p = 0;

    // c - number of times that the pseudostatistic is
    // greater or equal to the true statistic
    int c = 0;
    for (int i = 0; i < iterations; i++) {
      List baselineCopy = copy(baseline);
      List preferredCopy = copy(preferred);

      swap(baselineCopy, preferredCopy, new Random(i * 27));

      if (stat.toUpperCase().equals("F1")) {
        bs = new Evaluator(gold, baselineCopy).getF1();
        ps = new Evaluator(gold, preferredCopy).getF1();
      } else if (stat.toUpperCase().equals("P")) {
        bs = new Evaluator(gold, baselineCopy).getPrecision();
        ps = new Evaluator(gold, preferredCopy).getPrecision();
      } else if (stat.toUpperCase().equals("R")) {
        bs = new Evaluator(gold, baselineCopy).getRecall();
        ps = new Evaluator(gold, preferredCopy).getRecall();
      }

      double di = Math.abs(ps - bs);
      sum += di;
      ssum += Math.pow(di, 2);
      if (di >= d) {
        c++;

        // logger.info("score at " + i + " c, bs, ps,d: " + c + ", " + formatter.format(bs * 100) +
        // "%, " + formatter.format(ps * 100) + "%, " + formatter.format(di * 100) + "%, (" +
        // formatter.format(d * 100) + "%)");
      }
    } // end for i

    mean = sum / iterations;
    variance = (iterations * ssum - Math.pow(sum, 2)) / iterations * (iterations - 1);

    p = (double) (c + 1) / (iterations + 1);
    logger.info("mean " + formatter.format(mean) + ", " + formatter.format(Math.sqrt(variance)));
    logger.info(p + " = (" + c + " + 1) / (" + iterations + " +  1)");

    return p;
  } // end test
예제 #23
0
 public double getPerimeter() {
   return side1 + side2 + Math.sqrt(Math.pow(side1, 2) + Math.pow(side2, 2));
 }
예제 #24
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int row = in.nextInt();
    int col = in.nextInt();
    BigInteger r = in.nextBigInteger();

    //        System.out.println(row);
    //        System.out.println(col);
    //        System.out.println(r);

    int m[][] = new int[row][col];
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        m[i][j] = in.nextInt();
      }
    }

    //        for (int i = 0; i < row; i++) {
    //            for (int j = 0; j < col; j++) {
    //                System.out.print(m[i][j]);
    //            }
    //            System.out.println();
    //        }

    //        System.out.println(Math.ceil(Math.sqrt(col)));
    for (int n = 0; n < Math.ceil(Math.sqrt(col)); n++) {
      int max_row = row - n;
      int max_col = col - n;
      //            System.out.println("New array size: " + size);
      List<Integer> a = new ArrayList<Integer>();

      for (int i = n; i < max_row - 1; i++) {
        a.add(m[i][n]);
      }
      //            for (int i : a) {
      //                System.out.print(i);
      //            }
      //            System.out.println();

      for (int i = n; i < max_col - 1; i++) {
        a.add(m[max_row - 1][i]);
      }
      //            for (int i : a) {
      //                System.out.print(i);
      //            }
      //            System.out.println();

      for (int i = max_row - 1; i > n; i--) {
        a.add(m[i][max_col - 1]);
      }
      //            for (int i : a) {
      //                System.out.print(i);
      //            }
      //            System.out.println();

      for (int i = max_col - 1; i > n; i--) {
        a.add(m[n][i]);
      }
      //            for (int i : a) {
      //                System.out.print(i);
      //            }
      //            System.out.println();

      if (n == max_row - 1) {
        for (int i = n; i < max_col - n; i++) {
          a.add(m[n][i]);
        }
      }

      for (int i : a) {
        System.out.print(i);
      }
      System.out.println();
    }
  }
예제 #25
0
  /**
   * creates a new image
   *
   * @param svgHandle a svg handle
   * @param resourceId the id of the resource from which the image will be created
   */
  protected void createNewImage(SVGHandle svgHandle, String resourceId) {

    if (svgHandle != null && resourceId != null && !resourceId.equals("")) {

      Element resourceElement = null;

      resourceElement =
          svgHandle.getScrollPane().getSVGCanvas().getDocument().getElementById(resourceId);

      final String fresourceId = resourceId;

      if (resourceElement != null) {

        final SVGHandle fhandle = svgHandle;

        // creating the canvas and setting its properties
        final JSVGCanvas canvas =
            new JSVGCanvas() {

              @Override
              public void dispose() {

                removeKeyListener(listener);
                removeMouseMotionListener(listener);
                removeMouseListener(listener);
                disableInteractions = true;
                selectableText = false;
                userAgent = null;

                bridgeContext.dispose();
                super.dispose();
              }
            };

        // the element to be added
        Element elementToAdd = null;

        canvas.setDocumentState(JSVGComponent.ALWAYS_STATIC);
        canvas.setDisableInteractions(true);

        // creating the new document
        final String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
        final SVGDocument doc = (SVGDocument) resourceElement.getOwnerDocument().cloneNode(false);

        // creating the root element
        final Element root =
            (Element)
                doc.importNode(resourceElement.getOwnerDocument().getDocumentElement(), false);
        doc.appendChild(root);

        // removing all the attributes of the root element
        NamedNodeMap attributes = doc.getDocumentElement().getAttributes();

        for (int i = 0; i < attributes.getLength(); i++) {

          if (attributes.item(i) != null) {

            doc.getDocumentElement().removeAttribute(attributes.item(i).getNodeName());
          }
        }

        // adding the new attributes for the root
        root.setAttributeNS(null, "width", imageSize.width + "");
        root.setAttributeNS(null, "height", imageSize.height + "");
        root.setAttributeNS(null, "viewBox", "0 0 " + imageSize.width + " " + imageSize.height);

        // the defs element that will contain the cloned resource node
        final Element defs = (Element) doc.importNode(resourceElement.getParentNode(), true);
        root.appendChild(defs);

        if (resourceElement.getNodeName().equals("linearGradient")
            || resourceElement.getNodeName().equals("radialGradient")
            || resourceElement.getNodeName().equals("pattern")) {

          // the rectangle that will be drawn
          final Element rect = doc.createElementNS(svgNS, "rect");
          rect.setAttributeNS(null, "x", "0");
          rect.setAttributeNS(null, "y", "0");
          rect.setAttributeNS(null, "width", imageSize.width + "");
          rect.setAttributeNS(null, "height", imageSize.height + "");

          elementToAdd = rect;

          // setting that the rectangle uses the resource
          String id = resourceElement.getAttribute("id");

          if (id == null) {

            id = "";
          }

          rect.setAttributeNS(null, "style", "fill:url(#" + id + ");");

          // getting the cloned resource node
          Node cur = null;
          Element clonedResourceElement = null;
          String id2 = "";

          for (cur = defs.getFirstChild(); cur != null; cur = cur.getNextSibling()) {

            if (cur instanceof Element) {

              id2 = ((Element) cur).getAttribute("id");

              if (id2 != null && id.equals(id2)) {

                clonedResourceElement = (Element) cur;
              }
            }
          }

          if (clonedResourceElement != null) {

            // getting the root element of the initial resource
            // element
            Element initialRoot = resourceElement.getOwnerDocument().getDocumentElement();

            // getting the width and height of the initial root
            // element
            double initialWidth = 0, initialHeight = 0;

            try {
              initialWidth =
                  EditorToolkit.getPixelledNumber(initialRoot.getAttributeNS(null, "width"));
              initialHeight =
                  EditorToolkit.getPixelledNumber(initialRoot.getAttributeNS(null, "height"));
            } catch (DOMException ex) {
              ex.printStackTrace();
            }

            if (resourceElement.getNodeName().equals("linearGradient")) {

              if (resourceElement.getAttributeNS(null, "gradientUnits").equals("userSpaceOnUse")) {

                double x1 = 0, y1 = 0, x2 = 0, y2 = 0;

                // normalizing the values for the vector to fit
                // the rectangle
                try {
                  x1 = Double.parseDouble(resourceElement.getAttributeNS(null, "x1"));
                  y1 = Double.parseDouble(resourceElement.getAttributeNS(null, "y1"));
                  x2 = Double.parseDouble(resourceElement.getAttributeNS(null, "x2"));
                  y2 = Double.parseDouble(resourceElement.getAttributeNS(null, "y2"));

                  x1 = x1 / initialWidth * imageSize.width;
                  y1 = y1 / initialHeight * imageSize.height;
                  x2 = x2 / initialWidth * imageSize.width;
                  y2 = y2 / initialHeight * imageSize.height;
                } catch (NumberFormatException | DOMException ex) {
                  ex.printStackTrace();
                }

                clonedResourceElement.setAttributeNS(null, "x1", format.format(x1));
                clonedResourceElement.setAttributeNS(null, "y1", format.format(y1));
                clonedResourceElement.setAttributeNS(null, "x2", format.format(x2));
                clonedResourceElement.setAttributeNS(null, "y2", format.format(y2));
              }

            } else if (resourceElement.getNodeName().equals("radialGradient")) {

              if (resourceElement.getAttributeNS(null, "gradientUnits").equals("userSpaceOnUse")) {

                double cx = 0, cy = 0, r = 0, fx = 0, fy = 0;

                // normalizing the values for the circle to fit
                // the rectangle
                try {
                  cx = Double.parseDouble(resourceElement.getAttributeNS(null, "cx"));
                  cy = Double.parseDouble(resourceElement.getAttributeNS(null, "cy"));
                  r = Double.parseDouble(resourceElement.getAttributeNS(null, "r"));
                  fx = Double.parseDouble(resourceElement.getAttributeNS(null, "fx"));
                  fy = Double.parseDouble(resourceElement.getAttributeNS(null, "fy"));

                  cx = cx / initialWidth * imageSize.width;
                  cy = cy / initialHeight * imageSize.height;

                  r =
                      r
                          / (Math.abs(
                              Math.sqrt(Math.pow(initialWidth, 2) + Math.pow(initialHeight, 2))))
                          * Math.abs(
                              Math.sqrt(
                                  Math.pow(imageSize.width, 2) + Math.pow(imageSize.width, 2)));

                  fx = fx / initialWidth * imageSize.width;
                  fy = fy / initialHeight * imageSize.height;
                } catch (NumberFormatException | DOMException ex) {
                  ex.printStackTrace();
                }

                clonedResourceElement.setAttributeNS(null, "cx", format.format(cx));
                clonedResourceElement.setAttributeNS(null, "cy", format.format(cy));
                clonedResourceElement.setAttributeNS(null, "r", format.format(r));
                clonedResourceElement.setAttributeNS(null, "fx", format.format(fx));
                clonedResourceElement.setAttributeNS(null, "fy", format.format(fy));
              }

            } else if (resourceElement.getNodeName().equals("pattern")) {

              if (resourceElement.getAttributeNS(null, "patternUnits").equals("userSpaceOnUse")) {

                double x = 0, y = 0, w = 0, h = 0;

                // normalizing the values for the vector to fit
                // the rectangle
                try {
                  String xString = resourceElement.getAttributeNS(null, "x");
                  if (!xString.equals("")) {
                    x = Double.parseDouble(xString);
                  }
                  String yString = resourceElement.getAttributeNS(null, "y");
                  if (!yString.equals("")) {
                    y = Double.parseDouble(yString);
                  }
                  String wString = resourceElement.getAttributeNS(null, "w");
                  if (!wString.equals("")) {
                    w = Double.parseDouble(wString);
                  }
                  String hString = resourceElement.getAttributeNS(null, "h");
                  if (!hString.equals("")) {
                    h = Double.parseDouble(hString);
                  }

                  x = x / initialWidth * imageSize.width;
                  y = y / initialHeight * imageSize.height;
                  w = w / initialWidth * imageSize.width;
                  h = h / initialHeight * imageSize.height;
                } catch (NumberFormatException | DOMException ex) {
                  ex.printStackTrace();
                }

                clonedResourceElement.setAttributeNS(null, "x", format.format(x));
                clonedResourceElement.setAttributeNS(null, "y", format.format(y));
                clonedResourceElement.setAttributeNS(null, "width", format.format(w));
                clonedResourceElement.setAttributeNS(null, "height", format.format(h));
              }
            }
          }

        } else if (resourceElement.getNodeName().equals("marker")) {

          // the line that will be drawn
          final Element line = doc.createElementNS(svgNS, "line");
          line.setAttributeNS(null, "x1", (((double) imageSize.width) / 2) + "");
          line.setAttributeNS(null, "y1", (((double) imageSize.height) / 2) + "");
          line.setAttributeNS(null, "x2", ((double) imageSize.width / 2) + "");
          line.setAttributeNS(null, "y2", imageSize.height + "");

          elementToAdd = line;

          // setting that the line uses the resource
          String id = resourceElement.getAttribute("id");
          if (id == null) id = "";
          line.setAttributeNS(
              null, "style", "stroke:none;fill:none;marker-start:url(#" + id + ");");
        }

        root.appendChild(elementToAdd);

        // adding a rendering listener to the canvas
        GVTTreeRendererAdapter gVTTreeRendererAdapter =
            new GVTTreeRendererAdapter() {

              @Override
              public void gvtRenderingCompleted(GVTTreeRendererEvent evt) {

                Image bufferedImage = canvas.getOffScreen();

                if (bufferedImage != null) {

                  Graphics g = bufferedImage.getGraphics();
                  Color borderColor = MetalLookAndFeel.getSeparatorForeground();

                  g.setColor(borderColor);
                  g.drawRect(0, 0, imageSize.width - 1, imageSize.height - 1);
                }

                setImage(fhandle, fresourceId, bufferedImage);

                // refreshing the panels that have been created when no
                // image was available for them
                Image image = null;

                for (ResourceRepresentation resourceRepresentation :
                    new LinkedList<ResourceRepresentation>(resourceRepresentationList)) {

                  if (resourceRepresentation != null) {

                    resourceRepresentation.refreshRepresentation();
                    image = resourceRepresentation.getImage();

                    if (image != null) {

                      resourceRepresentationList.remove(resourceRepresentation);
                    }
                  }
                }

                canvas.removeGVTTreeRendererListener(this);
                canvas.stopProcessing();
                canvas.dispose();
              }
            };

        canvas.addGVTTreeRendererListener(gVTTreeRendererAdapter);

        // setting the document for the canvas
        canvas.setSVGDocument(doc);

        canvas.setBackground(Color.white);
        canvas.setBounds(1, 1, imageSize.width, imageSize.height);
      }
    }
  }
예제 #26
0
 /**
  * @param y data
  * @param k parameter giving the length 2 * k + 1 of the averaging interval
  * @param l oder of the averaging polynomial
  * @param p probability defining confidence limits
  */
 public TimeSeries(double[] y, int k, int l, double p) {
   this.y = y;
   this.k = k;
   this.l = l;
   this.p = p;
   n = y.length;
   eta = new double[n + 2 * k];
   coneta = new double[n + 2 * k];
   // quantile of Student's distribution
   pprime = 0.5 * (p + 1.);
   nf = 2 * k - l;
   talpha = StatFunct.quantileStudent(pprime, nf);
   // compute matrices depending on k and l
   k21 = 2 * k + 1;
   l1 = l + 1;
   a = new DatanMatrix(k21, l1);
   for (int i = 0; i < k21; i++) {
     for (int j = 0; j < l1; j++) {
       if (j == 0) a.setElement(i, j, -1.);
       else a.setElement(i, j, a.getElement(i, j - 1) * (double) (i - k));
     }
   }
   ata1 = a.multiplyTransposedWith(a);
   ata1.choleskyInversion();
   ata1at = ata1.multiplyWithTransposed(a);
   ata1at = ata1at.multiply(-1.);
   // moving averages and confidence limits for inner part
   ymat = new DatanMatrix(y);
   for (int i = 2 * k; i < n; i++) {
     ytmp = ymat.getSubmatrix(k21, 1, i - 2 * k, 0);
     x = ata1at.multiply(ytmp);
     eta[i] = x.getElement(0, 0);
     etatmp = a.multiply(x);
     etatmp = etatmp.add(ytmp);
     sy2 = etatmp.multiplyTransposedWith(etatmp);
     double s = sy2.getElement(0, 0) / (double) nf;
     double a0 = Math.sqrt(Math.abs(ata1.getElement(0, 0)));
     coneta[i] = a0 * Math.sqrt(s) * talpha;
     // moving averages and confidence limits for end sections
     if (i == 2 * k || i == n - 1) {
       tt = new double[l + 1];
       if (i == 2 * k) {
         iadd = 2 * k;
         is = -1;
       } else {
         iadd = n - 1;
         is = 1;
       }
       for (int i1 = 1; i1 <= 2 * k; i1++) {
         j = is * i1;
         for (int i2 = 0; i2 < l + 1; i2++) {
           for (int i3 = 0; i3 <= i2; i3++) {
             if (i3 == 0) tt[i2] = 1.;
             else tt[i2] = tt[i2] * (double) j;
           }
         }
         tmat = new DatanMatrix(tt);
         seta2 = tmat.multiplyTransposedWith(ata1.multiply(tmat));
         double se2 = s * seta2.getElement(0, 0);
         etai = tmat.multiplyTransposedWith(x);
         eta[iadd + j] = etai.getElement(0, 0);
         coneta[iadd + j] = Math.sqrt(Math.abs(se2)) * talpha;
       }
     }
   }
 }
예제 #27
0
 double calc_dist(Vec p2) {
   Vec p1 = this;
   return Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
 }
예제 #28
0
 double helper(long a, long b) {
   return 2 * Math.sqrt(a * b);
 }