Gapotchenko.FX.Math.Combinatorics
2022.1.4
See the version list below for details.
dotnet add package Gapotchenko.FX.Math.Combinatorics --version 2022.1.4
NuGet\Install-Package Gapotchenko.FX.Math.Combinatorics -Version 2022.1.4
<PackageReference Include="Gapotchenko.FX.Math.Combinatorics" Version="2022.1.4" />
paket add Gapotchenko.FX.Math.Combinatorics --version 2022.1.4
#r "nuget: Gapotchenko.FX.Math.Combinatorics, 2022.1.4"
// Install Gapotchenko.FX.Math.Combinatorics as a Cake Addin
#addin nuget:?package=Gapotchenko.FX.Math.Combinatorics&version=2022.1.4
// Install Gapotchenko.FX.Math.Combinatorics as a Cake Tool
#tool nuget:?package=Gapotchenko.FX.Math.Combinatorics&version=2022.1.4
Overview
The module provides the math operations for combinatorics.
Permutations
Permutation is a widely used operation that returns all possible permutations of elements in a sequence.
Let's take a look at the sample code:
using Gapotchenko.FX.Math.Combinatorics;
var seq = new[] { "A", "B", "C" };
foreach (var i in Permutations.Of(seq))
Console.WriteLine(string.Join(" ", i));
The code produces the following output:
A B C
A C B
B A C
B C A
C A B
C B A
Unique Permutations
By default, the generated permutations are positional, e.g. if the input sequence contains some duplicates, there will be duplicate permutations in the output.
Let's see an example of non-unique permutations:
var seq = new[] { 1, 2, 1 };
foreach (var i in Permutations.Of(seq))
Console.WriteLine(string.Join(" ", i.Select(x => x.ToString())));
The output contains some duplicates, which is expected:
1 2 1
1 1 2
2 1 1
2 1 1
1 1 2
1 2 1
But if you want to get the unique permutations, there exists a straightforward way (note the Distinct
operation):
var seq = new[] { 1, 2, 1 };
foreach (var i in Permutations.Of(seq).Distinct())
Console.WriteLine(string.Join(" ", i.Select(x => x.ToString())));
which will produce the following output:
1 1 2
1 2 1
2 1 1
An experienced engineer will definitely spot that while that approach produces the correct result, it is not the most efficient way of achieving it.
It all comes to the number of elements processed by the Distinct
operation.
The number of resulting permutations is n!
where n
is the size of the input sequence.
So if we take care to perform Distinct
on the input sequence instead, we are going to achieve considerable savings in the number of operations and amount of used memory.
Like so:
foreach (var i in Permutations.Of(seq.Distinct()))
Console.WriteLine(string.Join(" ", i.Select(x => x.ToString())));
(Note that Distinct
operation is now applied to the input sequence, supposedly making the whole algorithm top-efficient while producing the same results)
This whole way of thinking stands true but Gapotchenko.FX.Math.Combinatorics
goes ahead of that and provides the out-of-the-box support for such natural idiosyncrasies.
Whatever syntax is preferred: Permutations.Of(seq.Distinct())
or Permutations.Of(seq).Distinct()
,
the algorithm complexity stays at bay thanks to the built-in optimizer that chooses the best execution plan for a query <ins>automatically</ins>.
Permutations in LINQ
Permutations.Of(...)
is an explicit form of producing the permutations, but the LINQ shorthand Permute()
is also available as a part of the fluent API:
using Gapotchenko.FX.Math.Combinatorics;
foreach (var i in new[] { "A", "B", "C" }.Permute())
// ...
Cartesian Product
Another widespread combinatorial primitive is a Cartesian product:
using Gapotchenko.FX.Math.Combinatorics;
var seq1 = new[] { "1", "2" };
var seq2 = new[] { "A", "B", "C" };
foreach (var i in CartesianProduct.Of(seq1, seq2))
Console.WriteLine(string.Join(" ", i));
The output:
1 A
2 A
1 B
2 B
1 C
2 C
Cartesian Product in LINQ
CartesianProduct.Of(...)
is an explicit form of producing the Cartesian product, but the LINQ shorthand CrossJoin()
is also available as a part of the fluent API:
using Gapotchenko.FX.Math.Combinatorics;
foreach (var i in new[] { "1", "2" }.CrossJoin(new[] { "A", "B", "C" }))
// ...
Note the naming difference between CartesianProduct
and CrossJoin
.
LINQ is historically built around data access,
and data access, in turn, historically uses the term cross join
for Cartesian product.
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net46 net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETCoreApp 2.0
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
-
.NETCoreApp 2.1
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
-
.NETCoreApp 3.0
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
-
.NETFramework 4.6
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
- System.ValueTuple (>= 4.5.0)
-
.NETFramework 4.7.1
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
-
.NETFramework 4.7.2
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
-
.NETStandard 2.0
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
-
.NETStandard 2.1
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
-
net5.0
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
-
net6.0
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
-
net7.0
- Gapotchenko.FX.Collections (>= 2022.1.4)
- Gapotchenko.FX.Linq (>= 2022.1.4)
- Gapotchenko.FX.Math (>= 2022.1.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.