static void findMax Mtr ref int ri ref int rj decimal vl for int getRo

 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
static void findMax(Mtr A, ref int ri, ref int rj)
{
decimal vl = 0;
for (int i = 0; i < A.getRows(); ++i)
for (int j = 0; j < A.getColumns(); ++j)
{
if (vl < Math.Abs(A.getValue(i, j)))
{
vl = Math.Abs(A.getValue(i, j));
ri = i;
rj = j;
}
}
}
static void gauss_with_beaches(Mtr A, Mtr b)
{
int n = A.getRows();
Mtr perest = new Mtr(1, n);
for (int i = 0; i < n; ++i)
perest.setValue(i, 0, i);
for (int i = 0; i < n; ++i)
{
int im = 0, jm = 0;
findMax(A, ref im, ref jm);
A.swapRow(i, im);
b.swapRow(i, im);
A.swapColumn(i, jm);
perest.swapColumn(i, jm);
if (Math.Abs(A.getValue(i, i)) < 1e-9M)
return;
decimal lVal = A.getValue(i, i);
A.multRow(1 / lVal, i);
b.multRow(1 / lVal, i);
for (int j = i + 1; j < n; ++j)
{
decimal sVal = A.getValue(j, i);
A.subRow(j, i, sVal);
b.subRow(j, i, sVal);
}
}
decimal[] results = new decimal[n];
for (int x = n - 1; x >= 0; --x)
{
decimal vl = b.getValue(x, 0);
for (int j = x + 1; j < n; ++j)
{
vl -= A.getValue(x, j) * results[j];
}
decimal dividor = A.getValue(x, x);
results[x] = vl / dividor;
}
for (int i = 0; i < n; ++i)
Console.WriteLine(results[(int)perest.getValue(0, i)]);
}