-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.Generators.cs
140 lines (125 loc) · 5.64 KB
/
Matrix.Generators.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
namespace ABCNET.Utils
{
/// <summary>
/// Ïðåäîñòàâëÿåò ôóíêöèîíàë äëÿ ðàáîòû ñ ìàòðèöàìè.
/// </summary>
public static partial class Matrix
{
#region public
/// <summary>
/// Ñîçäà¸ò ìàòðèöó èç óêàçàííûõ çíà÷åíèé.
/// </summary>
/// <param name="rowsCount">Êîëè÷åñòâî ñòðîê.</param>
/// <param name="colsCount">Êîëè÷åñòâî ñòîëáöîâ.</param>
/// <param name="values">Çíà÷åíèÿ.</param>
/// <returns>Ìàòðèöà.</returns>
public static T[,] Of<T>(int rowsCount, int colsCount, params T[] values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
if (rowsCount < 0)
throw new ArgumentOutOfRangeException(nameof(rowsCount));
if (colsCount < 0)
throw new ArgumentOutOfRangeException(nameof(colsCount));
if (rowsCount * colsCount != values.Length)
throw new ArgumentNullException(nameof(values));
T[,] source = new T[rowsCount, colsCount];
int k = 0;
for (int i = 0; i < rowsCount; i++)
for (int j = 0; j < colsCount; j++)
{
source[i, j] = values[k];
k++;
}
return source;
}
/// <summary>
/// Ñîçäà¸ò ìàòðèöó íà îñíîâå ôóíêöèè ñåëåêòîðà.
/// </summary>
/// <param name="rowsCount">Êîëè÷åñòâî ñòðîê.</param>
/// <param name="colsCount">Êîëè÷åñòâî ñòîëáöîâ.</param>
/// <param name="selector">Ôóíêöèÿ ñåëåêòîð.</param>
/// <param name="rowFirstIndex">Íà÷àëüíûé èíäåêñ ñòðîêè.</param>
/// <param name="columnFirstIndex">Íà÷àëüíûé èíäåêñ ñòîëáöà.</param>
/// <returns>Ìàòðèöà.</returns>
public static T[,] By<T>(int rowsCount, int colsCount, Func<int, int, T> selector, int rowFirstIndex = 0, int columnFirstIndex = 0)
{
if (rowsCount < 0)
throw new ArgumentOutOfRangeException(nameof(rowsCount));
if (colsCount < 0)
throw new ArgumentOutOfRangeException(nameof(colsCount));
if (selector == null)
throw new ArgumentNullException(nameof(selector));
T[,] source = new T[rowsCount, colsCount];
for (int i = 0; i < source.GetLength(0); i++)
for (int j = 0; j < source.GetLength(1); j++)
source[i, j] = selector(i + rowFirstIndex, j + columnFirstIndex);
return source;
}
/// <summary>
/// Ñîçäà¸ò ìàòðèöó ñëó÷àéíûõ ÷èñåë òèïà Int32.
/// </summary>
/// <param name="rowsCount">Êîëè÷åñòâî ñòðîê.</param>
/// <param name="colsCount">Êîëè÷åñòâî ñòîëáöîâ.</param>
/// <param name="low">Íèæíÿÿ ãðàíèöà äèàïàçîíà.</param>
/// <param name="high">Âåðõíÿÿ ãðàíèöà äèàïàçîíà.</param>
/// <returns>Ìàòðèöà.</returns>
public static int[,] Random(int rowsCount, int colsCount, int low = Int32BordersHelper.Low, int high = Int32BordersHelper.High)
{
if (rowsCount < 0)
throw new ArgumentOutOfRangeException(nameof(rowsCount));
if (colsCount < 0)
throw new ArgumentOutOfRangeException(nameof(colsCount));
if (low > high)
throw new ArgumentException(nameof(low));
int[,] source = new int[rowsCount, colsCount];
for (int i = 0; i < source.GetLength(0); i++)
for (int j = 0; j < source.GetLength(1); j++)
source[i, j] = Base.Random(low, high);
return source;
}
/// <summary>
/// Ñîçäà¸ò ìàòðèöó ñëó÷àéíûõ ÷èñåë òèïà Double.
/// </summary>
/// <param name="rowsCount">Êîëè÷åñòâî ñòðîê.</param>
/// <param name="colsCount">Êîëè÷åñòâî ñòîëáöîâ.</param>
/// <param name="low">Íèæíÿÿ ãðàíèöà äèàïàçîíà.</param>
/// <param name="high">Âåðõíÿÿ ãðàíèöà äèàïàçîíà.</param>
/// <returns>Ìàòðèöà.</returns>
public static double[,] Random(int rowsCount, int colsCount, double low = DoubleBordersHelper.Low, double high = DoubleBordersHelper.High)
{
if (rowsCount < 0)
throw new ArgumentOutOfRangeException(nameof(rowsCount));
if (colsCount < 0)
throw new ArgumentOutOfRangeException(nameof(colsCount));
if (low > high)
throw new ArgumentException(nameof(low));
double[,] source = new double[rowsCount, colsCount];
for (int i = 0; i < source.GetLength(0); i++)
for (int j = 0; j < source.GetLength(1); j++)
source[i, j] = Base.Random(low, high);
return source;
}
/// <summary>
/// Ñîçäà¸ò ìàòðèöó, çàïîëíåííóþ óêàçàííûì çíà÷åíèåì.
/// </summary>
/// <param name="rowsCount">Êîëè÷åñòâî ñòðîê.</param>
/// <param name="colsCount">Êîëè÷åñòâî ñòîëáöîâ.</param>
/// <param name="value">Çíà÷åíèå.</param>
/// <returns>Ìàññèâ.</returns>
public static T[,] Fill<T>(int rowsCount, int colsCount, T value)
{
if (rowsCount < 0)
throw new ArgumentOutOfRangeException(nameof(rowsCount));
if (colsCount < 0)
throw new ArgumentOutOfRangeException(nameof(colsCount));
T[,] source = new T[rowsCount, colsCount];
for (int i = 0; i < source.GetLength(0); i++)
for (int j = 0; j < source.GetLength(1); j++)
source[i, j] = value;
return source;
}
#endregion public
}
}