Skip to content

Commit 26d3c62

Browse files
committed
refactored. narrowing preserves size optionally
1 parent af1ae2a commit 26d3c62

File tree

6 files changed

+112
-37
lines changed

6 files changed

+112
-37
lines changed

BinaryRelations/Binary/Extremums.cs

+16-20
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ public static IEnumerable<int> GetMaximums(this bool[,] matrix1)
5252

5353
for (int j = 0; j < length; j++)
5454
{
55-
if (!matrix1[i, j])
56-
{
57-
rulePassing = false;
58-
break;
59-
}
55+
if (matrix1[i, j])
56+
continue;
57+
rulePassing = false;
58+
break;
6059
}
6160

6261
if (rulePassing)
@@ -81,11 +80,10 @@ public static IEnumerable<int> GetMinimums(this bool[,] matrix1)
8180

8281
for (int j = 0; j < length; j++)
8382
{
84-
if (!matrix1[j, i])
85-
{
86-
rulePassing = false;
87-
break;
88-
}
83+
if (matrix1[j, i])
84+
continue;
85+
rulePassing = false;
86+
break;
8987
}
9088

9189
if (rulePassing)
@@ -110,11 +108,10 @@ public static IEnumerable<int> GetMajorants(this bool[,] matrix1)
110108

111109
for (int j = 0; j < length; j++)
112110
{
113-
if (matrix1[j, i])
114-
{
115-
rulePassing = false;
116-
break;
117-
}
111+
if (!matrix1[j, i])
112+
continue;
113+
rulePassing = false;
114+
break;
118115
}
119116

120117
if (rulePassing)
@@ -139,11 +136,10 @@ public static IEnumerable<int> GetMinorants(this bool[,] matrix1)
139136

140137
for (int j = 0; j < length; j++)
141138
{
142-
if (matrix1[i, j])
143-
{
144-
rulePassing = false;
145-
break;
146-
}
139+
if (!matrix1[i, j])
140+
continue;
141+
rulePassing = false;
142+
break;
147143
}
148144

149145
if (rulePassing)

BinaryRelations/Binary/UnaryOperations.cs

+29-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static partial class BinaryRelations
7070
}
7171

7272
/// <summary>
73-
/// Narrows matrix to region defined by the set of X..Xi..Xn. Indexes are starting from 1
73+
/// Narrows matrix to the region defined by the set of X..Xi..Xn. Indexes are starting from 1 and will be sorted
7474
/// </summary>
7575
/// <param name="matrix1">binary matrix</param>
7676
/// <param name="x">index [1..n]</param>
@@ -80,7 +80,7 @@ public static partial class BinaryRelations
8080
if (x == null) throw new ArgumentNullException(nameof(x));
8181
ThrowIfNull_NotQuad(matrix1);
8282
var length = matrix1.GetLength(0);
83-
var set = x.Select(p => --p).ToList();
83+
var set = x.Select(p => --p).OrderBy(a => a).ToList();
8484
var result = new bool[x.Length, x.Length];
8585

8686
for (int i = 0; i < length; i++)
@@ -92,7 +92,33 @@ public static partial class BinaryRelations
9292
}
9393
}
9494

95-
return result;
95+
return result;
96+
}
97+
98+
/// <summary>
99+
/// Narrows matrix to the region defined by the set of X..Xi..Xn, but preserves it's original size. Indexes are starting from 1 and will be sorted
100+
/// </summary>
101+
/// <param name="matrix1">binary matrix</param>
102+
/// <param name="x">index [1..n]</param>
103+
/// <returns>binary matrix</returns>
104+
public static bool[,] NarrowingPreserveSize(this bool[,] matrix1, params int[] x)
105+
{
106+
if (x == null) throw new ArgumentNullException(nameof(x));
107+
ThrowIfNull_NotQuad(matrix1);
108+
var length = matrix1.GetLength(0);
109+
var result = (bool[,])matrix1.Clone();
110+
x = x.Select(p => --p).OrderBy(a => a).ToArray();
111+
112+
for (int i = 0; i < length; i++)
113+
{
114+
for (int j = 0; j < length; j++)
115+
{
116+
if (!x.Contains(i) || !x.Contains(j))
117+
result[i, j] = false;
118+
}
119+
}
120+
121+
return result;
96122
}
97123

98124
#endregion

BinaryRelations/BinaryRelations.csproj

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>MaxRev.Extensions</RootNamespace>
6-
<Version>1.3.0</Version>
6+
<Version>1.3.2</Version>
77
<Authors>MaxRev</Authors>
88
<Copyright>MaxRev © 2019</Copyright>
99
<Description>Binary relations and matrix extensions library targeting netstandard2.0</Description>
1010
<PackageProjectUrl>https://github.com/MaxRev-Dev/binary-relations</PackageProjectUrl>
1111
<RepositoryUrl>https://github.com/MaxRev-Dev/binary-relations</RepositoryUrl>
1212
<RepositoryType>git</RepositoryType>
1313
<PackageTags>binary-relations, maxrev, matrix, matrix-functions, extension-methods, graphs-theory, graphs, matrix-extensions</PackageTags>
14-
<PackageReleaseNotes>added cartesian product</PackageReleaseNotes>
14+
<PackageReleaseNotes>- added cartesian product
15+
16+
- ordered indexes in narrowing method
17+
18+
- narrowing returns only a specified region (skips empty indexes)
19+
20+
- sorted indexes for narrowing
21+
22+
- added NarrowingPreserveSize</PackageReleaseNotes>
1523
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1624
<PackageId>MaxRev.BinaryRelations</PackageId>
17-
<Product>MaxRev.BinaryRelations</Product>
25+
<Product>MaxRev.BinaryRelations</Product>
1826
</PropertyGroup>
1927

2028
</Project>

BinaryRelationsTests/BinaryRelationsOperationsTests.cs

+50-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Linq;
31
using BinaryRelationsTests.Helpers;
42
using MaxRev.Extensions.Binary;
53
using MaxRev.Extensions.Matrix;
@@ -167,7 +165,8 @@ public void Narrowing()
167165
{1, 1, 1},
168166
{1, 1, 1},
169167
}.Cast<int, bool>();
170-
Assert.Equal(expected, m1.Narrowing(1, 3, 4));
168+
// this set will be ordered by ascending
169+
Assert.Equal(expected, m1.Narrowing(4, 1, 3));
171170

172171
expected = new[,]
173172
{
@@ -189,7 +188,8 @@ public void Narrowing()
189188
{1, 1, 1},
190189
{0, 1, 0},
191190
}.Cast<int, bool>();
192-
Assert.Equal(expected, m1.Narrowing(1, 3, 4));
191+
// this set will be ordered by ascending
192+
Assert.Equal(expected, m1.Narrowing(3, 1, 4));
193193

194194
m1 = new[,]
195195
{
@@ -207,6 +207,52 @@ public void Narrowing()
207207
Assert.Equal(expected, m1.Narrowing(3, 4));
208208
}
209209

210+
[Fact]
211+
public void NarrowingPreserveSize()
212+
{
213+
var m1 = new[,]
214+
{
215+
{1, 1, 1, 1},
216+
{1, 1, 1, 1},
217+
{1, 1, 1, 1},
218+
{1, 1, 1, 1},
219+
}.Cast<int, bool>();
220+
var expected = new[,]
221+
{
222+
{1, 0, 0, 1},
223+
{0, 0, 0, 0},
224+
{0, 0, 0, 0},
225+
{1, 0, 0, 1},
226+
}.Cast<int, bool>();
227+
Assert.Equal(expected, m1.NarrowingPreserveSize(1, 4));
228+
229+
expected = new[,]
230+
{
231+
{1, 1, 0, 0},
232+
{1, 1, 0, 0},
233+
{0, 0, 0, 0},
234+
{0, 0, 0, 0},
235+
}.Cast<int, bool>();
236+
Assert.Equal(expected, m1.NarrowingPreserveSize(1, 2));
237+
238+
expected = new[,]
239+
{
240+
{1, 0, 1, 1},
241+
{0, 0, 0, 0},
242+
{1, 0, 1, 1},
243+
{1, 0, 1, 1},
244+
}.Cast<int, bool>();
245+
Assert.Equal(expected, m1.NarrowingPreserveSize(1, 3, 4));
246+
247+
expected = new[,]
248+
{
249+
{1, 0, 0, 0},
250+
{0, 0, 0, 0},
251+
{0, 0, 0, 0},
252+
{0, 0, 0, 0},
253+
}.Cast<int, bool>();
254+
Assert.Equal(expected, m1.NarrowingPreserveSize(1));
255+
}
210256
[Fact]
211257
public void Reverse()
212258
{

BinaryRelationsTests/ExtremumsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Linq;
21
using BinaryRelationsTests.Helpers;
32
using MaxRev.Extensions.Binary;
3+
using System.Linq;
44
using Xunit;
55
using Xunit.Abstractions;
66

BinaryRelationsTests/MatrixOperationsTests.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
31
using BinaryRelationsTests.Helpers;
42
using MaxRev.Extensions.Binary;
53
using MaxRev.Extensions.Matrix;
4+
using System.Linq;
65
using Xunit;
76
using Xunit.Abstractions;
87

@@ -109,15 +108,15 @@ public void Permutations()
109108
public void CartesianProductDistinctPairs()
110109
{
111110
var expected = new[] {
112-
new[]
111+
new []
113112
{
114113
new[] {1},
115114
new[] {2},
116115
new[] {3},
117116
new[] {4},
118117
new[] {5},
119118
},
120-
new[]
119+
new []
121120
{
122121
new[] {1, 2},
123122
new[] {1, 3},
@@ -130,7 +129,7 @@ public void CartesianProductDistinctPairs()
130129
new[] {3, 5},
131130
new[] {4, 5},
132131
},
133-
new[]
132+
new []
134133
{
135134
new[] {1, 2, 3},
136135
new[] {1, 2, 4},
@@ -143,7 +142,7 @@ public void CartesianProductDistinctPairs()
143142
new[] {2, 4, 5},
144143
new[] {3, 4, 5},
145144
},
146-
new[]
145+
new []
147146
{
148147
new[] {1, 2, 3, 4},
149148
new[] {1, 2, 3, 5},

0 commit comments

Comments
 (0)