Advertisement
vencinachev

MatrixClass

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