Advertisement
Guest User

matrixOperations

a guest
Feb 19th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.38 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Task1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             try
  10.             {
  11.                 Console.WriteLine("Enter the size of [n x n] matrix");
  12.                 int n = int.Parse(Console.ReadLine());
  13.                 double[,] matrix1 = new double[n, n];
  14.                 double[,] matrix2 = new double[n, n];
  15.                 bool ok = true;
  16.                 ok = CreateMatrix(n, out matrix1);
  17.                 if (ok)
  18.                 {
  19.                     ok = CreateMatrix(n, out matrix2);
  20.                 }
  21.                 if (ok)
  22.                 {
  23.                     Console.Clear();
  24.                     Console.WriteLine("Matrix №1:");
  25.                     PrintMatrix(matrix1);
  26.                     Console.WriteLine("Matrix №2:");
  27.                     PrintMatrix(matrix2);
  28.  
  29.                     Console.WriteLine("Summarized Matrix:");
  30.                     PrintMatrix(SumMatrices(matrix1, matrix2));
  31.  
  32.                     Console.WriteLine("Multiplied Matrix:");
  33.                     PrintMatrix(MultiplyMatrices(matrix1, matrix2));
  34.  
  35.                     Console.WriteLine("Inverted Matrix №1:");
  36.                     PrintMatrix(InvertMatrix(matrix1));
  37.                     Console.WriteLine("Inverted Matrix №2:");
  38.                     PrintMatrix(InvertMatrix(matrix2));
  39.                 }
  40.             }
  41.             catch(FormatException)
  42.             {
  43.                 Console.WriteLine("Incorrect format!");
  44.             }
  45.         }
  46.  
  47.  
  48.         private static bool CreateMatrix(int n, out double[,] matrix)
  49.         {
  50.             bool ok = true;
  51.             matrix = new double[n, n];
  52.             try
  53.             {
  54.                 Console.WriteLine("Press 1 if you want to input values manually, otherwise press 2 to fill in the matrix with random values");
  55.                 int option = int.Parse(Console.ReadLine());
  56.                 bool minmax_flag = false;
  57.                 double min = 0;
  58.                 double max = 0;
  59.                 Random rnd = new Random();
  60.                 for (int i = 0; i < n; ++i)
  61.                 {
  62.                     for (int j = 0; j < n; ++j)
  63.                     {
  64.                         if (option == 1)
  65.                         {
  66.                             Console.Write("Matrix[{0}, {1}] = ", i, j);
  67.                             matrix[i, j] = double.Parse(Console.ReadLine());
  68.                         }
  69.                         else if (option == 2)
  70.                         {
  71.                             if (!minmax_flag)
  72.                             {
  73.                                 Console.WriteLine("Enter min and max values of the random numbers generation range");
  74.                                 Console.Write("Min: ");
  75.                                 min = double.Parse(Console.ReadLine());
  76.                                 Console.Write("Max: ");
  77.                                 max = double.Parse(Console.ReadLine());
  78.                                 minmax_flag = true;
  79.                                 Console.WriteLine("Matrix successfully filled in with random values!");
  80.                             }
  81.                             matrix[i, j] = rnd.NextDouble() * (max - min) + min;
  82.                             matrix[i, j] = Math.Round(matrix[i, j], 2);
  83.                         }
  84.                         else
  85.                         {
  86.                             throw new Exception("Incorrect option!");
  87.                         }
  88.                     }
  89.                 }
  90.             }
  91.             catch(FormatException)
  92.             {
  93.                 Console.WriteLine("Incorrect format!");
  94.                 ok = false;
  95.             }
  96.             catch(Exception)
  97.             {
  98.                 Console.WriteLine("Incorrect option!");
  99.                 ok = false;
  100.             }
  101.  
  102.             return ok;
  103.         }
  104.  
  105.  
  106.         private static void PrintMatrix(double[,] matrix)
  107.         {
  108.             for (int i = 0; i < matrix.GetLength(0); ++i)
  109.             {
  110.                 for (int j = 0; j < matrix.GetLength(1); ++j)
  111.                 {
  112.                     Console.Write(matrix[i, j] + "\t");
  113.                 }
  114.                 Console.WriteLine();
  115.             }
  116.         }
  117.  
  118.  
  119.         private static double[,] SumMatrices(double[,] matrix1, double[,] matrix2)
  120.         {
  121.             int size = matrix1.GetLength(0);
  122.             double[,] resultMatrix = new double[size, size];
  123.             for (int i = 0; i < size; ++i)
  124.             {
  125.                 for (int j = 0; j < size; ++j)
  126.                 {
  127.                     resultMatrix[i, j] = matrix1[i, j] + matrix2[i, j];
  128.                     resultMatrix[i, j] = Math.Round(resultMatrix[i, j], 2);
  129.                 }
  130.             }
  131.  
  132.             return resultMatrix;
  133.         }
  134.  
  135.  
  136.         private static double[,] MultiplyMatrices(double[,] matrix1, double[,] matrix2)
  137.         {
  138.             int size = matrix1.GetLength(0);
  139.             double[,] resultMatrix = new double[size, size];
  140.             for (int i = 0; i < size; ++i)
  141.             {
  142.                 for (int j = 0; j < size; ++j)
  143.                 {
  144.                     double el = 0;
  145.                     for(int m = 0; m < size; ++m)
  146.                     {
  147.                         el += matrix1[i, m] * matrix2[m, j];
  148.                     }
  149.                     resultMatrix[i, j] = el;
  150.                 }
  151.             }
  152.  
  153.             return resultMatrix;
  154.         }
  155.  
  156.  
  157.         private static double[,] MinorMatrix(double[,] matrix, int row, int column)
  158.         {
  159.             int size = matrix.GetLength(0);
  160.             int x = 0;
  161.             double[,] resultMatrix = new double[size - 1, size - 1];
  162.             for (int i = 0; i < size; i++)
  163.             {
  164.                 if (i == row) continue;
  165.                 int y = 0;
  166.                 for (int j = 0; j < size; ++j)
  167.                 {
  168.                     if (j == column) continue;
  169.                     resultMatrix[x, y] = matrix[i, j];
  170.                     y++;
  171.                 }
  172.  
  173.                 x++;
  174.             }
  175.  
  176.             return resultMatrix;
  177.         }
  178.  
  179.  
  180.         private static double DeterminantMatrix(double[,] matrix)
  181.         {
  182.             double result = 0;
  183.             int size = matrix.GetLength(0);
  184.             if (size == 1) return matrix[0, 0];
  185.             if (size == 2) return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0];
  186.             for (int i = 0; i < size; ++i)
  187.             {
  188.                 result += Math.Pow(-1, i + 2) * matrix[i, 0] * DeterminantMatrix(MinorMatrix(matrix, i, 0));
  189.             }
  190.  
  191.             return result;
  192.         }
  193.  
  194.  
  195.         private static double[,] InvertMatrix(double[,] matrix)
  196.         {
  197.             int size = matrix.GetLength(0);
  198.             double[,] resultMatrix = new double[size, size];
  199.             double[,] transposedMatrix = new double[size, size];
  200.             for (int i = 0; i < size; ++i)
  201.             {
  202.                 for (int j = 0; j < size; ++j)
  203.                 {
  204.                     transposedMatrix[i, j] = matrix[j, i];
  205.                 }
  206.             }
  207.  
  208.             for (int i = 0; i < size; ++i)
  209.             {
  210.                 for (int j = 0; j < size; ++j)
  211.                 {
  212.                     resultMatrix[i, j] = Math.Pow(-1, i + j + 2) * DeterminantMatrix(MinorMatrix(transposedMatrix, i, j)) / DeterminantMatrix(matrix);
  213.                 }
  214.             }
  215.  
  216.             return resultMatrix;
  217.         }
  218.  
  219.  
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement