using System using System Collections Generic using System Linq using

  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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LU
{
class Method
{
public double[,] _Amatrix = new double[,]
{
{ 1, 1, -3, 4 },
{ 3, 5, 6, 9 },
{ 1, 1, 2, 2 },
{ 2, 3, -1, 2 }
};
public double[] _Barray = new double[] { 1, 2, 3, 4 };
public double[] _Xarray;
public double[] _Yarray;
public double[,] _Lmatrix = new double[,]
{
{ 1, 0, 0 , 0 },
{ 1, 1, 0, 0 },
{ 1, 1, 1, 0 },
{ 1, 1, 1, 1 }
};
public double[,] _Umatrix;
public void LU()
{
_Umatrix = new double[_Amatrix.GetLength(0), _Amatrix.GetLength(1)];
_Umatrix = _Amatrix;
int n = _Amatrix.GetLength(0);
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
_Lmatrix[j, i] = _Umatrix[j, i] / _Umatrix[i, i];
}
}
for (int k = 1; k < n; k++)
{
for (int i = k - 1; i < n; i++)
{
for (int j = i; j < n; j++)
{
_Lmatrix[j, i] = _Umatrix[j, i] / _Umatrix[i, i];
}
}
for (int i = k; i < n; i++)
{
for (int j = k - 1; j < n; j++)
{
_Umatrix[i, j] = _Umatrix[i, j] - _Lmatrix[i, k - 1] * _Umatrix[k - 1, j];
}
}
}
}
public void solution()
{
_Yarray = new double[_Barray.Length];
_Xarray = new double[_Barray.Length];
for (int i = 0; i < _Barray.Length; i++)
{
_Yarray[i] = 0;
_Xarray[i] = 0;
}
for (int i = 0; i < _Lmatrix.GetLength(0); ++i)
{
double sum = 0;
for (int k = 0; k < i; ++k)
{
sum += _Lmatrix[i, k] * _Yarray[k];
}
_Yarray[i] = (_Barray[i] - sum) / _Lmatrix[i, i];
}
for (int i = _Umatrix.GetLength(0) - 1; i >= 0; i--)
{
double sum = 0;
for (int k = _Umatrix.GetLength(0) - 1; k >= i; k--)
{
sum += _Umatrix[i, k] * _Xarray[k];
}
_Xarray[i] = (_Yarray[i] - sum) / _Umatrix[i, i];
}
}
}
class Program
{
static void Main(string[] args)
{
Method method = new Method();
Console.WriteLine("Исходная марица:");
for (int i = 0; i < method._Amatrix.GetLength(0); i++)
{
for (int j = 0; j < method._Amatrix.GetLength(1); j++)
{
Console.Write("{0:f1} \t", method._Amatrix[i, j]);
}
Console.WriteLine();
}
method.LU();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("L матрица:");
for (int i = 0; i < method._Amatrix.GetLength(0); i++)
{
for (int j = 0; j < method._Amatrix.GetLength(1); j++)
{
Console.Write("{0:f2} \t", method._Lmatrix[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("U матрица:");
for (int i = 0; i < method._Amatrix.GetLength(0); i++)
{
for (int j = 0; j < method._Amatrix.GetLength(1); j++)
{
Console.Write("{0:f2} \t", method._Umatrix[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine();
method.solution();
Console.WriteLine("Корни:");
for (int i = 0; i < method._Barray.Length; i++)
{
Console.WriteLine("{0:f3}" , method._Xarray[i]);
}
Console.ReadKey();
}
}
}