-
Notifications
You must be signed in to change notification settings - Fork 0
/
readMatrices.js
221 lines (193 loc) · 6.21 KB
/
readMatrices.js
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
/**
* Extract the V matrix from the A matrix.
*
* @param A Constraint decision variable coefficient matrix.
* @return V, square matrix of coefficients of slack variables.
*/
function extractV(A) {
// Determine dimensionality
var m = A.length;
var mn = A[0].length;
var n = mn - m;
// Initialize array
var V = new Array(m);
// Loop over rows in A (and hence V)
for (let i = 0; i < m; i++) {
// Add second dimension to array
V[i] = new Array(m);
// Obtain V's elements from A
for (let j = 0; j < m; j++) {
V[i][j] = A[i][n + j];
}
}
return V;
}
/**
* Create a 1d array from numerical data in HTML element specified by name.
*
* @param name Name of the element you want to get the data from (string).
* @return 1d array containing the numerical data in name.
*/
function read1dNumArr(name) {
// Obtain html element and split on , semicolons or spaces
var htmlEl = document.getElementById(name).value.split(/[,; ][\s]*/);
// Initialize array to be filled
var arr = [];
// Loop over elements of htmlEl and add them to array.
for (let i = 0; i < htmlEl.length; i++) {
var el = htmlEl[i].replace(/\[/, '').replace(/\]/, '');
if (/[\-0-9./]+/.test(el)) {
// Using math.fraction is required in case users input fractions
arr.push(fracToDecimal(el));
} else if (name == "b") {
var msg = "b has unsuitable elements in it. Remember b is meant";
msg += " to be full of members separated by commas, spaces or ";
msg += "semicolons."
alert(msg);
} else if (name == "c") {
var msg = "c has unsuitable elements in it. Remember c is meant";
msg += " to be full of numbers separated by commas, spaces or ";
msg += "semicolons.";
alert(msg);
} else if (name == "A") {
var msg = "A has unsuitable elements in it. Remember A is meant";
msg += " to be full of numbers separated by commas, spaces or ";
msg += "semicolons.";
alert(msg);
}
}
return arr;
}
/**
* Create a 1d array from string data in HTML element specified by name.
*
* @param name Name of the element you want to get the data from (string).
* @return 1d array containing the string data in name.
*/
function read1dStrArr(name) {
var htmlEl = document.getElementById(name).value.split(/[,;\s][\s]*/);
var arr = [];
var el;
// Loop over each element of htmlEl, and add what needs to be added to arr
for (let i = 0; i < htmlEl.length; i++) {
el = htmlEl[i].replace(/\[/, '').replace(/\]/, '').replace(/"/g, '');
if (/[_a-zA-Z0-9]+/.test(el)) {
arr.push(el);
}
}
return arr;
}
/**
* Read 2d numerical array from specified form element.
*
* @param name HTML element from which the 2d numerical array is to be read.
* @return 2d numerical array.
*/
function read2dNumArr(name) {
// Obtain HTML element and initialize globals
var htmlEl = document.getElementById(name).value;
var arr = htmlEl.split(/[, ][\s]*/);
var A = [[]];
var k = 0;
var len;
// Loop over each element and add to A
for (let i = 0; i < arr.length; i++) {
// Element (potentially) with right bracket
var elRb = arr[i].replace(/\[/g, '');
// Element without right bracket
var el = elRb.replace(/\]/g, '');
// First condition yields true if a semicolon separate two numbers
if (/[0-9/]*[\s]*;/.test(elRb)) {
var elArr = el.split(/;/);
// Add first element to last row
if (/[0-9/]*/.test(elArr[0])) {
A[k].push(fracToDecimal(elArr[0]));
}
// Input validation
if ((len != undefined) && (A[k].length != len)) {
alert("A row length mismatch! For some reason your A matrix has rows of different lengths!");
throw console.error("A's rows must be equal in length!");
}
len = A[k].length;
k++;
// Add new blank row
if (i != arr.length - 1) {
A.push([]);
}
// Add second element to new row
if (/[0-9/]*/.test(elArr[1])) {
A[k].push(fracToDecimal(elArr[1]));
}
}
// This condition yields true for any other number
else if (/[0-9/]*[\s]*/.test(elRb)) {
A[k].push(fracToDecimal(el));
} else {
var msg = "A has unsuitable elements in it. Remember A is ";
msg += "meant to be full of numbers separated by commas, ";
msg += "spaces or semicolons.";
alert(msg);
}
}
// Input validation for final row
if ((len != undefined) && (A[k].length != len)) {
alert("A row length mismatch! For some reason your A matrix has rows of different lengths!");
console.error("A's rows must be equal in length!");
}
return A;
}
/**
* Determine A from HTML form and return it
*
* @params None.
* @return A, the 2d array of constraint coefficients.
*/
function readA() {
// Obtain HTML element for A
var htmlEl = document.getElementById("A").value;
// Both semicolons and spaces/commas separate elements, array is 2d
var testFor2D = htmlEl.match(/[0-9][, ]*[0-9]/) && htmlEl.match(/[;]/);
// Extract data from form
if (testFor2D) {
var A = read2dNumArr("A");
} else {
var A = [read1dNumArr("A")];
}
return A;
}
/**
* Create b array from data in the b field in the input table.
*
* @params None.
* @return Nothing.
*/
function readb() {
return read1dNumArr("b");
}
/**
* Create c array from data in the c field in the input table.
*
* @params None.
* @return Nothing.
*/
function readc() {
return read1dNumArr("c");
}
/**
* Returns an array of decision variables.
*
* @params None.
* @return Nothing.
*/
function readx() {
return read1dStrArr("x");
}
/**
* Returns an array of basis variables.
*
* @params None.
* @return Nothing.
*/
function readxB() {
return read1dStrArr("xB");
}