Gapotchenko.FX.Math.Combinatorics
2022.2.5
Prefix Reserved
See the version list below for details.
dotnet add package Gapotchenko.FX.Math.Combinatorics --version 2022.2.5
NuGet\Install-Package Gapotchenko.FX.Math.Combinatorics -Version 2022.2.5
<PackageReference Include="Gapotchenko.FX.Math.Combinatorics" Version="2022.2.5" />
paket add Gapotchenko.FX.Math.Combinatorics --version 2022.2.5
#r "nuget: Gapotchenko.FX.Math.Combinatorics, 2022.2.5"
// Install Gapotchenko.FX.Math.Combinatorics as a Cake Addin #addin nuget:?package=Gapotchenko.FX.Math.Combinatorics&version=2022.2.5 // Install Gapotchenko.FX.Math.Combinatorics as a Cake Tool #tool nuget:?package=Gapotchenko.FX.Math.Combinatorics&version=2022.2.5
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 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 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.
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:
- Gapotchenko.FX
- Gapotchenko.FX.AppModel.Information
- Gapotchenko.FX.Collections
- Gapotchenko.FX.Console
- Gapotchenko.FX.Data
- Gapotchenko.FX.Diagnostics
- Gapotchenko.FX.IO
- Gapotchenko.FX.Linq
- Gapotchenko.FX.Math
- Gapotchenko.FX.Memory
- Gapotchenko.FX.Security.Cryptography
- Gapotchenko.FX.Text
- Gapotchenko.FX.Threading
Or look at the full list of modules.
Product | Versions 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. |
-
.NETCoreApp 2.0
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
-
.NETCoreApp 2.1
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
-
.NETCoreApp 3.0
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
-
.NETFramework 4.6
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
- System.ValueTuple (>= 4.5.0)
-
.NETFramework 4.7.1
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
-
.NETFramework 4.7.2
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
-
.NETStandard 2.0
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
-
.NETStandard 2.1
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
-
net5.0
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
-
net6.0
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
-
net7.0
- Gapotchenko.FX.Collections (>= 2022.2.5)
- Gapotchenko.FX.Linq (>= 2022.2.5)
- Gapotchenko.FX.Math (>= 2022.2.5)
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.