/// <summary>
        /// Determine the value of the caplet using the Black-Caplet-formula
        /// </summary>
        /// <returns></returns>
        public double Value(YieldCurve yieldCurve, double delta, Func <double, double> volatilityFunc)
        {
            // valuation date
            var valuationDate = yieldCurve.SettleDate;

            var T = Utilities.ConvertToDayCountFraction(valuationDate, FixingDate);

            Func <double, double> func = t => volatilityFunc(T - t) * volatilityFunc(T - t);

            var sigma = Math.Sqrt(1 / T * Integrate.OnClosedInterval(func, 0, T));

            var tenor = Utilities.ConvertToDayCountFraction(FixingDate, ExpiryDate);
            var bond  = yieldCurve.GetDiscountFactors(new [] { FixingDate, ExpiryDate });

            var kappa = CapRate / (Tenor * Principal) + delta;

            var spotForwardRate = 1 / tenor * (bond[0] / bond[1] - 1);

            var logLK  = Math.Log((spotForwardRate + delta) / (kappa));
            var gamma  = T * sigma * sigma;
            var dPlus  = (logLK + 0.5 * gamma) / Math.Sqrt(gamma);
            var dMinus = (logLK - 0.5 * gamma) / Math.Sqrt(gamma);

            return(bond[1] * Principal * Tenor * (spotForwardRate * Normal.CDF(0, 1, dPlus) - kappa * Normal.CDF(0, 1, dMinus)));
        }
        public void TestIntegrateFacade()
        {
            Assert.AreEqual(
                TargetAreaA,
                Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA),
                1e-5,
                "Interval");

            Assert.AreEqual(
                TargetAreaA,
                Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA, 1e-10),
                1e-10,
                "Interval, Target 1e-10");

            Assert.AreEqual(
                Integrate.OnRectangle(TargetFunctionB, StartA, StopA, StartB, StopB),
                TargetAreaB,
                1e-12,
                "Rectangle");

            Assert.AreEqual(
                Integrate.OnRectangle(TargetFunctionB, StartA, StopA, StartB, StopB, 22),
                TargetAreaB,
                1e-10,
                "Rectangle, Gauss-Legendre Order 22");
        }
        /// <summary>
        /// Executes the example.
        /// </summary>
        /// <seealso cref="http://reference.wolfram.com/mathematica/ref/Integrate.html"/>
        /// <seealso cref="http://en.wikipedia.org/wiki/Trapezoidal_rule">Trapezoidal rule</seealso>
        public override void ExecuteExample()
        {
            // 1. Integrate x*x on interval [0, 10]
            Console.WriteLine(@"1. Integrate x*x on interval [0, 10]");
            var result = Integrate.OnClosedInterval(x => x * x, 0, 10);

            Console.WriteLine(result);
            Console.WriteLine();

            // 2. Integrate 1/(x^3 + 1) on interval [0, 1]
            Console.WriteLine(@"2. Integrate 1/(x^3 + 1) on interval [0, 1]");
            result = Integrate.OnClosedInterval(x => 1 / (Math.Pow(x, 3) + 1), 0, 1);
            Console.WriteLine(result);
            Console.WriteLine();

            // 3. Integrate f(x) = exp(-x/5) (2 + sin(2 * x)) on [0, 10]
            Console.WriteLine(@"3. Integrate f(x) = exp(-x/5) (2 + sin(2 * x)) on [0, 10]");
            result = Integrate.OnClosedInterval(x => Math.Exp(-x / 5) * (2 + Math.Sin(2 * x)), 0, 100);
            Console.WriteLine(result);
            Console.WriteLine();

            // 4. Integrate target function with absolute error = 1E-4
            Console.WriteLine(@"4. Integrate target function with absolute error = 1E-4 on [0, 10]");
            Console.WriteLine(@"public static double TargetFunctionA(double x) {
  return Math.Exp(-x / 5) * (2 + Math.Sin(2 * x));
}");
            result = Integrate.OnClosedInterval(TargetFunctionA, 0, 100, 1e-4);
            Console.WriteLine(result);
            Console.WriteLine();
        }
Example #4
0
        private void AreaDetection()
        {
            double shortSum = 0, longSum = 0, shortAve, longAve;

            for (int j = i; j > (i - shortNum + 1); j--)
            {
                double slope     = (data[j, 1] - data[j - 1, 1]) / (data[j, 0] - data[j - 1, 0]);
                double intercept = data[j, 1] - slope * data[j, 0];
                var    result1   = Integrate.OnClosedInterval(x => slope * x + intercept, data[j - 1, 0], (data[j, 0]));

                shortSum += Math.Abs(result1);
            }
            for (int j = i; j > (i - longNum + 1); j--)
            {
                double slope     = (data[j, 1] - data[j - 1, 1]) / (data[j, 0] - data[j - 1, 0]);
                double intercept = data[j, 1] - slope * data[j, 0];
                var    result2   = Integrate.OnClosedInterval(x => slope * x + intercept, data[j - 1, 0], (data[j, 0]));

                longSum += Math.Abs(result2);
            }
            shortAve = shortSum / shortNum;
            longAve  = longSum / longNum;

            if (Math.Abs((shortAve - longAve) / longAve) > ArgDetect)
            {
                Point point = new Point();
                point = new Point(data[i, 0], data[i, 1]);
                dataSource2.AppendAsync(base.Dispatcher, point);
                flag = 2;
            }
        }
Example #5
0
    float TestReslut()
    {
        float e_pow   = (Mathf.Pow(a, 2) - Mathf.Pow(b, 2)) / Mathf.Pow(a, 2);
        float resulte = a * (float)Integrate.OnClosedInterval(x => Mathf.Sqrt(1 - e_pow * Mathf.Pow(Mathf.Cos((float)x), 2)), down * Mathf.Deg2Rad, up * Mathf.Deg2Rad);

        return(Mathf.Abs(resulte));
    }
Example #6
0
        public static double simpsonRuleEs(Expr expr, string varName, double a, double b, int n)
        {
            var f = expr.Compile(varName);

            double srValue = SimpsonRule.IntegrateComposite(f, a, b, n);

            double trueValue = Integrate.OnClosedInterval(f, a, b);

            return(abs(trueValue - srValue));
        }
Example #7
0
        public static double trapezoidalRuleEt(Expr expr, string varName, double a, double b, int n)
        {
            var f = expr.Compile(varName);

            double trValue = NewtonCotesTrapeziumRule.IntegrateComposite(f, a, b, n);

            double trueValue = Integrate.OnClosedInterval(f, a, b);

            return(abs(trueValue - trValue));
        }
        public void TestPortal()
        {
            Assert.That(
                Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA),
                NumericIs.AlmostEqualTo(TargetAreaA, 1e-5),
                "Basic");

            Assert.That(
                Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA, 1e-10),
                NumericIs.AlmostEqualTo(TargetAreaA, 1e-10),
                "Basic Target 1e-10");
        }
Example #9
0
        public void TestIntegratePortal()
        {
            Assert.AreEqual(
                TargetAreaA,
                Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA),
                1e-5,
                "Basic");

            Assert.AreEqual(
                TargetAreaA,
                Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA, 1e-10),
                1e-10,
                "Basic Target 1e-10");
        }
Example #10
0
        Point3D ICompute.ElasticCenter()
        {
            double xcoord = Integrate.OnClosedInterval(
                x => x * dL(x) / L,
                -W / 2,
                W / 2,
                Prec
                );
            double zcoord = Integrate.OnClosedInterval(
                x => F(x) * dL(x) / L,
                -W / 2,
                W / 2,
                Prec
                );

            return(new Point3D(xcoord, 0, zcoord));
        }
Example #11
0
        public List <PointLoad> DiscretizeForce(Func <double, double> force, Vault structure, UnitVector3D projection)
        {
            var        res   = new List <PointLoad>();
            Boundaries bound = Boundaries[Axis];

            for (int i = 0; i < structure.Points.MidSegment.Count; i++)
            {
                double coord_prev = structure.Points.Segment[i].ToVector3D().DotProduct(Axis);
                double coord_next = structure.Points.Segment[i + 1].ToVector3D().DotProduct(Axis);

                if (coord_prev > coord_next)
                {
                    var temp = coord_next;
                    coord_next = coord_prev;
                    coord_prev = temp;
                }

                if (coord_next >= bound.Min && coord_prev <= bound.Max)
                {
                    var point = structure.Points.MidSegment[i];

                    double min = Max(coord_prev, bound.Min);
                    double max = Min(coord_next, bound.Max);
                    double force_integral;

                    if (Abs(min - max) > Prec)
                    {
                        force_integral = Integrate.OnClosedInterval(force, min, max);
                    }
                    else
                    {
                        max            = point.ToVector3D().DotProduct(Axis);
                        force_integral = 2 * Integrate.OnClosedInterval(force, min, max);
                    }

                    if (Abs(force_integral) > Prec)
                    {
                        res.Add(new VaultPointLoad(point, force_integral * projection));
                    }
                }
            }
            return(res);
        }
Example #12
0
        public virtual Length GetArcLength()
        {
            double length = GetFinalPoint().X.GetBasicVal();

            if (Settings.Quantities.Α.IsRight() || length == 0)
            {
                return(new Length(2.0 * GetHighestPoint().Y.GetBasicVal() - Settings.Quantities.H.GetBasicVal(), UnitLength.Basic));
            }

            return(new Length(
                       Integrate.OnClosedInterval(
                           x => Math.Sqrt(
                               1 + Math.Pow(
                                   Math.Tan(Settings.Quantities.Α.GetBasicVal()) -
                                   Settings.Quantities.G.GetBasicVal() /
                                   Math.Pow(GetInitialPoint().Vx.GetBasicVal(), 2.0) * x, 2.0)
                               ), 0, length
                           ), UnitLength.Basic
                       ));
        }
Example #13
0
        double ICompute.IntZSq()
        {
            Func <double, double> func;

            switch (Restraint)
            {
            case Restraint.Fixed:
                double Oz = Points.ElasticCenter.Z;
                func = u => Math.Pow(F(u) - Oz, 2) * dL(u);
                break;

            case Restraint.Pinned:
                func = u => Math.Pow(F(u), 2) * dL(u);
                break;

            default:
                var msg = "Restraint is unknown";
                throw new InvalidOperationException(msg);
            }
            return(Integrate.OnClosedInterval(func, -W / 2, W / 2, Prec));
        }
Example #14
0
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         double xp = Double.Parse(textBox7.Text);
         double xk = Double.Parse(textBox5.Text);
         int    n  = Int32.Parse(textBox6.Text);
         if (n % 2 == 0 && xp >= xk)
         {
             textBox1.Text = "" + MetodaProstokatow(h, xp, xk, n);
             textBox3.Text = "" + MetodaTrapezow(h, xp, xk, n);
             textBox4.Text = "" + Integrate.OnClosedInterval(h, xp, xk);
             textBox2.Text = "" + MathNet.Numerics.Integration.Algorithms.SimpsonRule.IntegrateComposite(h, xp, xk, n);
         }
         else
         {
             MessageBox.Show("Zły przedział, lub granica górna jest mniejsza od dolnej");
         }
     }
     catch (System.FormatException) { }
 }
        /// <summary>
        /// Compute correlation matrix
        /// </summary>
        /// <param name="t"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        private Matrix <double> CorrelationMatrix(double t, double dt)
        {
            var C = Matrix <double> .Build.Dense(NumRates, NumRates);

            for (var i = 0; i < NumRates; i++)
            {
                for (var j = 0; j <= i; j++)
                {
                    var i1 = i;
                    var j1 = j;

                    Func <double, double> func = x => Correlation[i1, j1] * Volatility[i1](T[i1] - x) * Volatility[j1](T[j1] - x);

                    var corr = Integrate.OnClosedInterval(func, t, t + dt);

                    C[i, j] = corr;
                    C[j, i] = corr;
                }
            }
            return(C);
        }
        /// <summary>
        /// Spawns cubes upon an arc of an ellipse.
        /// </summary>
        /// <param name="n"> The number of cubes. </param>
        /// <param name="center"> The center of the ellipse. </param>
        /// <param name="sectorChordLength"> The length of the arc's chord. </param>
        /// <param name="semiMinorAxisLength"> Half the length of the minor axis. </param>
        /// <param name="sectorAngleDeg"> The angular length of the arc in degrees. </param>
        /// <param name="cubePrefab"> The template for the cubes. </param>
        /// <param name="parent"> The parent of the cubes structure. </param>
        /// <returns> Returns the GameObject containing the cubes, whose parent is 'parent'. </returns>
        /// <remarks> This function is definitely very time-expensive. Use sparingly. </remarks>
        private static GameObject SpawnEllipseCubes(int n, Vector3 center, float sectorChordLength, float semiMinorAxisLength, float sectorAngleDeg, GameObject cubePrefab, Transform parent = null)
        {
            // TODO: Find constraints for every parameter and variable (minimum and maximum values)
            //       Problem: for some combination of input parameters, the FindRoots.OfFunction method fails with an exception:
            //                "ArithmeticException: Function does not accept floating point Not-a-Number values."
            //       Sample parameters: (16,, 12.0f, 6.0f, 90°,,)

            #region Maths

            /*
             * C: center of the ellipse
             * R: sectorChordLength, the length of the chord
             * a: half the length of the major axis of the ellipse
             * b: semiMinorAxisLength, half the length of the minor axis of the ellipse
             * α: sectorAngleDeg, the sector extension in degrees, from (90° - α/2) to (90° + α/2) counterclock-wise
             * n: number of cubes
             *
             *
             * § 1) ELLIPSE'S EQUATIONS
             *
             * ξ(ϑ): ellipse's parametric equations [-180° ≤ ϑ ≤ 180°]
             *     where ϑ is the angle of (ξˣ(ϑ), ξʸ(ϑ)) with the major axis of the ellipse itself
             *                       ___________________________
             *   ξˣ(ϑ) = a b cos(ϑ) √(a² sin²(ϑ) + b² cos²(ϑ))⁻¹
             *                       ___________________________
             *   ξʸ(ϑ) = a b sin(ϑ) √(a² sin²(ϑ) + b² cos²(ϑ))⁻¹
             *
             * A = ξ(90° + α/2)
             * B = ξ(90° - α/2)
             *
             *     __                   _______________________________
             * R = AB = 2 a b sin(α/2) √(a² cos²(α/2) + b² sin²(α/2))⁻¹
             *      _________________________________________                   _______________________
             * a = √(b² R² tan²(α/2)) / (4 b² tan²(α/2) - R²) = (b R tan(α/2)) √(4 b² tan²(α/2) - R²)⁻¹
             *
             *
             * § 2) CUBES PLACEMENT
             *      A _____________________
             * L = ∫(√D[ξˣ(ϑ)]² + D[ξʸ(ϑ)]²)dϑ: length of arc AB
             *    B
             * l = L/n: distance between each cube's center on the arc
             *
             * θᵢ: angle of the iᵗʰ arc segment; arc length to the iᵗʰ arc segment = i l
             * θᵢ = FindRoot[∫(D[ξˣ(ϑ)]² + D[ξʸ(ϑ)]²)dϑ = i l]   😭
             *
             * Pᵢ = ξ(θᵢ): position of the iᵗʰ cube
             *
             *
             * § 3) CUBES' SIDE LENGTH
             * Let Pᵢ and Pⱼ be the positions of two consecutive cubes (j = i + 1) with angles θᵢ and θⱼ.
             * Let Pₖ be a third point placed at the same arc-distance from both Pᵢ and Pⱼ; the angle of Pₖ is θₖ.
             *
             * rₖ: line from C to Pₖ
             * rᵢ: line from C to Pᵢ
             * rⱼ: line from C to Pⱼ
             *
             * M: parametric point on rₖ
             * dᵢ: segment MPᵢ
             * dⱼ: segment MPⱼ
             *
             *
             * M = (mˣ, mʸ) = (mˣ, mˣ tan(θₖ))
             *       ____________________________________
             * dᵢ = √(x(Pᵢ) - mˣ)² + (y(Pᵢ) - mˣ tan(θₖ))²
             *       ____________________________________
             * dⱼ = √(x(Pⱼ) - mˣ)² + (y(Pⱼ) - mˣ tan(θₖ))²
             *
             * ASSERTION
             * dᵢ = dⱼ = d: dᵢ and dⱼ are the semi-diagonals of the two squares
             *
             * (x(Pᵢ) - mˣ)² + (y(Pᵢ) - mˣ tan(θₖ))² = (x(Pⱼ) - mˣ)² + (y(Pⱼ) - mˣ tan(θₖ))²
             * x(Pᵢ)² + (mˣ)² - 2 x(Pᵢ) mˣ + y(Pᵢ)² + (mˣ tan(θₖ))² - 2 y(Pᵢ) mˣ tan(θₖ)  =  x(Pⱼ)² + (mˣ)² - 2 x(Pⱼ) mˣ + y(Pⱼ)² + (mˣ tan(θₖ))² - 2 y(Pⱼ) mˣ tan(θₖ)
             * x(Pᵢ)² - 2 x(Pᵢ) mˣ + y(Pᵢ)² - 2 y(Pᵢ) mˣ tan(θₖ)  =  x(Pⱼ)² - 2 x(Pⱼ) mˣ + y(Pⱼ)² - 2 y(Pⱼ) mˣ tan(θₖ)
             * 2 x(Pⱼ) mˣ + 2 y(Pⱼ) mˣ tan(θₖ) - 2 x(Pᵢ) mˣ - 2 y(Pᵢ) mˣ tan(θₖ)  =  x(Pⱼ)² + y(Pⱼ)² - x(Pᵢ)² - y(Pᵢ)²
             * 2 mˣ (x(Pⱼ) + y(Pⱼ) tan(θₖ) - x(Pᵢ) - y(Pᵢ) tan(θₖ))  =  x(Pⱼ)² + y(Pⱼ)² - x(Pᵢ)² - y(Pᵢ)²
             *
             *              x(Pⱼ)² + y(Pⱼ)² - x(Pᵢ)² - y(Pᵢ)²
             * mˣ = ——————————————————————————————————————————————————
             *       2 (x(Pⱼ) + y(Pⱼ) tan(θₖ) - x(Pᵢ) - y(Pᵢ) tan(θₖ))
             *
             * cube's side length = 2 (d/√2)
             *
             */

            #endregion Maths

            double R = sectorChordLength;
            double b = semiMinorAxisLength;

            #region § 1) ELLIPSE'S EQUATION

            double alphaRad      = Trig.DegreeToRadian(sectorAngleDeg);
            double bTanHalfAlpha = b * Trig.Tan(alphaRad / 2.0);
            double a             = (R * bTanHalfAlpha) / Math.Sqrt(4 * bTanHalfAlpha * bTanHalfAlpha - R * R);

            Func <double, double> EllipseX = t =>
            {
                double sin  = Trig.Sin(t);
                double cos  = Trig.Cos(t);
                double aSin = a * sin;
                double bCos = b * cos;
                return((a * bCos) / Math.Sqrt(aSin * aSin + bCos * bCos));
            };
            Func <double, double> EllipseY = t =>
            {
                double sin  = Trig.Sin(t);
                double cos  = Trig.Cos(t);
                double aSin = a * sin;
                double bCos = b * cos;
                return((b * aSin) / Math.Sqrt(aSin * aSin + bCos * bCos));
            };

            #endregion § 1) ELLIPSE'S EQUATION

            #region § 2) CUBES PLACEMENT

            Func <double, double> DEllipseX = Differentiate.FirstDerivativeFunc(EllipseX);
            Func <double, double> DEllipseY = Differentiate.FirstDerivativeFunc(EllipseY);

            Func <double, double> SqrtOfSumOfSquaredDerivatives = t =>
            {
                double x = DEllipseX(t);
                double y = DEllipseY(t);
                return(Math.Sqrt(x * x + y * y));
            };

            Func <double, double, double> AngleToArcLength = (fromRad, toRad) => Integrate.OnClosedInterval(SqrtOfSumOfSquaredDerivatives, fromRad, toRad);
            Func <double, double>         ArcLengthToAngle = arcLength => FindRoots.OfFunction(toRad => AngleToArcLength((Math.PI - alphaRad) / 2, toRad) - arcLength, 0, Math.PI);

            double L = AngleToArcLength((Math.PI - alphaRad) / 2, (Math.PI + alphaRad) / 2);
            double l = L / (n - 1);

            Func <int, object[]> CalculateCubePositionAndAngle = i =>
            {
                double  angle    = ArcLengthToAngle(i * l);
                Vector2 position = new Vector2((float)EllipseX(angle), (float)EllipseY(angle));
                return(new object[] { position, angle });
            };

            #endregion § 2) CUBES PLACEMENT

            #region § 3) CUBES' SIDE LENGTH

            Vector2[] positions    = new Vector2[n];
            double[]  angles       = new double[n];
            double[]  middleAngles = new double[n - 1];
            for (int i = 0; i < n; ++i)
            {
                object[] res = CalculateCubePositionAndAngle(i);
                positions[i] = (Vector2)res[0];
                angles[i]    = (double)res[1];

                if (i > 0)
                {
                    middleAngles[i - 1] = ArcLengthToAngle((2 * i - 1) * l / 2.0);
                }
            }

            double[]           sideLengths = new double[n - 1];
            Func <int, double> SideLength  = j =>
            {
                double xj   = positions[j].x;
                double yj   = positions[j].y;
                double xi   = positions[j - 1].x;
                double yi   = positions[j - 1].y;
                double tanK = Trig.Tan(middleAngles[j - 1]);
                double mx   = (xj * xj + yj * yj - (xi * xi + yi * yi)) / (2.0 * (xj + yj * tanK - (xi + yi * tanK)));
                return(Math.Sqrt((xi - mx) * (xi - mx) + (yi - mx * tanK) * (yi - mx * tanK)) * (2.0 / Math.Sqrt(2.0)));
            };
            for (int i = 1; i < n; ++i)
            {
                sideLengths[i - 1] = SideLength(i);
            }

            float squareSide = (float)sideLengths.Min();

            #endregion § 3) CUBES' SIDE LENGTH

            // Save parent's current local rotation and scale; it will be re-set later
            Quaternion parentRotation = parent?.localRotation ?? Quaternion.identity;
            Vector3    parentScale    = parent?.localScale ?? Vector3.one;
            if (parent != null)
            {
                parent.localRotation = Quaternion.identity;
                parent.localScale    = Vector3.one;
            }

            GameObject cubesContainer = parent?.gameObject ?? new GameObject();
            cubesContainer.name = $"Ellipse_{n}Cubes";
            cubesContainer.transform.localPosition = center;

            // Create the cubes
            for (int i = 0; i < n; ++i)
            {
                GameObject cube = Instantiate(cubePrefab);
                cube.name = $"Cube{i}";
                cube.SetActive(true); // Awake it now, to let it store the prefab's original scale
                cube.transform.localScale    = new Vector3(squareSide, squareSide, squareSide);
                cube.transform.position      = cubesContainer.transform.position + new Vector3(positions[i].x, 0.0f, positions[i].y);
                cube.transform.localRotation = Quaternion.Euler(0.0f, (float)(90.0 - Trig.RadianToDegree(angles[i])), 0.0f);
                cube.transform.parent        = cubesContainer.transform;
            }
            cubesContainer.transform.localRotation = Quaternion.identity;

            // Re-set parent's original local rotation
            if (parent != null)
            {
                parent.localRotation = parentRotation;
                parent.localScale    = parentScale;
            }

            return(cubesContainer);
        }
Example #17
0
 public virtual double XToLength(double x)
 {
     return(Integrate.OnClosedInterval(dL, -W / 2, x));
 }
Example #18
0
        public void TestIntegrateFacade()
        {
            // TargetFunctionA
            // integral_(0)^(10) exp(-x/5) (2 + sin(2 x)) dx = 9.1082

            Assert.AreEqual(
                TargetAreaA,
                Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA),
                1e-5,
                "Interval, Target 1e-08");

            Assert.AreEqual(
                TargetAreaA,
                Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA, 1e-10),
                1e-10,
                "Interval, Target 1e-10");

            // TargetFunctionB
            // integral_(0)^(1) integral_(0)^(10) exp(-x/5) (2 + sin(2 y)) dx dy = 11.7079

            Assert.AreEqual(
                Integrate.OnRectangle(TargetFunctionB, StartA, StopA, StartB, StopB),
                TargetAreaB,
                1e-12,
                "Rectangle, order 32");

            Assert.AreEqual(
                Integrate.OnRectangle(TargetFunctionB, StartA, StopA, StartB, StopB, 22),
                TargetAreaB,
                1e-10,
                "Rectangle, Order 22");

            // TargetFunctionC
            // integral_(-oo)^(oo) 1/(1 + x^2) dx = pi

            Assert.AreEqual(
                TargetAreaC,
                Integrate.DoubleExponential(TargetFunctionC, StartC, StopC),
                1e-5,
                "DoubleExponential, 1/(1 + x^2)");

            Assert.AreEqual(
                TargetAreaC,
                Integrate.DoubleExponential(TargetFunctionC, StartC, StopC, 1e-10),
                1e-10,
                "DoubleExponential, 1/(1 + x^2)");

            // TargetFunctionD
            // integral_(0)^(1) log(x) dx = -1

            Assert.AreEqual(
                TargetAreaD,
                Integrate.DoubleExponential(TargetFunctionD, StartD, StopD),
                1e-10,
                "DoubleExponential, log(x)");

            Assert.AreEqual(
                TargetAreaD,
                Integrate.GaussLegendre(TargetFunctionD, StartD, StopD, order: 1024),
                1e-10,
                "GaussLegendre, log(x), order 1024");

            Assert.AreEqual(
                TargetAreaD,
                Integrate.GaussKronrod(TargetFunctionD, StartD, StopD, 1e-10, order: 15),
                1e-10,
                "GaussKronrod, log(x), order 15");
            Assert.AreEqual(
                TargetAreaD,
                Integrate.GaussKronrod(TargetFunctionD, StartD, StopD, 1e-10, order: 21),
                1e-10,
                "GaussKronrod, log(x), order 21");
            Assert.AreEqual(
                TargetAreaD,
                Integrate.GaussKronrod(TargetFunctionD, StartD, StopD, 1e-10, order: 31),
                1e-10,
                "GaussKronrod, log(x), order 31");
            Assert.AreEqual(
                TargetAreaD,
                Integrate.GaussKronrod(TargetFunctionD, StartD, StopD, 1e-10, order: 41),
                1e-10,
                "GaussKronrod, log(x), order 41");
            Assert.AreEqual(
                TargetAreaD,
                Integrate.GaussKronrod(TargetFunctionD, StartD, StopD, 1e-10, order: 51),
                1e-10,
                "GaussKronrod, log(x), order 51");
            Assert.AreEqual(
                TargetAreaD,
                Integrate.GaussKronrod(TargetFunctionD, StartD, StopD, 1e-10, order: 61),
                1e-10,
                "GaussKronrod, log(x), order 61");

            double error, L1;
            var    Q = Integrate.GaussKronrod(TargetFunctionD, StartD, StopD, out error, out L1, 1e-10, order: 15);

            Assert.AreEqual(
                Math.Abs(TargetAreaD),
                Math.Abs(L1),
                1e-10,
                "GaussKronrod, L1");

            // TargetFunctionE
            // integral_(0)^(1) log^2(x) dx = 2

            Assert.AreEqual(
                TargetAreaE,
                Integrate.DoubleExponential(TargetFunctionE, StartE, StopE),
                1e-10,
                "DoubleExponential, log^2(x)");

            Assert.AreEqual(
                TargetAreaE,
                Integrate.GaussLegendre(TargetFunctionE, StartE, StopE, order: 128),
                1e-5,
                "GaussLegendre, log^2(x), order 128");

            Assert.AreEqual(
                TargetAreaE,
                Integrate.GaussKronrod(TargetFunctionE, StartE, StopE, 1e-10, order: 15),
                1e-10,
                "GaussKronrod, log^2(x), order 15");

            // TargetFunctionF
            // integral_(0)^(oo) exp(-x) cos(x) dx = 1/2

            Assert.AreEqual(
                TargetAreaF,
                Integrate.DoubleExponential(TargetFunctionF, StartF, StopF),
                1e-10,
                "DoubleExponential, e^(-x) cos(x)");

            Assert.AreEqual(
                TargetAreaF,
                Integrate.GaussLegendre(TargetFunctionF, StartF, StopF, order: 128),
                1e-10,
                "GaussLegendre, e^(-x) cos(x), order 128");

            Assert.AreEqual(
                TargetAreaF,
                Integrate.GaussKronrod(TargetFunctionF, StartF, StopF, 1e-10, order: 15),
                1e-10,
                "GaussKronrod, e^(-x) cos(x), order 15");

            // TargetFunctionG
            // integral_(0)^(1) sqrt(x)/sqrt(1 - x^2) dx = 1.19814

            Assert.AreEqual(
                TargetAreaG,
                Integrate.DoubleExponential(TargetFunctionG, StartG, StopG),
                1e-5,
                "DoubleExponential, sqrt(x)/sqrt(1 - x^2)");

            Assert.AreEqual(
                TargetAreaG,
                Integrate.GaussLegendre(TargetFunctionG, StartG, StopG, order: 128),
                1e-10,
                "GaussLegendre, sqrt(x)/sqrt(1 - x^2), order 128");

            Assert.AreEqual(
                TargetAreaG,
                Integrate.GaussKronrod(TargetFunctionG, StartG, StopG, 1e-10, order: 15),
                1e-10,
                "GaussKronrod, sqrt(x)/sqrt(1 - x^2), order 15");
        }
Example #19
0
        public static double integral(Expr expr, string varName, double a, double b)
        {
            var f = expr.Compile(varName);

            return(Integrate.OnClosedInterval(f, a, b));
        }
 public static double RealResult()
 {
     return(Integrate.OnClosedInterval(Integral, 1.5, 2.3, 1e-15));
 }
Example #21
0
        //计算信噪比
        public double SNR(double reflectivity, double altitudeAngle, double AOD,
                          double viewAngle, double sensorSize, double cameraSize,
                          double initialBand, double endBand, double integralTime)
        {
            double f   = 1.0 / (2 * integralTime);
            double tao = Math.Exp(-1 * AOD);
            double A0  = 3.14 * (cameraSize / 2.0) * (cameraSize / 2.0);
            double D   = cameraSize * Math.Sqrt(sensorSize * f);//D不确定
            double snr = D * reflectivity * viewAngle * A0 * tao * Math.Cos(altitudeAngle) * Math.Sqrt(integralTime) * Integrate.OnClosedInterval(x => x * x, 0, 10);

            return(snr);
        }