-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
245 lines (201 loc) · 6.26 KB
/
script.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
class Matrix {
constructor(data){
this.data = [];
this.size = 0;
if(data === undefined){
//matrice vide
}else{
this._load(data);
}
}
_load(d){
d = JSON.parse(d);
this.size = d.n[0]; //Get the size of the matrix
// Create a 2D matrix
let A = d.A.splice(0); //Get A matrice
let B = d.B.splice(0); //Get B vector
// Hyp : A.length == B.length*n
while(A.length) {
this.data.push(A.splice(0,this.size).concat(B.splice(0,1))); //Transform in 2D arrays
}
}
swapRows(i,j){
this.data[j] = [this.data[i], this.data[i] = this.data[j]][0]; // Swap 2 rows
}
indiceMaxColumn(iColumn,iRowStart=0){ //Find the greatest element in the column starting at iRowStart abd return the indice of this line
let n=this.size;
let iMax=iRowStart;
let max = Math.abs(this.data[iRowStart][iColumn]);
let tempMax = 0;
for(let i=iRowStart+1;i<n;++i){
tempMax = Math.abs(this.data[i][iColumn]);
if(max < tempMax){
max = tempMax,
iMax = i;
}
}
return iMax;
}
//Substract factor*iRowRef to iRowSub
substractLineFactor(iRowRef, iRowSub, factor, iColumnStart=0){
for(let i=iColumnStart; i<this.size+1; ++i) {
this.data[iRowSub][i] -= this.data[iRowRef][i] * factor;
}
}
}
class Solver {
constructor(){
this.x = [];
}
solve(matrix){ // A.X=B
//Récupération des données
this.x = [];
this.x.length = matrix.size;
let start;
let stop;
//Start counter
try{
start = performance.now();
this.gaussTransform(matrix); //Gauss transformation to upper diagonal matrix
this.resolveX(matrix,this.x); //Resolve upper diagonal matrix with substitution method
//Stop counter
stop = performance.now();
//Display time needed
document.getElementById("chronotime").innerHTML = (stop - start) + "ms";
}catch(error){
this.x = undefined;
}
}
gaussTransform(matrix){
//Cache variable to improve speed
let data = matrix.data;
let m = matrix.size;
if(m===0){
return;
}
let n = data[0].length;
let h = 0; /* initialization of the pivot row */
let k = 0; /* initialization of the pivot column */
/*
Search for the greatest value, reason:
- "In any case, choosing the largest possible absolute value of the pivot improves the numerical stability of the algorithm, when floating point is used for representing numbers."
Sources : Wikipedia -> https://en.wikipedia.org/wiki/Gaussian_elimination#Pseudocode
*/
let iMax = 0;
let f = 0;
while(h < m) { // && k < n){
// Find the k-th pivot:
iMax = matrix.indiceMaxColumn(k,h); // k -> column && h -> line start searching
if(data[iMax][k] == 0) {
// No pivot in this column, pass to next column
console.log("No constant solutions");
throw new Error("Résultat non-constant");
} else {
if(h != iMax){
matrix.swapRows(h, iMax); // h -> current row && iMax -> ligne max
}
// Do for all rows below pivot:
for(let i=h+1; i<m; ++i){
//Calcul the factor
f = data[i][k] / data[h][k];
// Fill with zeros the lower part of pivot column:
data[i][k] = 0;
// Do for all remaining elements in current row:
matrix.substractLineFactor(h, i, f, k+1);
}
// Increase pivot row and column
++h,
++k;
}
}
}
//Find the vector X, result of Ax=B
resolveX(matrix){
let n = matrix.size;
let data = matrix.data;
for (let i=n-1; i >= 0; --i) {
this.x[i] = data[i][n]/data[i][i];
for (let k=i-1; k >= 0; --k) {
data[k][n] -= data[k][i] * this.x[i];
}
}
}
//Display the result
displayX(){
let result = document.getElementById('result');
let htmlResult = "";
if(this.x != undefined){
if(this.x.length > 0){
for(let i=0;i<this.x.length;i++){
htmlResult += "x<sub>"+(i+1)+"</sub> = "+this.x[i]+"<br>";
}
}else{
htmlResult = '<div class="alert alert-warning">Matrice de taille null!</div>';
}
}else{
htmlResult = '<div class="alert alert-danger">Aucune solution constante trouvée!</div>';
}
result.innerHTML = htmlResult;
}
}
let matrix = new Matrix();
//Load in ajax the json file containing the matrix
function loadClickedFile(e) {
e = e || window.event;
let file = e.target || e.srcElement;
let path = './data/'+file.innerHTML;
let xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType('application/json');
xmlhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200)
{
holder.value = this.responseText;
matrix = new Matrix(this.responseText);
}
};
xmlhttp.open("GET", path, true);
xmlhttp.send(undefined);
}
//Method of initialisation
document.onreadystatechange = () => {
if (document.readyState === 'interactive') {
// document ready
//To avoid error with chrome and loading file with xmlhttprequest
let isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
if(!isChrome){
let ul = document.getElementById('files');
ul.onclick = function(event) {
loadClickedFile(event);
};
}
let holder = document.getElementById('holder');
let s = new Solver();
//Add action listener on solve button
document.getElementById('solve').onclick = function(){
matrix = new Matrix(holder.value);
s.solve(matrix);
//Display x
s.displayX();
};
// Setup the drag and drop option, modified from http://html5demos.com/file-api
holder.ondragover = function() {
this.className = 'hover';
return false;
};
holder.ondragend = function() {
this.className = '';
return false;
};
holder.ondrop = function(e) {
e.preventDefault();
let file = e.dataTransfer.files[0]; //Open the first file
let reader = new FileReader();
reader.onload = function(event) {
holder.value = event.target.result;
matrix = new Matrix(event.target.result);
};
reader.readAsText(file);
return false;
};
}
};