forked from federico2407/risolutore-algebrico-gauss-jordan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
302 lines (295 loc) · 13.2 KB
/
index.html
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
<html>
<head><meta charset="utf-8"/></head>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<body>
<div>
Dimensions: <input type="number" size=2 id="dimensionValue"> <button onclick="setDimension()" id="dimensionButton">Ok</button>
</div>
<div id="controls" style="visibility: hidden">
<button onclick="addVector()">Add vector</button>
<input type="radio" id="generic" name="result" onclick="resultType('generic')">
<label for="generic">Generic</label>
<input type="radio" id="specific" name="result" onclick="resultType('specific')">
<label for="specific">Specific</label>
<button onclick="startCalc()">Compute</button>
</div>
<div>
<span id="newVector"></span>
<span id="result" style="visibility: hidden"></span>
</div>
<div id="resultContainer"></div>
</body>
<br>
Translated by MellOTron
<script>
Math.MCD = function(a, b){
let sign = a / Math.abs(a);
a*=sign;
while(b != 0) [a, b] = [b, a%b];
return a * sign;
}
Math.mcm = function(a, b){
return Math.abs(a*b)/Math.MCD(a,b);
}
class Rational {
constructor(a, b = 1){
if(a instanceof Rational){
this.a = a.a;
this.b = a.b;
this.s = a.s;
}
else{
this.s = ((a*b)>=0)||-1;
a = Math.abs(a);
b = Math.abs(b);
for(let i = 2; i<=a&&i<=b; i++){
if(!(a%i||b%i)){
a /= i;
b /= i;
i--;
if(a==1||b==1) break;
}
}
this.a = a;
this.b = b;
}
}
print() {
let fraz = this.a!=0&&this.b!=1;
return (this.s==-1?"-":"")+(fraz?"\\dfrac{":"")+this.a+(fraz?('}{'+this.b)+"}":"");
}
printZeroHidden(coeff) {return this.a==0?"":((this.s==1?"+":"-")+(this.b!=1?"\\dfrac{":"")+(coeff==undefined?this.a:((this.a!=1?this.a:"")+coeff))+(this.b!=1?('}{'+this.b+"}"):""))}
val(){return this.s*this.a/this.b}
plus(v){return new Rational(this.s*this.a*v.b+v.s*v.a*this.b, this.b*v.b)}
minus(v){return new Rational(this.s*this.a*v.b-v.s*v.a*this.b, this.b*v.b)}
times(v){return new Rational(this.s*v.s*v.a*this.a, this.b*v.b)}
div(v){return new Rational(this.s*v.s*this.a*v.b, this.b*v.a)}
}
class Polynomial {
constructor(p){
this.p = p||[];
}
push(e){this.p.push(e)}
val(pos){return this.p[pos]}
print(){
console.log(this.p.map(e => e.print()).join("&"));
return this.p.map(e => e.print()).join("&");
}
printZeroHidden(){return this.p.map((e, i) => e.printZeroHidden("b_"+(i+1))).filter(e=>e!="").join("")||"0";}
plus(p){return new Polynomial(this.p.map((e, i)=>e.plus(p.p[i])))}
minus(p){return new Polynomial(this.p.map((e, i)=>e.minus(p.p[i])))}
times(v){return new Polynomial(this.p.map(e=>e.times(v)))}
div(v){return new Polynomial(this.p.map(e=>e.div(v)))}
semplify(){
let div = 1;
for(let i = 0; i<this.p.length; i++) if(this.p[i].val()){div = new Rational(this.p[i]); break;}
this.p = this.div(div).p;
return div;
}
}
class Equation {
constructor(p1, p2){
this.p1 = p1||new Polynomial();
this.p2 = p2||new Rational(0);
}
printP2(n){
if(this.p2 instanceof Rational) return (this.p2.print());
return this.p2.printZeroHidden();
}
push(e){this.p1.push(new Rational(e));}
val(pos){return this.p1.val(pos)}
print(){return this.p1.print()+"&|&"+this.printP2();}
plus(p){return new Equation(this.p1.plus(p.p1), this.p2.plus(p.p2))}
minus(p){return new Equation(this.p1.minus(p.p1), this.p2.minus(p.p2))}
times(v){return new Equation(this.p1.times(v), this.p2.times(v))}
div(v){return new Equation(this.p1.div(v), this.p2.div(v))}
semplify(){
let s = this.p1.semplify();
if(s.val() == 1) return;
this.p2 = this.p2.div(s);
return s;
}
equalsToNumber(n){this.p2 = new Rational(n)}
equalsToGeneric(height, n){
this.p2 = new Polynomial();
for(let i = 0; i<height; i++) this.p2.push(new Rational(0));
this.p2.p[n] = new Rational(1);
}
}
class Matrix {
constructor(){
this.m = [];
this.length = 0;
}
push(v){
this.length++;
for(let i in v){
if(!this.m[i]) this.m[i] = new Equation();
this.m[i].push(v[i]);
}
}
equalsTo(v){
for(let i in v) this.m[i].equalsToNumber(v[i]);
}
equalsToGeneric(){
for(let i = 0; i<this.m.length; i++) this.m[i].equalsToGeneric(this.m.length, i);
}
print(){
return "\\(\\begin{pmatrix}"+this.m.reduce((r, e) => r+e.print()+"\\\\", "")+"\\end{pmatrix}\\)";
}
exchangeLines(n1, n2){
[this.m[n1], this.m[n2]] = [this.m[n2], this.m[n1]];
}
gauss(){
let i, pivot;
let pivotlst = [];
let consistent = true;
console.log(this.print());
gaussPrint(this.print());
for(i = 0, this.pivHeight = 0; i<this.length; i++, this.pivHeight++){ //per ogni colonna della matrice
pivotlst[i] = true;
if(this.pivHeight>=this.m.length){pivotlst[i] = false; continue;} //pivot è variabile libera
pivot = this.m[this.pivHeight].val(i).val();
if(pivot == 0){
let j = this.pivHeight+1;
while(j<this.m.length&&this.m[j].val(i).val() == 0) j++;
if(j!=this.m.length){
gaussPrint(`\\(\\text{Swap lines ${i+1} and ${j+1}:}\\)`);
this.exchangeLines(i, j);
gaussPrint(this.print());
i--;
}
else pivotlst[i] = false;
this.pivHeight--;
continue;
}
for(let j = this.pivHeight+1; j<this.m.length; j++){ //per ogni riga sotto il pivot
var underPivot = this.m[j].val(i).val();
if(underPivot == 0) continue;
if(underPivot%pivot) {
let mul = Math.mcm(pivot, underPivot)/underPivot;
this.m[j] = this.m[j].times(new Rational(mul));
console.log(this.print());
gaussPrint(`\\(\\text{${underPivot} is not divisible by ${pivot}: multiply it by ${mul} in order to make it divisible:}\\)`);
gaussPrint(this.print());
} //se non è divisibile per il pivot lo rendo divisibile
underPivot = this.m[j].val(i).val();
this.m[j] = this.m[j].minus(this.m[this.pivHeight].times(new Rational(underPivot, pivot)));
gaussPrint(`\\(\\text{Subtract line ${this.pivHeight+1} to line ${j+1}, ${underPivot/pivot} times:}\\)`);
console.log(this.print());
gaussPrint(this.print());
}
}
if(pivotlst.some(e=>!e))
gaussPrint("\\(\\text{Free variables = " + pivotlst.map((e, i)=>!e?(i+1)+"°":"").filter(e=>e!="").join(' ')+"}\\)");
for(let j = this.pivHeight; j<this.m.length; j++){
gaussPrint(`\\(\\ ${this.m[j].printP2()} \\text{ must be = 0 ${isNaN(this.m[j].printP2()) || this.m[j].printP2() == 0 ? "" : ", but it isn't!!"}}\\)`);
if(!isNaN(this.m[j].printP2()) && this.m[j].printP2() != 0) consistent = false;
}
this.pivHeight--;
if(pivotlst.every(e => e) && consistent) this.jordan();
MathJax.typeset();
}
jordan(){
let s;
jordanPrint(this.print());
for(let i = this.pivHeight; i>=0; i--){
s = this.m[i].semplify();
if(s){
jordanPrint(`\\(\\text{Simplify line ${i} by dividing it by ${s.print()}}\\)`);
jordanPrint(this.print());
}
for(let j = i-1; j>=0; j--){
jordanPrint(`\\(\\text{Subtract line ${i} to line ${j} ${this.m[j].val(i).val()/this.m[i].val(i).val()} times}\\)`);
this.m[j] = this.m[j].minus(this.m[i].times(this.m[j].val(i).div(this.m[i].val(i))));
jordanPrint(this.print());
}
}
}
}
</script>
<script>
var dimension;
var vectors=0;
var m;
var generic;
var gaussPrintStarted;
var jordanPrintStarted;
document.getElementById("generic").click();
function setDimension(){
document.getElementById('dimensionValue').setAttribute('disabled', 'disabled');
document.getElementById('dimensionButton').setAttribute('disabled', 'disabled');
dimension = document.getElementById('dimensionValue').value;
document.getElementById('controls').style.visibility = "visible";
addVector(true);
}
function addVector(r){
let res = '<math xmlns="http:https://www.w3.org/1998/Math/MathML">';
res += '<mrow data-mjx-texclass="INNER">';
res += '<mtext><msub><mi>v</mi><mi>'+(r?"r":vectors)+'</mi></msub> =</mtext>';
res += '<mo data-mjx-texclass="OPEN">(</mo><mtable columnspacing="1em" rowspacing="4pt" id="input-base">';
for(let i = 0; i<dimension; i++)
res+='<mtr><mtd><mi><semantics><annotation><input type="text" id='+(r?"res":('vr'+vectors+'-'))+i+' size=1/></annotation></semantics></mi></mtd></mtr>';
res+='</mtable><mo data-mjx-texclass="CLOSE">)</mo>';
res+='</mrow></math>';
if(r){
document.getElementById("result").innerHTML=res;
}
else{
res+='<span id="newVector"></span>';
vectors++;
document.getElementById("newVector").outerHTML=res;
}
MathJax.typeset();
}
function resultType(t){
if(t=="generic"){
generic = true;
document.getElementById("result").style.visibility = "hidden";
}
else{
generic = false;
document.getElementById("result").style.visibility = "visible";
}
}
function startCalc(){
m = new Matrix();
gaussPrintStarted = false;
jordanPrintStarted = false;
document.getElementById("resultContainer").innerHTML = '<div id="gauss"></div><div id="jordan" style="margin-top: 50px"></div>';
for(let i = 0; i<vectors; i++){
let v = [];
for(let j = 0; j<dimension; j++){
v.push(document.getElementById("vr"+i+'-'+j).value);
}
m.push(v);
}
if(generic){
m.equalsToGeneric();
}
else{
let v = [];
for(let i = 0; i<dimension; i++){
v.push(document.getElementById("res"+i).value);
}
m.equalsTo(v);
}
m.gauss();
}
function gaussPrint(t){
if(!gaussPrintStarted){
gaussPrintStarted = true;
document.getElementById("gauss").outerHTML = "<h2>Gauss:</h2><div id='gauss'></div>";
}
document.getElementById("gauss").outerHTML = "<div style='margin:20px'>"+t+"</div><div id='gauss'></div>";
}
function jordanPrint(t){
if(!jordanPrintStarted){
jordanPrintStarted = true;
document.getElementById("jordan").outerHTML = "<h2>Jordan:</h2><div id='jordan'></div>";
}
document.getElementById("jordan").outerHTML = "<div style='margin:20px'>"+t+"</div><div id='jordan'></div>";
}
</script>
</html>