/** * Computes the square of this value. * * @return the square of this value. */ public DD sqr() { return this.multiply(this); }
/** * Computes the sign of the determinant of the 2x2 matrix * with the given entries. * * @return -1 if the determinant is negative, * @return 1 if the determinant is positive, * @return 0 if the determinant is 0. */ public static int signOfDet2x2(DD x1, DD y1, DD x2, DD y2) { DD det = x1.multiply(y2).selfSubtract(y1.multiply(x2)); return det.signum(); }
public static boolean isInCircleDDNormalized( Coordinate a, Coordinate b, Coordinate c, Coordinate p) { DD adx = DD.valueOf(a.x).selfSubtract(p.x); DD ady = DD.valueOf(a.y).selfSubtract(p.y); DD bdx = DD.valueOf(b.x).selfSubtract(p.x); DD bdy = DD.valueOf(b.y).selfSubtract(p.y); DD cdx = DD.valueOf(c.x).selfSubtract(p.x); DD cdy = DD.valueOf(c.y).selfSubtract(p.y); DD abdet = adx.multiply(bdy).selfSubtract(bdx.multiply(ady)); DD bcdet = bdx.multiply(cdy).selfSubtract(cdx.multiply(bdy)); DD cadet = cdx.multiply(ady).selfSubtract(adx.multiply(cdy)); DD alift = adx.multiply(adx).selfAdd(ady.multiply(ady)); DD blift = bdx.multiply(bdx).selfAdd(bdy.multiply(bdy)); DD clift = cdx.multiply(cdx).selfAdd(cdy.multiply(cdy)); DD sum = alift.selfMultiply(bcdet) .selfAdd(blift.selfMultiply(cadet)) .selfAdd(clift.selfMultiply(abdet)); boolean isInCircle = sum.doubleValue() > 0; return isInCircle; }
/** * Computes twice the area of the oriented triangle (a, b, c), i.e., the area * is positive if the triangle is oriented counterclockwise. * The computation uses {@link DD} arithmetic for robustness. * * @param ax the x ordinate of a vertex of the triangle * @param ay the y ordinate of a vertex of the triangle * @param bx the x ordinate of a vertex of the triangle * @param by the y ordinate of a vertex of the triangle * @param cx the x ordinate of a vertex of the triangle * @param cy the y ordinate of a vertex of the triangle */ public static DD triAreaDDSlow(DD ax, DD ay, DD bx, DD by, DD cx, DD cy) { return (bx.subtract(ax).multiply(cy.subtract(ay)).subtract(by.subtract(ay) .multiply(cx.subtract(ax)))); }
/** * Computes the sign of the determinant of the 2x2 matrix * with the given entries. * * @return -1 if the determinant is negative, * @return 1 if the determinant is positive, * @return 0 if the determinant is 0. */ public static int signOfDet2x2(double dx1, double dy1, double dx2, double dy2) { DD x1 = DD.valueOf(dx1); DD y1 = DD.valueOf(dy1); DD x2 = DD.valueOf(dx2); DD y2 = DD.valueOf(dy2); DD det = x1.multiply(y2).selfSubtract(y1.multiply(x2)); return det.signum(); }
private void checkDivideMultiply(DD a, DD b, double errBound) { DD a2 = a.divide(b).multiply(b); checkErrorBound("DivideMultiply", a, a2, errBound); }
private void checkSqrt(DD x, double errBound) { DD sqrt = x.sqrt(); DD x2 = sqrt.multiply(sqrt); checkErrorBound("Sqrt", x, x2, errBound); }
private void checkMultiplyDivide(DD a, DD b, double errBound) { DD a2 = a.multiply(b).divide(b); checkErrorBound("MultiplyDivide", a, a2, errBound); }
private DD slowPow(DD x, int exp) { if (exp == 0) return DD.valueOf(1.0); int n = Math.abs(exp); // MD - could use binary exponentiation for better precision & speed DD pow = new DD(x); for (int i = 1; i < n; i++) { pow = pow.multiply(x); } if (exp < 0) { return pow.reciprocal(); } return pow; }
public static boolean isInCircleDD3( Coordinate a, Coordinate b, Coordinate c, Coordinate p) { DD adx = DD.valueOf(a.x).selfSubtract(p.x); DD ady = DD.valueOf(a.y).selfSubtract(p.y); DD bdx = DD.valueOf(b.x).selfSubtract(p.x); DD bdy = DD.valueOf(b.y).selfSubtract(p.y); DD cdx = DD.valueOf(c.x).selfSubtract(p.x); DD cdy = DD.valueOf(c.y).selfSubtract(p.y); DD abdet = adx.multiply(bdy).selfSubtract(bdx.multiply(ady)); DD bcdet = bdx.multiply(cdy).selfSubtract(cdx.multiply(bdy)); DD cadet = cdx.multiply(ady).selfSubtract(adx.multiply(cdy)); DD alift = adx.multiply(adx).selfSubtract(ady.multiply(ady)); DD blift = bdx.multiply(bdx).selfSubtract(bdy.multiply(bdy)); DD clift = cdx.multiply(cdx).selfSubtract(cdy.multiply(cdy)); DD sum = alift.selfMultiply(bcdet) .selfAdd(blift.selfMultiply(cadet)) .selfAdd(clift.selfMultiply(abdet)); boolean isInCircle = sum.doubleValue() > 0; return isInCircle; }
private void checkAddMult2(DD dd) { DD sum = dd.add(dd); DD prod = dd.multiply(new DD(2.0)); checkErrorBound("AddMult2", sum, prod, 0.0); }
/** * Computes twice the area of the oriented triangle (a, b, c), i.e., the area * is positive if the triangle is oriented counterclockwise. * The computation uses {@link DD} arithmetic for robustness. * * @param ax the x ordinate of a vertex of the triangle * @param ay the y ordinate of a vertex of the triangle * @param bx the x ordinate of a vertex of the triangle * @param by the y ordinate of a vertex of the triangle * @param cx the x ordinate of a vertex of the triangle * @param cy the y ordinate of a vertex of the triangle */ public static DD triAreaDD(DD ax, DD ay, DD bx, DD by, DD cx, DD cy) { return (bx.subtract(ax).multiply(cy.subtract(ay)).subtract(by.subtract(ay) .multiply(cx.subtract(ax)))); }
public double xrunDoubleDoubleSelf(int nIter) { Stopwatch sw = new Stopwatch(); for (int i = 0; i < nIter; i++) { DD a = new DD(9.0); DD factor = new DD(10.0); DD aMul = factor.multiply(a); DD aDiv = a.divide(factor); DD det = a.multiply(a) .subtract(aMul.multiply(aDiv)); // System.out.println(aDiv); // System.out.println(det); } sw.stop(); System.out.println("DD: nIter = " + nIter + " time = " + sw.getTimeString()); return sw.getTime() / (double) nIter; }
public double runDoubleDouble(int nIter) { Stopwatch sw = new Stopwatch(); for (int i = 0; i < nIter; i++) { DD a = new DD(9.0); DD factor = new DD(10.0); DD aMul = factor.multiply(a); DD aDiv = a.divide(factor); DD det = a.multiply(a) .subtract(aMul.multiply(aDiv)); // System.out.println(aDiv); // System.out.println(det); } sw.stop(); System.out.println("DD: nIter = " + nIter + " time = " + sw.getTimeString()); return sw.getTime() / (double) nIter; }
/** * Uses Machin's arctangent formula to compute Pi: * * Pi / 4 = 4 arctan(1/5) - arctan(1/239) * * @return an approximation to Pi */ private DD computePiByMachin() { DD t1 = DD.valueOf(1.0).divide(DD.valueOf(5.0)); DD t2 = DD.valueOf(1.0).divide(DD.valueOf(239.0)); DD pi4 = (DD.valueOf(4.0) .multiply(arctan(t1))) .subtract(arctan(t2)); DD pi = DD.valueOf(4.0).multiply(pi4); //System.out.println("Computed value = " + pi); return pi; }
/** * Tests that printing values with many decimal places works. * This tests the correctness and robustness of both output and input. * * @param x the value to test */ private void writeAndReadSqrt(double x) { DD xdd = DD.valueOf(x); DD xSqrt = xdd.sqrt(); String s = xSqrt.toString(); // System.out.println(s); DD xSqrt2 = DD.parse(s); DD xx = xSqrt2.multiply(xSqrt2); String xxStr = xx.toString(); // System.out.println("==> " + xxStr); DD xx2 = DD.parse(xxStr); double err = Math.abs(xx2.doubleValue() - x); assertTrue(err < 1e-10); }
DD xx = xSqrt2.multiply(xSqrt2); double err = Math.abs(xx.doubleValue() - x);
public void testNaN() { assertTrue(DD.valueOf(1).divide(DD.valueOf(0)).isNaN()); assertTrue(DD.valueOf(1).multiply(DD.NaN).isNaN()); }
t = t.multiply(t2); sign = -sign;