-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathMatrix.cs
283 lines (254 loc) · 11.1 KB
/
Matrix.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
using System;
using UnityEngine;
//To write a Kalman Filter, one must first invent Linear Algebra
namespace SimpleMatrices {
public static class MatrixExtensions {
//Makes an identity matrix with these dimensions
public static double[,] makeIdentity(int inwidth, int inheight) {
double[,] toReturn = new double[inheight, inwidth];
//Set the diagonals to 1
for (int i = 0; i < Mathf.Min(inwidth, inheight); i++) {
toReturn[i, i] = 1f;
}
return toReturn;
}
public static double[,] makeIdentity(int indimension) {
return makeIdentity(indimension, indimension);
}
//Multiplies this Matrix by another Matrix
public static double[,] multiply(this double[,] thisMatrix, double[,] inMatrix) {
if (thisMatrix.GetLength(1) == inMatrix.GetLength(0)) {
double[,] toReturn = new double[thisMatrix.GetLength(0), inMatrix.GetLength(1)];
for (int i = 0; i < thisMatrix.GetLength(0); i++) { //i is the row in this matrix
for (int j = 0; j < inMatrix.GetLength(1); j++) { //j is the column in the other matrix
for (int k = 0; k < thisMatrix.GetLength(1); k++) { //k is the column in this matrix
toReturn[i, j] += thisMatrix[i, k] * inMatrix[k, j];
}
}
}
return toReturn;
} else {
return thisMatrix;
}
}
//Returns a Vector3 column of a 3x3 matrix
public static Vector3 col(this double[,] thisMatrix, int i) {
if (thisMatrix.GetLength(0) == 3 && thisMatrix.GetLength(1) == 3) {
return new Vector3((float)thisMatrix[0, i], (float)thisMatrix[1, i], (float)thisMatrix[2, i]);
} else {
Debug.LogError("Not a 3x3 Matrix!");
return Vector3.one;
}
}
//Returns a Vector3 row of a 3x3 matrix
public static Vector3 row(this double[,] thisMatrix, int i) {
if (thisMatrix.GetLength(0) == 3 && thisMatrix.GetLength(1) == 3) {
return new Vector3((float)thisMatrix[i, 0], (float)thisMatrix[i, 1], (float)thisMatrix[i, 2]);
} else {
Debug.LogError("Not a 3x3 Matrix!");
return Vector3.one;
}
}
//Multiplies this Matrix by a Scalar
public static double[,] multiply(this double[,] thisMatrix, double inScalar) {
double[,] toReturn = new double[thisMatrix.GetLength(0), thisMatrix.GetLength(1)];
for (int i = 0; i < thisMatrix.GetLength(0); i++) { //i is the row in this matrix
for (int j = 0; j < thisMatrix.GetLength(1); j++) { //j is the column in the this matrix
toReturn[i, j] = thisMatrix[i, j] * inScalar;
}
}
return toReturn;
}
//Performs a component-wise sqrt on this matrix
public static double[,] sqrt(this double[,] thisMatrix) {
double[,] toReturn = new double[thisMatrix.GetLength(0), thisMatrix.GetLength(1)];
for (int i = 0; i < thisMatrix.GetLength(0); i++) { //i is the row in this matrix
for (int j = 0; j < thisMatrix.GetLength(1); j++) { //j is the column in the this matrix
toReturn[i, j] = Math.Sqrt(toReturn[i, j]);
}
}
return toReturn;
}
//Performs a component-wise addition between two matrices
public static double[,] add(this double[,] thisMatrix, double[,] inMatrix) {
if (thisMatrix.GetLength(0) == inMatrix.GetLength(0) && thisMatrix.GetLength(1) == inMatrix.GetLength(1)) {
double[,] toReturn = new double[thisMatrix.GetLength(0), thisMatrix.GetLength(1)];
for (int i = 0; i < thisMatrix.GetLength(0); i++) { //i is the row in this matrix
for (int j = 0; j < thisMatrix.GetLength(1); j++) { //j is the column in this matrix
toReturn[i, j] = thisMatrix[i, j] + inMatrix[i, j];
}
}
return toReturn;
} else {
Debug.LogWarning("Matrices are not of equal dimensions!\nThis Matrix is a " +
thisMatrix.GetLength(0) + "x" + thisMatrix.GetLength(1) + " while the other is a " +
inMatrix.GetLength(0) + "x" + inMatrix.GetLength(1) + "!");
return thisMatrix;
}
}
//Performs a component-wise subtraction between two matrices
public static double[,] subtract(this double[,] thisMatrix, double[,] inMatrix) {
if (thisMatrix.GetLength(0) == inMatrix.GetLength(0) && thisMatrix.GetLength(1) == inMatrix.GetLength(1)) {
double[,] toReturn = new double[thisMatrix.GetLength(0), thisMatrix.GetLength(1)];
for (int i = 0; i < thisMatrix.GetLength(0); i++) { //i is the row in this matrix
for (int j = 0; j < thisMatrix.GetLength(1); j++) { //j is the column in this matrix
toReturn[i, j] = thisMatrix[i, j] - inMatrix[i, j];
}
}
return toReturn;
} else {
Debug.LogWarning("Matrices are not of equal dimensions!\nThis Matrix is a " +
thisMatrix.GetLength(0) + "x" + thisMatrix.GetLength(1) + " while the other is a " +
inMatrix.GetLength(0) + "x" + inMatrix.GetLength(1) + "!");
return thisMatrix;
}
}
//Transposes this Matrix
public static double[,] transpose(this double[,] thisMatrix) {
double[,] toReturn = new double[thisMatrix.GetLength(1), thisMatrix.GetLength(0)];
for (int i = 0; i < thisMatrix.GetLength(0); i++) { //i is the row in this matrix
for (int j = 0; j < thisMatrix.GetLength(1); j++) { //j is the column in this matrix
toReturn[j, i] = thisMatrix[i, j];
}
}
return toReturn;
}
//Gloms another Matrix onto the right side of this one
public static double[,] concatenate(this double[,] thisMatrix, double[,] inMatrix) {
if (thisMatrix.GetLength(0) == inMatrix.GetLength(1)) {
double[,] toReturn = new double[thisMatrix.GetLength(0), thisMatrix.GetLength(1) + inMatrix.GetLength(1)];
for (int i = 0; i < thisMatrix.GetLength(0); i++) { //i is the row in this matrix
for (int j = 0; j < thisMatrix.GetLength(1); j++) { //j is the column in this matrix
toReturn[i, j] = thisMatrix[i, j];
}
for (int j = inMatrix.GetLength(1); j < inMatrix.GetLength(1) + thisMatrix.GetLength(1); j++) {
toReturn[i, j] = inMatrix[i, j - thisMatrix.GetLength(1)];
}
}
return toReturn;
} else {
Debug.LogWarning("Matrices are not of equal height!");
return thisMatrix;
}
}
//Trims away the excess identity Matrix once you're done inverting it
public static double[,] deconcatenate(this double[,] thisMatrix, int startColumn) {
double[,] toReturn = new double[thisMatrix.GetLength(0), thisMatrix.GetLength(1) - startColumn];
for (int i = 0; i < thisMatrix.GetLength(0); i++) { //i is the row in this matrix
for (int j = startColumn; j < thisMatrix.GetLength(1); j++) { //j is the column in the this matrix
toReturn[i, j - startColumn] = thisMatrix[i, j];
}
}
return toReturn;
}
//Swaps two rows in this Matrix
public static double[,] swapRow(this double[,] thisMatrix, int row1, int row2) {
double[,] toReturn = (double[,])thisMatrix.Clone();
double[] tempRow = new double[thisMatrix.GetLength(1)];
for (int i = 0; i < thisMatrix.GetLength(1); i++) {
tempRow[i] = toReturn[row2, i];
toReturn[row2, i] = toReturn[row1, i];
toReturn[row1, i] = tempRow[i];
}
return toReturn;
}
//Code ported from Vik's Blog on Matrix Inversion ----
//http://www.vikparuchuri.com/blog/inverting-your-very-own-matrix/
//Checks Matrix to see if only zeros exist at or below row in column
public static bool checkforAllZeros(this double[,] thisMatrix, int row, int column, out double sum, out int firstnonzeroindex) {
sum = 0;
firstnonzeroindex = -1;
for (int i = row; i < thisMatrix.GetLength(0); i++) {
sum += thisMatrix[i, column];
if (firstnonzeroindex == -1 && sum != 0) {
firstnonzeroindex = i;
}
}
return sum == 0 ? true : false;
}
//Inverts this Matrix using Gauss-Jordan Elimination
//THIS FUNCTION IS BROKEN
public static double[,] inverse(this double[,] thisMatrix) {
//Add an Identity Matrix on to the side of this Matrix
double[,] Inversion = thisMatrix.concatenate(makeIdentity(thisMatrix.GetLength(0), thisMatrix.GetLength(1)));
//Begin Converting left Matrix to Row-Echelon form
int i = 0;
for (int j = 0; j < thisMatrix.GetLength(1); j++) {
//Debug.Log ("On col: "+j+" and row: "+i);
double sum;
int firstnonzeroindex;
if (Inversion.checkforAllZeros(i, j, out sum, out firstnonzeroindex)) {
if (j == thisMatrix.GetLength(1)) {
Debug.Log("wtf is this");
return Inversion;
}
Debug.LogError("Matrix isn't invertible.");
} else {
if (firstnonzeroindex != i) {
Inversion = Inversion.swapRow(i, firstnonzeroindex);
}
for (int k = 0; k < Inversion.GetLength(1); k++) {
double Normalizer = Inversion[i, j];
Inversion[i, k] /= Normalizer;
}
for (int q = 0; q < Inversion.GetLength(0); q++) {
if (q != i) {
double scale = Inversion[q, j];
for (int k = 0; k < Inversion.GetLength(1); k++) {
Inversion[q, k] -= scale * Inversion[i, k];
}
}
}
if (i == thisMatrix.GetLength(0) || j == thisMatrix.GetLength(1)) {
break;
}
i += 1;
}
}
//Trim off the husk Identity left over from the Inversion
Inversion = Inversion.deconcatenate(thisMatrix.GetLength(1));
return Inversion;
}
//------------------------------------------------------
//Copied from Rosetta Code
//https://rosettacode.org/wiki/Cholesky_decomposition#C.23
//
public static double[,] cholesky(this double[,] thisMatrix) {
int n = (int)Mathf.Sqrt(thisMatrix.Length);
double[,] ret = new double[n, n];
for (int r = 0; r < n; r++)
for (int c = 0; c <= r; c++) {
if (c == r) {
double sum = 0;
for (int j = 0; j < c; j++) {
sum += ret[c, j] * ret[c, j];
}
ret[c, c] = Math.Sqrt(thisMatrix[c, c] - sum);
} else {
double sum = 0;
for (int j = 0; j < c; j++)
sum += ret[r, j] * ret[c, j];
ret[r, c] = 1.0f / ret[c, c] * (thisMatrix[r, c] - sum);
}
}
return ret;
}
//Prints this Matrix out all pretty-like
public static string MatrixToString(this double[,] thisMatrix) {
string MatrixString = "[ ";
for (int i = 0; i < thisMatrix.GetLength(0); i++) {
for (int j = 0; j < thisMatrix.GetLength(1); j++) {
MatrixString += thisMatrix[i, j];
if (i == thisMatrix.GetLength(0) - 1 && j == thisMatrix.GetLength(1) - 1) {
MatrixString += " ]";
} else if (j == thisMatrix.GetLength(1) - 1) {
MatrixString += ",\n ";
} else {
MatrixString += ", ";
}
}
}
return MatrixString;
}
}
}