Advertisement
vencinachev

MatrixFull

Jan 26th, 2021
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.23 KB | None | 0 0
  1. static class MatrixMath
  2.     {
  3.         public static void PrintMatrix(int[,] matrix)
  4.         {
  5.             int n = matrix.GetLength(0);
  6.             int m = matrix.GetLength(1);
  7.             for (int i = 0; i < n; i++)
  8.             {
  9.                 for (int j = 0; j < m; j++)
  10.                 {
  11.                     Console.Write($"{matrix[i, j]} ");
  12.                 }
  13.                 Console.WriteLine();
  14.             }
  15.         }
  16.  
  17.         public static void RandomElements(int[,] matrix)
  18.         {
  19.             Random rand = new Random();
  20.             int n = matrix.GetLength(0);
  21.             int m = matrix.GetLength(1);
  22.             for (int i = 0; i < n; i++)
  23.             {
  24.                 for (int j = 0; j < m; j++)
  25.                 {
  26.                     matrix[i, j] = rand.Next(10);
  27.                 }
  28.             }
  29.         }
  30.  
  31.         public static void ChangeRows(int[,] matrix, int row1, int row2)
  32.         {
  33.             int n = matrix.GetLength(0);
  34.             int m = matrix.GetLength(1);
  35.             if (row1 < 0 || row2 < 0 || row1 >= n || row2 >= n)
  36.             {
  37.                 throw new ArgumentException("Rows problem");
  38.             }
  39.             for (int i = 0; i < m; i++)
  40.             {
  41.                 // matrix[row1, i] <=> matrix[row2, i]
  42.                 int temp = matrix[row1, i];
  43.                 matrix[row1, i] = matrix[row2, i];
  44.                 matrix[row2, i] = temp;
  45.             }
  46.         }
  47.  
  48.         public static int[,] ScalarMult(int[,] matrix, int num)
  49.         {
  50.             int n = matrix.GetLength(0);
  51.             int m = matrix.GetLength(1);
  52.             int[,] result = new int[n, m];
  53.             for (int i = 0; i < n; i++)
  54.             {
  55.                 for (int j = 0; j < m; j++)
  56.                 {
  57.                     result[i, j] = matrix[i, j] * num;
  58.                 }
  59.             }
  60.             return result;
  61.         }
  62.  
  63.         public static int[,] AddMatrix(int[,] matrix1, int[,] matrix2)
  64.         {
  65.             int n1 = matrix1.GetLength(0);
  66.             int m1 = matrix1.GetLength(1);
  67.             int n2 = matrix1.GetLength(0);
  68.             int m2 = matrix1.GetLength(1);
  69.             if (n1 != n2 || m1 != m2)
  70.             {
  71.                 throw new ArgumentException("Matrix dimensions problem!");
  72.             }
  73.             int[,] result = new int[n1, m1];
  74.             for (int i = 0; i < n1; i++)
  75.             {
  76.                 for (int j = 0; j < m1; j++)
  77.                 {
  78.                     result[i, j] = matrix1[i, j] + matrix2[i, j];
  79.                 }
  80.             }
  81.             return result;
  82.         }
  83.  
  84.         public static int[,] Identity(int n)
  85.         {
  86.             int[,] result = new int[n, n];
  87.             for (int i = 0; i < n; i++)
  88.             {
  89.                 result[i, i] = 1;
  90.             }
  91.             return result;
  92.         }
  93.  
  94.         public static int[,] Scalar(int size, int num)
  95.         {
  96.             return ScalarMult(Identity(size), num);
  97.         }
  98.  
  99.         public static int[,] Transponate(int[,] matrix)
  100.         {
  101.             int n = matrix.GetLength(0);
  102.             int m = matrix.GetLength(1);
  103.             int[,] result = new int[m, n];
  104.             for (int row = 0; row < n; row++)
  105.             {
  106.                 for (int col = 0; col < m; col++)
  107.                 {
  108.                     result[col, row] = matrix[row, col];
  109.                 }
  110.             }
  111.             return result;
  112.         }
  113.  
  114.         public static int[,] Multiply(int[,] A, int[,] B)
  115.         {
  116.             int rowa = A.GetLength(0);
  117.             int cola = A.GetLength(1);
  118.             int rowb = B.GetLength(0);
  119.             int colb = B.GetLength(1);
  120.  
  121.             if (cola != rowb)
  122.             {
  123.                 throw new ArithmeticException("Matrix dimensions problem!");
  124.             }
  125.  
  126.             int[,] mult = new int[rowa, colb];
  127.             int n = rowa;
  128.             int m = colb;
  129.  
  130.             for (int i = 0; i < n; i++)
  131.             {
  132.                 for (int j = 0; j < m; j++)
  133.                 {
  134.                     mult[i, j] = 0;
  135.                     for (int k = 0; k < m; k++)
  136.                     {
  137.                         mult[i, j] += A[i, k] * B[k, j];
  138.                     }
  139.                 }
  140.             }
  141.             return mult;
  142.  
  143.         }
  144.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement