Skip to content

Commit 780eeaa

Browse files
committed
fixed narrowing
1 parent 876be37 commit 780eeaa

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

BinaryRelations/Binary/UnaryOperations.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using MaxRev.Extensions.Matrix;
34

45
namespace MaxRev.Extensions.Binary
@@ -72,24 +73,21 @@ public static partial class BinaryRelations
7273
/// Narrows matrix to region defined by X1 and X2. Indexes are starting from 1
7374
/// </summary>
7475
/// <param name="matrix1">binary matrix</param>
75-
/// <param name="x1">index [1..n]</param>
76-
/// <param name="x2">index [1..n]</param>
76+
/// <param name="x">index [1..n]</param>
7777
/// <returns>binary matrix</returns>
78-
public static bool[,] Narrowing(this bool[,] matrix1, int x1, int x2)
78+
public static bool[,] Narrowing(this bool[,] matrix1, params int[] x)
7979
{
80-
if (x1 > x2) throw new InvalidOperationException(nameof(x1) + " must be bigger than " + nameof(x2));
80+
if (x == null) throw new ArgumentNullException(nameof(x));
8181
ThrowIfNull_NotQuad(matrix1);
8282
var length = matrix1.GetLength(0);
8383
var result = new bool[length, length];
84-
x1--;
85-
x2--;
84+
x = x.Select(p => --p).ToArray();
8685
for (int i = 0; i < length; i++)
8786
{
8887
for (int j = 0; j < length; j++)
8988
{
90-
result[i, j] = i >= x1 && i <= x2
91-
&& j <= x2 && j >= x1
92-
&& matrix1[i, j];
89+
if (x.Contains(i) || x.Contains(j))
90+
result[i, j] = matrix1[i, j];
9391
}
9492
}
9593

BinaryRelations/BinaryRelations.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>MaxRev.Extensions</RootNamespace>
6-
<Version>1.1.0</Version>
6+
<Version>1.2.0</Version>
77
<Authors>MaxRev</Authors>
88
<Copyright>MaxRev © 2019</Copyright>
99
<Description>Binary relations and matrix extensions library targeting netstandard2.0</Description>

BinaryRelationsTests/BinaryRelationsOperationsTests.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,46 @@ public void Narrowing()
145145
{
146146
var m1 = new[,]
147147
{
148+
{1, 1, 1, 1},
149+
{1, 1, 1, 1},
150+
{1, 1, 1, 1},
151+
{1, 1, 1, 1},
152+
}.Cast<int, bool>();
153+
var expected = new[,]
154+
{
155+
{1, 1, 1, 1},
148156
{1, 0, 0, 1},
149-
{1, 1, 1, 0},
150-
{0, 1, 0, 1},
157+
{1, 0, 0, 1},
158+
{1, 1, 1, 1},
159+
}.Cast<int, bool>();
160+
Assert.Equal(expected, m1.Narrowing(1, 4));
161+
162+
expected = new[,]
163+
{
164+
{1, 1, 1, 1},
165+
{1, 1, 1, 1},
166+
{1, 1, 0, 0},
151167
{1, 1, 0, 0},
152168
}.Cast<int, bool>();
153-
var expected = new[,]
169+
Assert.Equal(expected, m1.Narrowing(1, 2));
170+
171+
expected = new[,]
154172
{
155-
{0, 0, 0, 0},
156-
{0, 1, 1, 0},
157-
{0, 1, 0, 0},
158-
{0, 0, 0, 0},
173+
{1, 1, 1, 1},
174+
{1, 0, 1, 1},
175+
{1, 1, 1, 1},
176+
{1, 1, 1, 1},
177+
}.Cast<int, bool>();
178+
Assert.Equal(expected, m1.Narrowing(1, 3, 4));
179+
180+
expected = new[,]
181+
{
182+
{1, 1, 1, 1},
183+
{1, 0, 0, 0},
184+
{1, 0, 0, 0},
185+
{1, 0, 0, 0},
159186
}.Cast<int, bool>();
160-
Assert.Equal(expected, m1.Narrowing(2, 3));
187+
Assert.Equal(expected, m1.Narrowing(1));
161188
}
162189

163190
[Fact]

0 commit comments

Comments
 (0)