-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrixSolver.js
More file actions
246 lines (204 loc) · 7.79 KB
/
MatrixSolver.js
File metadata and controls
246 lines (204 loc) · 7.79 KB
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
// A = [[1, 2, 3], [2, 1, 3]]
// A = [[1, 0, 7], [0, 1, 4]]
// *** A = [[1, 1, 0, 1, 21], [1, 1, 1, 0, 21], [0, 2, 3, 0, 37], [2, 1, 0, 0, 19]] ***
/* Old algo
function gauss(rows, columns, A) {
var n = A.length;
if (n !== rows) { // Number of expected rows != number of rows
var x = "The dimensions of the expected matrix are different from the dimensions of the given matrix!"
return x
}
for (var i = 0; i < n - 1; i ++) {
if (A[i].length !== A[i + 1].length) {
var x = "The dimensions of the expected matrix are different from the dimensions of the given matrix!"
return x
}
}
for (var i=0; i<n; i++) {
// Search for maximum in this column
var maxEl = Math.abs(A[i][i]);
var maxRow = i;
for(var k=i+1; k<n; k++) {
if (Math.abs(A[k][i]) > maxEl) {
maxEl = Math.abs(A[k][i]);
maxRow = k;
}
}
// Swap maximum row with current row (column by column)
for (var k=i; k<n+1; k++) {
var tmp = A[maxRow][k];
A[maxRow][k] = A[i][k];
A[i][k] = tmp;
}
// Make all rows below this one 0 in current column
for (k=i+1; k<n; k++) {
var c = -A[k][i]/A[i][i];
for(var j=i; j<n+1; j++) {
if (i==j) {
A[k][j] = 0;
} else {
A[k][j] += c * A[i][j];
}
}
}
}
///////////////////SEE NEW IMPLEMENTATION FOR UPDATED VERSION OF THIS CHECK///////////
var zeroes = []
for (var i = 0; i < A[0].length - 1; i ++) { //checking simplified matrix for dependency
zeroes.push(0)
}
for (var i = 0; i < n; i ++) {
if (JSON.stringify(A[i].slice(0, A[0].length - 1)) == JSON.stringify(zeroes)){ //if LHS is zeroes only (ie. row of zeroes for LHS)
if (A[i][A[0].length - 1] == 0){ //if 0 = 0
//if (A.slice())
return gauss(rows-1, columns, A.splice(i,1))
}else { //if 0 = 1 or something
return "No solutions" // check for row comparison
}
}
}
///////////////////SEE NEW IMPLEMENTATION FOR UPDATED VERSION OF THIS CHECK///////////
// Solve equation Ax=b for an upper triangular matrix A
var x= new Array(n);
for (var i=n-1; i>-1; i--) {
x[i] = A[i][n]/A[i][i];
for (var k=i-1; k>-1; k--) {
A[k][n] -= A[k][i] * x[i];
}
}
return x;
}
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//TRYING TO IMPLEMENT GAUSSIAN ELIMINATION ON OUR OWN
//////////////////////////////////////////////////////////////////////
// TODO: Replace A.length and equivalents with "rows" and "columns" //
// - fix swap so that final vars match order of inital vars //
//////////////////////////////////////////////////////////////////////
var A = [[0, 2, 3], [2, 1, 3]]
A= [[1,-2,1,-6],[2,-3,0,-7], [-2, 6, -6, 22], [-2, 6, -6, 22], [-2, 6, -6, 22]]
//A = [[1,0,6],[1,0,3]]
// A= [[0,0,2,6],[0,2,0,8],[2,0,0,10]]
//console.log(A)
console.log(gauss(5,4,A))
//algorithm adapted from: https://www.csun.edu/~panferov/math262/262_rref.pdf
function gauss(rows, columns, A){
var sol = []
var i = 0
var j = 0
while (i < (rows) && j < (columns - 1) && (j!= null)) { //last column is augmented, we don't want to change it
A, j = swap(i, j, A) //swap swaps rows and reassigns column
console.log("swapped", A)
//might need conditions to handle end j value
if(j==null) {
return solutions(i,j,A)
}
A = divide(i, j, A)
console.log("divided", A)
A = eliminate(i, j, A)
console.log("eliminated", A)
i = i + 1
j = j + 1
console.log(i, j)
}
console.log("getting out of functions")
return solutions(i, j, A);
//Should have RREF at this point according to algorithm
function solutions(i, j, A){
//Check for infinite/no solutions
var zeroes = []
for (var i = 0; i < A[0].length - 1; i ++) { //checking simplified matrix for dependency
zeroes.push(0)
}
for (var i = 0; i < rows; i ++) {
if (JSON.stringify(A[i].slice(0, A[0].length - 1)) == JSON.stringify(zeroes)){ //if LHS is zeroes only (ie. row of zeroes for LHS)
if (A[i][A[0].length - 1] == 0){ //if 0 = 0
if ((columns) > A.length){ // if more variables than equations
A= convert_dec(rows, columns, A)
return ["Infinite solutions", A]
} else{
A.splice(i, 1)
return gauss(rows-1, columns, A)
}
}else { //if 0 = 1 or something
A= convert_dec(rows, columns, A)
return ["No solutions", A] // check for row comparison
}
}
}
A = convert_dec(rows, columns, A)
for (k = 0; k < A.length; k++){
sol.push(A[k][columns - 1])
}
return ["Unique solutions", sol]
}
//functions
function col_all_zeroes(i, j, A){ //Step 1: Change columns until we get to a pivot column (non-zero)
while (j < (columns - 1)){
for (iter = i; iter < A.length; iter++){ //j is column input
if (A[iter][j] != 0){
console.log('here,'+ j)
return j
}
}
j = j + 1
}
return (null) //returns None if every element after is 0 within the bounds of the curent row and column (basically everything in the inner square)
}
function swap(i, j, A){ //Step 1: Swap i-th row with some other row so that element ij is nonzero
var oldj = j
if (A[i][j] == 0){
j = col_all_zeroes(i, j, A)
}
if (j==null){
return A, j
}
var curr = i //other row so that the first element != 0
var first = A[i]
while (A[i][j] == 0 && curr < rows){
var swap = A[curr]
if (swap[j] != 0){
A[i] = swap
A[curr] = first
}
curr = curr + 1
}
return A, j
}
function divide(i, j, A){ //Step 2: Divide all elements in row by Aij
var divconstant = A[i][j]
for (iter = 0; iter < A[0].length; iter++){
A[i][iter] = A[i][iter] / divconstant
}
return A
}
function eliminate(i, j, A){ //Step 3: Make other elements in the column zero
for (var k = 0; k < rows; k++){ //j is which column we are "on"
if (k!=i) {
var multiplier = A[k][j]
for (var l = 0; l < A[0].length; l++){
A[k][l] = A[k][l] - multiplier * A[i][l]
}
}
}
return A
}
function convert_dec(rows, columns, A){
function isInt(value) {
return !isNaN(value) &&
parseInt(Number(value)) == value &&
!isNaN(parseInt(value, 10));
}
for (k=0; k<rows; k++){
for (i=0; i<columns; i++){
var x = A[k][i]
if (!isInt(x)){
A[k][i] = parseFloat(Number.parseFloat(x).toFixed(5))
}
}
}
return A
}
}