コード例 #1
0
ファイル: PerceptronUtils.cs プロジェクト: 2021SIA/TP3
        public static (List <(Vector <double>[], Vector <double>[])>, List <(Vector <double>[], Vector <double>[])>) GetNTestGroups(IEnumerable <Vector <double> > input, IEnumerable <Vector <double> > output, int testSize, int N)
        {
            var inputGroups  = new List <(Vector <double>[], Vector <double>[])>();
            var outputGroups = new List <(Vector <double>[], Vector <double>[])>();

            if (testSize == 0)
            {
                inputGroups.Add((input.ToArray(), new Vector <double> [0]));
                outputGroups.Add((output.ToArray(), new Vector <double> [0]));
            }
            else
            {
                for (int i = 0; i < N; i++)
                {
                    int[] rand        = Combinatorics.GeneratePermutation(input.Count());
                    var   testInput   = rand.Take(testSize).Select(input.ElementAt);
                    var   trainInput  = rand.Skip(testSize).Select(input.ElementAt);
                    var   testOutput  = rand.Take(testSize).Select(output.ElementAt);
                    var   trainOutput = rand.Skip(testSize).Select(output.ElementAt);
                    inputGroups.Add((testInput.ToArray(), trainInput.ToArray()));
                    outputGroups.Add((testOutput.ToArray(), trainOutput.ToArray()));
                }
            }
            return(inputGroups, outputGroups);
        }
コード例 #2
0
        public IObservable <byte[]> Process <TSource>(IObservable <TSource> source, IObservable <Random> randomSource)
        {
            var distributionSource = randomSource.Select(random => new DiscreteUniform(0, 1, random));

            return(source.CombineLatest(
                       distributionSource,
                       (input, distribution) =>
            {
                var result = new byte[Rows * Columns];
                for (int i = 0; i < result.Length; i++)
                {
                    if (i < ActiveQuads)
                    {
                        result[i] = (byte)(distribution.Sample() * byte.MaxValue);
                    }
                    else
                    {
                        result[i] = 128;
                    }
                }

                Combinatorics.SelectPermutationInplace(result, distribution.RandomSource);
                return result;
            }));
        }
コード例 #3
0
ファイル: Polynomial.cs プロジェクト: zadiran/framework
        /// <summary>
        ///   Projects an input point into feature space.
        /// </summary>
        ///
        /// <param name="input">The input point to be projected into feature space.</param>
        /// <param name="constant">The <see cref="Constant"/> parameter of the kernel.</param>
        /// <param name="degree">The <see cref="Degree"/> parameter of the kernel.</param>
        ///
        /// <returns>
        ///   The feature space representation of the given <paramref name="input"/> point.
        /// </returns>
        ///
        public static double[] Transform(double[] input, int degree, double constant)
        {
            if (constant != 0)
            {
                throw new NotSupportedException(
                          "The explicit feature-space projection function for degrees "
                          + " higher than two is only available for homogeneous kernels"
                          + " (i.e. kernel functions with the constant term set to zero).");
            }

            int n = input.Length;
            int m = (int)Math.Pow(n, degree);

            double[] features = new double[m];

            int index = 0;

            foreach (int[] s in Combinatorics.Sequences(input.Length, degree))
            {
                double prod = 1;
                for (int i = 0; i < s.Length; i++)
                {
                    prod *= input[s[i]];
                }
                features[index++] = prod;
            }

            return(features);
        }
コード例 #4
0
 public void Combinatorics_GetPermutations_OneString_ShouldNotContainsDuplicates()
 {
     ICombinatorics combinator = new Combinatorics();
     var combinations = combinator.GetPermutation("albero");
     var duplicateItems = combinations.GroupBy(x => x).Where(grouped => grouped.Count() > 1).Select(grouped => grouped.Key);
     Assert.IsTrue(!duplicateItems.Any());
 }
コード例 #5
0
 public IObservable <TElement> Process <TElement>(IObservable <TElement> source)
 {
     return(source
            .ToArray()
            .Do(elements => Combinatorics.SelectPermutationInplace(elements))
            .SelectMany(elements => elements));
 }
コード例 #6
0
        private static List <Tuple <uint, uint> > CreatePairs2(List <uint> aPlayers, ref List <Tuple <uint, uint> > aPreviousPairs, int aTotalPlayers)
        {
            if (aPlayers.Count % 2 > 0)
            {
                throw new Exception("Not even count");
            }

            var lPlayers = aPlayers.ToList();
            var lReturn  = new List <Tuple <uint, uint> >();

            var lAssertCounter = 0;

            for (int i = 0; i < aPlayers.Count / 2; i++)
            {
                uint[] lGenerated;
                do
                {
                    lGenerated = lPlayers.SelectCombination(2, mRandom).ToArray();
                    SwapIfNeeded(lGenerated);
                    lAssertCounter++;
                    if (lAssertCounter > 100000)
                    {
                        return(CreatePairs2(aPlayers, ref aPreviousPairs, aTotalPlayers));
                    }
                } while (PairDuplicated(aPreviousPairs, lGenerated[0], lGenerated[1]) &&
                         Combinatorics.Combinations(aTotalPlayers, 2) > aPreviousPairs.Count * 2);

                lPlayers.Remove(lGenerated[0]);
                lPlayers.Remove(lGenerated[1]);

                lReturn.Add(new Tuple <uint, uint>(lGenerated[0], lGenerated[1]));
            }
            return(lReturn);
        }
コード例 #7
0
        public void combinations_of_size_k()
        {
            // Let's say we would like to compute all the
            // combinations of size 2 the elements (1,2,3):
            //
            int[] elements = new[] { 1, 2, 3 };

            // The number of possible subsets might be too large
            // to fit on memory. For this reason, we can compute
            // values on-the-fly using foreach:

            foreach (int[] combination in Combinatorics.Combinations(elements, k: 2))
            {
                // ...
            }

            // Or we could try to compute them all and store in an array:
            int[][] combinations = Combinatorics.Combinations(elements, k: 2).ToArray();

            // In either case, the result will be:

            int[][] expected =
            {
                new [] { 1, 2 },
                new [] { 1, 3 },
                new [] { 2, 3 },
            };

            string str = combinations.Apply(x => x.ToArray()).ToCSharp();

            for (int i = 0; i < combinations.Length; i++)
            {
                CollectionAssert.AreEqual(combinations[i], expected[i]);
            }
        }
コード例 #8
0
        /// <inheritdoc />
        public double Evaluate(ClusterSet <TInstance> clusterSet, IDictionary <TInstance, TClass> instanceClasses)
        {
            // counts the positives for each cluster
            var truePositives = 0L;
            var positives     = 0L;

            foreach (var cluster in clusterSet)
            {
                // gets class counts
                var clusterClassCounts = new Dictionary <TClass, int>();
                foreach (var instance in cluster)
                {
                    var instanceClass = instanceClasses[instance];
                    if (clusterClassCounts.ContainsKey(instanceClass))
                    {
                        clusterClassCounts[instanceClass]++;
                    }
                    else
                    {
                        clusterClassCounts[instanceClass] = 1;
                    }
                }

                // updates positives
                positives += Combinatorics.GetCombinations(cluster.Count, 2);

                // updates true positives (pairs of same class within cluster)
                truePositives += clusterClassCounts.Values
                                 .Where(count => count > 1)
                                 .Sum(count => Combinatorics.GetCombinations(count, 2));
            }

            // returns precision
            return((double)truePositives / positives);
        }
コード例 #9
0
 public void CanCountVariationsWithRepetition([Values(0, 1, 10, 10, 10, 10, 10, 10, 10)] int n, [Values(0, 0, 0, 2, 4, 6, 9, 10, 11)] int k, [Values(1, 1, 1, 100, 10000, 1000000, 1000000000, 10000000000, 100000000000)] long expected)
 {
     Assert.AreEqual(
         expected,
         Combinatorics.VariationsWithRepetition(n, k),
         "Count the number of variations with repetition");
 }
コード例 #10
0
 public void OutOfRangeVariationsWithRepetitionCountToZero([Values(0, 0, 1, -1, -1)] int n, [Values(1, -1, -1, 0, 1)] int k)
 {
     Assert.AreEqual(
         0,
         Combinatorics.VariationsWithRepetition(n, k),
         "The number of variations with repetition but out of the range must be 0.");
 }
コード例 #11
0
 public void CanCountPermutations([Values(0, 1, 2, 8, 15)] int n, [Values(1, 1, 2, 40320, 1307674368000)] long expected)
 {
     Assert.AreEqual(
         expected,
         Combinatorics.Permutations(n),
         "Count the number of permutations");
 }
コード例 #12
0
 public void CanCountVariations([Values(0, 1, 10, 10, 10, 10, 10, 10)] int n, [Values(0, 0, 0, 2, 4, 6, 9, 10)] int k, [Values(1, 1, 1, 90, 5040, 151200, 3628800, 3628800)] long expected)
 {
     Assert.AreEqual(
         expected,
         Combinatorics.Variations(n, k),
         "Count the number of variations without repetition");
 }
コード例 #13
0
 public void CanCountCombinationsWithRepetition([Values(0, 1, 10, 10, 10, 10, 10, 10, 10)] int n, [Values(0, 0, 0, 2, 4, 6, 9, 10, 11)] int k, [Values(1, 1, 1, 55, 715, 5005, 48620, 92378, 167960)] long expected)
 {
     Assert.AreEqual(
         expected,
         Combinatorics.CombinationsWithRepetition(n, k),
         "Count the number of combinations with repetition");
 }
コード例 #14
0
 public void OutOfRangeCombinationsCountToZero([Values(0, 10, 0, 1, -1, -1)] int n, [Values(1, 11, -1, -1, 0, 1)] int k)
 {
     Assert.AreEqual(
         0,
         Combinatorics.Combinations(n, k),
         "The number of combinations without repetition but out of the range must be 0.");
 }
コード例 #15
0
 public void CanCountCombinations([Values(0, 1, 10, 10, 10, 10, 10, 10)] int n, [Values(0, 0, 0, 2, 4, 6, 9, 10)] int k, [Values(1, 1, 1, 45, 210, 210, 10, 1)] long expected)
 {
     Assert.AreEqual(
         expected,
         Combinatorics.Combinations(n, k),
         "Count the number of combinations without repetition");
 }
コード例 #16
0
        public void GetAllPartitions()
        {
            var partitions = Combinatorics.GetAllPartitions(new[] { 1, 2, 3, 4 }).ToArray();

            int[][][] sets =
            {
                new int[][] { new int[] { 1, 2, 3, 4 } },
                new int[][] { new int[] { 1, 3,4 }, new int[] { 2 } },
                new int[][] { new int[] { 1, 2,4 }, new int[] { 3 } },
                new int[][] { new int[] { 1,4 }, new int[] { 2, 3 } },
                new int[][] { new int[] { 1,4 }, new int[] { 2 }, new int[] { 3 } },
                new int[][] { new int[] { 1, 2,3 }, new int[] { 4 } },
                new int[][] { new int[] { 1,3 }, new int[] { 2, 4 } },
                new int[][] { new int[] { 1,3 }, new int[] { 2 }, new int[] { 4 } },
                new int[][] { new int[] { 1,2 }, new int[] { 3, 4 } },
                new int[][] { new int[] { 1,2 }, new int[] { 3 }, new int[] { 4 } },
                new int[][] { new int[] {1 }, new int[] { 2, 3, 4 } },
                new int[][] { new int[] {1 }, new int[] { 2, 4 }, new int[] { 3 } },
                new int[][] { new int[] {1 }, new int[] { 2, 3 }, new int[] { 4 } },
                new int[][] { new int[] {1 }, new int[] { 2 }, new int[] { 3, 4 } },
                new int[][] { new int[] {1 }, new int[] { 2 }, new int[] { 3 }, new int[] { 4 } }
            };

            Assert.AreEqual(sets.Length, partitions.Length);
            for (int i = 0; i < sets.Length; i++)
            {
                Assert.AreEqual(sets[i].Length, partitions[i].Length);
                for (int j = 0; j < sets[i].Length; j++)
                {
                    Assert.IsTrue(sets[i][j].SequenceEqual(partitions[i][j]));
                }
            }
        }
コード例 #17
0
    public void KOfNTest()
    {
        void AllKOfN(int n, int k, bool exactlyK)
        {
            Out.WriteLine($"{k} of {n} {(exactlyK ? "(exactly)" : "")}:");
            var allKofN = Combinatorics.KOfN(n, k, exactlyK);

            foreach (var subset in allKofN)
            {
                Out.WriteLine("  " + subset.ToDelimitedString());
            }
            if (exactlyK)
            {
                var expected = (int)Combinatorics.Cnk(n, k);
                Assert.Equal(expected, allKofN.Count());
            }
        }

        AllKOfN(5, 5, false);
        AllKOfN(5, 4, true);
        AllKOfN(5, 4, false);
        AllKOfN(5, 3, true);
        AllKOfN(5, 3, false);
        AllKOfN(5, 2, true);
        AllKOfN(5, 2, false);
    }
コード例 #18
0
 public void CanCountVariationsWithRepetition(int n, int k, long expected)
 {
     Assert.AreEqual(
         expected,
         Combinatorics.VariationsWithRepetition(n, k),
         "Count the number of variations with repetition");
 }
コード例 #19
0
        unsafe static void Main(string[] args)
        {
            init();

            // load mnist
            var imagesTrain = LoadImages(args[0]);
            var labelsTrain = LoadLabels(args[1]);
            var imagesTest  = LoadImages(args[2]);
            var labelsTest  = LoadLabels(args[3]);

            var imagesShuffled = new float[imagesTrain.GetLength(0), 28 * 28];
            var labelsShuffled = new long[labelsTrain.GetLength(0)];

            for (int epoch = 0; epoch < 10; ++epoch)
            {
                // shuffle
                var idxs = Combinatorics.GeneratePermutation(imagesTrain.GetLength(0));
                int i    = 0;
                foreach (int idx in idxs)
                {
                    Array.Copy(imagesTrain, idx * 28 * 28, imagesShuffled, i * 28 * 28, 28 * 28);
                    labelsShuffled[i] = labelsTrain[idx];
                    ++i;
                }

                fixed(float *pImagesTrain = &imagesShuffled[0, 0])
                fixed(long *pLabelsTrain = &labelsShuffled[0])
                fixed(float *pImagesTest = &imagesTest[0, 0])
                fixed(long *pLabelsTest  = &labelsTest[0])
                {
                    train(pImagesTrain, pLabelsTrain, imagesTrain.GetLength(0));
                    test(pImagesTest, pLabelsTest, imagesTest.GetLength(0));
                }
            }
        }
コード例 #20
0
 public void CanCountPermutations(int n, long expected)
 {
     Assert.AreEqual(
         expected,
         Combinatorics.Permutations(n),
         "Count the number of permutations");
 }
コード例 #21
0
 public void OutOfRangeVariationsCountToZero(int n, int k)
 {
     Assert.AreEqual(
         0,
         Combinatorics.Variations(n, k),
         "The number of variations without repetition but out of the range must be 0.");
 }
コード例 #22
0
 public void CanCountCombinations(int n, int k, long expected)
 {
     Assert.AreEqual(
         expected,
         Combinatorics.Combinations(n, k),
         "Count the number of combinations without repetition");
 }
コード例 #23
0
 public void OutOfRangeCombinationsWithRepetitionCountToZero(int n, int k)
 {
     Assert.AreEqual(
         0,
         Combinatorics.CombinationsWithRepetition(n, k),
         "The number of combinations with repetition but out of the range must be 0.");
 }
コード例 #24
0
        /// <summary>
        ///   Creates a new Wilcoxon's W+ distribution.
        /// </summary>
        ///
        /// <param name="ranks">The rank statistics for the samples.</param>
        /// <param name="forceExact">True to compute the exact test. May requires
        ///   a significant amount of processing power for large samples (n > 12).</param>
        ///
        public WilcoxonDistribution(double[] ranks, bool forceExact)
        {
            // Remove zero elements
            int[] idx = ranks.Find(x => x != 0);
            this.Ranks   = ranks.Submatrix(idx);
            this.Samples = idx.Length;

            if (forceExact || Samples < 12)
            {
                // For a small sample (i.e. n < 12)
                // the distribution will be exact.

                // Generate all possible combinations in advance
                int[][] combinations = Combinatorics.TruthTable(Samples);

                // Transform binary into sign combinations
                for (int i = 0; i < combinations.Length; i++)
                {
                    for (int j = 0; j < combinations[i].Length; j++)
                    {
                        combinations[i][j] = Math.Sign(combinations[i][j] * 2 - 1);
                    }
                }

                // Compute all possible values for W+ considering those signs
                this.table = new double[combinations.Length];
                for (int i = 0; i < combinations.Length; i++)
                {
                    table[i] = WPositive(combinations[i], ranks);
                }

                Array.Sort(table);
            }
        }
コード例 #25
0
        public void TruthTableTest2()
        {
            // Suppose we would like to generate a truth table (i.e. all possible
            // combinations of a set of discrete symbols) for variables that contain
            // different numbers symbols. Let's say, for example, that the first
            // variable may contain symbols 0 and 1, the second could contain either
            // 0, 1, or 2, and the last one again could contain only 0 and 1. Thus
            // we can generate the truth table in the following way:

            int[] symbols = { 2, 3, 2 };

            int[][] actual = Combinatorics.TruthTable(symbols);

            int[][] expected =
            {
                new int[] { 0, 0, 0 },
                new int[] { 0, 0, 1 },
                new int[] { 0, 1, 0 },
                new int[] { 0, 1, 1 },
                new int[] { 0, 2, 0 },
                new int[] { 0, 2, 1 },
                new int[] { 1, 0, 0 },
                new int[] { 1, 0, 1 },
                new int[] { 1, 1, 0 },
                new int[] { 1, 1, 1 },
                new int[] { 1, 2, 0 },
                new int[] { 1, 2, 1 },
            };

            Assert.IsTrue(expected.IsEqual(actual));
        }
コード例 #26
0
        public void CombinationsTest()
        {
            Assert.IsTrue(Combinatorics.CombinationsCount(8, 5) == 792);
            Assert.IsTrue(Combinatorics.CombinationsCount(8, 2) == 36);
            Assert.IsTrue(Combinatorics.CombinationsCount(18, 5) == 26334);
            Assert.IsTrue(Combinatorics.CombinationsCount(17, 6) == 74613);

            int[,] expected = new int[, ] {
                { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, { 0, 0, 3 }, { 0, 0, 4 }, { 0, 0, 5 }, { 0, 1, 1 }, { 0, 1, 2 },
                { 0, 1, 3 }, { 0, 1, 4 }, { 0, 1, 5 }, { 0, 2, 2 }, { 0, 2, 3 }, { 0, 2, 4 }, { 0, 2, 5 }, { 0, 3, 3 },
                { 0, 3, 4 }, { 0, 3, 5 }, { 0, 4, 4 }, { 0, 4, 5 }, { 0, 5, 5 }, { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 },
                { 1, 1, 4 }, { 1, 1, 5 }, { 1, 2, 2 }, { 1, 2, 3 }, { 1, 2, 4 }, { 1, 2, 5 }, { 1, 3, 3 }, { 1, 3, 4 },
                { 1, 3, 5 }, { 1, 4, 4 }, { 1, 4, 5 }, { 1, 5, 5 }, { 2, 2, 2 }, { 2, 2, 3 }, { 2, 2, 4 }, { 2, 2, 5 },
                { 2, 3, 3 }, { 2, 3, 4 }, { 2, 3, 5 }, { 2, 4, 4 }, { 2, 4, 5 }, { 2, 5, 5 }, { 3, 3, 3 }, { 3, 3, 4 },
                { 3, 3, 5 }, { 3, 4, 4 }, { 3, 4, 5 }, { 3, 5, 5 }, { 4, 4, 4 }, { 4, 4, 5 }, { 4, 5, 5 }, { 5, 5, 5 }
            };

            int[] arr = Enumerable.Range(0, 6).ToArray();

            Assert.IsTrue(Combinatorics.CombinationsCount(arr, 3) == 56);

            int[,] x = arr.Combinations(3).Select(X => X.ToArray()).ToArray().FromJagged();
            Assert.IsTrue(expected.ArrayEquals(x));

            int[,] x1 = arr.Combinations(3, true).Select(X => X.ToArray()).ToArray().FromJagged();
            Assert.IsTrue(expected.ArrayEquals(x1));
        }
コード例 #27
0
        public void UniquePermutationsTest()
        {
            Assert.IsTrue(Combinatorics.UniquePermutationsCount(8, 5) == 6720);
            Assert.IsTrue(Combinatorics.UniquePermutationsCount(8, 2) == 56);
            Assert.IsTrue(Combinatorics.UniquePermutationsCount(18, 5) == 1028160);
            Assert.IsTrue(Combinatorics.UniquePermutationsCount(17, 6) == 8910720);

            int[,] expected = new int[, ] {
                { 0, 1, 2 }, { 0, 1, 3 }, { 0, 1, 4 }, { 0, 1, 5 }, { 0, 2, 1 }, { 0, 2, 3 }, { 0, 2, 4 }, { 0, 2, 5 },
                { 0, 3, 1 }, { 0, 3, 2 }, { 0, 3, 4 }, { 0, 3, 5 }, { 0, 4, 1 }, { 0, 4, 2 }, { 0, 4, 3 }, { 0, 4, 5 },
                { 0, 5, 1 }, { 0, 5, 2 }, { 0, 5, 3 }, { 0, 5, 4 }, { 1, 0, 2 }, { 1, 0, 3 }, { 1, 0, 4 }, { 1, 0, 5 },
                { 1, 2, 0 }, { 1, 2, 3 }, { 1, 2, 4 }, { 1, 2, 5 }, { 1, 3, 0 }, { 1, 3, 2 }, { 1, 3, 4 }, { 1, 3, 5 },
                { 1, 4, 0 }, { 1, 4, 2 }, { 1, 4, 3 }, { 1, 4, 5 }, { 1, 5, 0 }, { 1, 5, 2 }, { 1, 5, 3 }, { 1, 5, 4 },
                { 2, 0, 1 }, { 2, 0, 3 }, { 2, 0, 4 }, { 2, 0, 5 }, { 2, 1, 0 }, { 2, 1, 3 }, { 2, 1, 4 }, { 2, 1, 5 },
                { 2, 3, 0 }, { 2, 3, 1 }, { 2, 3, 4 }, { 2, 3, 5 }, { 2, 4, 0 }, { 2, 4, 1 }, { 2, 4, 3 }, { 2, 4, 5 },
                { 2, 5, 0 }, { 2, 5, 1 }, { 2, 5, 3 }, { 2, 5, 4 }, { 3, 0, 1 }, { 3, 0, 2 }, { 3, 0, 4 }, { 3, 0, 5 },
                { 3, 1, 0 }, { 3, 1, 2 }, { 3, 1, 4 }, { 3, 1, 5 }, { 3, 2, 0 }, { 3, 2, 1 }, { 3, 2, 4 }, { 3, 2, 5 },
                { 3, 4, 0 }, { 3, 4, 1 }, { 3, 4, 2 }, { 3, 4, 5 }, { 3, 5, 0 }, { 3, 5, 1 }, { 3, 5, 2 }, { 3, 5, 4 },
                { 4, 0, 1 }, { 4, 0, 2 }, { 4, 0, 3 }, { 4, 0, 5 }, { 4, 1, 0 }, { 4, 1, 2 }, { 4, 1, 3 }, { 4, 1, 5 },
                { 4, 2, 0 }, { 4, 2, 1 }, { 4, 2, 3 }, { 4, 2, 5 }, { 4, 3, 0 }, { 4, 3, 1 }, { 4, 3, 2 }, { 4, 3, 5 },
                { 4, 5, 0 }, { 4, 5, 1 }, { 4, 5, 2 }, { 4, 5, 3 }, { 5, 0, 1 }, { 5, 0, 2 }, { 5, 0, 3 }, { 5, 0, 4 },
                { 5, 1, 0 }, { 5, 1, 2 }, { 5, 1, 3 }, { 5, 1, 4 }, { 5, 2, 0 }, { 5, 2, 1 }, { 5, 2, 3 }, { 5, 2, 4 },
                { 5, 3, 0 }, { 5, 3, 1 }, { 5, 3, 2 }, { 5, 3, 4 }, { 5, 4, 0 }, { 5, 4, 1 }, { 5, 4, 2 }, { 5, 4, 3 }
            };

            int[] arr = Enumerable.Range(0, 6).ToArray();

            Assert.IsTrue(Combinatorics.UniquePermutationsCount(arr, 3) == 120);

            int[,] x = arr.UniquePermutations(3).Select(X => X.ToArray()).ToArray().FromJagged();
            Assert.IsTrue(expected.ArrayEquals(x));

            int[,] x1 = arr.Permutations(3, false).Select(X => X.ToArray()).ToArray().FromJagged();
            Assert.IsTrue(expected.ArrayEquals(x1));
        }
コード例 #28
0
        public void UniqueCombinationsTest()
        {
            Assert.IsTrue(Combinatorics.UniqueCombinationsCount(8, 5) == 56);
            Assert.IsTrue(Combinatorics.UniqueCombinationsCount(8, 2) == 28);
            Assert.IsTrue(Combinatorics.UniqueCombinationsCount(18, 5) == 8568);
            Assert.IsTrue(Combinatorics.UniqueCombinationsCount(17, 6) == 12376);

            int[,] expected = new int[, ] {
                { 0, 1, 2 }, { 0, 1, 3 }, { 0, 1, 4 }, { 0, 1, 5 },
                { 0, 2, 3 }, { 0, 2, 4 }, { 0, 2, 5 }, { 0, 3, 4 },
                { 0, 3, 5 }, { 0, 4, 5 }, { 1, 2, 3 }, { 1, 2, 4 },
                { 1, 2, 5 }, { 1, 3, 4 }, { 1, 3, 5 }, { 1, 4, 5 },
                { 2, 3, 4 }, { 2, 3, 5 }, { 2, 4, 5 }, { 3, 4, 5 }
            };

            int[] arr = Enumerable.Range(0, 6).ToArray();

            Assert.IsTrue(Combinatorics.UniqueCombinationsCount(arr, 3) == 20);

            int[,] x = arr.UniqueCombinations(3).Select(X => X.ToArray()).ToArray().FromJagged();
            Assert.IsTrue(expected.ArrayEquals(x));

            int[,] x1 = arr.Combinations(3, false).Select(X => X.ToArray()).ToArray().FromJagged();
            Assert.IsTrue(expected.ArrayEquals(x1));
        }
コード例 #29
0
        public Anagram.Models.AnagramDataModel GetFormattedAnagrams(String phrase)
        {
            String [] resultsArray = GetAnagrams(phrase).ToArray();

            var phraseLetterValues = new LetterValues(phrase);

            if (resultsArray.Length >= 2)
            {
                var watch = Stopwatch.StartNew();


                Parallel.ForEach(Combinatorics.Combinations <String>(resultsArray, 2, false), (combination) =>
                {
                    if (CheckIfStringsHaveTheSameFrequency(combination, phrase))
                    {
                        AnagramsFormattedResults.Add(ComposeAnagramsStrings(combination));
                    }
                });

                watch.Stop();

                return(new Models.AnagramDataModel(AnagramsFormattedResults, watch.ElapsedMilliseconds));
            }
            return(new Models.AnagramDataModel(new List <string>(), 0));
        }
コード例 #30
0
        /// <summary>
        ///   Constructs a Mann-Whitney's U-statistic distribution.
        /// </summary>
        ///
        /// <param name="ranks">The rank statistics.</param>
        /// <param name="n1">The number of observations in the first sample.</param>
        /// <param name="n2">The number of observations in the second sample.</param>
        ///
        public MannWhitneyDistribution(double[] ranks, int n1, int n2)
        {
            this.Ranks    = ranks;
            this.Samples1 = n1;
            this.Samples2 = n2;
            int nt = n1 + n2;

            this.smallSample = (n1 <= 30 && n2 <= 30);

            if (smallSample)
            {
                // For a small sample (< 30) the distribution is exact.

                int nc = (int)Special.Binomial(nt, n1);
                table = new double[nc];

                int i = 0; // Consider all possible combinations of samples
                foreach (double[] combination in Combinatorics.Combinations(Ranks, n1))
                {
                    table[i++] = USample1(combination, Samples2);
                }

                Array.Sort(table);
            }
        }
コード例 #31
0
        /// <summary>
        ///   Gets the marginal distribution of a given variable evaluated at a given value.
        /// </summary>
        ///
        /// <param name="index">The variable index.</param>
        /// <param name="value">The variable value.</param>
        ///
        public double MarginalDistributionFunction(int index, int value)
        {
            double num = 0;
            double den = 0;

            int[] symbols = Support.Apply(x => x.Length);
            int[] probe   = new int[Dimension];

            foreach (int[] input in Combinatorics.Sequences(symbols, inPlace: true))
            {
                for (int i = 0; i < probe.Length; i++)
                {
                    probe[i] = input[i] + Support[i].Min;
                }

                double p = ProbabilityMassFunction(probe);

                for (int k = 0; k < probe.Length; k++)
                {
                    if (k == index)
                    {
                        continue;
                    }

                    if (probe[k] == Support[k].Min + value)
                    {
                        num += p;
                    }
                }

                den += p;
            }

            return(num == 0 ? 0 : num / den);
        }
コード例 #32
0
 public void Combinatorics_GetPermutations_OneCharString_ShouldReturn_SameString()
 {
     ICombinatorics combinator = new Combinatorics();
     var combinations = combinator.GetPermutation("a");
     Assert.IsTrue(combinations.FirstOrDefault() == "a");
 }
コード例 #33
0
 public void Combinatorics_GetPermutations_Null_ShouldReturn_EmptyStringList()
 {
     ICombinatorics combinator = new Combinatorics();
     var combinations = combinator.GetPermutation(null);
     Assert.IsTrue(!combinations.Any());
 }
コード例 #34
0
 public void Combinatorics_GetPermutations_OneString_ShouldReturn_Combinatorics()
 {
     ICombinatorics combinator = new Combinatorics();
     var combinations = combinator.GetPermutation("pippo");
     Assert.IsTrue(combinations.Any());
 }