Реализовать класс для матриц. В этом классе реализовать интерфейс, содержащий методы для выполнения операций - C#

Узнай цену своей работы

Формулировка задачи:

Реализовать класс для матриц. В этом классе реализовать интерфейс, содержащий методы для выполнения следующих операций: - сложение - умножение - транспонирование.

Решение задачи: «Реализовать класс для матриц. В этом классе реализовать интерфейс, содержащий методы для выполнения операций»

textual
Листинг программы
  1.     class Program
  2.     {
  3.  
  4.         /*
  5.          *  real (row x col) matrix        
  6.          */
  7.  
  8.         public class RMatrix  
  9.         {
  10.             private int nRows;
  11.             private int nCols;
  12.             private double[,] matrix;
  13.  
  14.             public RMatrix(int nRows, int nCols)
  15.             {
  16.                 this.nRows = nRows;
  17.                 this.nCols = nCols;
  18.                 this.matrix = new double[nRows, nCols];
  19.                 for (int i = 0; i < nRows; i++)
  20.                 {
  21.                     for (int j = 0; j < nCols; j++)
  22.                     {
  23.                         matrix[i, j] = 0.0;
  24.                     }
  25.                 }
  26.             }
  27.  
  28.             public RMatrix(double[,] matrix)
  29.             {
  30.                 this.nRows = matrix.GetLength(0);
  31.                 this.nCols = matrix.GetLength(1);
  32.                 this.matrix = matrix;
  33.             }
  34.  
  35.             public RMatrix(RMatrix m)
  36.             {
  37.                 nRows = m.GetnRows;
  38.                 nCols = m.GetnCols;
  39.                 matrix = m.matrix;
  40.             }
  41.  
  42.             public RMatrix IdentityMatrix()
  43.             {
  44.                 RMatrix m = new RMatrix(nRows, nCols);
  45.                 for (int i = 0; i < nRows; i++)
  46.                 {
  47.                     for (int j = 0; j < nCols; j++)
  48.                     {
  49.                         if (i == j)
  50.                         {
  51.                             m[i, j] = 1;
  52.                         }
  53.                     }
  54.                 }
  55.                 return m;
  56.             }
  57.  
  58.             public double this[int m, int n]
  59.             {
  60.                 get
  61.                 {
  62.                     if (m < 0 || m > nRows)
  63.                     {
  64.                         throw new Exception("m-th row is out of range!");
  65.                     }
  66.                     if (n < 0 || n > nCols)
  67.                     {
  68.                         throw new Exception("n-th col is out of range!");
  69.                     }
  70.                     return matrix[m, n];
  71.                 }
  72.                 set { matrix[m, n] = value; }
  73.             }
  74.  
  75.             public int GetnRows
  76.             {
  77.                 get { return nRows; }
  78.             }
  79.  
  80.             public int GetnCols
  81.             {
  82.                 get { return nCols; }
  83.             }
  84.  
  85.  
  86.             public override string ToString()
  87.             {
  88.                 string strMatrix = "(";
  89.                 for (int i = 0; i < nRows; i++)
  90.                 {
  91.                     string str = "";
  92.                     for (int j = 0; j < nCols - 1; j++)
  93.                     {
  94.                         str += matrix[i, j].ToString() + ", ";
  95.                     }
  96.                     str += matrix[i, nCols - 1].ToString();
  97.                     if (i != nRows - 1 && i == 0)
  98.                         strMatrix += str + "\n";
  99.                     else if (i != nRows - 1 && i != 0)
  100.                         strMatrix += " " + str + "\n";
  101.                     else
  102.                         strMatrix += " " + str + ")";
  103.                 }
  104.                 return strMatrix;
  105.             }
  106.  
  107.  
  108.             public static bool operator ==(RMatrix m1, RMatrix m2)
  109.             {
  110.                 return m1.Equals(m2);
  111.             }
  112.  
  113.             public static bool operator !=(RMatrix m1, RMatrix m2)
  114.             {
  115.                 return !m1.Equals(m2);
  116.             }
  117.  
  118.             public static RMatrix operator +(RMatrix m)
  119.             {
  120.                 return m;
  121.             }
  122.  
  123.             public static RMatrix operator +(RMatrix m1, RMatrix m2)
  124.             {
  125.                 RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
  126.                 for (int i = 0; i < m1.GetnRows; i++)
  127.                 {
  128.                     for (int j = 0; j < m1.GetnCols; j++)
  129.                     {
  130.                         result[i, j] = m1[i, j] + m2[i, j];
  131.                     }
  132.                 }
  133.                 return result;
  134.             }
  135.  
  136.             public static RMatrix operator -(RMatrix m)
  137.             {
  138.                 for (int i = 0; i < m.GetnRows; i++)
  139.                 {
  140.                     for (int j = 0; j < m.GetnCols; j++)
  141.                     {
  142.                         m[i, j] = -m[i, j];
  143.                     }
  144.                 }
  145.                 return m;
  146.             }
  147.  
  148.             public static RMatrix operator -(RMatrix m1, RMatrix m2)
  149.             {
  150.                 RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
  151.                 for (int i = 0; i < m1.GetnRows; i++)
  152.                 {
  153.                     for (int j = 0; j < m1.GetnCols; j++)
  154.                     {
  155.                         result[i, j] = m1[i, j] - m2[i, j];
  156.                     }
  157.                 }
  158.                 return result;
  159.             }
  160.  
  161.             public static RMatrix operator *(RMatrix m, double d)
  162.             {
  163.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  164.                 for (int i = 0; i < m.GetnRows; i++)
  165.                 {
  166.                     for (int j = 0; j < m.GetnCols; j++)
  167.                     {
  168.                         result[i, j] = m[i, j] * d;
  169.                     }
  170.                 }
  171.                 return result;
  172.             }
  173.  
  174.             public static RMatrix operator *(double d, RMatrix m)
  175.             {
  176.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  177.                 for (int i = 0; i < m.GetnRows; i++)
  178.                 {
  179.                     for (int j = 0; j < m.GetnCols; j++)
  180.                     {
  181.                         result[i, j] = m[i, j] * d;
  182.                     }
  183.                 }
  184.                 return result;
  185.             }
  186.  
  187.             public static RMatrix operator /(RMatrix m, double d)
  188.             {
  189.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  190.                 for (int i = 0; i < m.GetnRows; i++)
  191.                 {
  192.                     for (int j = 0; j < m.GetnCols; j++)
  193.                     {
  194.                         result[i, j] = m[i, j] / d;
  195.                     }
  196.                 }
  197.                 return result;
  198.             }
  199.  
  200.             public static RMatrix operator /(double d, RMatrix m)
  201.             {
  202.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  203.                 for (int i = 0; i < m.GetnRows; i++)
  204.                 {
  205.                     for (int j = 0; j < m.GetnCols; j++)
  206.                     {
  207.                         result[i, j] = m[i, j] / d;
  208.                     }
  209.                 }
  210.                 return result;
  211.             }
  212.  
  213.             public static RMatrix operator *(RMatrix m1, RMatrix m2)
  214.             {
  215.                 if (m1.GetnCols != m2.GetnRows)
  216.                 {
  217.                     throw new Exception("The numbers of columns of the" +
  218.                      " first matrix must be equal to the number of " +
  219.                      " rows of the second matrix!");
  220.                 }
  221.                 double tmp;
  222.                 RMatrix result = new RMatrix(m1.GetnRows, m2.GetnCols);
  223.                 for (int i = 0; i < m1.GetnRows; i++)
  224.                 {
  225.                     for (int j = 0; j < m2.GetnCols; j++)
  226.                     {
  227.                         tmp = result[i, j];
  228.                         for (int k = 0; k < result.GetnRows; k++)
  229.                         {
  230.                             tmp += m1[i, k] * m2[k, j];
  231.                         }
  232.                         result[i, j] = tmp;
  233.                     }
  234.                 }
  235.                 return result;
  236.             }
  237.  
  238.             public RMatrix Transpose()
  239.             {
  240.                 RMatrix m = new RMatrix(nCols, nRows);
  241.                 for (int i = 0; i < nRows; i++)
  242.                 {
  243.                     for (int j = 0; j < nCols; j++)
  244.                     {
  245.                         m[j, i] = matrix[i, j];
  246.                     }
  247.                 }
  248.                 return m;
  249.             }
  250.         }
  251.  
  252.          static void Main(string[] args)
  253.         {
  254.             RMatrix m1 = new RMatrix(new double[3, 3] { {3.0, 2.0, 7.0},
  255.                                                         {9.0, 3.0, 5.0},
  256.                                                         {4.0, 1.0, 0.0}});
  257.             RMatrix m2 = new RMatrix(3, 3);
  258.             m2[0, 0] = 23.0; m2[0, 1] = 6.0; m2[0, 2] = 8.0;
  259.             m2[1, 0] = 2.0; m2[1, 1] = 55.0; m2[1, 2] = 39.0;
  260.             m2[2, 0] = 1.0; m2[2, 1] = 4.0; m2[2, 2] = 10.0;
  261.             Console.WriteLine("\nOriginal matrix: m1 = \n{0}", m1);
  262.             Console.WriteLine("\nOriginal matrix: m2 = \n{0}", m2);
  263.  
  264.             Console.WriteLine("\nm1 + m2 = \n{0}", (m1 + m2));
  265.             Console.WriteLine("\nm1 - m2 = \n{0}", (m1 - m2));
  266.             Console.WriteLine("\nm1 * m2 = \n{0}", (m1 * m2));
  267.             Console.WriteLine("\nTranspose of m2 = \n{0}", m2.Transpose());
  268.             Console.ReadKey();
  269.         }
  270.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы