LU

  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
class Program
{
public static double[,] MultyMtx(double[,] a, double[,] b)
{
int rows = a.GetLength(0);
int columns = b.GetLength(1);
double[,] result =new double[rows, columns];
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
for (int k = 0; k < b.GetLength(0); k++)
{
result[i, j] += a[i, k] * b[k, j];
}
}
}
return result;
}
public static double[,] pushMtx(double[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
for (int j = 0; j < a.GetLength(1); j++)
a[i, j] = double.Parse(Console.ReadLine());
return a;
}
public static void LUpartition(int n, double[,] L, double[,] U)
{
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
L[j, i] = U[j, i] / U[i, i];
}
}
for (int k = 1; k < n; k++)
{
for (int i = k - 1; i < n; i++)
{
for (int j = i; j < n; j++)
{
L[j, i] = U[j, i] / U[i, i];
}
}
for (int i = k; i < n; i++)
{
for (int j = k - 1; j < n; j++)
{
U[i, j] = U[i, j] - L[i, k - 1] * U[k - 1, j];
}
}
}
}
public static void printMtx(double[,] x)
{
for (int i = 0; i < x.GetLength(0); i++)
{
for (int j = 0; j < x.GetLength(1); j++)
{
Console.Write("{0:F2}\t", x[i, j]);
}
Console.WriteLine();
}
Console.Write("\n\n");
}
public static double[,] inverseMtx(double[,] a, double[,] e, int n)
{
for (int i = 0; i < 1; i++)
{
double value = 1/a[i,i];
for (int j = 0; j < 1; j++)
{
a[i, j] = a[i, j] * value;
e[i, j] = e[i, j] * value;
}
/*for (int l = i; l < 1 - 1; l++)
{
for (int k = 0; k < 1; k++)
{
a[l + 1, k] += a[l, k] * (-a[l, 0]);
e[l + 1, k] += e[l, k] * (-e[l, 0]);
}
}*/
}
return e;
}
static void Main(string[] args)
{
Console.WriteLine("Введите размер квадратной матрицы n = ");
int n = int.Parse(Console.ReadLine());
Console.Write("\n");
Console.WriteLine("Введите значения матрицы A =");
double[,] A = new double[,] { { 3, 4, -9, 5 },
{ -15, -12, 50, -16 },
{ -27, -36, 73, 8 },
{ 9, 12, -10, -16 }};
//pushMtx(A);
Console.Write("\n");
printMtx(A);
double[,] L = new double[n, n];
double[,] U = new double[n, n];
U = A;
LUpartition(n, L, U);
printMtx(L);
printMtx(U);
double[,] E = new double[,] { { 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }};
double[,] invA = new double[,] { };
double[,] b = new double[4, 1] { {-14}, {44}, {142}, {-76} };
invA = inverseMtx(A, E, n);
printMtx(A);
printMtx(E);
Console.ReadKey();
}
}