Exemple #1
0
        // --------------------------------------------
        // --------- ЧИСЛЕННОЕ ИНТЕГРИРОВАНИЕ ---------
        // --------------------------------------------

        /// <summary>
        /// Returns the Monte-Carlo stochastic approximation of the function integral.
        ///
        /// Requirements: calculator for the function argument/value should provide
        /// reasonable implementation of the 'fromDouble()' method.
        /// </summary>
        /// <typeparam name="T">The type of the function's argument/value.</typeparam>
        /// <typeparam name="C">The calculator for the function argument.</typeparam>
        /// <param name="obj">The calling function object.</param>
        /// <param name="generator">The uniform distribution (pseudo)random generator.</param>
        /// <param name="interval">The interval on which the integral will be approximated.</param>
        /// <param name="rectangleHeight">The height of the testing rectangle. Should be more than the function's absolute maximum on the interval tested, otherwise the method would return wrong results. On the other side, the difference between the height and the function's absolute maximum should not be very large as it will reduce the accuracy of the method. The ideal case is equality of the max f(x) on [a; b] and the rectangle height.</param>
        /// <param name="throwsCount">A positive integer value of overall tests.</param>
        /// <returns>The value approximating the function integral on the interval specified.</returns>
        public static T IntegralMonteCarloApproximation <T, C>(this IFunction <T, T> obj, IRandomBounded <T> generator, BoundedInterval <T, C> interval, T rectangleHeight, T throwsCount) where C : ICalc <T>, new()
        {
            ICalc <T> calc = Numeric <T, C> .Calculator;

            T         hits = calc.zero; // overall hits.
            Point <T> randomPoint;      // random point.

            if (!calc.mor(throwsCount, calc.zero))
            {
                throw new ArgumentException("The amount of point throws count should be a positive integer value.");
            }

            for (T i = calc.zero; calc.mor(throwsCount, i); i = calc.increment(i))
            {
                randomPoint = new Point <T>(generator.Next(interval.LeftBound, interval.RightBound), generator.Next(calc.zero, rectangleHeight));

                // Если попали под функцию - увеличиваем количество хитов.

                if (!calc.mor(randomPoint.Y, obj.Value(randomPoint.X)))
                {
                    hits = calc.increment(hits);
                }
            }

            T result = calc.div(calc.mul(calc.mul(interval.Length, rectangleHeight), hits), throwsCount);

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Converts a numeric sequence into a list of index-value points, where index has the same numeric type
        /// as the elements of the sequence.
        /// </summary>
        /// <typeparam name="T">The type of elements in the sequence.</typeparam>
        /// <typeparam name="C">A calculator for the <typeparamref name="T"/> type.</typeparam>
        /// <param name="sequence">The calling sequence object.</param>
        /// <returns>a list of index-value points, where index has the same numeric type as the elements of the sequence.</returns>
        public static List <Point <T> > toListOfIndexValuePoints <T, C>(this IEnumerable <T> sequence) where C : ICalc <T>, new()
        {
            ICalc <T> calc = Numeric <T, C> .Calculator;

            T index = calc.zero;

            List <Point <T> > result = new List <Point <T> >(sequence.Count());

            foreach (T value in sequence)
            {
                result.Add(new Point <T>(index, value));
                index = calc.increment(index);
            }

            return(result);
        }