Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static void TestCompute(long a, long b, long expectedGCD, long expectedBe
var eeaResult = ExtendedEuclideanAlgorithm.Compute(a, b);

// Assert
Assert.That(eeaResult.gcd, Is.EqualTo(expectedGCD));
Assert.That(eeaResult.bezoutA, Is.EqualTo(expectedBezoutOfA));
Assert.That(eeaResult.bezoutB, Is.EqualTo(expectedBezoutOfB));
Assert.That(eeaResult.Gcd, Is.EqualTo(expectedGCD));
Assert.That(eeaResult.BezoutA, Is.EqualTo(expectedBezoutOfA));
Assert.That(eeaResult.BezoutB, Is.EqualTo(expectedBezoutOfB));
}

[TestCase(240, 46, 2, -9, 47)]
Expand All @@ -45,8 +45,8 @@ public static void TestCompute_BigInteger(long a, long b, long expectedGCD, long
var eeaResult = ExtendedEuclideanAlgorithm.Compute(new BigInteger(a), new BigInteger(b));

// Assert
Assert.That(eeaResult.gcd, Is.EqualTo(new BigInteger(expectedGCD)));
Assert.That(eeaResult.bezoutA, Is.EqualTo(new BigInteger(expectedBezoutOfA)));
Assert.That(eeaResult.bezoutB, Is.EqualTo(new BigInteger(expectedBezoutOfB)));
Assert.That(eeaResult.Gcd, Is.EqualTo(new BigInteger(expectedGCD)));
Assert.That(eeaResult.BezoutA, Is.EqualTo(new BigInteger(expectedBezoutOfA)));
Assert.That(eeaResult.BezoutB, Is.EqualTo(new BigInteger(expectedBezoutOfB)));
}
}
2 changes: 1 addition & 1 deletion Algorithms/Algorithms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion Algorithms/DataCompression/BurrowsWheelerTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BurrowsWheelerTransform
/// rotation matrix.
/// </summary>
/// <param name="s">Input string.</param>
public (string encoded, int index) Encode(string s)
public (string Encoded, int Index) Encode(string s)
{
if (s.Length == 0)
{
Expand Down
4 changes: 2 additions & 2 deletions Algorithms/DataCompression/HuffmanCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public HuffmanCompressor(IComparisonSorter<ListNode> sorter, Translator translat
/// </summary>
/// <param name="uncompressedText">Text message to compress.</param>
/// <returns>Compressed string and keys to decompress it.</returns>
public (string compressedText, Dictionary<string, string> decompressionKeys) Compress(string uncompressedText)
public (string CompressedText, Dictionary<string, string> DecompressionKeys) Compress(string uncompressedText)
{
if (string.IsNullOrEmpty(uncompressedText))
{
Expand Down Expand Up @@ -70,7 +70,7 @@ private static ListNode[] GetListNodesFromText(string text)
return occurenceCounts.Select(kvp => new ListNode(kvp.Key, 1d * kvp.Value / text.Length)).ToArray();
}

private (Dictionary<string, string> compressionKeys, Dictionary<string, string> decompressionKeys) GetKeys(
private (Dictionary<string, string> CompressionKeys, Dictionary<string, string> DecompressionKeys) GetKeys(
ListNode tree)
{
var compressionKeys = new Dictionary<string, string>();
Expand Down
18 changes: 9 additions & 9 deletions Algorithms/DataCompression/ShannonFanoCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace Algorithms.DataCompression;
/// </summary>
public class ShannonFanoCompressor
{
private readonly IHeuristicKnapsackSolver<(char symbol, double frequency)> splitter;
private readonly IHeuristicKnapsackSolver<(char Symbol, double Frequency)> splitter;
private readonly Translator translator;

public ShannonFanoCompressor(
IHeuristicKnapsackSolver<(char symbol, double frequency)> splitter,
IHeuristicKnapsackSolver<(char Symbol, double Frequency)> splitter,
Translator translator)
{
this.splitter = splitter;
Expand All @@ -27,7 +27,7 @@ public ShannonFanoCompressor(
/// </summary>
/// <param name="uncompressedText">Text message to compress.</param>
/// <returns>Compressed string and keys to decompress it.</returns>
public (string compressedText, Dictionary<string, string> decompressionKeys) Compress(string uncompressedText)
public (string CompressedText, Dictionary<string, string> DecompressionKeys) Compress(string uncompressedText)
{
if (string.IsNullOrEmpty(uncompressedText))
{
Expand All @@ -49,16 +49,16 @@ public ShannonFanoCompressor(
return (translator.Translate(uncompressedText, compressionKeys), decompressionKeys);
}

private (Dictionary<string, string> compressionKeys, Dictionary<string, string> decompressionKeys) GetKeys(
private (Dictionary<string, string> CompressionKeys, Dictionary<string, string> DecompressionKeys) GetKeys(
ListNode tree)
{
var compressionKeys = new Dictionary<string, string>();
var decompressionKeys = new Dictionary<string, string>();

if (tree.Data.Length == 1)
{
compressionKeys.Add(tree.Data[0].symbol.ToString(), string.Empty);
decompressionKeys.Add(string.Empty, tree.Data[0].symbol.ToString());
compressionKeys.Add(tree.Data[0].Symbol.ToString(), string.Empty);
decompressionKeys.Add(string.Empty, tree.Data[0].Symbol.ToString());
return (compressionKeys, decompressionKeys);
}

Expand Down Expand Up @@ -86,7 +86,7 @@ private ListNode GenerateShannonFanoTree(ListNode node)
return node;
}

var left = splitter.Solve(node.Data, 0.5 * node.Data.Sum(x => x.frequency), x => x.frequency, _ => 1);
var left = splitter.Solve(node.Data, 0.5 * node.Data.Sum(x => x.Frequency), x => x.Frequency, _ => 1);
var right = node.Data.Except(left).ToArray();

node.LeftChild = GenerateShannonFanoTree(new ListNode(left));
Expand Down Expand Up @@ -122,9 +122,9 @@ private ListNode GetListNodeFromText(string text)
/// </summary>
public class ListNode
{
public ListNode((char symbol, double frequency)[] data) => Data = data;
public ListNode((char Symbol, double Frequency)[] data) => Data = data;

public (char symbol, double frequency)[] Data { get; }
public (char Symbol, double Frequency)[] Data { get; }

public ListNode? RightChild { get; set; }

Expand Down
16 changes: 8 additions & 8 deletions Algorithms/Encoders/NysiisEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ private string RemoveDuplicates(string text)

private string TrimEnd(string text)
{
var checks = new (string from, string to)?[]
var checks = new (string From, string To)?[]
{
("S", string.Empty),
("AY", "Y"),
("A", string.Empty),
};
var replacement = checks.FirstOrDefault(t => text.EndsWith(t!.Value.from));
var replacement = checks.FirstOrDefault(t => text.EndsWith(t!.Value.From));
if (replacement is { })
{
var (from, to) = replacement!.Value;
Expand All @@ -69,7 +69,7 @@ private string TrimEnd(string text)

private string ReplaceStep(string text, int i)
{
(string from, string to)[] replacements =
(string From, string To)[] replacements =
{
("EV", "AF"),
("E", "A"),
Expand Down Expand Up @@ -134,7 +134,7 @@ private bool TryReplace(string text, int index, (string, string)[] opts, out str

private string StartReplace(string start)
{
var checks = new (string from, string to)?[]
var checks = new (string From, string To)?[]
{
("MAC", "MCC"),
("KN", "NN"),
Expand All @@ -143,7 +143,7 @@ private string StartReplace(string start)
("PF", "FF"),
("SCH", "SSS"),
};
var replacement = checks.FirstOrDefault(t => start.StartsWith(t!.Value.from));
var replacement = checks.FirstOrDefault(t => start.StartsWith(t!.Value.From));
if (replacement is { })
{
var (from, to) = replacement!.Value;
Expand All @@ -155,7 +155,7 @@ private string StartReplace(string start)

private string EndReplace(string end)
{
var checks = new (string from, string to)?[]
var checks = new (string From, string To)?[]
{
("EE", "Y"),
("IE", "Y"),
Expand All @@ -164,7 +164,7 @@ private string EndReplace(string end)
("NT", "D"),
("ND", "D"),
};
var replacement = checks.FirstOrDefault(t => end.EndsWith(t!.Value.from));
var replacement = checks.FirstOrDefault(t => end.EndsWith(t!.Value.From));
if (replacement is { })
{
var (from, to) = replacement!.Value;
Expand All @@ -175,5 +175,5 @@ private string EndReplace(string end)
}

private string Replace(string text, int index, int length, string substitute) =>
text[..index] + substitute + text[(index + length) ..];
text[..index] + substitute + text[(index + length)..];
}
2 changes: 1 addition & 1 deletion Algorithms/Graph/FloydWarshall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class FloydWarshall<T>
{
for (var j = 0; j < distances.GetLength(0); j++)
{
var dist = graph.AdjacentDistance(graph.Vertices[i] !, graph.Vertices[j] !);
var dist = graph.AdjacentDistance(graph.Vertices[i]!, graph.Vertices[j]!);
distances[i, j] = dist != 0 ? dist : double.PositiveInfinity;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Algorithms/LinearAlgebra/Eigenvalue/PowerIteration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class PowerIteration
/// <returns>Dominant eigenvalue and eigenvector pair.</returns>
/// <exception cref="ArgumentException">The <paramref name="source" /> matrix is not square-shaped.</exception>
/// <exception cref="ArgumentException">The length of the start vector doesn't equal the size of the source matrix.</exception>
public static (double eigenvalue, double[] eigenvector) Dominant(
public static (double Eigenvalue, double[] Eigenvector) Dominant(
double[,] source,
double[] startVector,
double error = 0.00001)
Expand Down Expand Up @@ -61,7 +61,7 @@ public static (double eigenvalue, double[] eigenvector) Dominant(

var eigenvalue = source.Multiply(currentEigenVector.ToColumnVector()).ToRowVector().Magnitude();

return (eigenvalue, eigenvector: currentEigenVector);
return (eigenvalue, Eigenvector: currentEigenVector);
}

/// <summary>
Expand All @@ -81,6 +81,6 @@ public static (double eigenvalue, double[] eigenvector) Dominant(
/// <returns>Dominant eigenvalue and eigenvector pair.</returns>
/// <exception cref="ArgumentException">The <paramref name="source" /> matrix is not square-shaped.</exception>
/// <exception cref="ArgumentException">The length of the start vector doesn't equal the size of the source matrix.</exception>
public static (double eigenvalue, double[] eigenvector) Dominant(double[,] source, double error = 0.00001) =>
public static (double Eigenvalue, double[] Eigenvector) Dominant(double[,] source, double error = 0.00001) =>
Dominant(source, new Random().NextVector(source.GetLength(1)), error);
}
8 changes: 4 additions & 4 deletions Algorithms/ModularArithmetic/ChineseRemainderTheorem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static long Compute(List<long> listOfAs, List<long> listOfNs)
var n_i = listOfNs[i];
var modulus_i = prodN / n_i;

var bezout_modulus_i = ExtendedEuclideanAlgorithm.Compute(n_i, modulus_i).bezoutB;
var bezout_modulus_i = ExtendedEuclideanAlgorithm.Compute(n_i, modulus_i).BezoutB;
result += a_i * bezout_modulus_i * modulus_i;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ public static BigInteger Compute(List<BigInteger> listOfAs, List<BigInteger> lis
var n_i = listOfNs[i];
var modulus_i = prodN / n_i;

var bezout_modulus_i = ExtendedEuclideanAlgorithm.Compute(n_i, modulus_i).bezoutB;
var bezout_modulus_i = ExtendedEuclideanAlgorithm.Compute(n_i, modulus_i).BezoutB;
result += a_i * bezout_modulus_i * modulus_i;
}

Expand Down Expand Up @@ -145,7 +145,7 @@ private static void CheckRequirements(List<long> listOfAs, List<long> listOfNs)
for (var j = i + 1; j < listOfNs.Count; j++)
{
long gcd;
if ((gcd = ExtendedEuclideanAlgorithm.Compute(listOfNs[i], listOfNs[j]).gcd) != 1L)
if ((gcd = ExtendedEuclideanAlgorithm.Compute(listOfNs[i], listOfNs[j]).Gcd) != 1L)
{
throw new ArgumentException($"The GCD of n_{i} = {listOfNs[i]} and n_{j} = {listOfNs[j]} equals {gcd} and thus these values aren't coprime.");
}
Expand Down Expand Up @@ -182,7 +182,7 @@ private static void CheckRequirements(List<BigInteger> listOfAs, List<BigInteger
for (var j = i + 1; j < listOfNs.Count; j++)
{
BigInteger gcd;
if ((gcd = ExtendedEuclideanAlgorithm.Compute(listOfNs[i], listOfNs[j]).gcd) != BigInteger.One)
if ((gcd = ExtendedEuclideanAlgorithm.Compute(listOfNs[i], listOfNs[j]).Gcd) != BigInteger.One)
{
throw new ArgumentException($"The GCD of n_{i} = {listOfNs[i]} and n_{j} = {listOfNs[j]} equals {gcd} and thus these values aren't coprime.");
}
Expand Down
20 changes: 10 additions & 10 deletions Algorithms/ModularArithmetic/ExtendedEuclideanAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Algorithms.ModularArithmetic;
public static class ExtendedEuclideanAlgorithm
{
/// <summary>
/// Computes the greatest common divisor (gcd) of integers a and b, also the coefficients of Bézout's identity,
/// which are integers x and y such that a*bezoutCoefficientOfA + b*bezoutCoefficientOfB = gcd(a, b).
/// Computes the greatest common divisor (Gcd) of integers a and b, also the coefficients of Bézout's identity,
/// which are integers x and y such that a*bezoutCoefficientOfA + b*bezoutCoefficientOfB = Gcd(a, b).
/// </summary>
/// <param name="a">Input number.</param>
/// <param name="b">Second input number.</param>
/// <returns>A record of ExtendedEuclideanAlgorithmResult containing the bezout coefficients of a and b as well as the gcd(a,b).</returns>
/// <returns>A record of ExtendedEuclideanAlgorithmResult containing the bezout coefficients of a and b as well as the Gcd(a,b).</returns>
public static ExtendedEuclideanAlgorithmResult<long> Compute(long a, long b)
{
long quotient;
Expand Down Expand Up @@ -46,12 +46,12 @@ public static ExtendedEuclideanAlgorithmResult<long> Compute(long a, long b)
}

/// <summary>
/// Computes the greatest common divisor (gcd) of integers a and b, also the coefficients of Bézout's identity,
/// which are integers x and y such that a*bezoutCoefficientOfA + b*bezoutCoefficientOfB = gcd(a, b).
/// Computes the greatest common divisor (Gcd) of integers a and b, also the coefficients of Bézout's identity,
/// which are integers x and y such that a*bezoutCoefficientOfA + b*bezoutCoefficientOfB = Gcd(a, b).
/// </summary>
/// <param name="a">Input number.</param>
/// <param name="b">Second input number.</param>
/// <returns>A record of ExtendedEuclideanAlgorithmResult containing the bezout coefficients of a and b as well as the gcd(a,b).</returns>
/// <returns>A record of ExtendedEuclideanAlgorithmResult containing the bezout coefficients of a and b as well as the Gcd(a,b).</returns>
public static ExtendedEuclideanAlgorithmResult<BigInteger> Compute(BigInteger a, BigInteger b)
{
BigInteger quotient;
Expand Down Expand Up @@ -87,8 +87,8 @@ public static ExtendedEuclideanAlgorithmResult<BigInteger> Compute(BigInteger a,
/// The result type for the computation of the Extended Euclidean Algorithm.
/// </summary>
/// <typeparam name="T">The data type of the computation (i.e. long or BigInteger).</typeparam>
/// <param name="bezoutA">The bezout coefficient of the parameter a to the computation.</param>
/// <param name="bezoutB">The bezout coefficient of the parameter b to the computation.</param>
/// <param name="gcd">The greatest common divisor of the parameters a and b to the computation.</param>
public record ExtendedEuclideanAlgorithmResult<T>(T bezoutA, T bezoutB, T gcd);
/// <param name="BezoutA">The bezout coefficient of the parameter a to the computation.</param>
/// <param name="BezoutB">The bezout coefficient of the parameter b to the computation.</param>
/// <param name="Gcd">The greatest common divisor of the parameters a and b to the computation.</param>
public record ExtendedEuclideanAlgorithmResult<T>(T BezoutA, T BezoutB, T Gcd);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public static long Compute(long a, long n)
var eeaResult = ExtendedEuclideanAlgorithm.Compute(a, n);

// Check if there is an inverse:
if (eeaResult.gcd != 1)
if (eeaResult.Gcd != 1)
{
throw new ArithmeticException($"{a} is not invertible in Z/{n}Z.");
}

// Make sure, inverseOfA (i.e. the bezout coefficient of a) is in the interval [0, n).
var inverseOfA = eeaResult.bezoutA;
var inverseOfA = eeaResult.BezoutA;
if (inverseOfA < 0)
{
inverseOfA += n;
Expand All @@ -47,13 +47,13 @@ public static BigInteger Compute(BigInteger a, BigInteger n)
var eeaResult = ExtendedEuclideanAlgorithm.Compute(a, n);

// Check if there is an inverse:
if (eeaResult.gcd != 1)
if (eeaResult.Gcd != 1)
{
throw new ArithmeticException($"{a} is not invertible in Z/{n}Z.");
}

// Make sure, inverseOfA (i.e. the bezout coefficient of a) is in the interval [0, n).
var inverseOfA = eeaResult.bezoutA;
var inverseOfA = eeaResult.BezoutA;
if (inverseOfA < 0)
{
inverseOfA += n;
Expand Down
Loading
Loading