class Lab1
{
double[] mAvailableTasks = new double[5] {
1.1,
1.2,
3.1,
3.2,
10.1
};
public void execute()
{
Console.WriteLine("Выберите номер задания:");
printTasks();
//Console.WriteLine("Например, \"3.1\"");
Console.Write("Номер задания: ");
runTaskListener();
Console.WriteLine("Работа программы завершена. Нажмите любую кнопку.");
Console.ReadKey();
}
private void printTasks()
{
Console.WriteLine("Метод Крамера.");
Console.WriteLine("Метод релаксации.");
Console.WriteLine("Модифицированный метод Гаусса.");
Console.WriteLine("Метод Зейделя.");
//Console.WriteLine("10.1 Нахождение собственных чисел и собственных векторов матрицы при помощи QR\nалгоритма.\n");
}
private void runTaskListener()
{
double selected_task;
double.TryParse(Console.ReadLine().Replace('.', ','), out selected_task);
double found = Array.Find(mAvailableTasks, x => x == selected_task);
if ((int)found != 0)
{
switch ((int)(found * 10))
{
case 11:
runKramer();
break;
case 12:
runRelaxation();
break;
case 31:
runModifyGauss();
break;
case 32:
runSeidel();
break;
case 101:
runQR();
break;
}
}
else
{
Console.WriteLine("Введен несуществующий номер. Попробуйте снова.");
runTaskListener();
}
}
private void reboot() {
Console.Clear();
Console.WriteLine("Программа перезапущена.\n");
execute();
}
private void runKramer()
{
const bool CONSOLE_MODE = false;
Console.Clear();
Console.WriteLine("Номер 1.1. Выбран метод Крамера.");
double[,] input_matrix;
Matrix cur_matrix;
if (CONSOLE_MODE) {
Console.WriteLine("Введите количество неизвестных системы уравнений:");
int n = -1;
int.TryParse(Console.ReadLine(), out n);
if (n == -1)
{
reboot();
return;
}
if (n == 1)
{
Console.WriteLine("Введена некорректная матрица.\nРабота программы будет продолжена на ваш страх и риск.");
}
input_matrix = new double[n, n + 1];
for (int i = 0; i < n; ++i)
{
Console.WriteLine("Введите через пробел значения строки №" + (i + 1).ToString());
string[] cur_row = Console.ReadLine().Replace('.', ',').Split(' ');
for (int j = 0; j < n + 1; ++j)
{
double.TryParse(cur_row[j], out input_matrix[i, j]);
}
}
cur_matrix = new Matrix(input_matrix);
} else {
input_matrix = new double[,] {
{9, 3, 7, 3, 2},
{3, 15, 4, 0, 7},
{4, 4, 11, -4, 9},
{4, 3, 12, 13, 3}
};
cur_matrix = new Matrix(input_matrix);
Console.WriteLine("Входная матрица");
Console.WriteLine(cur_matrix.ToString());
}
Kramer kr = new Kramer(cur_matrix);
try
{
double[] solve = kr.getSolve();
Console.WriteLine("Решение СЛАУ:");
for (int i = 0; i < solve.Length; ++i)
{
Console.Write("x" + (i + 1).ToString() + " = " + solve[i] + ";\n");
}
} catch (Exception err) {
Console.WriteLine(err.Message);
}
}
private void runRelaxation()
{
const bool CONSOLE_MODE = false;
Console.Clear();
Console.WriteLine("Номер 1.2. Выбран метод релаксации.");
double[,] input_matrix;
Matrix cur_matrix;
if (CONSOLE_MODE)
{
Console.WriteLine("Введите количество неизвестных системы уравнений:");
int n = -1;
int.TryParse(Console.ReadLine(), out n);
if (n == -1)
{
reboot();
return;
}
if (n == 1)
{
Console.WriteLine("Введена некорректная матрица.\nРабота программы будет продолжена на ваш страх и риск.");
}
input_matrix = new double[n, n + 1];
for (int i = 0; i < n; ++i)
{
Console.WriteLine("Введите через пробел значения строки №" + (i + 1).ToString());
string[] cur_row = Console.ReadLine().Replace('.', ',').Split(' ');
for (int j = 0; j < n + 1; ++j)
{
double.TryParse(cur_row[j], out input_matrix[i, j]);
}
}
cur_matrix = new Matrix(input_matrix);
}
else
{
input_matrix = new double[,] {
{9, 3, 7, 3, 2},
{3, 15, 4, 0, 7},
{4, 4, 11, -4, 9},
{4, 3, 12, 13, 3}
};
cur_matrix = new Matrix(input_matrix);
Console.WriteLine("Входная матрица");
Console.WriteLine(cur_matrix.ToString());
}
Relaxation relaxation_method = new Relaxation(cur_matrix.GetMainKoeff(), cur_matrix.GetFreeKoeff());
double omega = 1;
Console.Write("Введите омегу (параметр релаксации): ");
double.TryParse(Console.ReadLine().Replace('.', ','), out omega);
try
{
if (!relaxation_method.isValid())
{
Console.WriteLine("Решение не сходится, так как матрица не имеет диагонального преобладания.");
return;
}
double[] solve = relaxation_method.getSolve(omega, 0.00001);
int numIter = relaxation_method.getNumIter();
Console.WriteLine("Решение СЛАУ:");
for (int i = 0; i < solve.Length; ++i)
{
Console.Write("x" + (i + 1).ToString() + " = " + solve[i] + ";\n");
}
Console.WriteLine("Количество итераций: " + numIter.ToString());
}
catch (Exception err)
{
Console.WriteLine(err.StackTrace);
}
}
private void runSeidel()
{
const bool CONSOLE_MODE = false;
Console.Clear();
Console.WriteLine("Номер 3.2. Выбран метод Зейделя.");
double[,] input_matrix;
Matrix cur_matrix;
if (CONSOLE_MODE)
{
Console.WriteLine("Введите количество неизвестных системы уравнений:");
int n = -1;
int.TryParse(Console.ReadLine(), out n);
if (n == -1)
{
reboot();
return;
}
if (n == 1)
{
Console.WriteLine("Введена некорректная матрица.\nРабота программы будет продолжена на ваш страх и риск.");
}
input_matrix = new double[n, n + 1];
for (int i = 0; i < n; ++i)
{
Console.WriteLine("Введите через пробел значения строки №" + (i + 1).ToString());
string[] cur_row = Console.ReadLine().Replace('.', ',').Split(' ');
for (int j = 0; j < n + 1; ++j)
{
double.TryParse(cur_row[j], out input_matrix[i, j]);
}
}
cur_matrix = new Matrix(input_matrix);
}
else
{
input_matrix = new double[,] {
{9, 3, 7, 3, 2},
{3, 15, 4, 0, 7},
{4, 4, 11, -4, 9},
{4, 3, 12, 13, 3}
};
cur_matrix = new Matrix(input_matrix);
Console.WriteLine("Входная матрица");
Console.WriteLine(cur_matrix.ToString());
}
Seidel seidel_method = new Seidel(cur_matrix.GetMainKoeff(), cur_matrix.GetFreeKoeff());
try
{
if (!seidel_method.isValid())
{
Console.WriteLine("Решение не сходится.");
return;
}
double[] solve = seidel_method.getSolve(0.0001);
int numIter = seidel_method.getNumIter();
Console.WriteLine("Решение СЛАУ:");
for (int i = 0; i < solve.Length; ++i)
{
Console.Write("x" + (i + 1).ToString() + " = " + solve[i] + ";\n");
}
Console.WriteLine("Количество итераций: " + numIter.ToString());
}
catch (Exception err)
{
Console.WriteLine(err.StackTrace);
}
}
private void runModifyGauss()
{
const bool CONSOLE_MODE = false;
Console.Clear();
Console.WriteLine("Номер 3.1. Выбран модифицированный метод Гаусса.");
double[,] input_matrix;
Matrix cur_matrix;
if (CONSOLE_MODE)
{
Console.WriteLine("Введите количество неизвестных системы уравнений:");
int n = -1;
int.TryParse(Console.ReadLine(), out n);
if (n == -1)
{
reboot();
return;
}
if (n == 1)
{
Console.WriteLine("Введена некорректная матрица.\nРабота программы будет продолжена на ваш страх и риск.");
}
input_matrix = new double[n, n + 1];
for (int i = 0; i < n; ++i)
{
Console.WriteLine("Введите через пробел значения строки №" + (i + 1).ToString());
string[] cur_row = Console.ReadLine().Replace('.', ',').Split(' ');
for (int j = 0; j < n + 1; ++j)
{
double.TryParse(cur_row[j], out input_matrix[i, j]);
}
}
cur_matrix = new Matrix(input_matrix);
}
else
{
input_matrix = new double[,] {
{9, 3, 7, 3, 2},
{3, 15, 4, 0, 7},
{4, 4, 11, -4, 9},
{4, 3, 12, 13, 3}
};
cur_matrix = new Matrix(input_matrix);
Console.WriteLine("Входная матрица");
Console.WriteLine(cur_matrix.ToString());
}
ModifyGauss gauss_method = new ModifyGauss(cur_matrix.GetMainKoeff(), cur_matrix.GetFreeKoeff());
try
{
double[] solve = gauss_method.getSolve();
Console.WriteLine("Решение СЛАУ:");
for (int i = 0; i < solve.Length; ++i)
{
Console.Write("x" + (i + 1).ToString() + " = " + solve[i] + ";\n");
}
}
catch (Exception err)
{
Console.WriteLine(err.StackTrace);
}
}
private void runQR()
{
const bool CONSOLE_MODE = false;
Console.Clear();
Console.WriteLine("Номер 10. Выбран QR алгоритм.");
double[,] input_matrix;
Matrix cur_matrix;
if (CONSOLE_MODE)
{
Console.WriteLine("Введите количество неизвестных системы уравнений:");
int n = -1;
int.TryParse(Console.ReadLine(), out n);
if (n == -1)
{
reboot();
return;
}
if (n == 1)
{
Console.WriteLine("Введена некорректная матрица.\nРабота программы будет продолжена на ваш страх и риск.");
}
input_matrix = new double[n, n + 1];
for (int i = 0; i < n; ++i)
{
Console.WriteLine("Введите через пробел значения строки №" + (i + 1).ToString());
string[] cur_row = Console.ReadLine().Replace('.', ',').Split(' ');
for (int j = 0; j < n + 1; ++j)
{
double.TryParse(cur_row[j], out input_matrix[i, j]);
}
}
cur_matrix = new Matrix(input_matrix);
}
else
{
input_matrix = new double[,] {
{24, 20, 8, 1},
{20, 35, 9, 2},
{8, 9, 3, 3}
};
cur_matrix = new Matrix(input_matrix);
Console.WriteLine("Входная матрица");
Console.WriteLine(cur_matrix.ToString());
}
QR qr_method = new QR();
try
{
qr_method.solve(cur_matrix.GetMainKoeff(), cur_matrix.GetFreeKoeff(), 0.00001);
List<double> eigenValues = qr_method.GetEigenvalues(cur_matrix.GetMainKoeff(), 0.0000001);
List<Matrix> eigenVectors = qr_method.GetEigenvectors(cur_matrix.GetMainKoeff(), eigenValues);
for (int i = 0; i < eigenVectors.Count; ++i) {
Console.WriteLine("Собственное значение: {0:f5}", eigenValues[i]);
Console.WriteLine("Собственный вектор: ");
Console.WriteLine(eigenVectors[i]);
Console.WriteLine();
}
}
catch (Exception err)
{
Console.WriteLine(err.StackTrace);
}
}
}