Gapotchenko.FX.Math.Combinatorics 2022.1.4

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
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
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Gapotchenko.FX.Math.Combinatorics" Version="2022.1.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gapotchenko.FX.Math.Combinatorics --version 2022.1.4
#r "nuget: Gapotchenko.FX.Math.Combinatorics, 2022.1.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 is compatible.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2022.2.7 489 5/1/2022
2022.2.5 384 5/1/2022
2022.1.4 409 4/6/2022
2021.2.21 419 1/21/2022
2021.2.20 418 1/17/2022
2021.1.5 389 7/6/2021
2020.2.2-beta 326 11/21/2020