/// <summary>
        /// Tests if a point is inside the circle defined by
        /// the triangle with vertices a, b, c (oriented counter-clockwise).
        /// </summary>
        /// <remarks>
        /// The computation uses <see cref="DD"/> arithmetic for robustness.
        /// </remarks>
        /// <param name="a">A vertex of the triangle</param>
        /// <param name="b">A vertex of the triangle</param>
        /// <param name="c">A vertex of the triangle</param>
        /// <param name="p">The point to test</param>
        /// <returns>true if this point is inside the circle defined by the points a, b, c</returns>
        public static bool IsInCircleDDSlow(
            Coordinate a, Coordinate b, Coordinate c,
            Coordinate p)
        {
            DD px = DD.ValueOf(p.X);
            DD py = DD.ValueOf(p.Y);
            DD ax = DD.ValueOf(a.X);
            DD ay = DD.ValueOf(a.Y);
            DD bx = DD.ValueOf(b.X);
            DD by = DD.ValueOf(b.Y);
            DD cx = DD.ValueOf(c.X);
            DD cy = DD.ValueOf(c.Y);

            DD aTerm = (ax.Multiply(ax).Add(ay.Multiply(ay)))
                       .Multiply(TriAreaDDSlow(bx, by, cx, cy, px, py));
            DD bTerm = (bx.Multiply(bx).Add(by.Multiply(by)))
                       .Multiply(TriAreaDDSlow(ax, ay, cx, cy, px, py));
            DD cTerm = (cx.Multiply(cx).Add(cy.Multiply(cy)))
                       .Multiply(TriAreaDDSlow(ax, ay, bx, by, px, py));
            DD pTerm = (px.Multiply(px).Add(py.Multiply(py)))
                       .Multiply(TriAreaDDSlow(ax, ay, bx, by, cx, cy));

            DD   sum        = aTerm.Subtract(bTerm).Add(cTerm).Subtract(pTerm);
            bool isInCircle = sum.ToDoubleValue() > 0;

            return(isInCircle);
        }
        private static void CheckBinomial2(double a, double b)
        {
            // binomial product
            var add    = new DD(a);
            var bdd    = new DD(b);
            var aPlusb = add.Add(bdd);
            var aSubb  = add.Subtract(bdd);
            var abProd = aPlusb.Multiply(aSubb);
            // System.out.println("(a+b)^2 = " + abSq);

            // expansion
            var a2DD = add.Multiply(add);
            var b2DD = bdd.Multiply(bdd);

            // System.out.println("2ab+b^2 = " + sum);

            // this should equal b^2
            var diff = abProd.Subtract(a2DD).Negate();
            // System.out.println("(a+b)^2 - a^2 = " + diff);

            var delta = diff.Subtract(b2DD);

            // System.Console.WriteLine("\nA = " + a + ", B = " + b);
            // System.Console.WriteLine("[DD] (a+b)(a-b) = " + abProd
            //                          + "   -((a^2 - b^2) - a^2) = " + diff
            //                          + "   delta = " + delta);
            // printBinomialSquareDouble(a,b);

            bool isSame = diff.Equals(b2DD);

            Assert.IsTrue(isSame);
            bool isDeltaZero = delta.IsZero;

            Assert.IsTrue(isDeltaZero);
        }
        private static void CheckErrorBound(String tag, DD x, DD y, double errBound)
        {
            DD err = x.Subtract(y).Abs();

            Console.WriteLine(tag + " err=" + err);
            var isWithinEps = err.ToDoubleValue() <= errBound;

            Assert.True(isWithinEps);
        }
        private static void CheckParse(String str, DD expectedVal,
                                       double relErrBound)
        {
            DD     xdd    = DD.Parse(str);
            double err    = xdd.Subtract(expectedVal).ToDoubleValue();
            double relErr = err / xdd.ToDoubleValue();

            Console.WriteLine("Parsed= " + xdd + " rel err= " + relErr);

            Assert.IsTrue(err <= relErrBound);
        }
        private static void CheckReciprocal(double x, double errBound)
        {
            var xdd = new DD(x);
            var rr  = xdd.Reciprocal().Reciprocal();

            var err = xdd.Subtract(rr).ToDoubleValue();

            Console.WriteLine("DD Recip = " + xdd
                              + " DD delta= " + err
                              + " double recip delta= " + (x - 1.0 / (1.0 / x)));

            Assert.IsTrue(err <= errBound);
        }
        /// <summary>
        /// Computes the arctangent based on the Taylor series expansion
        /// <para/>
        /// arctan(x) = x - x^3 / 3 + x^5 / 5 - x^7 / 7 + ...
        /// </summary>
        /// <param name="x">The argument</param>
        /// <returns>An approximation to the arctangent of the input</returns>
        private static DD ArcTan(DD x)
        {
            var t    = x;
            var t2   = t.Sqr();
            var at   = new DD(0.0);
            var two  = new DD(2.0);
            var k    = 0;
            var d    = new DD(1.0);
            var sign = 1;

            while (t.ToDoubleValue() > DD.Epsilon)
            {
                k++;
                at = sign < 0 ? at.Subtract(t.Divide(d)) : at.Add(t.Divide(d));

                d    = d.Add(two);
                t    = t.Multiply(t2);
                sign = -sign;
            }
            Console.WriteLine("Computed DD.atan(): " + at
                              + "    Math.atan = " + Math.Atan(x.ToDoubleValue()));
            return(at);
        }
 private static DD Delta(DD x, DD y)
 {
     return(x.Subtract(y).Abs());
 }
 /// <summary>
 /// Computes twice the area of the oriented triangle (a, b, c), i.e., the area
 /// is positive if the triangle is oriented counterclockwise.
 /// </summary>
 /// <remarks>
 /// The computation uses {@link DD} arithmetic for robustness.
 /// </remarks>
 /// <param name="ax">x ordinate of a vertex of the triangle</param>
 /// <param name="ay">y ordinate of a vertex of the triangle</param>
 /// <param name="bx">x ordinate of a vertex of the triangle</param>
 /// <param name="by">y ordinate of a vertex of the triangle</param>
 /// <param name="cx">x ordinate of a vertex of the triangle</param>
 /// <param name="cy">y ordinate of a vertex of the triangle</param>
 /// <returns>The area of a triangle defined by the points a, b and c</returns>
 private 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))));
 }
        /// <summary>
        /// Computes the arctangent based on the Taylor series expansion
        /// <para/>
        /// arctan(x) = x - x^3 / 3 + x^5 / 5 - x^7 / 7 + ...
        /// </summary>
        /// <param name="x">The argument</param>
        /// <returns>An approximation to the arctangent of the input</returns>
        private static DD ArcTan(DD x)
        {
            var t = x;
            var t2 = t.Sqr();
            var at = new DD(0.0);
            var two = new DD(2.0);
            var k = 0;
            var d = new DD(1.0);
            var sign = 1;
            while (t.ToDoubleValue() > DD.Epsilon)
            {
                k++;
                at = sign < 0 ? at.Subtract(t.Divide(d)) : at.Add(t.Divide(d));

                d = d.Add(two);
                t = t.Multiply(t2);
                sign = -sign;
            }
            Console.WriteLine("Computed DD.atan(): " + at
                              + "    Math.atan = " + Math.Atan(x.ToDoubleValue()));
            return at;
        }
Example #10
0
 private 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))));
 }
        private static void CheckReciprocal(double x, double errBound)
        {
            var xdd = new DD(x);
            var rr = xdd.Reciprocal().Reciprocal();

            var err = xdd.Subtract(rr).ToDoubleValue();

            Console.WriteLine("DD Recip = " + xdd
                              + " DD delta= " + err
                              + " double recip delta= " + (x - 1.0/(1.0/x)));

            Assert.IsTrue(err <= errBound);
        }
        private static void CheckBinomial2(double a, double b)
        {
            // binomial product
            var add = new DD(a);
            var bdd = new DD(b);
            var aPlusb = add.Add(bdd);
            var aSubb = add.Subtract(bdd);
            var abProd = aPlusb.Multiply(aSubb);
            //  	System.out.println("(a+b)^2 = " + abSq);

            // expansion
            var a2DD = add.Multiply(add);
            var b2DD = bdd.Multiply(bdd);

            //  	System.out.println("2ab+b^2 = " + sum);

            // this should equal b^2
            var diff = abProd.Subtract(a2DD).Negate();
            //  	System.out.println("(a+b)^2 - a^2 = " + diff);

            var delta = diff.Subtract(b2DD);

            Console.WriteLine("\nA = " + a + ", B = " + b);
            Console.WriteLine("[DD] (a+b)(a-b) = " + abProd
                              + "   -((a^2 - b^2) - a^2) = " + diff
                              + "   delta = " + delta);
            //  	printBinomialSquareDouble(a,b);

            var isSame = diff.Equals(b2DD);
            Assert.IsTrue(isSame);
            var isDeltaZero = delta.IsZero;
            Assert.IsTrue(isDeltaZero);
        }
 private static void CheckErrorBound(String tag, DD x, DD y, double errBound)
 {
     DD err = x.Subtract(y).Abs();
     Console.WriteLine(tag + " err=" + err);
     var isWithinEps = err.ToDoubleValue() <= errBound;
     Assert.True(isWithinEps);
 }
 private static DD Delta(DD x, DD y)
 {
     return x.Subtract(y).Abs();
 }