-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinearAlgebra.cs
417 lines (358 loc) · 11.9 KB
/
LinearAlgebra.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
using System;
namespace SimpleQRAlgorithm
{
public static class LinearAlgebra
{
/// <summary>
/// Gets the j-th column of the given matrix.
/// </summary>
/// <param name="A">The matrix.</param>
/// <param name="j">The index of the column.</param>
/// <returns>The column vector.</returns>
public static float[] GetColumn(float[,] A, int j)
{
int n = A.GetLength(0);
float[] column = new float[n];
for (int i = 0; i < n; i++)
{
column[i] = A[i, j];
}
return column;
}
/// <summary>
/// Duplicates the given matrix.
/// </summary>
/// <param name="A">The matrix to duplicate.</param>
/// <returns>The duplicated matrix.</returns>
public static float[,] Duplicate(float[,] A)
{
int n = A.GetLength(0);
float[,] B = new float[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
B[i, j] = A[i, j];
}
}
return B;
}
/// <summary>
/// Scales the given vector by a scalar.
/// </summary>
/// <param name="a">The vector to scale.</param>
/// <param name="s">The scalar.</param>
/// <returns></returns>
public static float[] Scale(float[] a, float s)
{
int n = a.Length;
float[] b = new float[n];
for (int i = 0; i < n; i++)
{
b[i] = s * a[i];
}
return b;
}
/// <summary>
/// Calculates the inner product between two vectors.
/// </summary>
/// <param name="a">The first vector.</param>
/// <param name="b">The second vector.</param>
/// <returns>The inner product value.</returns>
public static float InnerProduct(float[] a, float[] b)
{
float innerProduct = 0;
for (int i = 0; i < a.Length; i++)
{
innerProduct += a[i] * b[i];
}
return innerProduct;
}
/// <summary>
/// Calculates the outer product between two vectors.
/// </summary>
/// <param name="a">The first vector.</param>
/// <param name="b">The second vector.</param>
/// <returns>The outer product matrix.</returns>
public static float[,] OuterProduct(float[] a, float[] b)
{
int n = a.Length;
float[,] outerProduct = new float[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
outerProduct[i, j] = a[i] * b[j];
}
}
return outerProduct;
}
/// <summary>
/// Projects the vector "a" orthogonally onto vector "u".
/// </summary>
/// <param name="a">The vector to project.</param>
/// <param name="b">The vector on which will be projected.</param>
/// <returns>The projection.</returns>
public static float[] Project(float[] a, float[] b)
{
return Scale(a, InnerProduct(a, b) / InnerProduct(a, a));
}
/// <summary>
/// Substracts vector "b" from vector "a".
/// </summary>
/// <param name="a">The vector to be subtracted.</param>
/// <param name="b">The substracting vector.</param>
/// <returns>The substracted vector.</returns>
public static float[] Subtract(float[] a, float[] b)
{
int n = a.Length;
float[] c = new float[n];
for (int i = 0; i < n; i++)
{
c[i] = a[i] - b[i];
}
return c;
}
/// <summary>
/// Adds vector "b" from vector "a".
/// </summary>
/// <param name="a">The vector to be added.</param>
/// <param name="b">The adding vector.</param>
/// <returns>The added vector.</returns>
public static float[] Add(float[] a, float[] b)
{
int n = a.Length;
float[] c = new float[n];
for (int i = 0; i < n; i++)
{
c[i] = a[i] + b[i];
}
return c;
}
/// <summary>
/// Adds matrix B to matrix A.
/// </summary>
/// <param name="a">The matrix to be added.</param>
/// <param name="b">The adding matrix.</param>
/// <returns>The added matrix.</returns>
public static float[,] Add(float[,] A, float[,] B)
{
int n = A.GetLength(0);
float[,] C = new float[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
C[i, j] = A[i, j] + B[i, j];
}
}
return C;
}
/// <summary>
/// Multiplies matrix A with matrix B.
/// </summary>
/// <param name="A">The first matrix.</param>
/// <param name="B">The second matrix.</param>
/// <returns>The resulting matrix.</returns>
public static float[,] Product(float[,] A, float[,] B)
{
int n = A.GetLength(0);
float[,] C = new float[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
C[i, j] += A[i, k] * B[k, j];
}
}
}
return C;
}
public static float[] Product(float[,] A, float[] b)
{
int n = A.GetLength(0);
float[] c = new float[n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
c[i] += A[i, j] * b[j];
}
}
return c;
}
/// <summary>
/// Calculates the transpose of the given matrix.
/// </summary>
/// <param name="A">The matrix to transpose.</param>
/// <returns>The transpose.</returns>
public static float[,] Transpose(float[,] A)
{
int n = A.GetLength(0);
float[,] B = new float[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
B[i, j] = A[j, i];
}
}
return B;
}
/// <summary>
/// Calculates the magnitude of the given vector.
/// </summary>
/// <param name="a">The vector to compute the magnitude of.</param>
/// <returns>The magnitude.</returns>
public static float Magnitude(float[] a)
{
float magnitude = 0;
for (int i = 0; i < a.Length; i++)
{
magnitude += a[i] * a[i];
}
return (float)Math.Sqrt(magnitude);
}
/// <summary>
/// Constructs an n-by-n identity matrix.
/// </summary>
/// <param name="n">The size of the matrix.</param>
/// <returns>The identity matrix.</returns>
public static float[,] Identity(int n)
{
float[,] I = new float[n, n];
for (int i = 0; i < n; i++)
{
I[i, i] = 1;
}
return I;
}
/// <summary>
/// Calculates the inverse of the given matrix using the
/// Gauss-Jordan Method (see https://en.wikipedia.org/wiki/Gaussian_elimination).
/// </summary>
/// <param name="A">The matrix to invert.</param>
/// <returns>The inverse of the given matrix.</returns>
public static float[,] Inverse(float[,] A)
{
// Initialize the augmented matrix B.
int n = A.GetLength(0);
float[,] B = new float[n, 2 * n];
// In the augmented matrix B, the first 3 columns are the original
// matrix A, and the last 3 columns are the identity matrix C.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
B[i, j] = A[i, j];
}
for (int j = n; j < 2 * n; j++)
{
if (i == j - n) B[i, j] = 1;
}
}
// Swap rows of the augmented matrix B.
for (int i = n - 1; i > 0; i--)
{
if (B[i - 1, 0] >= B[i, 0]) continue;
for (int j = 0; j < 2 * n; j++)
{
float temp = B[i, j];
B[i, j] = B[i - 1, j];
B[i - 1, j] = temp;
}
}
// Substract each row by a multiple of another row.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j) continue;
float temp = B[j, i] / B[i, i];
for (int k = 0; k < 2 * n; k++)
{
B[j, k] -= B[i, k] * temp;
}
}
}
// Divide each row element by the diagonal element.
for (int i = 0; i < n; i++)
{
float temp = B[i, i];
for (int j = 0; j < 2 * n; j++)
{
B[i, j] = B[i, j] / temp;
}
}
// Strip the augmented matrix B of the first three columns
// to get the inverse matrix C of the original matrix A.
float[,] C = new float[n, n];
for (int i = 0; i < n; i++)
{
for (int j = n; j < 2 * n; j++)
{
C[i, j - n] = B[i, j];
}
}
// Return the inverse matrix C.
return C;
}
/// <summary>
/// Takes the square root of the given diagonal matrix.
/// </summary>
/// <param name="A">The diagonal matrix.</param>
/// <returns>The square root of the given matrix.</returns>
public static float[,] Sqrt(float[,] A)
{
int n = A.GetLength(0);
float[,] B = Duplicate(A);
for (int i = 0; i < n; i++)
{
if (A[i, i] <= 0) { throw new DivideByZeroException(); };
B[i, i] = (float)Math.Sqrt(A[i, i]);
}
return B;
}
/// <summary>
/// Prints the given matrix.
/// </summary>
/// <param name="A">The matrix to print.</param>
/// <returns>The string representation of the given matrix.</returns>
public static string ToString(float[,] A)
{
int rowCount = A.GetLength(0);
int columnCount = A.GetLength(1);
string text = "";
for (int i = 0; i < rowCount; i++)
{
if (i > 0) text += ',';
text += '{';
for (int j = 0; j < columnCount; j++)
{
if (j > 0) text += ',';
text += A[i, j];
}
text += '}';
}
return text;
}
/// <summary>
/// Prints the given vector.
/// </summary>
/// <param name="a">The vector to print.</param>
/// <returns>The string representation of the given vector.</returns>
public static string ToString(float[] a)
{
string text = "{";
for (int i = 0; i < a.Length; i++)
{
if (i > 0) text += ',';
text += a[i];
}
text += '}';
return text;
}
}
}