Skip to content

Commit 0e50bef

Browse files
committed
v1.1.0 package, splited extensions, added comments, fixed typos
1 parent 0b4113e commit 0e50bef

13 files changed

+1209
-1146
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
3+
namespace MaxRev.Extensions.Binary
4+
{
5+
public static partial class BinaryRelations
6+
{
7+
#region Argument check
8+
9+
private static void ThrowIfNull(params object[] matrix1)
10+
{
11+
foreach (object o in matrix1)
12+
{
13+
if (o == null) throw new ArgumentNullException(nameof(o));
14+
}
15+
}
16+
17+
private static void ThrowIfNotQuad<T>(T[,] array1)
18+
{
19+
if (array1.GetLength(0) != array1.GetLength(1))
20+
{
21+
throw new ArgumentNullException(nameof(array1), "This matrix must be a quad matrix");
22+
}
23+
}
24+
25+
private static void ThrowIfNull_NotQuad<T>(T[,] array1)
26+
{
27+
ThrowIfNull(array1);
28+
ThrowIfNotQuad(array1);
29+
}
30+
31+
private static void ThrowIfNull_NotQuad_SizeDiffers<T>(T[,] array1, T[,] array2)
32+
{
33+
ThrowIfNull(array1, array2);
34+
ThrowIfNotQuad(array1);
35+
if (array1.GetLength(0) != array2.GetLength(0))
36+
{
37+
throw new ArgumentException("matrices sizes are not equal");
38+
}
39+
}
40+
41+
#endregion
42+
}
43+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
namespace MaxRev.Extensions.Binary
2+
{
3+
public static partial class BinaryRelations
4+
{
5+
#region Binary Operations
6+
7+
/// <summary>
8+
/// An intersection of two binary matrices
9+
/// </summary>
10+
/// <param name="matrix1">binary matrix</param>
11+
/// <param name="maxrix2">binary matrix</param>
12+
/// <returns>binary matrix</returns>
13+
public static bool[,] Intersection(this bool[,] matrix1, bool[,] maxrix2)
14+
{
15+
ThrowIfNull_NotQuad_SizeDiffers(matrix1, maxrix2);
16+
var length = matrix1.GetLength(0);
17+
var result = new bool[length, length];
18+
19+
for (int i = 0; i < length; i++)
20+
{
21+
for (int j = 0; j < length; j++)
22+
{
23+
// min of two cells
24+
result[i, j] = matrix1[i, j] & maxrix2[i, j];
25+
}
26+
}
27+
28+
return result;
29+
}
30+
31+
/// <summary>
32+
/// Union of two binary matrices
33+
/// </summary>
34+
/// <param name="matrix1">binary matrix</param>
35+
/// <param name="maxrix2">binary matrix</param>
36+
/// <returns>binary matrix</returns>
37+
public static bool[,] Union(this bool[,] matrix1, bool[,] maxrix2)
38+
{
39+
ThrowIfNull_NotQuad_SizeDiffers(matrix1, maxrix2);
40+
41+
var length = matrix1.GetLength(0);
42+
var result = new bool[length, length];
43+
44+
for (int i = 0; i < length; i++)
45+
{
46+
for (int j = 0; j < length; j++)
47+
{
48+
// max of two cells
49+
result[i, j] = matrix1[i, j] | maxrix2[i, j];
50+
}
51+
}
52+
53+
return result;
54+
}
55+
56+
/// <summary>
57+
/// Finds a difference between two binary matrices
58+
/// </summary>
59+
/// <param name="matrix1">binary matrix</param>
60+
/// <param name="maxrix2">binary matrix</param>
61+
/// <returns>binary matrix</returns>
62+
public static bool[,] Difference(this bool[,] matrix1, bool[,] maxrix2)
63+
{
64+
ThrowIfNull_NotQuad_SizeDiffers(matrix1, maxrix2);
65+
66+
var length = matrix1.GetLength(0);
67+
var result = new bool[length, length];
68+
69+
for (int i = 0; i < length; i++)
70+
{
71+
for (int j = 0; j < length; j++)
72+
{
73+
result[i, j] = matrix1[i, j] && !maxrix2[i, j];
74+
}
75+
}
76+
77+
return result;
78+
}
79+
80+
/// <summary>
81+
/// Returns a b-matrix of symmetric difference between two b-matrices
82+
/// </summary>
83+
/// <param name="matrix1">binary matrix</param>
84+
/// <param name="maxrix2">binary matrix</param>
85+
/// <returns>binary matrix</returns>
86+
public static bool[,] SymmetricDifference(this bool[,] matrix1, bool[,] maxrix2)
87+
{
88+
ThrowIfNull_NotQuad_SizeDiffers(matrix1, maxrix2);
89+
90+
var length = matrix1.GetLength(0);
91+
var result = new bool[length, length];
92+
93+
// next code is equal to a block below, but more efficient)
94+
// var m1 = matrix1.Difference(maxrix2);
95+
// var m2 = maxrix2.Difference(matrix1);
96+
// return m1.Union(m2);
97+
for (int i = 0; i < length; i++)
98+
{
99+
for (int j = 0; j < length; j++)
100+
{
101+
result[i, j] = maxrix2[i, j] ^ matrix1[i, j];
102+
}
103+
}
104+
105+
return result;
106+
}
107+
108+
/// <summary>
109+
/// A product of two b-matrices
110+
/// </summary>
111+
/// <param name="matrix1">binary matrix</param>
112+
/// <param name="maxrix2">binary matrix</param>
113+
/// <returns>binary matrix</returns>
114+
public static bool[,] Product(this bool[,] matrix1, bool[,] maxrix2)
115+
{
116+
ThrowIfNull_NotQuad_SizeDiffers(matrix1, maxrix2);
117+
118+
var length = matrix1.GetLength(0);
119+
var result = new bool[length, length];
120+
121+
for (int i = 0; i < length; i++)
122+
{
123+
for (int j = 0; j < length; j++)
124+
{
125+
var max = false;
126+
127+
for (int k = 0; k < length; k++)
128+
{
129+
max |= matrix1[i, k] & maxrix2[k, j];
130+
}
131+
132+
result[i, j] = max;
133+
}
134+
135+
}
136+
137+
return result;
138+
}
139+
140+
#endregion
141+
}
142+
}

BinaryRelations/Binary/Closures.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace MaxRev.Extensions.Binary
2+
{
3+
public static partial class BinaryRelations
4+
{
5+
#region Closures
6+
7+
/// <summary>
8+
/// Returns a transitive closure for a provided one. Floyd–Warshall algorithm
9+
/// <see href="http://courses.ics.hawaii.edu/ReviewICS241/morea/relations/Relations4-QA.pdf"/>
10+
/// </summary>
11+
/// <param name="matrix1"></param>
12+
/// <returns></returns>
13+
public static bool[,] TransitiveClosure(this bool[,] matrix1)
14+
{
15+
ThrowIfNull_NotQuad(matrix1);
16+
17+
var length = matrix1.GetLength(0);
18+
var result = (bool[,])matrix1.Clone();
19+
for (int k = 0; k < length; k++)
20+
for (int i = 0; i < length; i++)
21+
for (int j = 0; j < length; j++)
22+
result[i, j] = result[i, j] || result[i, k] && result[k, j];
23+
24+
return result;
25+
}
26+
27+
/// <summary>
28+
/// Returns a reflexive closure for a provided one.
29+
/// <see href="http://courses.ics.hawaii.edu/ReviewICS241/morea/relations/Relations4-QA.pdf"/>
30+
/// </summary>
31+
/// <param name="matrix1"></param>
32+
/// <returns></returns>
33+
public static bool[,] ReflexiveClosure(this bool[,] matrix1)
34+
{
35+
ThrowIfNull_NotQuad(matrix1);
36+
37+
var length = matrix1.GetLength(0);
38+
var result = (bool[,])matrix1.Clone();
39+
for (int i = 0; i < length; i++)
40+
{
41+
result[i, i] = true;
42+
}
43+
44+
return result;
45+
}
46+
47+
/// <summary>
48+
/// Returns a reflexive closure for a provided one.
49+
/// <see href="http://courses.ics.hawaii.edu/ReviewICS241/morea/relations/Relations4-QA.pdf"/>
50+
/// </summary>
51+
/// <param name="matrix1"></param>
52+
/// <returns></returns>
53+
public static bool[,] SymmetricClosure(this bool[,] matrix1)
54+
{
55+
ThrowIfNull_NotQuad(matrix1);
56+
57+
var length = matrix1.GetLength(0);
58+
var result = (bool[,])matrix1.Clone();
59+
for (int i = 0; i < length; i++)
60+
{
61+
for (int j = 0; j < length; j++)
62+
{
63+
if (result[i, j])
64+
{
65+
result[j, i] = true;
66+
}
67+
}
68+
}
69+
70+
return result;
71+
}
72+
73+
#endregion
74+
}
75+
}

0 commit comments

Comments
 (0)