Skip to content

Commit f718a64

Browse files
code cleanup
1 parent 92a966f commit f718a64

File tree

18 files changed

+316
-122
lines changed

18 files changed

+316
-122
lines changed

Laboratory.Test/Generics/ExtensionsTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
using Labratory.Generics;
1+
using Labratory.Extensions;
32
using Shouldly;
43

54
namespace Laboratory.Test.Generics;
@@ -13,7 +12,7 @@ public void Permutate_ShouldReturnAllPossibleCombinations()
1312
int[] numbers = [1, 2, 3];
1413
int[][] expectedCombinations = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]];
1514

16-
foreach (IDictionary<int, int> combination in numbers.Permutate())
15+
foreach (IDictionary<int, int> combination in numbers.Permutations())
1716
{
1817
expectedCombinations.Any(permutation => permutation.SequenceEqual(combination.Values)).ShouldBe(true);
1918
}

Laboratory.Test/Mathematics/Algebra/Linear/Core/Concretes/MatrixTests.IsDiagonal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public partial class MatrixTests
3333
public void TransferToRowReduced_ShouldReturnCorrectlyRowReducedMatrix(int matIndex, int rowReducedMatIndex)
3434
{
3535
Matrix mat = new(_matrices[matIndex]);
36-
mat.TransferToRowReduced();
36+
_ = mat.TransferToRowReduced();
3737

3838
mat.Equals(new Matrix(_matricesRowReduced[rowReducedMatIndex]))
3939
.ShouldBeTrue();

Laboratory.Test/Mathematics/Algebra/Linear/Core/Concretes/MatrixTests.IsEye.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public partial class MatrixTests
1111
public void IsEye_ShouldReturnTrue_ForEyeMatrix()
1212
{
1313
Matrix matrix = new(10, 10);
14-
matrix.Eye();
14+
_ = matrix.Eye();
1515
matrix.IsEye(value: 1.0D).ShouldBeTrue();
1616
}
1717

1818
[Fact]
1919
public void IsEye_ShouldReturnFalse_ForNoneEyeMatrix()
2020
{
2121
Matrix matrix = new(10, 10);
22-
matrix.Randomize();
22+
_ = matrix.Randomize();
2323
matrix.IsEye(value: 1.0D).ShouldBeFalse();
2424
}
2525

2626
[Fact]
2727
public void IsEye_ShouldReturnFalse_ForNonEyeMatrix2()
2828
{
2929
Matrix matrix = new(10, 10);
30-
matrix.Eye();
30+
_ = matrix.Eye();
3131
matrix.AtRef(1, 5) = 1;
3232
matrix.IsEye(value: 1.0D).ShouldBeFalse();
3333
}
@@ -38,6 +38,6 @@ public void IsEye_ShouldReturnFalse_ForNonEyeMatrix2()
3838
public void IsEye_ShouldThrowLaboratoryException_ForNonSquareMatrix(int rows, int cols)
3939
{
4040
Matrix matrix = new(rows, cols);
41-
Assert.Throws<LaboratoryException>(() => matrix.IsEye(value: 1.0D));
41+
_ = Assert.Throws<LaboratoryException>(() => matrix.IsEye(value: 1.0D));
4242
}
4343
}

Laboratory.Test/Mathematics/Algebra/Linear/Core/Concretes/MatrixTests.IsFilled.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public partial class MatrixTests
1414
public void IsFilled_ShouldReturnTrue_ForFilledMatrix(double value)
1515
{
1616
Matrix matrix = new(Random.Shared.Next(1, 10), Random.Shared.Next(1, 10));
17-
matrix.Fill(value);
17+
_ = matrix.Fill(value);
1818
matrix.IsFilled(value).ShouldBeTrue();
1919
}
2020

@@ -26,7 +26,7 @@ public void IsFilled_ShouldReturnTrue_ForFilledMatrix(double value)
2626
public void IsFilled_ShouldReturnFalse_ForNotFilledMatrix(double value)
2727
{
2828
Matrix matrix = new(Random.Shared.Next(1, 10), Random.Shared.Next(1, 10));
29-
matrix.Randomize();
29+
_ = matrix.Randomize();
3030
matrix.IsFilled(value).ShouldBeFalse();
3131
}
3232
}

Laboratory.Test/Mathematics/Algebra/Linear/Core/Concretes/MatrixTests.IsInRowReducedForm.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Laboratory.Test.Mathematics.Algebra.Linear.Core.Concretes;
66

77
public partial class MatrixTests
88
{
9-
private readonly List<double[,]> _notRowReducedMatrices = new() {
9+
private readonly List<double[,]> _notRowReducedMatrices = [
1010
new double[4, 5] {
1111
{1, 1, -2, 4, 7},
1212
{0, 0, -6, 5, 7},
@@ -24,9 +24,9 @@ public partial class MatrixTests
2424
{1, -2, 3, 3},
2525
{0, 0, 1, -3},
2626
{0, 0, 1, 0}},
27-
};
27+
];
2828

29-
private readonly List<double[,]> _rowReducedMatrices = new() {
29+
private readonly List<double[,]> _rowReducedMatrices = [
3030
new double[4, 5] {
3131
{1, 1, -2, 4, 7},
3232
{0, 0, 1, 5, 7},
@@ -44,7 +44,7 @@ public partial class MatrixTests
4444
{1, -2, 3, 3},
4545
{0, 1, 1, -3},
4646
{0, 0, 1, 0}},
47-
};
47+
];
4848

4949
[Theory]
5050
[InlineData(0)]

Laboratory.Test/Mathematics/Algebra/Linear/Core/Concretes/MatrixTests.Multiplicate.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,9 @@ public void Multiplicate_ShouldThrowException_ForIncompatibleShapes()
9090
Matrix right = new Matrix(c, d).Randomize();
9191

9292
// Then
93-
Should.Throw<LaboratoryException>(() => left.Multiplicate(right));
93+
_ = Should.Throw<LaboratoryException>(() => left.Multiplicate(right));
9494
}
9595

96-
9796
[Fact]
9897
public void Multiplicate_ShouldWorkCorrectly_ForLargeMatrices()
9998
{
@@ -106,7 +105,7 @@ public void Multiplicate_ShouldWorkCorrectly_ForLargeMatrices()
106105
Matrix right = new Matrix(b, c).Randomize();
107106

108107
// Then
109-
Should.NotThrow(() => left.Multiplicate(right));
108+
_ = Should.NotThrow(() => left.Multiplicate(right));
110109
}
111110

112111
[Fact]
@@ -117,7 +116,8 @@ public void Multiplicate_ShouldSatisfyAssociativity()
117116
Matrix mat2 = new Matrix(25, 15).Randomize();
118117
Matrix mat3 = new Matrix(15, 20).Randomize();
119118

120-
Matrix expected = mat1.Multiplicate(mat2).Multiplicate(mat3); ;
119+
Matrix expected = mat1.Multiplicate(mat2).Multiplicate(mat3);
120+
;
121121

122122
// When
123123
Matrix result = mat1.Multiplicate(mat2.Multiplicate(mat3));

Laboratory.Test/Mathematics/Algebra/Linear/Core/Concretes/MatrixTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void ShloudCreateNewMatrix_WithPositiveRowsAndColsNumber(int rows, int co
2929
[InlineData(-1, -11)]
3030
public void ShloudThrowException_WhenCreatingNewMatrix_WithNonPositiveGivenRowsAndColsNumber(int rows, int cols)
3131
{
32-
Assert.Throws<LaboratoryException>(() => new Matrix(rows, cols));
32+
_ = Assert.Throws<LaboratoryException>(() => new Matrix(rows, cols));
3333
}
3434

3535
[Fact]

Labratory/Exceptions/LaboratoryException.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ namespace Labratory.Exceptions;
66

77
public sealed class LaboratoryException : Exception
88
{
9-
public LaboratoryExceptionType ExceptionType => _exceptionType;
10-
private readonly LaboratoryExceptionType _exceptionType;
9+
public LaboratoryExceptionType ExceptionType { get; }
1110

1211
internal LaboratoryException(
1312
LaboratoryExceptionType exceptionType,
1413
string message) : base(message)
1514
{
16-
_exceptionType = exceptionType;
15+
ExceptionType = exceptionType;
1716
}
1817

1918
[DoesNotReturn]
@@ -30,9 +29,9 @@ internal static void ThrowIfNot(bool condition, string message, LaboratoryExcept
3029
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3130
public static void ThrowIf(bool condition, string message, LaboratoryExceptionType exceptionType = LaboratoryExceptionType.Internal)
3231
{
33-
if (condition)
32+
if (condition)
3433
{
3534
throw new LaboratoryException(exceptionType, message);
3635
}
37-
}
36+
}
3837
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace Labratory.Extensions;
2+
3+
public static partial class Extensions
4+
{
5+
public static IEnumerable<IDictionary<T, T>> Permutations<T>(this IEnumerable<T> values)
6+
where T : notnull
7+
{
8+
var distinctValues = values
9+
.Distinct()
10+
.ToList();
11+
12+
foreach (IEnumerable<T> permutation in PermutateInternal(distinctValues, []))
13+
{
14+
yield return distinctValues
15+
.Zip(permutation, (key, value) => (key, value))
16+
.ToDictionary(pair => pair.key, pair => pair.value);
17+
}
18+
}
19+
20+
private static IEnumerable<IEnumerable<T>> PermutateInternal<T>(List<T> remaining, List<T> current)
21+
{
22+
if (remaining.Count == 0)
23+
{
24+
yield return current;
25+
}
26+
else
27+
{
28+
for (int i = 0; i < remaining.Count; i++)
29+
{
30+
List<T> nextCurrent = [.. current, remaining[i]];
31+
32+
List<T> nextRemaining = [.. remaining];
33+
nextRemaining.RemoveAt(i);
34+
35+
foreach (IEnumerable<T> result in PermutateInternal(nextRemaining, nextCurrent))
36+
{
37+
yield return result;
38+
}
39+
}
40+
}
41+
}
42+
43+
public static IEnumerable<IEnumerable<T>> Permutations<T>(IEnumerable<T> list, int length)
44+
{
45+
return length == 1
46+
? list.Select(t => new T[] { t })
47+
: Permutations(list, length - 1)
48+
.SelectMany(t => list.Where(e => !t.Contains(e)),
49+
(t1, t2) => t1.Concat(new T[] { t2 }));
50+
}
51+
52+
public static IEnumerable<IEnumerable<T>> Combinations<T>(IEnumerable<T> list, int length)
53+
{
54+
if (length == 0)
55+
{
56+
yield return new T[0];
57+
}
58+
59+
var enumerable = list.ToList();
60+
61+
for (int i = 0; i < enumerable.Count; i++)
62+
{
63+
IEnumerable<T> head = enumerable.Skip(i).Take(1);
64+
IEnumerable<IEnumerable<T>> tailCombinations = Combinations(enumerable.Skip(i + 1), length - 1);
65+
foreach (IEnumerable<T> tail in tailCombinations)
66+
{
67+
yield return head.Concat(tail);
68+
}
69+
}
70+
}
71+
}

Labratory/Generics/Extensions.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)