Skip to content

Commit

Permalink
narrowing now returns only a specified region
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxRev-Dev committed Oct 29, 2019
1 parent 5a93af1 commit af1ae2a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
14 changes: 7 additions & 7 deletions BinaryRelations/Binary/UnaryOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static partial class BinaryRelations
}

/// <summary>
/// Narrows matrix to region defined by X1 and X2. Indexes are starting from 1
/// Narrows matrix to region defined by the set of X..Xi..Xn. Indexes are starting from 1
/// </summary>
/// <param name="matrix1">binary matrix</param>
/// <param name="x">index [1..n]</param>
Expand All @@ -80,19 +80,19 @@ public static partial class BinaryRelations
if (x == null) throw new ArgumentNullException(nameof(x));
ThrowIfNull_NotQuad(matrix1);
var length = matrix1.GetLength(0);
var result = (bool[,])matrix1.Clone(); //new bool[length, length];
x = x.Select(p => --p).ToArray();
var set = x.Select(p => --p).ToList();
var result = new bool[x.Length, x.Length];

for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
if (!x.Contains(i) || !x.Contains(j))
result[i, j] = false;
if (set.Contains(i) && set.Contains(j))
result[set.IndexOf(i), set.IndexOf(j)] = matrix1[i, j];
}
}

return result;
return result;
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions BinaryRelations/BinaryRelations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>MaxRev.Extensions</RootNamespace>
<Version>1.2.2</Version>
<Version>1.3.0</Version>
<Authors>MaxRev</Authors>
<Copyright>MaxRev © 2019</Copyright>
<Description>Binary relations and matrix extensions library targeting netstandard2.0</Description>
<PackageProjectUrl>https://github.com/MaxRev-Dev/binary-relations</PackageProjectUrl>
<RepositoryUrl>https://github.com/MaxRev-Dev/binary-relations</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>binary-relations, maxrev, matrix, matrix-functions, extension-methods, graphs-theory, graphs, matrix-extensions</PackageTags>
<PackageReleaseNotes>fixed narrowing operation</PackageReleaseNotes>
<PackageReleaseNotes>added cartesian product</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageId>MaxRev.BinaryRelations</PackageId>
<Product>MaxRev.BinaryRelations</Product>
Expand Down
56 changes: 38 additions & 18 deletions BinaryRelationsTests/BinaryRelationsOperationsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Linq;
using BinaryRelationsTests.Helpers;
using MaxRev.Extensions.Binary;
using MaxRev.Extensions.Matrix;
Expand Down Expand Up @@ -152,39 +154,57 @@ public void Narrowing()
}.Cast<int, bool>();
var expected = new[,]
{
{1, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 0, 0, 1},
{1, 1},
{1, 1},
}.Cast<int, bool>();
Assert.Equal(expected, m1.Narrowing(1, 4));
Assert.Equal(expected, m1.Narrowing(1, 2));
Assert.Equal(expected, m1.Narrowing(3, 4));

expected = new[,]
{
{1, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 1},
{1, 1, 1},
{1, 1, 1},
}.Cast<int, bool>();
Assert.Equal(expected, m1.Narrowing(1, 2));
Assert.Equal(expected, m1.Narrowing(1, 3, 4));

expected = new[,]
{
{1, 0, 1, 1},
{0, 0, 0, 0},
{1, 0, 1, 1},
{1, 0, 1, 1},
{1},
}.Cast<int, bool>();
Assert.Equal(expected, m1.Narrowing(1));

m1 = new[,]
{
{1, 1, 0, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{0, 1, 1, 0},
}.Cast<int, bool>();

expected = new[,]
{
{1, 0, 1},
{1, 1, 1},
{0, 1, 0},
}.Cast<int, bool>();
Assert.Equal(expected, m1.Narrowing(1, 3, 4));

m1 = new[,]
{
{1, 1, 0, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{0, 1, 1, 0},
}.Cast<int, bool>();

expected = new[,]
{
{1, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{ 1, 1},
{ 1, 0},
}.Cast<int, bool>();
Assert.Equal(expected, m1.Narrowing(1));
Assert.Equal(expected, m1.Narrowing(3, 4));
}

[Fact]
Expand Down
7 changes: 7 additions & 0 deletions BinaryRelationsTests/ExtremumsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using BinaryRelationsTests.Helpers;
using MaxRev.Extensions.Binary;
using Xunit;
Expand All @@ -11,6 +12,12 @@ public ExtremumsTests(ITestOutputHelper output) : base(output)
{
}

[Fact]
public void ExtremumTest0()
{
Assert.True(new[,] { { true } }.GetMaximums().Any());
}

[Fact]
public void ExtremumTest1()
{
Expand Down

0 comments on commit af1ae2a

Please sign in to comment.