-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crout.js
174 lines (155 loc) · 7.36 KB
/
Crout.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
class Crout {
constructor(size, data) {
printHeading("Crout's Method", true, "h1");
this.size = size;
this.data = data;
this.L = [
["L<sub>11</sub>",0,0],
["L<sub>21</sub>", "L<sub>22</sub>", 0],
["L<sub>31</sub>", "L<sub>32</sub>", "L<sub>33</sub>"],
];
this.U = [
[1,"U<sub>12</sub>","U<sub>13</sub>"],
[0,1,"U<sub>23</sub>"],
[0,0,1]
];
this.LxU = [
["L<sub>11</sub>","L<sub>11</sub>U<sub>12</sub>","L<sub>11</sub>U<sub>13</sub>"],
["L<sub>21</sub>","L<sub>21</sub>U<sub>12</sub>+L<sub>22</sub>","L<sub>21</sub>U<sub>13</sub>+L<sub>22</sub>U<sub>23</sub>"],
["L<sub>31","L<sub>31</sub>U<sub>12</sub>+L<sub>32</sub>","L<sub>31</>U<sub>13</sub>+L<sub>32</sub>U<sub>23</sub>+L<sub>33</sub>"]
]
this.printSize([["","L",""]], true);
this.printSize(this.L, false);
this.printSize([["","U",""]], true);
this.printSize(this.U, false);
this.printSize([["","A = LxU",""]], true);
this.printSize(this.LxU, false);
this.printSize([["","Equations",""]], true);
this.printWithData(this.LxU, false);
math.config({number: 'Fraction'});
this.L11 = data[0][0];
this.L21 = data[1][0];
this.L31 = data[2][0];
this.U12 = math.evaluate(this.data[0][1]+"/"+this.L11);
this.L22 = math.fraction(math.subtract(this.data[1][1], math.multiply(this.L21, this.U12)));
this.L32 = math.fraction(math.subtract(this.data[2][1], math.multiply(this.L31, this.U12)));
this.U13 = math.fraction(math.divide(this.data[0][2],this.L11));
this.U23 = math.fraction(math.divide(math.subtract(this.data[1][2],math.multiply(this.L21,this.U13)),this.L22));
this.L33 = math.subtract(this.data[2][2],math.add(math.multiply(this.L31, this.U13),math.multiply(this.L32, this.U23)));
this.LSolved = [
[this.L11,0,0],
[this.L21, this.L22.toFraction(), 0],
[this.L31, this.L32.toFraction(), this.L33.toFraction()],
];
this.USolved = [
[1,this.U12.toFraction(),this.U13.toFraction()],
[0,1,this.U23.toFraction()],
[0,0,1]
];
this.LxUSolution = [
[
["L<sub>11</sub> = " + this.L11],
["L<sub>21</sub> = " + this.L21],
["L<sub>31</sub> = " + this.L31]
],
[
[
"L<sub>11</sub>U<sub>12</sub> = " + this.data[0][1],
`(${this.L11})U<sub>12</sub> = ` + this.data[0][1],
`U<sub>12</sub> = ` + this.data[0][1]+`/${this.L11}`,
`U<sub>12</sub> = ` + this.U12.toFraction()
],
[
"L<sub>21</sub>U<sub>12</sub>+L<sub>22</sub> = " + this.data[1][1],
`(${this.L21})(${this.U12.toFraction()})+L<sub>22</sub> = ` + this.data[1][1],
math.fraction(math.multiply(this.L21, this.U12)).toFraction()+` + L<sub>22</sub> = ` + this.data[1][1],
`L<sub>22</sub> = ` + this.data[1][1]+" - "+math.fraction(math.multiply(this.L21, this.U12)).toFraction(),
`L<sub>22</sub> = ` + this.L22.toFraction()
],
[
"L<sub>31</sub>U<sub>12</sub>+L<sub>32</sub> = " + this.data[2][1],
`(${this.L31})(${this.U12.toFraction()})+U<sub>23</sub> = ` + this.data[2][1],
math.fraction(math.multiply(this.L31, this.U12)).toFraction()+` + L<sub>32</sub> = ` + this.data[2][1],
`L<sub>32</sub> = ` + this.data[2][1]+"-"+math.fraction(math.multiply(this.L31, this.U12)).toFraction(),
`L<sub>32</sub> = ` + this.L32.toFraction()
]
],
[
[
"L<sub>11</sub>U<sub>13</sub> = " + this.data[0][2],
`(${this.L11})U<sub>13</sub> = ` + this.data[0][2],
`U<sub>13</sub> = ` + this.U13.toFraction()
],
[
"L<sub>21</sub>U<sub>13</sub>+L<sub>22</sub>U<sub>23</sub> = " + this.data[1][2],
`(${this.L21})(${this.U13.toFraction()})+(${this.L22.toFraction()})U<sub>23</sub> = ` + this.data[1][2],
math.fraction(math.multiply(this.L21, this.U13)).toFraction()+` + (${this.L22.toFraction()})U<sub>23</sub> = ` + this.data[1][2],
`(${this.L22.toFraction()})U<sub>23</sub> = ` + this.data[1][2]+"-"+math.fraction(math.multiply(this.L21, this.U13)).toFraction(),
`U<sub>23</sub> = ` + this.U23.toFraction()
],
[
"L<sub>31</sub>U<sub>13</sub>+L<sub>32</sub>U<sub>23</sub>+L<sub>33</sub> = " + this.data[2][2],
`(${this.L31})(${this.U13.toFraction()})+(${this.L32.toFraction()})(${this.U23.toFraction()})+L<sub>33</sub> = ` + this.data[2][2],
`${math.multiply(this.L31, this.U13).toFraction()} + ${math.multiply(this.L32, this.U23).toFraction()} +L<sub>33</sub> = ` + this.data[2][2],
`${math.add(math.multiply(this.L31, this.U13),math.multiply(this.L32, this.U23)).toFraction()} + L<sub>33</sub> = ` + this.data[2][2],
`L<sub>33</sub> = ${this.data[2][2]} - ${math.add(math.multiply(this.L31, this.U13),math.multiply(this.L32, this.U23)).toFraction()}`,
`L<sub>33</sub> = ${math.subtract(this.data[2][2],math.add(math.multiply(this.L31, this.U13),math.multiply(this.L32, this.U23))).toFraction()}`,
`L<sub>33</sub> = ` + this.L33.toFraction()
],
]
]
for(let i=0; i<this.LxUSolution.length; i++)
{
if(i >= this.size) break;
println("<hr>")
for(let j=0; j<this.LxUSolution[i].length; j++)
{
if(j >= this.size) break;
for(let k=0; k<this.LxUSolution[i][j].length; k++)
println(this.LxUSolution[i][j][k]);
println("<br>")
}
}
printHeading("L and U after Crout's Method", true, "h3");
this.printSize([["","L",""]], true);
this.printSize(this.LSolved, false);
LSolved = this.LSolved;
this.printSize([["","U",""]], true);
this.printSize(this.USolved, false);
USolved = this.USolved;
}
printSize(data, heading = false)
{
for(let i=0; i<data.length; i++)
{
if(i >= this.size) break;
var dis = []
for(let j=0; j<data[i].length; j++)
{
if(j >= this.size) break;
dis[j] = data[i][j];
}
if(heading)
createTable(dis);
else
addTableRow(dis);
}
}
printWithData(data, heading = false)
{
for(let i=0; i<data.length; i++)
{
if(i >= this.size) break;
var dis = []
for(let j=0; j<data[i].length; j++)
{
if(j >= this.size) break;
dis[j] = data[i][j]+" = "+ this.data[i][j];
}
if(heading)
createTable(dis);
else
addTableRow(dis);
}
}
}