Beispiel #1
0
 /**
  * Correct all pointers that point behind a new entry.
  *
  * @param startOffset the startoffset of the current node
  * @param oldOffset the offset of the new entry, only pointer that point behind it need to
  *     correct.
  * @param diff the differenz that need added to the pointers
  * @param level the stack level. There are only 3 levels with pointers.
  */
 private final void correctPointers(int startOffset, int oldOffset, int diff, int level) {
   offset = startOffset;
   boolean firstNode = true;
   while (offset < size) {
     if (offset == oldOffset) {
       int absDiff = Math.abs(diff);
       if (absDiff == 2) return;
       offset += absDiff;
       firstNode = false;
       continue;
     }
     int value = getUnsignedShort();
     if (value != 0 || firstNode) {
       int pointer = getPointer();
       if (pointer > oldOffset) {
         offset -= pointerSize;
         writePointer(pointer + diff);
         if (diff > 0) pointer += diff;
       }
       if (level < 2) {
         startOffset = offset;
         correctPointers(pointer, oldOffset, diff, level + 1);
         offset = startOffset;
       }
       firstNode = false;
     } else {
       return;
     }
   }
 }
Beispiel #2
0
  /**
   * Compare approximate and exact diameter computation. Approximate is very fast but provides only
   * a rough upper bound. For a discretized pairwise distance distribution, this is absolutely
   * sufficient.
   */
  public void testMaxPhaseSpaceDiameter() {

    int tsLen = 25000;
    TimeSeriesGenerator tsg = new TimeSeriesGenerator(1, tsLen, "lorenz");
    double[][][][] trajectories = tsg.getAllTrajectories();
    double[][] ts = trajectories[4][0];
    System.out.println("Computing max phase space diameter.");
    long tic = System.currentTimeMillis();
    double diam = PhaseSpaceDistribution.maxPhaseSpaceDiameter(ts, ts);
    System.out.println("diam = " + diam);
    long toc = System.currentTimeMillis();
    System.out.println(toc - tic);

    System.out.println("Computing max phase space diameter, approximate.");
    tic = System.currentTimeMillis();
    double diamApprox = PhaseSpaceDistribution.maxPhaseSpaceDiameterApproximate(ts);
    System.out.println("diamApprox = " + diamApprox);
    toc = System.currentTimeMillis();
    System.out.println(toc - tic);
    System.out.println("Relative error: " + (Math.abs(diamApprox - diam) / diam));

    //        GeometryFactory factory = new GeometryFactory(new
    // PrecisionModel(PrecisionModel.FIXED));
    //        Point[] points = new Point[tsLen];
    //        for (int i = 0; i < tsLen; i++) {
    //            Coordinate[] coordinates = new Coordinate[ts.length];
    //            for (int j = 0; j < ts.length; j++) {
    //                coordinates[j] = new Co
    //            }
    //            points[i] = new Point(new CoordinateArraySequence(coordinates), factory);
    //        }
    //        MultiPoint data = new MultiPoint(points, factory);
    //        MinimumDiameter minimumDiameter = new MinimumDiameter(geom);

  }
Beispiel #3
0
  public boolean equalRecords(Object a, Object b) {
    if (a instanceof java.sql.Timestamp && b instanceof java.sql.Timestamp) {
      // hack here, since different DB realization;
      long diff = ((Timestamp) a).getTime() - ((Timestamp) b).getTime();
      // if more than N milliseconds;
      if (Math.abs(diff) < stampPrecision) {
        return true;
      }
    }

    return a.equals(b);
  }
Beispiel #4
0
  public static void main(String[] args) {
    //////////////////////////
    // The values in following 4 lines should be user input
    int startPos = 200;
    int endPos = 1000;
    int totalNumPixel = 1044;
    double threshhold = 0.0001;
    /////////////////////////
    int numPixel = endPos - startPos;
    double wavelength[] = new double[totalNumPixel];
    double photonCount[] = new double[totalNumPixel];
    double SpecNoBk[] = new double[numPixel];
    double ThisSpectrum[] = new double[numPixel];
    double ThisXaxis[] = new double[numPixel];
    double Po[] = new double[numPixel];
    double Re[] = new double[numPixel];
    double P[] = new double[6];
    double Re2[] = new double[numPixel - 1];
    double mySUM[] = new double[numPixel];
    int ind[];
    double DEV;
    double prevDEV;
    Connection connection = null;
    Statement stmt = null;
    String pattern = "##.##";

    try {
      Scanner in = new Scanner(new FileReader(args[0]));
      int i = 0;
      while (in.hasNextDouble()) {
        wavelength[i] = in.nextDouble();
        photonCount[i] = in.nextDouble();
        ++i;
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    ThisSpectrum = Arrays.copyOfRange(photonCount, startPos, endPos);
    ThisXaxis = Arrays.copyOfRange(wavelength, startPos, endPos);
    final WeightedObservedPoints obs = new WeightedObservedPoints();

    for (int i = 0; i < numPixel; i++) {
      obs.add(ThisXaxis[i], ThisSpectrum[i]);
    }

    final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(5);
    P = fitter.fit(obs.toList());
    Polyval pVal = new Polyval(P, ThisXaxis, numPixel);
    Po = pVal.evl();

    for (int i = 0; i < numPixel; i++) {
      Re[i] = ThisSpectrum[i] - Po[i];
    }

    for (int i = 0; i < numPixel - 1; i++) {
      Re2[i] = Re[i + 1] - Re[i];
    }

    DEV = Math.sqrt(StatUtils.populationVariance(Re2, 0, Re2.length));
    for (int i = 0; i < numPixel; i++) {
      mySUM[i] = Po[i] + DEV;
    }

    int jj = 0; // jj is the length of points to be removed
    for (int i = 0; i < numPixel; i++) {
      if (ThisSpectrum[i] > mySUM[i]) {
        jj++;
        ;
      }
    }
    ind = new int[jj];

    int jjj = 0;
    for (int i = 0; i < numPixel; i++) {
      if (ThisSpectrum[i] > mySUM[i]) {
        ind[jjj] = i;
        jjj++;
      }
    }

    int indKeepLength = numPixel - ind.length;
    int indKeep[] = new int[indKeepLength];
    int k = 0;
    for (int i = 0; i < numPixel; i++) {
      if (!ArrayUtils.contains(ind, i)) {
        indKeep[k] = i;
        k++;
      }
    }
    double ThisSpectrumKeep[] = new double[indKeepLength];
    double ThisXaxisKeep[] = new double[indKeepLength];
    double PoKeep[] = new double[indKeepLength];
    double ReKeep[] = new double[indKeepLength];
    double Re2Keep[] = new double[indKeepLength - 1];
    double mySUMKeep[] = new double[indKeepLength];

    for (int i = 0; i < indKeepLength; i++) {
      ThisSpectrumKeep[i] = ThisSpectrum[indKeep[i]];
      ThisXaxisKeep[i] = ThisXaxis[indKeep[i]];
    }

    prevDEV = DEV;

    // at the point, ThisSpectrum and ThisXaxis should have reduced size
    final WeightedObservedPoints obs1 = new WeightedObservedPoints();

    for (int i = 0; i < indKeepLength; i++) {
      obs1.add(ThisXaxisKeep[i], ThisSpectrumKeep[i]);
    }

    while (true) {
      final PolynomialCurveFitter fitter1 = PolynomialCurveFitter.create(5);
      P = fitter1.fit(obs1.toList());
      Polyval pVal1 = new Polyval(P, ThisXaxisKeep, indKeepLength);
      PoKeep = pVal1.evl();

      for (int i = 0; i < indKeepLength; i++) {
        ReKeep[i] = ThisSpectrumKeep[i] - PoKeep[i];
      }

      for (int i = 0; i < indKeepLength - 1; i++) {
        Re2Keep[i] = ReKeep[i + 1] - ReKeep[i];
      }

      DEV = Math.sqrt(StatUtils.populationVariance(Re2Keep, 0, Re2Keep.length));

      for (int i = 0; i < indKeepLength; i++) {
        mySUMKeep[i] = PoKeep[i] + DEV;
      }

      for (int i = 0; i < indKeepLength; i++) {
        if (ThisSpectrumKeep[i] > mySUMKeep[i]) ThisSpectrumKeep[i] = mySUMKeep[i];
      }
      if ((Math.abs(DEV - prevDEV) / DEV) < threshhold) break;
      prevDEV = DEV;

      obs1.clear();
      for (int i = 0; i < indKeepLength; i++) {
        obs1.add(ThisXaxisKeep[i], ThisSpectrumKeep[i]);
      }
    }
    Polyval pVal2 = new Polyval(P, ThisXaxis, numPixel);
    double FLbk[] = pVal2.evl();
    for (int i = 0; i < ThisXaxis.length; i++) {
      SpecNoBk[i] = ThisSpectrum[i] - FLbk[i];
    }

    // the write-to-file part is only for testing purpose, ThisXaxis and SpecNoBk are two outputs
    try {
      FileWriter fr = new FileWriter(args[1]);
      BufferedWriter br = new BufferedWriter(fr);
      PrintWriter out = new PrintWriter(br);
      DecimalFormat df = new DecimalFormat(pattern);
      for (int j = 0; j < ThisXaxis.length; j++) {
        if (Double.toString(wavelength[j]) != null) out.write(ThisXaxis[j] + "\t" + SpecNoBk[j]);
        out.write("\r\n");
      }
      out.close();
    } catch (IOException e) {
      System.out.println(e);
    }
  }
Beispiel #5
0
  private void jButton3ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton3ActionPerformed
    jTextArea1.setText("" + "Termset\n\t\t");
    int c = 0;
    String[] tn = new String[50];
    int i1 = 0;
    float v1 = 0;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection con =
          DriverManager.getConnection("jdbc:mysql://localhost:3306/txt mine", "root", "root");
      Statement st = con.createStatement();
      Statement st1 = con.createStatement();
      // ResultSet rs=st.executeQuery("");
      ResultSet rs = st.executeQuery("SELECT DISTINCT `sumwt`,`tname` FROM `totwet`");
      while (rs.next()) {
        c = c + rs.getInt(1);
        tn[i1] = rs.getString(2);
        i1++;
      }
      for (int i = 0; i < i1; i++) {
        st1.executeUpdate(
            "UPDATE `totwet` SET `nd`='" + c + "'" + " WHERE `tname`='" + tn[i] + "'");
      }

      System.out.println("SUM OF THE VALUE IS:" + c);
      for (int i = 0; i < i1; i++) {
        jTextArea1.append("(" + tna[i] + "," + sumt[i] + "/" + c + ")\n\t\t");
      }
      jTextArea1.append("\n Offender\n\t\t");
      for (int i = 0; i < k; i++) {
        jTextArea1.append(rwt[i]);
        st.executeUpdate("INSERT INTO `txt mine`.`offender` (`nd`) VALUES ('" + rwt[i] + "')");
      }
      jTextArea1.append("\n\n Experimental Coefficient:" + 2);
      ResultSet rs1 = st.executeQuery("SELECT DISTINCT `nd`FROM `offender`");
      while (rs1.next()) {
        ResultSet rs2 =
            st1.executeQuery(
                "SELECT DISTINCT `sumwt`,`nd` FROM `totwet` WHERE `tname`='"
                    + rs1.getString(1)
                    + "'");
        while (rs2.next()) {
          v1 += (float) rs2.getInt(1) / (float) rs2.getInt(2);
        }
      }

      float res = val * v1;
      System.out.println("offering:" + res);
      double n = res;

      denominator =
          (int)
              (1
                  / (Math.abs(
                      n - (int) n
                          - 0.0001))); // - 0.0001 so the division doesn't get affected by the float
      // point aproximated representation
      int units = (int) n;

      numerator = units * denominator + 1;

      System.out.println("" + numerator + "/" + denominator);
      JOptionPane.showMessageDialog(this, "Suffle Process is done");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    // TODO add your handling code here:
  } // GEN-LAST:event_jButton3ActionPerformed
  private String processRequest(
      HttpServletRequest request, HttpServletResponse response, ConnectionPool conPool)
      throws Exception {
    // getting id parameters
    ParameterParser parameter = new ParameterParser(request);
    String bookID = parameter.getString(bookInterface.FIELD_ID, null);
    String sellerID = parameter.getString(bookInterface.FIELD_SELLERID, null);
    String message = parameter.getString(FIELD_MESSAGE, null);
    int codeID = parameter.getInt(bookInterface.FIELD_HIDDENID, 0);
    // get buyer's user object
    User user = (User) request.getSession().getAttribute(StringInterface.USERATTR);
    // security feauture:
    // if one of ids is missing or incorrect return false
    if (bookID == null
        || sellerID == null
        || codeID == 0
        || bookID.length() != booksTable.ID_LENGTH
        || sellerID.length() != usersTable.ID_LENGTH
        || codeID != Math.abs(bookID.hashCode())) {
      return "We were unable to find the book you specified! Please make sure that the book id is correct.";
    }
    if (user.getID().equals(sellerID)) {
      return "You may not purchase an item from yourself!";
    }
    // get connection
    Connection con = conPool.getConnection();
    try {
      booksTable book = generalUtils.getBook(bookID, con);
      /*security feauture:
       *check seller id == passed hidden id
       *book != null
       */
      if (book == null || !book.getSellerID().equals(sellerID)) {
        return "We were unable to find the book you specified! Please make sure that the book id is correct.";
      }
      usersTable sellerInfo = userUtils.getUserInfo(sellerID, con);
      usersTable buyerInfo = userUtils.getUserInfo(user.getID(), con);
      collegeTable college = getCollege(book.getCollegeID() + "", con);
      // if still here continue
      if (message == null) {
        request.setAttribute(ATTR_BOOK, book);
        request.setAttribute(ATTR_SELLER, sellerInfo);
        request.setAttribute(ATTR_BUYER, buyerInfo);
        request.setAttribute(ATTR_COLLEGE, college.getFull());
        RequestDispatcher rd = getServletContext().getRequestDispatcher(PATH_BUY_CONFIRM);
        rd.include(request, response);
        return null;
      } else if (buy(book, user, con)) {
        // sending email to buyer
        request.setAttribute(mailInterface.USERATTR, buyerInfo.getUsername());
        request.setAttribute(mailInterface.EMAILATTR, buyerInfo.getEmail());
        request.setAttribute(mailInterface.BOOKATTR, book);
        request.setAttribute("book_id", bookID);
        request.setAttribute("seller_id", sellerID);

        RequestDispatcher rd = getServletContext().getRequestDispatcher(PATHBIDCONFIRMATION);
        rd.include(request, response);
        // sending email to seller
        request.setAttribute(ATTR_COLLEGE, college.getFull());
        request.setAttribute(mailInterface.USERATTR, sellerInfo.getUsername());
        request.setAttribute(mailInterface.EMAILATTR, sellerInfo.getEmail());
        request.setAttribute(mailInterface.MESSAGEATTR, message);
        request.setAttribute(mailInterface.BOOKATTR, book);
        request.setAttribute(mailInterface.MOREATTR, buyerInfo);

        request.setAttribute("book_id", bookID);
        request.setAttribute("buyer_id", user.getID());
        rd = getServletContext().getRequestDispatcher(PATHBOOKUPDATE);
        rd.include(request, response);
        // showing success message
        rd = getServletContext().getRequestDispatcher(PATH_BUY_SUCCESS);
        rd.include(request, response);
        return null;
      } else {
        throw new Exception("failed to process with buy");
      }
    } catch (Exception e) {
      throw e;
    } finally {
      // recycle
      conPool.free(con);
      con = null;
    }
  }