Exemplo n.º 1
0
        public void SumOfSquares()
        {
            var sampler = new UniformDistributionSampler(20.0, true, 0);

            // Test with a range of array lengths;
            // the vectorised code has edge cases related to array length, so this is a sensible test to do.
            for (int len = 1; len < 20; len++)
            {
                SumOfSquares_Inner(sampler, len);
            }
        }
Exemplo n.º 2
0
        public void MeanSquaredDelta()
        {
            var sampler = new UniformDistributionSampler(10f, true, 0);

            // Test with a range of array lengths;
            // the vectorised code has edge cases related to array length, so this is a sensible test to do.
            for (int len = 1; len < 40; len++)
            {
                MeanSquaredDelta_Inner(sampler, len);
            }
        }
Exemplo n.º 3
0
        private static void Max_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            float[] a = new float[len];
            sampler.Sample(a);

            // Calc results and compare.
            float expected = PointwiseMax(a);
            float actual   = MathSpan.Max(a);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 4
0
        private static void Min_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            double[] a = new double[len];
            sampler.Sample(a);

            // Calc results and compare.
            double expected = PointwiseMin(a);
            double actual   = MathSpan.Min(a);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 5
0
        private static void SumOfSquares_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc array and fill with uniform random noise.
            float[] x = new float[len];
            sampler.Sample(x);

            // Sum the array elements.
            float expected = PointwiseSumOfSquares(x);
            float actual   = MathSpan.SumOfSquares(x);

            // Compare expected and actual sum.
            Assert.Equal(expected, actual, 3);
        }
Exemplo n.º 6
0
        private static void Sum_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc array and fill with uniform random noise.
            double[] x = new double[len];
            sampler.Sample(x);

            // Sum the array elements.
            double expected = PointwiseSum(x);
            double actual   = MathSpan.Sum(x);

            // Compare expected and actual sum.
            Assert.Equal(expected, actual, 12);
        }
Exemplo n.º 7
0
        private static void MinMax_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            double[] a = new double[len];
            sampler.Sample(a);

            // Calc results and compare.
            PointwiseMinMax(a, out double expectedMin, out double expectedMax);
            MathSpan.MinMax(a, out double actualMin, out double actualMax);

            Assert.Equal(expectedMin, actualMin, 10);
            Assert.Equal(expectedMax, actualMax, 10);
        }
Exemplo n.º 8
0
        private static void MeanSquaredDelta_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            float[] a = new float[len];
            float[] b = new float[len];
            sampler.Sample(a);
            sampler.Sample(b);

            // Calc results and compare.
            float expected = PointwiseSumSquaredDelta(a, b) / a.Length;
            float actual   = MathSpan.MeanSquaredDelta(a, b);

            Assert.Equal(expected, actual, 3);
        }
Exemplo n.º 9
0
        private static void SumSquaredDelta_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            double[] a = new double[len];
            double[] b = new double[len];
            sampler.Sample(a);
            sampler.Sample(b);

            // Calc results and compare.
            double expected = PointwiseSumSquaredDelta(a, b);
            double actual   = MathSpanUtils.SumSquaredDelta(a, b);

            Assert.Equal(expected, actual, 10);
        }
Exemplo n.º 10
0
        private static void MeanSquaredDelta(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            double[] a = new double[len];
            double[] b = new double[len];
            sampler.Sample(a);
            sampler.Sample(b);

            // Calc results and compare.
            double expected = SumSquaredDelta(a, b) / a.Length;
            double actual   = MathArrayUtils.MeanSquaredDelta(a, b);

            Assert.AreEqual(expected, actual, 1e-10);
        }
Exemplo n.º 11
0
        private static void Clip_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc array and fill with uniform random noise.
            float[] x = new float[len];
            sampler.Sample(x);

            // Clip the elements of the array with the safe routine.
            float[] expected = (float[])x.Clone();
            PointwiseClip(expected, -1.1f, 18.8f);

            // Clip the elements of the array.
            float[] actual = (float[])x.Clone();
            MathSpan.Clip(actual, -1.1f, 18.8f);

            // Compare expected with actual array.
            Assert.True(SpanUtils.Equal <float>(expected, actual));
        }
Exemplo n.º 12
0
        private static void Clip(UniformDistributionSampler sampler, int len)
        {
            // Alloc array and fill with uniform random noise.
            double[] x = new double[len];
            sampler.Sample(x);

            // Clip the elements of the array with the safe routine.
            double[] expected = (double[])x.Clone();
            Clip(expected, -1.1, 18.8);

            // Clip the elements of the array.
            double[] actual = (double[])x.Clone();
            MathArrayUtils.Clip(actual, -1.1, 18.8);

            // Compare expected with actual array.
            Assert.IsTrue(ArrayUtils.Equals(expected, actual));
        }
Exemplo n.º 13
0
        public void Sample()
        {
            int sampleCount = 10_000_000;
            UniformDistributionSampler sampler = new();
            var sampleArr = new double[sampleCount];

            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = sampler.Sample();
            }

            UniformDistributionTest(sampleArr, 0.0, 1.0);

            // Configure a scale and a signed flag.
            sampler = new UniformDistributionSampler(100.0, true);

            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = sampler.Sample();
            }

            UniformDistributionTest(sampleArr, -100.0, 100.0);
        }