Esempio n. 1
0
        public void Statistcs_EnumGeneralizeMeanAlgorithmTest()
        {
            var integerNumb = new IntegerDomain();
            var target      = new EnumGeneralizedMeanAlgorithm <int, double, int>(
                i => i,
                d => d,
                (d, i) => d / i,
                new DoubleField(),
                integerNumb);

            var integerSequence = new IntegerSequence();

            for (var i = 1; i < 5000; ++i)
            {
                integerSequence.Add(i);
                var expected = (i + 1) / 2.0;
                var actual   = target.Run(integerSequence);
                Assert.AreEqual(expected, actual);
            }

            integerSequence = new IntegerSequence();
            var n = 1000000;

            integerSequence.Add(1, n);
            var outerExpected = (n + 1) / 2.0;
            var outerActual   = target.Run(integerSequence);

            Assert.AreEqual(outerExpected, outerActual);

            var bigIntegerDomain = new BigIntegerDomain();
            var fractionField    = new FractionField <BigInteger>(bigIntegerDomain);
            var fractionTarget   = new EnumGeneralizedMeanAlgorithm <int, Fraction <BigInteger>, int>(
                i => new Fraction <BigInteger>(i, 1, bigIntegerDomain),
                d => d,
                (d, i) => d.Divide(i, bigIntegerDomain),
                fractionField,
                integerNumb);
            var fractionExpected = new Fraction <BigInteger>(n + 1, 2, bigIntegerDomain);
            var fractionActual   = fractionTarget.Run(integerSequence);

            Assert.AreEqual(fractionExpected, fractionActual);

            // Teste com alteração da função directa
            fractionTarget.DirectFunction = i => new Fraction <BigInteger>(new BigInteger(i) * i, 1, bigIntegerDomain);
            fractionExpected = new Fraction <BigInteger>(
                (new BigInteger(n) + BigInteger.One) * (2 * new BigInteger(n) + 1), 6, bigIntegerDomain);
            fractionActual = fractionTarget.Run(integerSequence);
            Assert.AreEqual(fractionExpected, fractionActual);

            // Teste com transformação
            var transformedTarget = new EnumGeneralizedMeanAlgorithm <BigInteger, Fraction <BigInteger>, int>(
                i => new Fraction <BigInteger>(i, 1, bigIntegerDomain),
                d => d,
                (d, i) => d.Divide(i, bigIntegerDomain),
                fractionField,
                integerNumb);
            var transformedSeq = new TransformEnumerable <int, BigInteger>(
                integerSequence,
                i => new BigInteger(i) * i);
            var transformedExpected = new Fraction <BigInteger>(
                (new BigInteger(n) + BigInteger.One) * (2 * new BigInteger(n) + 1), 6, bigIntegerDomain);
            var transformedActual = transformedTarget.Run(transformedSeq);

            Assert.AreEqual(transformedExpected, transformedActual);
        }
Esempio n. 2
0
        public void Statistcs_EnumGeneralizeMeanBlockAlgorithmTest()
        {
            var integerNumb = new IntegerDomain();
            var target      = new EnumGeneralizedMeanAlgorithm <int, double, int>(
                i => i,
                d => d,
                (d, i) => d / i,
                new DoubleField(),
                integerNumb);
            var blockNumber = 2500;

            var integerSequence = new IntegerSequence();

            for (var i = 1; i < 5500; ++i)
            {
                integerSequence.Add(i);
                var expected = (i + 1) / 2.0;
                var actual   = target.Run <double>(
                    integerSequence,
                    blockNumber,
                    (j, k) => j / (double)k,
                    (d1, d2) => d1 * d2);
                Assert.IsTrue(Math.Abs(expected - actual) < 0.0001);
            }

            integerSequence = new IntegerSequence();
            var n = 1000500;

            integerSequence.Add(1, n);
            var outerExpected = (n + 1) / 2.0;
            var outerActual   = target.Run <double>(
                integerSequence,
                blockNumber,
                (j, k) => j / (double)k,
                (d1, d2) => d1 * d2);

            Assert.AreEqual(outerExpected, outerActual);

            var integerDomain = new BigIntegerDomain();
            var fractionField = new FractionField <BigInteger>(integerDomain);
            var fracTarget    = new EnumGeneralizedMeanAlgorithm <int, Fraction <BigInteger>, int>(
                i => new Fraction <BigInteger>(i, 1, integerDomain),
                d => d,
                (d, i) => d.Divide(i, integerDomain),
                fractionField,
                integerNumb);

            var fractionExpected = new Fraction <BigInteger>(n + 1, 2, integerDomain);
            var fractionActual   = fracTarget.Run <Fraction <BigInteger> >(
                integerSequence,
                blockNumber,
                (j, k) => new Fraction <BigInteger>(j, k, integerDomain),
                (d1, d2) => d1.Multiply(d2, integerDomain));

            Assert.AreEqual(fractionExpected, fractionActual);

            // Teste com alteração da função directa
            fracTarget.DirectFunction = i => new Fraction <BigInteger>(new BigInteger(i) * i, 1, integerDomain);
            fractionExpected          = new Fraction <BigInteger>(
                (new BigInteger(n) + BigInteger.One) * (2 * new BigInteger(n) + 1), 6, integerDomain);
            fractionActual = fracTarget.Run <Fraction <BigInteger> >(
                integerSequence,
                blockNumber,
                (j, k) => new Fraction <BigInteger>(j, k, integerDomain),
                (d1, d2) => d1.Multiply(d2, integerDomain));

            // Teste com transformação
            var transformedTarget = new EnumGeneralizedMeanAlgorithm <BigInteger, Fraction <BigInteger>, int>(
                i => new Fraction <BigInteger>(i, 1, integerDomain),
                d => d,
                (d, i) => d.Divide(i, integerDomain),
                fractionField,
                integerNumb);
            var transformedSeq = new TransformEnumerable <int, BigInteger>(
                integerSequence,
                i => new BigInteger(i) * i);
            var transformedExpected = new Fraction <BigInteger>(
                (new BigInteger(n) + BigInteger.One) * (2 * new BigInteger(n) + 1), 6, integerDomain);
            var transformedActual = transformedTarget.Run <Fraction <BigInteger> >(
                transformedSeq,
                blockNumber,
                (j, k) => new Fraction <BigInteger>(j, k, integerDomain),
                (d1, d2) => d1.Multiply(d2, integerDomain));

            Assert.AreEqual(transformedExpected, transformedActual);
        }