-
Notifications
You must be signed in to change notification settings - Fork 0
/
eq-calc.js
112 lines (99 loc) · 2.84 KB
/
eq-calc.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
exports.eqcalc = function (eq) {
return (seq(eq));
function setDemo() {
k2 = 2; j = 2;
fkt[0][0] = 1;
fkt[1][0] = 2;
fkt[2][0] = 4;
fkt[0][1] = 1;
fkt[1][1] = -3;
fkt[2][1] = -5;
fkt[0][2] = 1;
fkt[1][2] = -1;
fkt[2][2] = -1;
knt[0] = 3;
knt[1] = 2;
knt[2] = 1;
}
function CGls(s,fkt,knt,k2) {
let z;
let fakt;
let id;
z = s + 1;
id = 0;
for (z = s + 1; z < k2 + 1; z++) {
if (fkt[z][s] !== 0) {
fakt = (-1 * fkt[z][s]) / fkt[s][s];
for (id = 0; id <= k2; id++) {
fkt[z][id] = fkt[z][id] + fkt[s][id] * fakt;
}
knt[z] = knt[z] + (knt[s] * fakt);
}
}
}
function CFkt(fkt,knt,erg,k2) {
let z;
let id;
let sum;
let result = [];
for (z = k2; z >= 0; z--) {
sum = 0;
for (id = 0; id < k2 + 1; id++) {
sum = sum + (erg[id] * fkt[z][id]);
}
erg[z] = (knt[z] + sum * -1) / fkt[z][z];
erg[z] = Math.round(erg[z] * 1000) / 1000;
result.push(' res(' + z + ') => ' + erg[z]);
}
return result
}
function CalcGls(fkt,knt,erg,k2) {
let result = '';
let i;
for (i = 0; i < k2 + 1; i++) {
CGls(i,fkt,knt,k2);
}
res = CFkt(fkt,knt,erg,k2);
return(res);
}
function doSetMatrix(matrix, size,fkt,knt) {
let i = 0;
let k = 0;
for (i = 0; i < size; i++) {
for (k = 0; k < size; k++) {
fkt[i][k] = matrix[i][k];
}
knt[i] = matrix[i][size];
}
}
function seq(equation) {
var fkt = [
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]
];
var knt = [0, 0, 0, 0, 0, 0];
var erg = [0, 0, 0, 0, 0, 0];
var result;
var qMax = 2;
var k2 = 2;
var j = 2;
var result;
const json = JSON.parse(equation);
console.log(json.size);
console.log(json.matrix);
// load matrix from json-value
var k2 = json.size - 1;
if (!json.size) {
setDemo();
} else {
doSetMatrix(json.matrix, json.size,fkt,knt);
}
// clac eq by gauss agorithmus
result = CalcGls(fkt,knt,erg,k2);
return (result);
}
}