Ejemplo n.º 1
0
        public void Gaussian(
            int sampleSize,
            double[] destinationArray,
            int destinationIndex,
            double mu,
            double sigma)
        {
            ProbabilityDistribution.ValidateSampleInput(
                sampleSize,
                destinationArray,
                destinationIndex);

            if (sigma <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sigma),
                                                      ImplementationServices.GetResourceString(
                                                          "STR_EXCEPT_PAR_MUST_BE_POSITIVE"));
            }

            unsafe
            {
                fixed(double *destinationPointer = &destinationArray[destinationIndex])
                {
                    SafeNativeMethods.VSL.vdRngGaussian(
                        0,
                        this.descriptor.DangerousGetHandle().ToPointer(),
                        sampleSize,
                        destinationPointer,
                        mu,
                        sigma);
                }
            }
        }
Ejemplo n.º 2
0
        public void Uniform(
            int sampleSize,
            double[] destinationArray,
            int destinationIndex,
            double lowerBound,
            double upperBound)
        {
            ProbabilityDistribution.ValidateSampleInput(
                sampleSize,
                destinationArray,
                destinationIndex);

            UniformDistribution.ValidateBounds(
                lowerBound,
                upperBound);

            unsafe
            {
                fixed(double *destinationPointer = &destinationArray[destinationIndex])
                {
                    SafeNativeMethods.VSL.vdRngUniform(
                        0,
                        this.descriptor.DangerousGetHandle().ToPointer(),
                        sampleSize,
                        destinationPointer,
                        lowerBound,
                        upperBound);
                }
            }
        }
Ejemplo n.º 3
0
        internal static void ValidateSampleInput(
            int sampleSize,
            int[] destinationArray,
            int destinationIndex)
        {
            ProbabilityDistribution.ValidateSampleInput(sampleSize);

            if (destinationArray is null)
            {
                throw new ArgumentNullException(nameof(destinationArray));
            }

            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationIndex),
                                                      ImplementationServices.GetResourceString(
                                                          "STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE"));
            }

            int length = destinationArray.Length;

            if (sampleSize > (length - destinationIndex))
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              ImplementationServices.GetResourceString(
                                  "STR_EXCEPT_PDF_SAMPLESIZE_ARRAYLENGTH_MISMATCH"),
                              nameof(sampleSize),
                              nameof(destinationArray),
                              nameof(destinationIndex)),
                          nameof(sampleSize));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Draws a sample from this instance having the specified size
        /// and returns it
        /// in a destination array.
        /// </summary>
        /// <param name="sampleSize">The sample size.</param>
        /// <param name="destinationArray">The destination array that
        /// receives the sampled data.</param>
        /// <param name="destinationIndex">The index in
        /// <paramref name="destinationArray"/> at which storing
        /// begins.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="destinationArray"/> is <b>null</b>.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="sampleSize"/> is not positive.<br/>
        /// -or-<br/>
        /// <paramref name="destinationIndex"/> is negative.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Parameter <paramref name="sampleSize"/> must be less than or
        /// equal to the
        /// difference between the length of
        /// parameter <paramref name="destinationArray"/> and
        /// <paramref name="destinationIndex"/>.
        /// </exception>
        public void Sample(
            int sampleSize,
            double[] destinationArray,
            int destinationIndex)
        {
            ProbabilityDistribution.ValidateSampleInput(
                sampleSize, destinationArray, destinationIndex);

            this.OnSample(sampleSize, destinationArray, destinationIndex);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Draws a sample from this instance having the specified size
        /// and returns it as a matrix.
        /// </summary>
        /// <param name="sampleSize">The sample size.</param>
        /// <returns>The matrix whose entries store the sample.</returns>
        /// <remarks>
        /// <para>
        /// The returned matrix has one column and a number of rows
        /// equal to <paramref name="sampleSize"/>.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="sampleSize"/> is not positive.
        /// </exception>
        public DoubleMatrix Sample(int sampleSize)
        {
            ProbabilityDistribution.ValidateSampleInput(sampleSize);

            DoubleMatrix results = DoubleMatrix.Dense(
                sampleSize,
                1);

            double[] resultsArray = results.GetStorage();

            this.OnSample(sampleSize, resultsArray, 0);

            return(results);
        }
Ejemplo n.º 6
0
        public void DefaultUniform(
            int sampleSize,
            double[] destinationArray,
            int destinationIndex)
        {
            ProbabilityDistribution.ValidateSampleInput(
                sampleSize,
                destinationArray,
                destinationIndex);

            unsafe
            {
                fixed(double *destinationPointer = &destinationArray[destinationIndex])
                {
                    SafeNativeMethods.VSL.vdRngUniform(
                        0,
                        this.descriptor.DangerousGetHandle().ToPointer(),
                        sampleSize,
                        destinationPointer,
                        0.0,
                        1.0);
                }
            }
        }