Skip to content

Commit af1ae2a

Browse files
committed
narrowing now returns only a specified region
1 parent 5a93af1 commit af1ae2a

File tree

4 files changed

+54
-27
lines changed

4 files changed

+54
-27
lines changed

BinaryRelations/Binary/UnaryOperations.cs

Lines changed: 7 additions & 7 deletions
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 X1 and X2. Indexes are starting from 1
73+
/// Narrows matrix to region defined by the set of X..Xi..Xn. Indexes are starting from 1
7474
/// </summary>
7575
/// <param name="matrix1">binary matrix</param>
7676
/// <param name="x">index [1..n]</param>
@@ -80,19 +80,19 @@ 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 result = (bool[,])matrix1.Clone(); //new bool[length, length];
84-
x = x.Select(p => --p).ToArray();
85-
83+
var set = x.Select(p => --p).ToList();
84+
var result = new bool[x.Length, x.Length];
85+
8686
for (int i = 0; i < length; i++)
8787
{
8888
for (int j = 0; j < length; j++)
8989
{
90-
if (!x.Contains(i) || !x.Contains(j))
91-
result[i, j] = false;
90+
if (set.Contains(i) && set.Contains(j))
91+
result[set.IndexOf(i), set.IndexOf(j)] = matrix1[i, j];
9292
}
9393
}
9494

95-
return result;
95+
return result;
9696
}
9797

9898
#endregion

BinaryRelations/BinaryRelations.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>MaxRev.Extensions</RootNamespace>
6-
<Version>1.2.2</Version>
6+
<Version>1.3.0</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>fixed narrowing operation</PackageReleaseNotes>
14+
<PackageReleaseNotes>added cartesian product</PackageReleaseNotes>
1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1616
<PackageId>MaxRev.BinaryRelations</PackageId>
1717
<Product>MaxRev.BinaryRelations</Product>

BinaryRelationsTests/BinaryRelationsOperationsTests.cs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Linq;
13
using BinaryRelationsTests.Helpers;
24
using MaxRev.Extensions.Binary;
35
using MaxRev.Extensions.Matrix;
@@ -152,39 +154,57 @@ public void Narrowing()
152154
}.Cast<int, bool>();
153155
var expected = new[,]
154156
{
155-
{1, 0, 0, 1},
156-
{0, 0, 0, 0},
157-
{0, 0, 0, 0},
158-
{1, 0, 0, 1},
157+
{1, 1},
158+
{1, 1},
159159
}.Cast<int, bool>();
160160
Assert.Equal(expected, m1.Narrowing(1, 4));
161+
Assert.Equal(expected, m1.Narrowing(1, 2));
162+
Assert.Equal(expected, m1.Narrowing(3, 4));
161163

162164
expected = new[,]
163165
{
164-
{1, 1, 0, 0},
165-
{1, 1, 0, 0},
166-
{0, 0, 0, 0},
167-
{0, 0, 0, 0},
166+
{1, 1, 1},
167+
{1, 1, 1},
168+
{1, 1, 1},
168169
}.Cast<int, bool>();
169-
Assert.Equal(expected, m1.Narrowing(1, 2));
170+
Assert.Equal(expected, m1.Narrowing(1, 3, 4));
170171

171172
expected = new[,]
172173
{
173-
{1, 0, 1, 1},
174-
{0, 0, 0, 0},
175-
{1, 0, 1, 1},
176-
{1, 0, 1, 1},
174+
{1},
175+
}.Cast<int, bool>();
176+
Assert.Equal(expected, m1.Narrowing(1));
177+
178+
m1 = new[,]
179+
{
180+
{1, 1, 0, 1},
181+
{1, 1, 1, 1},
182+
{1, 1, 1, 1},
183+
{0, 1, 1, 0},
184+
}.Cast<int, bool>();
185+
186+
expected = new[,]
187+
{
188+
{1, 0, 1},
189+
{1, 1, 1},
190+
{0, 1, 0},
177191
}.Cast<int, bool>();
178192
Assert.Equal(expected, m1.Narrowing(1, 3, 4));
179193

194+
m1 = new[,]
195+
{
196+
{1, 1, 0, 1},
197+
{1, 1, 1, 1},
198+
{1, 1, 1, 1},
199+
{0, 1, 1, 0},
200+
}.Cast<int, bool>();
201+
180202
expected = new[,]
181203
{
182-
{1, 0, 0, 0},
183-
{0, 0, 0, 0},
184-
{0, 0, 0, 0},
185-
{0, 0, 0, 0},
204+
{ 1, 1},
205+
{ 1, 0},
186206
}.Cast<int, bool>();
187-
Assert.Equal(expected, m1.Narrowing(1));
207+
Assert.Equal(expected, m1.Narrowing(3, 4));
188208
}
189209

190210
[Fact]

BinaryRelationsTests/ExtremumsTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using BinaryRelationsTests.Helpers;
23
using MaxRev.Extensions.Binary;
34
using Xunit;
@@ -11,6 +12,12 @@ public ExtremumsTests(ITestOutputHelper output) : base(output)
1112
{
1213
}
1314

15+
[Fact]
16+
public void ExtremumTest0()
17+
{
18+
Assert.True(new[,] { { true } }.GetMaximums().Any());
19+
}
20+
1421
[Fact]
1522
public void ExtremumTest1()
1623
{

0 commit comments

Comments
 (0)