Skip to content

Commit 0b4113e

Browse files
committed
splited tests and added some docs
1 parent 33a564e commit 0b4113e

12 files changed

+839
-604
lines changed

BinaryRelations/BinaryRelations.cs

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ public static class BinaryRelations
1010
{
1111
#region Helpers
1212

13+
/// <summary>
14+
/// Creates diagonal matrix with the same size of given matrix
15+
/// </summary>
16+
/// <param name="matrix1"></param>
17+
/// <returns></returns>
1318
public static bool[,] GetDiagonalRelation(this bool[,] matrix1)
1419
{
1520
ThrowIfNotQuad(matrix1);
1621
return GetDiagonalRelation(matrix1.GetLength(0));
1722
}
1823

24+
/// <summary>
25+
/// Creates a diagonal matrix of specified length
26+
/// </summary>
27+
/// <param name="len"></param>
28+
/// <returns></returns>
1929
public static bool[,] GetDiagonalRelation(int len)
2030
{
2131
if (len <= 0) throw new ArgumentOutOfRangeException(nameof(len));
@@ -28,6 +38,7 @@ public static class BinaryRelations
2838

2939
return matrix1;
3040
}
41+
3142
/// <summary>
3243
/// Converts elements of one dimensional array from <see cref="R"/> to <see cref="T"/>
3344
/// </summary>
@@ -49,6 +60,7 @@ public static T[] Cast<R, T>(this R[] array)
4960

5061
return result;
5162
}
63+
5264
/// <summary>
5365
/// Converts elements of two dimensional array from <see cref="R"/> to <see cref="T"/>
5466
/// </summary>
@@ -98,7 +110,34 @@ public static T[] Cast<R, T>(this R[] array)
98110
return matrix1;
99111
}
100112

101-
public static bool IsEqualTo<T>(this T[,] matrix1, T[,] matrix2)
113+
/// <summary>
114+
/// Prints matrix to <see cref="Console.Out"/> or specified text writer
115+
/// </summary>
116+
/// <typeparam name="T"></typeparam>
117+
/// <param name="matrix1"></param>
118+
/// <param name="writer"></param>
119+
/// <returns></returns>
120+
public static T[] PrintThrough<T>(this T[] matrix1, TextWriter writer = default)
121+
{
122+
ThrowIfNull(matrix1);
123+
if (writer == default) writer = Console.Out;
124+
var length = matrix1.GetLength(0);
125+
for (int i = 0; i < length; i++)
126+
{
127+
writer.Write(matrix1[i].ToString() + ' ');
128+
}
129+
130+
return matrix1;
131+
}
132+
133+
/// <summary>
134+
/// Sequentially compares elements of two-dimensional arrays using <see cref="object.ReferenceEquals"/>
135+
/// </summary>
136+
/// <typeparam name="T"></typeparam>
137+
/// <param name="matrix1"></param>
138+
/// <param name="matrix2"></param>
139+
/// <returns></returns>
140+
public static bool IsReferenceSequenceEqualTo<T>(this T[,] matrix1, T[,] matrix2)
102141
{
103142
if (matrix1 is null || matrix2 is null)
104143
return false;
@@ -118,6 +157,34 @@ public static bool IsEqualTo<T>(this T[,] matrix1, T[,] matrix2)
118157

119158
return true;
120159
}
160+
161+
/// <summary>
162+
/// Sequentially compares elements of arrays using <see cref="object.ReferenceEquals"/>
163+
/// </summary>
164+
/// <typeparam name="T"></typeparam>
165+
/// <param name="matrix1"></param>
166+
/// <param name="matrix2"></param>
167+
/// <returns></returns>
168+
public static bool IsReferenceSequenceEqualTo<T>(this T[] matrix1, T[] matrix2)
169+
{
170+
if (matrix1 is null || matrix2 is null)
171+
return false;
172+
var len1 = matrix1.GetLength(0);
173+
var len2 = matrix1.GetLength(1);
174+
if (len1 != matrix2.GetLength(0)
175+
&& len2 != matrix2.GetLength(1))
176+
return false;
177+
for (int i = 0; i < len1; i++)
178+
{
179+
for (int j = 0; j < len2; j++)
180+
{
181+
if (!ReferenceEquals(matrix1[i], matrix2[i]))
182+
return false;
183+
}
184+
}
185+
186+
return true;
187+
}
121188
#endregion
122189

123190
#region Argument check
@@ -458,11 +525,40 @@ private static void ThrowIfNull_NotQuad_SizeDiffers<T>(T[,] array1, T[,] array2)
458525

459526
#region Extremums
460527

528+
/// <summary>
529+
/// Returns true if matrix have any relational maximum
530+
/// </summary>
531+
/// <param name="matrix1"></param>
532+
/// <returns></returns>
461533
public static bool HasMaximum(this bool[,] matrix1) => GetMaximums(matrix1).Any();
534+
535+
/// <summary>
536+
/// Returns true if matrix have any relational minimum
537+
/// </summary>
538+
/// <param name="matrix1"></param>
539+
/// <returns></returns>
462540
public static bool HasMinimum(this bool[,] matrix1) => GetMinimums(matrix1).Any();
541+
542+
/// <summary>
543+
/// Returns true if matrix have any relational majorants
544+
/// </summary>
545+
/// <param name="matrix1"></param>
546+
/// <returns></returns>
463547
public static bool HasMajorant(this bool[,] matrix1) => GetMajorants(matrix1).Any();
548+
549+
/// <summary>
550+
/// Returns true if matrix have any relational minorants
551+
/// </summary>
552+
/// <param name="matrix1"></param>
553+
/// <returns></returns>
464554
public static bool HasMinorant(this bool[,] matrix1) => GetMinorants(matrix1).Any();
465555

556+
/// <summary>
557+
/// Returns indexes of relations those are maximums
558+
/// <see href="http://www.u.arizona.edu/~mwalker/econ519/Econ519LectureNotes/BinaryRelations.pdf"/>
559+
/// </summary>
560+
/// <param name="matrix1"></param>
561+
/// <returns></returns>
466562
public static IEnumerable<int> GetMaximums(this bool[,] matrix1)
467563
{
468564
ThrowIfNull_NotQuad(matrix1);
@@ -486,6 +582,12 @@ public static IEnumerable<int> GetMaximums(this bool[,] matrix1)
486582
}
487583
}
488584

585+
/// <summary>
586+
/// Returns indexes of relations those are minimums
587+
/// <see href="http://www.u.arizona.edu/~mwalker/econ519/Econ519LectureNotes/BinaryRelations.pdf"/>
588+
/// </summary>
589+
/// <param name="matrix1"></param>
590+
/// <returns></returns>
489591
public static IEnumerable<int> GetMinimums(this bool[,] matrix1)
490592
{
491593
ThrowIfNull_NotQuad(matrix1);
@@ -509,6 +611,12 @@ public static IEnumerable<int> GetMinimums(this bool[,] matrix1)
509611
}
510612
}
511613

614+
/// <summary>
615+
/// Returns indexes of relations those are majorants
616+
/// <see href="http://www.u.arizona.edu/~mwalker/econ519/Econ519LectureNotes/BinaryRelations.pdf"/>
617+
/// </summary>
618+
/// <param name="matrix1"></param>
619+
/// <returns></returns>
512620
public static IEnumerable<int> GetMajorants(this bool[,] matrix1)
513621
{
514622
ThrowIfNull_NotQuad(matrix1);
@@ -532,6 +640,12 @@ public static IEnumerable<int> GetMajorants(this bool[,] matrix1)
532640
}
533641
}
534642

643+
/// <summary>
644+
/// Returns indexes of relations those are minorants
645+
/// <see href="http://www.u.arizona.edu/~mwalker/econ519/Econ519LectureNotes/BinaryRelations.pdf"/>
646+
/// </summary>
647+
/// <param name="matrix1"></param>
648+
/// <returns></returns>
535649
public static IEnumerable<int> GetMinorants(this bool[,] matrix1)
536650
{
537651
ThrowIfNull_NotQuad(matrix1);
@@ -559,11 +673,11 @@ public static IEnumerable<int> GetMinorants(this bool[,] matrix1)
559673
#region Relation properties check
560674

561675
/// <summary>
562-
/// Is a full related binary matrix
676+
/// Is a total relation binary matrix
563677
/// </summary>
564678
/// <param name="matrix1">binary matrix</param>
565679
/// <returns>bool</returns>
566-
public static bool IsFullRelation(this bool[,] matrix1)
680+
public static bool IsTotalRelation(this bool[,] matrix1)
567681
{
568682
ThrowIfNull_NotQuad(matrix1);
569683

BinaryRelations/MatrixExtensions.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,41 @@ namespace MaxRev.Extensions.Matrix
77
{
88
public static class MatrixExtensions
99
{
10+
#region Fill
11+
12+
public static T[] Fill<T>(this T[] array, T value)
13+
{
14+
if (array == null) throw new ArgumentNullException(nameof(array));
15+
16+
var length = array.GetLength(0);
17+
18+
for (int i = 0; i < length; i++)
19+
{
20+
array[i] = value;
21+
}
22+
23+
return array;
24+
}
25+
26+
public static T[,] Fill<T>(this T[,] array, T value)
27+
{
28+
if (array == null) throw new ArgumentNullException(nameof(array));
29+
30+
var length = array.GetLength(0);
31+
32+
for (int i = 0; i < length; i++)
33+
{
34+
for (int j = 0; j < length; j++)
35+
{
36+
array[i, j] = value;
37+
}
38+
}
39+
40+
return array;
41+
}
42+
43+
#endregion
44+
1045
#region Coll & Row
1146

1247
public static T[] GetRow<T>(this T[,] matrix, in int row)
@@ -483,6 +518,23 @@ public static void Print<T>(this T[,] arr, int floatTolerance = 5)
483518
Console.WriteLine();
484519
}
485520
}
521+
public static void Print<T>(this T[] arr, int floatTolerance = 5)
522+
{
523+
var a = arr.GetLength(0);
524+
for (int i = 0; i < a; i++)
525+
{
526+
string fx = default;
527+
if (arr[i] is float f)
528+
fx = f.ToString("f" + floatTolerance);
529+
if (arr[i] is double d)
530+
fx = d.ToString("f" + floatTolerance);
531+
if (arr[i] is decimal dc)
532+
fx = dc.ToString("f" + floatTolerance);
533+
if (fx == default) fx = arr[i].ToString();
534+
Console.Write(fx);
535+
Console.Write(new string(' ', 1));
536+
}
537+
}
486538

487539
[MethodImpl(MethodImplOptions.AggressiveInlining)]
488540
private static void ThrowIfSizeNotEqual<T>(this T[,] array1, in T[,] array2)

BinaryRelations/RandomExtensions.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,5 @@ public static double[] AllocateRandomVector(int size)
4343
Randomize(array);
4444
return array;
4545
}
46-
47-
public static T[,] Fill<T>(this T[,] array, T value)
48-
{
49-
if (array == null) throw new ArgumentNullException(nameof(array));
50-
51-
var length = array.GetLength(0);
52-
53-
for (int i = 0; i < length; i++)
54-
{
55-
for (int j = 0; j < length; j++)
56-
{
57-
array[i, j] = value;
58-
}
59-
}
60-
61-
return array;
62-
}
6346
}
6447
}

0 commit comments

Comments
 (0)