Gapotchenko.FX.Math.Combinatorics 2024.1.3

Prefix Reserved
dotnet add package Gapotchenko.FX.Math.Combinatorics --version 2024.1.3                
NuGet\Install-Package Gapotchenko.FX.Math.Combinatorics -Version 2024.1.3                
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="2024.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gapotchenko.FX.Math.Combinatorics --version 2024.1.3                
#r "nuget: Gapotchenko.FX.Math.Combinatorics, 2024.1.3"                
#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=2024.1.3

// Install Gapotchenko.FX.Math.Combinatorics as a Cake Tool
#tool nuget:?package=Gapotchenko.FX.Math.Combinatorics&version=2024.1.3                

Overview

The module provides 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 more 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 used: 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 of a query automatically.

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 Gapotchenko.FX.Math.Combinatorics 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 Gapotchenko.FX.Math.Combinatorics 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.

Commonly Used Types

  • Gapotchenko.FX.Math.Combinatorics.Permutations
  • Gapotchenko.FX.Math.Combinatorics.CartesianProduct

Other Modules

Let's continue with a look at some other modules provided by Gapotchenko.FX:

Or look at the full list of modules.

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 is compatible.  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.  net9.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  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 net461 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on Gapotchenko.FX.Math.Combinatorics:

Package Downloads
Gapotchenko.FX.Profiles.Math

Represents the Math profile of Gapotchenko.FX.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2024.1.3 111 11/10/2024
2022.2.7 597 5/1/2022
2022.2.5 412 5/1/2022
2022.1.4 444 4/6/2022
2021.2.21 445 1/21/2022
2021.2.20 442 1/17/2022
2021.1.5 427 7/6/2021
2020.2.2-beta 356 11/21/2020