Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Task1
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- Console.WriteLine("Enter the size of [n x n] matrix");
- int n = int.Parse(Console.ReadLine());
- double[,] matrix1 = new double[n, n];
- double[,] matrix2 = new double[n, n];
- bool ok = true;
- ok = CreateMatrix(n, out matrix1);
- if (ok)
- {
- ok = CreateMatrix(n, out matrix2);
- }
- if (ok)
- {
- Console.Clear();
- Console.WriteLine("Matrix №1:");
- PrintMatrix(matrix1);
- Console.WriteLine("Matrix №2:");
- PrintMatrix(matrix2);
- Console.WriteLine("Summarized Matrix:");
- PrintMatrix(SumMatrices(matrix1, matrix2));
- Console.WriteLine("Multiplied Matrix:");
- PrintMatrix(MultiplyMatrices(matrix1, matrix2));
- Console.WriteLine("Inverted Matrix №1:");
- PrintMatrix(InvertMatrix(matrix1));
- Console.WriteLine("Inverted Matrix №2:");
- PrintMatrix(InvertMatrix(matrix2));
- }
- }
- catch(FormatException)
- {
- Console.WriteLine("Incorrect format!");
- }
- }
- private static bool CreateMatrix(int n, out double[,] matrix)
- {
- bool ok = true;
- matrix = new double[n, n];
- try
- {
- Console.WriteLine("Press 1 if you want to input values manually, otherwise press 2 to fill in the matrix with random values");
- int option = int.Parse(Console.ReadLine());
- bool minmax_flag = false;
- double min = 0;
- double max = 0;
- Random rnd = new Random();
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- if (option == 1)
- {
- Console.Write("Matrix[{0}, {1}] = ", i, j);
- matrix[i, j] = double.Parse(Console.ReadLine());
- }
- else if (option == 2)
- {
- if (!minmax_flag)
- {
- Console.WriteLine("Enter min and max values of the random numbers generation range");
- Console.Write("Min: ");
- min = double.Parse(Console.ReadLine());
- Console.Write("Max: ");
- max = double.Parse(Console.ReadLine());
- minmax_flag = true;
- Console.WriteLine("Matrix successfully filled in with random values!");
- }
- matrix[i, j] = rnd.NextDouble() * (max - min) + min;
- matrix[i, j] = Math.Round(matrix[i, j], 2);
- }
- else
- {
- throw new Exception("Incorrect option!");
- }
- }
- }
- }
- catch(FormatException)
- {
- Console.WriteLine("Incorrect format!");
- ok = false;
- }
- catch(Exception)
- {
- Console.WriteLine("Incorrect option!");
- ok = false;
- }
- return ok;
- }
- private static void PrintMatrix(double[,] matrix)
- {
- for (int i = 0; i < matrix.GetLength(0); ++i)
- {
- for (int j = 0; j < matrix.GetLength(1); ++j)
- {
- Console.Write(matrix[i, j] + "\t");
- }
- Console.WriteLine();
- }
- }
- private static double[,] SumMatrices(double[,] matrix1, double[,] matrix2)
- {
- int size = matrix1.GetLength(0);
- double[,] resultMatrix = new double[size, size];
- for (int i = 0; i < size; ++i)
- {
- for (int j = 0; j < size; ++j)
- {
- resultMatrix[i, j] = matrix1[i, j] + matrix2[i, j];
- resultMatrix[i, j] = Math.Round(resultMatrix[i, j], 2);
- }
- }
- return resultMatrix;
- }
- private static double[,] MultiplyMatrices(double[,] matrix1, double[,] matrix2)
- {
- int size = matrix1.GetLength(0);
- double[,] resultMatrix = new double[size, size];
- for (int i = 0; i < size; ++i)
- {
- for (int j = 0; j < size; ++j)
- {
- double el = 0;
- for(int m = 0; m < size; ++m)
- {
- el += matrix1[i, m] * matrix2[m, j];
- }
- resultMatrix[i, j] = el;
- }
- }
- return resultMatrix;
- }
- private static double[,] MinorMatrix(double[,] matrix, int row, int column)
- {
- int size = matrix.GetLength(0);
- int x = 0;
- double[,] resultMatrix = new double[size - 1, size - 1];
- for (int i = 0; i < size; i++)
- {
- if (i == row) continue;
- int y = 0;
- for (int j = 0; j < size; ++j)
- {
- if (j == column) continue;
- resultMatrix[x, y] = matrix[i, j];
- y++;
- }
- x++;
- }
- return resultMatrix;
- }
- private static double DeterminantMatrix(double[,] matrix)
- {
- double result = 0;
- int size = matrix.GetLength(0);
- if (size == 1) return matrix[0, 0];
- if (size == 2) return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0];
- for (int i = 0; i < size; ++i)
- {
- result += Math.Pow(-1, i + 2) * matrix[i, 0] * DeterminantMatrix(MinorMatrix(matrix, i, 0));
- }
- return result;
- }
- private static double[,] InvertMatrix(double[,] matrix)
- {
- int size = matrix.GetLength(0);
- double[,] resultMatrix = new double[size, size];
- double[,] transposedMatrix = new double[size, size];
- for (int i = 0; i < size; ++i)
- {
- for (int j = 0; j < size; ++j)
- {
- transposedMatrix[i, j] = matrix[j, i];
- }
- }
- for (int i = 0; i < size; ++i)
- {
- for (int j = 0; j < size; ++j)
- {
- resultMatrix[i, j] = Math.Pow(-1, i + j + 2) * DeterminantMatrix(MinorMatrix(transposedMatrix, i, j)) / DeterminantMatrix(matrix);
- }
- }
- return resultMatrix;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement