Skip to content

Commit

Permalink
added cartesian product
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxRev-Dev committed Oct 29, 2019
1 parent 9c3e605 commit 5a93af1
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
41 changes: 41 additions & 0 deletions BinaryRelations/Matrix/MatrixExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -548,5 +549,45 @@ private static void ThrowIfSizeNotEqual<T>(this T[,] array1, in T[,] array2)
}

#endregion

#region Combinatorics

public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from acc in accumulator
from item in sequence
select acc.Concat(new[] { item }));
}

public static IEnumerable<IEnumerable<IEnumerable<int>>> CartesianProductDistinctPairs(int size, int start = 1)
{
var range = Enumerable.Range(start, size).ToArray();
for (var i = 1; i <= size; i++)
yield return Permutations(range, i);
}

public static IEnumerable<IEnumerable<T>> Permutations<T>(IEnumerable<T> array, int elementsInArray)
{
var range = array as T[] ?? array.ToArray();
var i = 1;
foreach (var item in range)
{
if (elementsInArray == 1)
{
yield return new[] { item };
}
else
{
foreach (var result in Permutations(range.Skip(i++), elementsInArray - 1))
yield return new[] { item }.Concat(result);
}
}
}

#endregion
}
}
76 changes: 76 additions & 0 deletions BinaryRelationsTests/MatrixOperationsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using BinaryRelationsTests.Helpers;
using MaxRev.Extensions.Binary;
using MaxRev.Extensions.Matrix;
Expand Down Expand Up @@ -83,5 +85,79 @@ public void SubtractMatrices()
};
Assert.Equal(expected, m1.Subtract(m2));
}

[Fact]
public void Permutations()
{
var expected = new[]
{
new[] {1, 2, 3},
new[] {1, 2, 4},
new[] {1, 2, 5},
new[] {1, 3, 4},
new[] {1, 3, 5},
new[] {1, 4, 5},
new[] {2, 3, 4},
new[] {2, 3, 5},
new[] {2, 4, 5},
new[] {3, 4, 5},
};
Assert.Equal(expected, MatrixExtensions.Permutations(Enumerable.Range(1, 5), 3));
}

[Fact]
public void CartesianProductDistinctPairs()
{
var expected = new[] {
new[]
{
new[] {1},
new[] {2},
new[] {3},
new[] {4},
new[] {5},
},
new[]
{
new[] {1, 2},
new[] {1, 3},
new[] {1, 4},
new[] {1, 5},
new[] {2, 3},
new[] {2, 4},
new[] {2, 5},
new[] {3, 4},
new[] {3, 5},
new[] {4, 5},
},
new[]
{
new[] {1, 2, 3},
new[] {1, 2, 4},
new[] {1, 2, 5},
new[] {1, 3, 4},
new[] {1, 3, 5},
new[] {1, 4, 5},
new[] {2, 3, 4},
new[] {2, 3, 5},
new[] {2, 4, 5},
new[] {3, 4, 5},
},
new[]
{
new[] {1, 2, 3, 4},
new[] {1, 2, 3, 5},
new[] {1, 2, 4, 5},
new[] {1, 3, 4, 5},
new[] {2, 3, 4, 5},
},
new []
{
new[] {1, 2, 3, 4, 5}
}
};

Assert.Equal(expected, MatrixExtensions.CartesianProductDistinctPairs(5));
}
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ Provides a bunch of extension methods for binary matrices including:
- Randomize
- AllocateRandomSquareMatrix
- AllocateRandomVector
- CartesianProduct
- CartesianProductDistinctPairs

0 comments on commit 5a93af1

Please sign in to comment.