Beispiel #1
0
 public static boolean equalsWithinOneSmallUlp(double a, double b) {
   double ulp_a = Math.ulp(a);
   double ulp_b = Math.ulp(b);
   double small_ulp = Math.min(ulp_a, ulp_b);
   double absdiff_a_b = Math.abs(a - b); // subtraction order does not matter, due to IEEE 754 spec
   return absdiff_a_b <= small_ulp;
 }
  public void testGetCoordinatesBetween() {

    try {
      // what if the null value is passed
      CoordinateSequence[] cs = nullLine.getCoordinatesBetween(0.0, 5.0);
      assertTrue("cs.length = " + cs.length + ". Should be 1", cs.length == 1);
      assertEquals(cs[0].size(), 0);

      arbitraryLine.measureOnLength(false);
      // what if from/to is outside of the range of values
      double maxM = arbitraryLine.getMaxM();
      cs = arbitraryLine.getCoordinatesBetween(maxM + 1.0, maxM + 10.0);

      // check for several ascending M-values
      int minIdx = (int) (Math.random() * (arbitraryLine.getNumPoints() - 1));
      int maxIdx = Math.min((arbitraryLine.getNumPoints() - 1), minIdx + 10);
      double minM = ((MCoordinate) arbitraryLine.getCoordinateN(minIdx)).m;
      maxM = ((MCoordinate) arbitraryLine.getCoordinateN(maxIdx)).m;
      cs = arbitraryLine.getCoordinatesBetween(minM, maxM);
      assertNotNull(cs);
      assertTrue(cs.length > 0);
      Coordinate[] coar = cs[0].toCoordinateArray();
      int j = 0;
      for (int i = minIdx; i <= maxIdx; i++) {
        assertEquals((MCoordinate) arbitraryLine.getCoordinateN(i), coar[j]);
        j++;
      }

      minM = Math.max(0.0, minM - Math.random() * 10);
      cs = arbitraryLine.getCoordinatesBetween(minM, maxM);
      coar = cs[0].toCoordinateArray();
      MCoordinate mctest = (MCoordinate) coar[0];
      MCoordinate mcexp = (MCoordinate) arbitraryLine.getCoordinateAtM(minM);
      assertEquals(mcexp, mctest);
      assertEquals(mctest.m, minM, DoubleComparator.defaultNumericalPrecision());

      maxM = Math.min(arbitraryLine.getLength(), maxM + Math.random() * 10);
      cs = arbitraryLine.getCoordinatesBetween(minM, maxM);
      coar = cs[0].toCoordinateArray();
      mctest = (MCoordinate) coar[coar.length - 1];
      mcexp = (MCoordinate) arbitraryLine.getCoordinateAtM(maxM);
      assertEquals(mcexp.x, mctest.x, Math.ulp(mcexp.x) * 100);
      assertEquals(mcexp.y, mctest.y, Math.ulp(mcexp.y) * 100);
      assertEquals(mctest.m, maxM, DoubleComparator.defaultNumericalPrecision());

    } catch (Exception e) {
      e.printStackTrace();
      assertTrue(false); // should never reach here
    }
  }
  /** @tests java.lang.StrictMath#ulp(float) */
  @SuppressWarnings("boxing")
  public void test_ulp_f() {
    // Test for special cases
    assertTrue("Should return NaN", Float.isNaN(StrictMath.ulp(Float.NaN)));
    assertEquals(
        "Returned incorrect value",
        Float.POSITIVE_INFINITY,
        StrictMath.ulp(Float.POSITIVE_INFINITY),
        0f);
    assertEquals(
        "Returned incorrect value",
        Float.POSITIVE_INFINITY,
        StrictMath.ulp(Float.NEGATIVE_INFINITY),
        0f);
    assertEquals("Returned incorrect value", Float.MIN_VALUE, StrictMath.ulp(0.0f), 0f);
    assertEquals("Returned incorrect value", Float.MIN_VALUE, StrictMath.ulp(+0.0f), 0f);
    assertEquals("Returned incorrect value", Float.MIN_VALUE, StrictMath.ulp(-0.0f), 0f);
    assertEquals("Returned incorrect value", 2.028241E31f, StrictMath.ulp(Float.MAX_VALUE), 0f);
    assertEquals("Returned incorrect value", 2.028241E31f, StrictMath.ulp(-Float.MAX_VALUE), 0f);

    assertEquals("Returned incorrect value", 1.4E-45f, StrictMath.ulp(Float.MIN_VALUE), 0f);
    assertEquals("Returned incorrect value", 1.4E-45f, StrictMath.ulp(-Float.MIN_VALUE), 0f);

    assertEquals("Returned incorrect value", 1.1920929E-7f, StrictMath.ulp(1.0f), 0f);
    assertEquals("Returned incorrect value", 1.1920929E-7f, StrictMath.ulp(-1.0f), 0f);
    assertEquals("Returned incorrect value", 1.2207031E-4f, StrictMath.ulp(1153.0f), 0f);
    assertEquals("Returned incorrect value", 5.6E-45f, Math.ulp(9.403954E-38f), 0f);
  }
Beispiel #4
0
 public void testLog2Accuracy() {
   for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
     double dmLog2 = DoubleMath.log2(d);
     double trueLog2 = trueLog2(d);
     assertTrue(Math.abs(dmLog2 - trueLog2) <= Math.ulp(trueLog2));
   }
 }
Beispiel #5
0
 public void testFactorial() {
   for (int i = 0; i <= DoubleMath.MAX_FACTORIAL; i++) {
     double actual = BigIntegerMath.factorial(i).doubleValue();
     double result = DoubleMath.factorial(i);
     assertEquals(actual, result, Math.ulp(actual));
   }
 }
 /** {@inheritDoc} */
 public OpenMapRealVector mapUlpToSelf() {
   Iterator iter = entries.iterator();
   while (iter.hasNext()) {
     iter.advance();
     entries.put(iter.key(), Math.ulp(iter.value()));
   }
   return this;
 }
Beispiel #7
0
 private void makeClipsValid() {
   if (_clipMin >= _clipMax) {
     double clipAvg = 0.5 * (_clipMin + _clipMax);
     double tiny = max(1.0, Math.ulp(1.0f) * abs(clipAvg));
     _clipMin -= tiny;
     _clipMax += tiny;
   }
 }
  @Test
  public void testResultValue03() throws Exception {
    Log10Function function = new Log10Function();
    function.addArgument(new ValueFunctionArgument("1000"));

    assertTrue(function.parse());

    assertEquals(3.0, function.getInternalValue(), Math.ulp(1.0));
  }
  public void testGetCoordinateAtM() {
    // what if null string
    try {
      Coordinate mc = nullLine.getCoordinateAtM(2);
      assertNull(mc);

      // get two neighbouring points along the arbitraryline
      arbitraryLine.measureOnLength(false);
      int elem1Indx = (int) (Math.random() * (arbitraryLine.getNumPoints() - 1));
      int elem2Indx = 0;
      if (elem1Indx == arbitraryLine.getNumPoints() - 1) {
        elem2Indx = elem1Indx - 1;
      } else {
        elem2Indx = elem1Indx + 1;
      }

      // if m is value of a coordinate, the returned coordinate should
      // equal that coordinate

      MCoordinate mco1 = (MCoordinate) arbitraryLine.getCoordinateN(elem1Indx);
      MCoordinate mcotest = (MCoordinate) arbitraryLine.getCoordinateAtM(mco1.m);
      assertNotSame(mco1, mcotest);
      assertEquals(mco1.x, mcotest.x, Math.ulp(100 * mco1.x));
      assertEquals(mco1.y, mcotest.y, Math.ulp(100 * mco1.y));
      assertEquals(mco1.m, mcotest.m, Math.ulp(100 * mco1.m));

      MCoordinate mco2 = (MCoordinate) arbitraryLine.getCoordinateN(elem2Indx);
      double offset = Math.random();
      double newM = mco1.m + offset * (mco2.m - mco1.m);
      MCoordinate mcexp =
          new MCoordinate(
              mco1.x + offset * (mco2.x - mco1.x),
              mco1.y + offset * (mco2.y - mco1.y),
              Double.NaN,
              mco1.m + offset * (mco2.m - mco1.m));
      MCoordinate mctest = (MCoordinate) arbitraryLine.getCoordinateAtM(newM);
      assertEquals(mcexp.x, mctest.x, 0.0001);
      assertEquals(mcexp.y, mctest.y, 0.0001);
      assertEquals(mcexp.m, mctest.m, 0.0001);

    } catch (Exception e) {
      System.err.println(e);
    }
  }
Beispiel #10
0
 public static int checkedFCMPL(float a, float b, int reaction, String logFileName) {
   if (Float.isNaN(a) || Float.isNaN(b)) return -1;
   int r = a == b ? 0 : a < b ? -1 : 1;
   if (a == 2.0f * a || b == 2.0f * b) // means here: isInfinite(a) (can't be 0 on NaN)
   return r;
   if (r != 0 && Math.abs(a - b) <= CLOSENESS_ULP_FACTOR_FLOAT * Math.ulp(a)) {
     Reactions.react(reaction, VERY_CLOSE_MSG + "FCMP", logFileName);
   }
   return r;
 }
Beispiel #11
0
 private Histo(double lb, double ub, double start_row, double nrows, boolean isInt) {
   boolean is_int = (isInt && (ub - lb < NBINS));
   _nbins = is_int ? (int) (ub - lb + 1) : NBINS;
   _lb = lb;
   double ulp = Math.ulp(Math.max(Math.abs(lb), Math.abs(ub)));
   _step = is_int ? 1 : (ub + ulp - lb) / _nbins;
   _start_row = start_row;
   _nrows = nrows;
   _isInt = isInt;
 }
  /** {@inheritDoc} */
  public int getRank() throws IllegalStateException {

    final double threshold = Math.max(m, n) * Math.ulp(singularValues[0]);

    for (int i = singularValues.length - 1; i >= 0; --i) {
      if (singularValues[i] > threshold) {
        return i + 1;
      }
    }
    return 0;
  }
Beispiel #13
0
  public static float checkedFREM(float a, float b, int reaction, String logFileName) {
    float r = a % b;

    if (a == a && b == b && r != r) {
      Reactions.react(reaction, RESULT_IS_NAN_MSG + "FREM", logFileName);
    }
    if (Math.ulp(a) > Math.abs(b)) {
      Reactions.react(reaction, PRECISION_MSG + "FREM", logFileName);
    }

    return r;
  }
 private void init() {
   if (radiusDEG > 90) {
     // --spans more than half the globe
     assert enclosingBox.getWidth() == 360;
     double backDistDEG = 180 - radiusDEG;
     if (backDistDEG > 0) {
       double backRadius = 180 - radiusDEG;
       double backX = DistanceUtils.normLonDEG(getCenter().getX() + 180);
       double backY = DistanceUtils.normLatDEG(getCenter().getY() + 180);
       // Shrink inverseCircle as small as possible to avoid accidental overlap.
       // Note that this is tricky business to come up with a value small enough
       // but not too small or else numerical conditioning issues become a problem.
       backRadius -=
           Math.max(
               Math.ulp(Math.abs(backY) + backRadius), Math.ulp(Math.abs(backX) + backRadius));
       if (inverseCircle != null) {
         inverseCircle.reset(backX, backY, backRadius);
       } else {
         inverseCircle = new GeoCircle(ctx.makePoint(backX, backY), backRadius, ctx);
       }
     } else {
       inverseCircle = null; // whole globe
     }
     horizAxisY = getCenter().getY(); // although probably not used
   } else {
     inverseCircle = null;
     double _horizAxisY =
         ctx.getDistCalc().calcBoxByDistFromPt_yHorizAxisDEG(getCenter(), radiusDEG, ctx);
     // some rare numeric conditioning cases can cause this to be barely beyond the box
     if (_horizAxisY > enclosingBox.getMaxY()) {
       horizAxisY = enclosingBox.getMaxY();
     } else if (_horizAxisY < enclosingBox.getMinY()) {
       horizAxisY = enclosingBox.getMinY();
     } else {
       horizAxisY = _horizAxisY;
     }
     // assert enclosingBox.relate_yRange(horizAxis,horizAxis,ctx).intersects();
   }
 }
Beispiel #15
0
 @Test
 public void testReduceComparedWithNormalizeAngle() {
   final double tol = Math.ulp(1d);
   final double period = 2 * Math.PI;
   for (double a = -15; a <= 15; a += 0.5) {
     for (double center = -15; center <= 15; center += 1) {
       final double nA = MathUtils.normalizeAngle(a, center);
       final double offset = center - Math.PI;
       final double r = MathUtils.reduce(a, period, offset);
       Assert.assertEquals(nA, r + offset, tol);
     }
   }
 }
  /**
   * Checks that the value of the integral of each monomial <code>x<sup>0</sup>, ... , x<sup>p</sup>
   * </code> returned by the quadrature rule under test conforms with the expected value. Here
   * {@code p} denotes the degree of the highest polynomial for which exactness is to be expected.
   */
  @Test
  public void testAllMonomials() {
    for (int n = 0; n <= maxDegree; n++) {
      final double expected = getExpectedValue(n);

      final Power monomial = new Power(n);
      final double actual = integrator.integrate(monomial);

      // System.out.println(n + "/" + maxDegree + " " + integrator.getNumberOfPoints()
      //                    + " " + expected + " " + actual + " " + Math.ulp(expected));
      if (expected == 0) {
        Assert.assertEquals(
            "while integrating monomial x**"
                + n
                + " with a "
                + integrator.getNumberOfPoints()
                + "-point quadrature rule",
            expected,
            actual,
            eps);
      } else {
        double err = FastMath.abs(actual - expected) / Math.ulp(expected);
        Assert.assertEquals(
            "while integrating monomial x**"
                + n
                + " with a "
                + +integrator.getNumberOfPoints()
                + "-point quadrature rule, "
                + " error was "
                + err
                + " ulps",
            expected,
            actual,
            Math.ulp(expected) * numUlps);
      }
    }
  }
  @Test
  public void testQuadraticMean() {
    final double[] values = {1.2, 3.4, 5.6, 7.89};
    final DescriptiveStatistics stats = new DescriptiveStatistics(values);

    final int len = values.length;
    double expected = 0;
    for (int i = 0; i < len; i++) {
      final double v = values[i];
      expected += v * v / len;
    }
    expected = Math.sqrt(expected);

    Assert.assertEquals(expected, stats.getQuadraticMean(), Math.ulp(expected));
  }
 @Test
 public void testLineSegments() {
   PointSequenceBuilder builder = new FixedSizePointSequenceBuilder(3, DimensionalFlag.XYZM);
   builder.add(new double[] {1, 1, 1, 1});
   builder.add(new double[] {2, 2, 2, 2});
   builder.add(new double[] {3, 3, 3, 3});
   PointSequence sequence = builder.toPointSequence();
   int cnt = 0;
   double startX = 1.0d;
   for (LineSegment ls : new LineSegments(sequence)) {
     assertEquals(startX, ls.getStartPoint().getX(), Math.ulp(1.0d));
     startX = ls.getEndPoint().getX();
     cnt++;
   }
   assertEquals(2, cnt);
 }
Beispiel #19
0
  /**
   * Add an event handler for end time checking.
   *
   * <p>This method can be used to simplify handling of integration end time. It leverages the
   * nominal stop condition with the exceptional stop conditions.
   *
   * @param endTime desired end time
   * @param manager manager containing the user-defined handlers
   * @return a new manager containing all the user-defined handlers plus a dedicated manager
   *     triggering a stop event at entTime
   */
  protected CombinedEventsManager addEndTimeChecker(
      final double startTime, final double endTime, final CombinedEventsManager manager) {

    CombinedEventsManager newManager = new CombinedEventsManager();
    for (final EventState state : manager.getEventsStates()) {
      newManager.addEventHandler(
          state.getEventHandler(),
          state.getMaxCheckInterval(),
          state.getConvergence(),
          state.getMaxIterationCount());
    }
    newManager.addEventHandler(
        new EndTimeChecker(endTime),
        Double.POSITIVE_INFINITY,
        Math.ulp(Math.max(Math.abs(startTime), Math.abs(endTime))),
        100);

    return newManager;
  }
Beispiel #20
0
  @Test
  public void testOuterProduct() {
    final ArrayFieldVector<Fraction> u =
        new ArrayFieldVector<Fraction>(
            FractionField.getInstance(),
            new Fraction[] {new Fraction(1), new Fraction(2), new Fraction(-3)});
    final ArrayFieldVector<Fraction> v =
        new ArrayFieldVector<Fraction>(
            FractionField.getInstance(), new Fraction[] {new Fraction(4), new Fraction(-2)});

    final FieldMatrix<Fraction> uv = u.outerProduct(v);

    final double tol = Math.ulp(1d);
    Assert.assertEquals(new Fraction(4).doubleValue(), uv.getEntry(0, 0).doubleValue(), tol);
    Assert.assertEquals(new Fraction(-2).doubleValue(), uv.getEntry(0, 1).doubleValue(), tol);
    Assert.assertEquals(new Fraction(8).doubleValue(), uv.getEntry(1, 0).doubleValue(), tol);
    Assert.assertEquals(new Fraction(-4).doubleValue(), uv.getEntry(1, 1).doubleValue(), tol);
    Assert.assertEquals(new Fraction(-12).doubleValue(), uv.getEntry(2, 0).doubleValue(), tol);
    Assert.assertEquals(new Fraction(6).doubleValue(), uv.getEntry(2, 1).doubleValue(), tol);
  }
Beispiel #21
0
  public static float checkedFADD(float a, float b, int reaction, String logFileName) {
    float r = a + b;

    if (b != 0 && r == a || a != 0 && r == b) {
      if (r != 2.0 * r) // means here: isInfinite(r) (can't be 0 on NaN)
      {
        Reactions.react(reaction, PRECISION_MSG + "FADD", logFileName);
      }
    } else if (a != POSITIVE_INFINITY
        && a != NEGATIVE_INFINITY
        && b != POSITIVE_INFINITY
        && b != NEGATIVE_INFINITY
        && r != 0.0f) {
      if (r == POSITIVE_INFINITY) {
        Reactions.react(reaction, RESULT_IS_POS_INF_MSG + "FADD", logFileName);
      } else if (r == NEGATIVE_INFINITY) {
        Reactions.react(reaction, RESULT_IS_NEG_INF_MSG + "FADD", logFileName);
      } else if (Math.abs(r) <= CANCELLATION_ULP_FACTOR_FLOAT * Math.ulp(a)) {
        Reactions.react(reaction, CANCELLATION_MSG + "FADD", logFileName);
      }
    }
    return r;
  }
  @Test
  public void testGetM() {
    assertTrue(Double.isNaN(testSeq2D.getM(0)));
    assertTrue(Double.isNaN(testSeq2D.getM(1)));
    assertTrue(Double.isNaN(testSeq2D.getM(2)));
    assertTrue(Double.isNaN(testSeq3D.getM(0)));
    assertTrue(Double.isNaN(testSeq3D.getM(1)));
    assertTrue(Double.isNaN(testSeq3D.getM(2)));
    assertEquals(0d, testSeq2DM.getM(0), Math.ulp(10d));
    assertEquals(1d, testSeq2DM.getM(1), Math.ulp(10d));
    assertEquals(2d, testSeq2DM.getM(2), Math.ulp(10d));
    assertEquals(1d, testSeq3DM.getM(0), Math.ulp(10d));
    assertEquals(2d, testSeq3DM.getM(1), Math.ulp(10d));
    assertEquals(3d, testSeq3DM.getM(2), Math.ulp(10d));

    try {
      testSeq3D.getX(3);
      fail();
    } catch (IndexOutOfBoundsException e) {
    }
  }
Beispiel #23
0
  @Test
  public void test_inflate_impl() {
    final int K = 1 << 16;
    for (Double d :
        new Double[] {
          3.14159265358,
          Double.POSITIVE_INFINITY,
          Double.NEGATIVE_INFINITY,
          Double.MAX_VALUE,
          Double.NaN
        }) {
      NewChunk nc = new NewChunk(null, 0);
      for (int i = 0; i < K; ++i) nc.addNum(d);
      Assert.assertEquals(K, nc.len());
      Assert.assertEquals(K, nc.sparseLen());

      Chunk cc = nc.compress();
      Assert.assertEquals(K, cc.len());
      Assert.assertTrue(cc instanceof C0DChunk);
      for (int i = 0; i < K; ++i) Assert.assertEquals(d, cc.at0(i), Math.ulp(d));
      for (int i = 0; i < K; ++i) Assert.assertEquals(d, cc.at(i), Math.ulp(d));

      nc = cc.inflate_impl(new NewChunk(null, 0));
      nc.values(0, nc.len());
      Assert.assertEquals(K, nc.len());
      Assert.assertEquals(K, nc.sparseLen());
      for (int i = 0; i < K; ++i) Assert.assertEquals(d, nc.at0(i), Math.ulp(d));
      for (int i = 0; i < K; ++i) Assert.assertEquals(d, nc.at(i), Math.ulp(d));

      Chunk cc2 = nc.compress();
      Assert.assertEquals(K, cc2.len());
      Assert.assertTrue(cc2 instanceof C0DChunk);
      for (int i = 0; i < K; ++i) Assert.assertEquals(d, cc2.at0(i), Math.ulp(d));
      for (int i = 0; i < K; ++i) Assert.assertEquals(d, cc2.at(i), Math.ulp(d));

      Assert.assertTrue(Arrays.equals(cc._mem, cc2._mem));
    }
  }
  @Override
  public Object visit(RealUnaryExpression n, Void arg) {
    Double doubleObject = (Double) n.getOperand().accept(this, null);
    double doubleVal = doubleObject.doubleValue();

    Operator op = n.getOperator();
    switch (op) {
      case ABS:
        return Math.abs(doubleVal);
      case ACOS:
        return Math.acos(doubleVal);
      case ASIN:
        return Math.asin(doubleVal);
      case ATAN:
        return Math.atan(doubleVal);
      case CBRT:
        return Math.cbrt(doubleVal);
      case CEIL:
        return Math.ceil(doubleVal);
      case COS:
        return Math.cos(doubleVal);
      case COSH:
        return Math.cosh(doubleVal);
      case EXP:
        return Math.exp(doubleVal);
      case EXPM1:
        return Math.expm1(doubleVal);
      case FLOOR:
        return Math.floor(doubleVal);
      case LOG:
        return Math.log(doubleVal);
      case LOG10:
        return Math.log10(doubleVal);
      case LOG1P:
        return Math.log1p(doubleVal);
      case NEG:
        return -doubleVal;
      case NEXTUP:
        return Math.nextUp(doubleVal);
      case RINT:
        return Math.rint(doubleVal);
      case SIGNUM:
        return Math.signum(doubleVal);
      case SIN:
        return Math.sin(doubleVal);
      case SINH:
        return Math.sinh(doubleVal);
      case SQRT:
        return Math.sqrt(doubleVal);
      case TAN:
        return Math.tan(doubleVal);
      case TANH:
        return Math.tanh(doubleVal);
      case TODEGREES:
        return Math.toDegrees(doubleVal);
      case TORADIANS:
        return Math.toRadians(doubleVal);
      case ULP:
        return Math.ulp(doubleVal);

      default:
        log.warn("RealUnaryExpression: unimplemented operator: " + op);
        return null;
    }
  }
  @Test
  public void testGetOrdinate() {

    for (int i = 0; i < 3; i++) {
      // X
      Assert.assertEquals(
          testSeq2D.getX(i), testSeq2D.getOrdinate(i, CoordinateSequence.X), Math.ulp(10d));
      Assert.assertEquals(
          testSeq2DM.getX(i), testSeq2DM.getOrdinate(i, CoordinateSequence.X), Math.ulp(10d));
      Assert.assertEquals(
          testSeq3D.getX(i), testSeq3D.getOrdinate(i, CoordinateSequence.X), Math.ulp(10d));
      Assert.assertEquals(
          testSeq3DM.getX(i), testSeq3DM.getOrdinate(i, CoordinateSequence.X), Math.ulp(10d));

      // Y
      Assert.assertEquals(
          testSeq2D.getY(i), testSeq2D.getOrdinate(i, CoordinateSequence.Y), Math.ulp(10d));
      Assert.assertEquals(
          testSeq2DM.getY(i), testSeq2DM.getOrdinate(i, CoordinateSequence.Y), Math.ulp(10d));
      Assert.assertEquals(
          testSeq3D.getY(i), testSeq3D.getOrdinate(i, CoordinateSequence.Y), Math.ulp(10d));
      Assert.assertEquals(
          testSeq3DM.getY(i), testSeq3DM.getOrdinate(i, CoordinateSequence.Y), Math.ulp(10d));

      // Z
      Assert.assertEquals(
          testSeq2D.getZ(i), testSeq2D.getOrdinate(i, CoordinateSequence.Z), Math.ulp(10d));
      Assert.assertEquals(
          testSeq2DM.getZ(i), testSeq2DM.getOrdinate(i, CoordinateSequence.Z), Math.ulp(10d));
      Assert.assertEquals(
          testSeq3D.getZ(i), testSeq3D.getOrdinate(i, CoordinateSequence.Z), Math.ulp(10d));
      Assert.assertEquals(
          testSeq3DM.getZ(i), testSeq3DM.getOrdinate(i, CoordinateSequence.Z), Math.ulp(10d));

      // M
      Assert.assertEquals(
          testSeq2D.getM(i), testSeq2D.getOrdinate(i, CoordinateSequence.M), Math.ulp(10d));
      Assert.assertEquals(
          testSeq2DM.getM(i), testSeq2DM.getOrdinate(i, CoordinateSequence.M), Math.ulp(10d));
      Assert.assertEquals(
          testSeq3D.getM(i), testSeq3D.getOrdinate(i, CoordinateSequence.M), Math.ulp(10d));
      Assert.assertEquals(
          testSeq3DM.getM(i), testSeq3DM.getOrdinate(i, CoordinateSequence.M), Math.ulp(10d));

      try {
        testSeq2D.getOrdinate(i, 5);
        fail();
      } catch (IllegalArgumentException e) {
        // OK
      }
    }
    try {
      testSeq2D.getOrdinate(2, 5);
      fail();
    } catch (IllegalArgumentException e) {
      // OK
    }

    try {
      testSeq2D.getOrdinate(4, 1);
      fail();
    } catch (IndexOutOfBoundsException e) {
      // OK
    }

    try {
      testSeq3D.getOrdinate(3, 0);
      fail();
    } catch (IndexOutOfBoundsException e) {
    }
  }
Beispiel #26
0
  /**
   * getImageData(...) provides an image in a given palette data and origin. Faster than getting a
   * resolved image
   *
   * <p>This method should be thread safe.
   */
  public ImageData getImageData(ImageServiceBean bean) {
    ImageOrigin origin = bean.getOrigin();
    if (origin == null) origin = ImageOrigin.TOP_LEFT;

    // orientate the image
    Dataset oImage =
        DatasetUtils.rotate90(DatasetUtils.convertToDataset(bean.getImage()), origin.ordinal());
    Dataset image = oImage;

    if (image instanceof RGBDataset) {
      return SWTImageUtils.createImageData(
          (RGBDataset) image, 0, 255, null, null, null, false, false, false);
    }

    createMaxMin(bean);
    double max = getMax(bean);
    double min = getMin(bean);

    double maxCut = getMaxCut(bean);
    double minCut = getMinCut(bean);

    // now deal with the log if needed
    if (bean.isLogColorScale()) {
      image = DatasetUtils.rotate90(getImageLoggedData(bean), origin.ordinal());
      max = Math.log10(max);
      // note createMaxMin() -> getFastStatistics() -> getImageLogged() which ensures min >= 0
      min = Math.log10(min);
      maxCut = Math.log10(maxCut);
      // no guarantees for minCut though
      minCut = minCut <= 0 ? Double.NEGATIVE_INFINITY : Math.log10(minCut);
    }

    if (oImage.isComplex()) { // handle complex datasets by creating RGB dataset
      Dataset hue = Maths.angle(oImage, true);
      Dataset value = DatasetUtils.rotate90(getImageLoggedData(bean), origin.ordinal());
      double maxmax = Math.max(Math.abs(max), Math.abs(min));
      if (max - min > Math.ulp(maxmax)) {
        value.isubtract(min);
        value.imultiply(1. / (max - min));
      } else {
        value.imultiply(1. / maxmax);
      }
      image = RGBDataset.createFromHSV(hue, null, value);
      return SWTImageUtils.createImageData(image, 0, 255, null, null, null, false, false, false);
    }

    if (bean.getFunctionObject() != null && bean.getFunctionObject() instanceof FunctionContainer) {
      final FunctionContainer fc = (FunctionContainer) bean.getFunctionObject();
      // TODO This does not support masking or cut bounds for zingers and dead pixels.
      return SWTImageUtils.createImageData(
          image,
          min,
          max,
          fc.getRedFunc(),
          fc.getGreenFunc(),
          fc.getBlueFunc(),
          fc.isInverseRed(),
          fc.isInverseGreen(),
          fc.isInverseBlue());
    }

    return SWTImageUtils.createImageData(min, max, minCut, maxCut, image, bean);
  }
Beispiel #27
0
 public double epsilon() {
   return Math.ulp(1.0f);
 }
 // To add/remove functions change evaluateOperator() and registration
 public double evaluateFunction(String fncnam, ArgParser fncargs) throws ArithmeticException {
   switch (Character.toLowerCase(fncnam.charAt(0))) {
     case 'a':
       {
         if (fncnam.equalsIgnoreCase("abs")) {
           return Math.abs(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("acos")) {
           return Math.acos(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("asin")) {
           return Math.asin(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("atan")) {
           return Math.atan(fncargs.next());
         }
       }
       break;
     case 'c':
       {
         if (fncnam.equalsIgnoreCase("cbrt")) {
           return Math.cbrt(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("ceil")) {
           return Math.ceil(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("cos")) {
           return Math.cos(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("cosh")) {
           return Math.cosh(fncargs.next());
         }
       }
       break;
     case 'e':
       {
         if (fncnam.equalsIgnoreCase("exp")) {
           return Math.exp(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("expm1")) {
           return Math.expm1(fncargs.next());
         }
       }
       break;
     case 'f':
       {
         if (fncnam.equalsIgnoreCase("floor")) {
           return Math.floor(fncargs.next());
         }
       }
       break;
     case 'g':
       {
         //              if(fncnam.equalsIgnoreCase("getExponent"   )) { return
         // Math.getExponent(fncargs.next());                } needs Java 6
       }
       break;
     case 'l':
       {
         if (fncnam.equalsIgnoreCase("log")) {
           return Math.log(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("log10")) {
           return Math.log10(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("log1p")) {
           return Math.log1p(fncargs.next());
         }
       }
       break;
     case 'm':
       {
         if (fncnam.equalsIgnoreCase("max")) {
           return Math.max(fncargs.next(), fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("min")) {
           return Math.min(fncargs.next(), fncargs.next());
         }
       }
       break;
     case 'n':
       {
         //              if(fncnam.equalsIgnoreCase("nextUp"        )) { return Math.nextUp
         // (fncargs.next());                } needs Java 6
       }
       break;
     case 'r':
       {
         if (fncnam.equalsIgnoreCase("random")) {
           return Math.random();
         } // impure
         if (fncnam.equalsIgnoreCase("round")) {
           return Math.round(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("roundHE")) {
           return Math.rint(fncargs.next());
         } // round half-even
       }
       break;
     case 's':
       {
         if (fncnam.equalsIgnoreCase("signum")) {
           return Math.signum(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sin")) {
           return Math.sin(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sinh")) {
           return Math.sinh(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sqrt")) {
           return Math.sqrt(fncargs.next());
         }
       }
       break;
     case 't':
       {
         if (fncnam.equalsIgnoreCase("tan")) {
           return Math.tan(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("tanh")) {
           return Math.tanh(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("toDegrees")) {
           return Math.toDegrees(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("toRadians")) {
           return Math.toRadians(fncargs.next());
         }
       }
       break;
     case 'u':
       {
         if (fncnam.equalsIgnoreCase("ulp")) {
           return Math.ulp(fncargs.next());
         }
       }
       break;
       // no default
   }
   throw new UnsupportedOperationException(
       "MathEval internal function setup is incorrect - internal function \""
           + fncnam
           + "\" not handled");
 }
Beispiel #29
0
 protected double getPadding(double x) {
   return Math.ulp(x);
 }
  @Test
  public void testGetY() {
    assertEquals(0d, testSeq2D.getY(0), Math.ulp(10d));
    assertEquals(-1d, testSeq2D.getY(1), Math.ulp(10d));
    assertEquals(-2d, testSeq2D.getY(2), Math.ulp(10d));
    assertEquals(0d, testSeq2DM.getY(0), Math.ulp(10d));
    assertEquals(-1d, testSeq2DM.getY(1), Math.ulp(10d));
    assertEquals(-2d, testSeq2DM.getY(2), Math.ulp(10d));
    assertEquals(0d, testSeq3D.getY(0), Math.ulp(10d));
    assertEquals(-1d, testSeq3D.getY(1), Math.ulp(10d));
    assertEquals(-2d, testSeq3D.getY(2), Math.ulp(10d));
    assertEquals(0d, testSeq3DM.getY(0), Math.ulp(10d));
    assertEquals(-1d, testSeq3DM.getY(1), Math.ulp(10d));
    assertEquals(-2d, testSeq3DM.getY(2), Math.ulp(10d));

    try {
      testSeq3D.getY(3);
      fail();
    } catch (IndexOutOfBoundsException e) {
    }
  }