Методы для класса «Матрица» - C#

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

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

Класс:

Матрица

Члены класса:

Размерность матрицы,элементы матрицы.

Методы:

Конструктор, деструктор,метод вывода матрицы,метод вычисления определителя матрицы

Операторы перегрузки:

Сложение (+),вычитание (-),умножение (*) 2-х матриц,умножение матрицы на число (*=)

Исходные данные:

А=(5 1 7 10 -2 1 0 1 2) B=(2 4 1 2 1 0 7 2 1) -матрицы размером 3х3

Результаты:

С=2(A–B)(A^2 + B)

Найти:

|C|

Решение задачи: «Методы для класса «Матрица»»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Matrix
  7. {
  8.     class Program
  9.     {
  10.         public class RMatrix
  11.         {
  12.             private int nRows;
  13.             private int nCols;
  14.             private double[,] matrix;
  15.  
  16.             public RMatrix(int nRows, int nCols)
  17.             {
  18.                 this.nRows = nRows;
  19.                 this.nCols = nCols;
  20.                 this.matrix = new double[nRows, nCols];
  21.                 for (int i = 0; i < nRows; i++)
  22.                 {
  23.                     for (int j = 0; j < nCols; j++)
  24.                     {
  25.                         matrix[i, j] = 0.0;
  26.                     }
  27.                 }
  28.             }
  29.  
  30.             public RMatrix(double[,] matrix)
  31.             {
  32.                 this.nRows = matrix.GetLength(0);
  33.                 this.nCols = matrix.GetLength(1);
  34.                 this.matrix = matrix;
  35.             }
  36.  
  37.             public RMatrix(RMatrix m)
  38.             {
  39.                 nRows = m.GetnRows;
  40.                 nCols = m.GetnCols;
  41.                 matrix = m.matrix;
  42.             }
  43.  
  44.             public double this[int m, int n]
  45.             {
  46.                 get
  47.                 {
  48.                     if (m < 0 || m > nRows)
  49.                     {
  50.                         throw new Exception("m-th row is out of range!");
  51.                     }
  52.                     if (n < 0 || n > nCols)
  53.                     {
  54.                         throw new Exception("n-th col is out of range!");
  55.                     }
  56.                     return matrix[m, n];
  57.                 }
  58.                 set { matrix[m, n] = value; }
  59.             }
  60.  
  61.             public int GetnRows
  62.             {
  63.                 get { return nRows; }
  64.             }
  65.  
  66.             public int GetnCols
  67.             {
  68.                 get { return nCols; }
  69.             }
  70.  
  71.             public override string ToString()
  72.             {
  73.                 string strMatrix = "(";
  74.                 for (int i = 0; i < nRows; i++)
  75.                 {
  76.                     string str = "";
  77.                     for (int j = 0; j < nCols - 1; j++)
  78.                     {
  79.                         str += matrix[i, j].ToString() + ", ";
  80.                     }
  81.                     str += matrix[i, nCols - 1].ToString();
  82.                     if (i != nRows - 1 && i == 0)
  83.                         strMatrix += str + "\n";
  84.                     else if (i != nRows - 1 && i != 0)
  85.                         strMatrix += " " + str + "\n";
  86.                     else
  87.                         strMatrix += " " + str + ")";
  88.                 }
  89.                 return strMatrix;
  90.             }
  91.  
  92.             public override bool Equals(object obj)
  93.             {
  94.                 return (obj is RMatrix) && this.Equals((RMatrix)obj);
  95.             }
  96.  
  97.             public bool Equals(RMatrix m)
  98.             {
  99.                 return matrix == m.matrix;
  100.             }
  101.  
  102.             public override int GetHashCode()
  103.             {
  104.                 return matrix.GetHashCode();
  105.             }
  106.  
  107.             public static RMatrix operator +(RMatrix m1, RMatrix m2)
  108.             {
  109.                 if (!RMatrix.CompareDimension(m1, m2))
  110.                 {
  111.                     throw new Exception("The dimensions of two matrices must be the same!");
  112.                 }
  113.                 RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
  114.                 for (int i = 0; i < m1.GetnRows; i++)
  115.                 {
  116.                     for (int j = 0; j < m1.GetnCols; j++)
  117.                     {
  118.                         result[i, j] = m1[i, j] + m2[i, j];
  119.                     }
  120.                 }
  121.                 return result;
  122.             }
  123.  
  124.             public static RMatrix operator -(RMatrix m)
  125.             {
  126.                 for (int i = 0; i < m.GetnRows; i++)
  127.                 {
  128.                     for (int j = 0; j < m.GetnCols; j++)
  129.                     {
  130.                         m[i, j] = -m[i, j];
  131.                     }
  132.                 }
  133.                 return m;
  134.             }
  135.  
  136.             public static RMatrix operator -(RMatrix m1, RMatrix m2)
  137.             {
  138.                 if (!RMatrix.CompareDimension(m1, m2))
  139.                 {
  140.                     throw new Exception("The dimensions of two matrices must be the same!");
  141.                 }
  142.                 RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
  143.                 for (int i = 0; i < m1.GetnRows; i++)
  144.                 {
  145.                     for (int j = 0; j < m1.GetnCols; j++)
  146.                     {
  147.                         result[i, j] = m1[i, j] - m2[i, j];
  148.                     }
  149.                 }
  150.                 return result;
  151.             }
  152.  
  153.             public static RMatrix operator *(RMatrix m, double d)
  154.             {
  155.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  156.                 for (int i = 0; i < m.GetnRows; i++)
  157.                 {
  158.                     for (int j = 0; j < m.GetnCols; j++)
  159.                     {
  160.                         result[i, j] = m[i, j] * d;
  161.                     }
  162.                 }
  163.                 return result;
  164.             }
  165.  
  166.             public static RMatrix operator *(double d, RMatrix m)
  167.             {
  168.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  169.                 for (int i = 0; i < m.GetnRows; i++)
  170.                 {
  171.                     for (int j = 0; j < m.GetnCols; j++)
  172.                     {
  173.                         result[i, j] = m[i, j] * d;
  174.                     }
  175.                 }
  176.                 return result;
  177.             }
  178.  
  179.             public static RMatrix operator /(RMatrix m, double d)
  180.             {
  181.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  182.                 for (int i = 0; i < m.GetnRows; i++)
  183.                 {
  184.                     for (int j = 0; j < m.GetnCols; j++)
  185.                     {
  186.                         result[i, j] = m[i, j] / d;
  187.                     }
  188.                 }
  189.                 return result;
  190.             }
  191.  
  192.             public static RMatrix operator /(double d, RMatrix m)
  193.             {
  194.                 RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
  195.                 for (int i = 0; i < m.GetnRows; i++)
  196.                 {
  197.                     for (int j = 0; j < m.GetnCols; j++)
  198.                     {
  199.                         result[i, j] = m[i, j] / d;
  200.                     }
  201.                 }
  202.                 return result;
  203.             }
  204.  
  205.             public static RMatrix operator *(RMatrix m1, RMatrix m2)
  206.             {
  207.                 if (m1.GetnCols != m2.GetnRows)
  208.                 {
  209.                     throw new Exception("The numbers of columns of the" +
  210.                      " first matrix must be equal to the number of " +
  211.                      " rows of the second matrix!");
  212.                 }
  213.                 double tmp;
  214.                 RMatrix result = new RMatrix(m1.GetnRows, m2.GetnCols);
  215.                 for (int i = 0; i < m1.GetnRows; i++)
  216.                 {
  217.                     for (int j = 0; j < m2.GetnCols; j++)
  218.                     {
  219.                         tmp = result[i, j];
  220.                         for (int k = 0; k < result.GetnRows; k++)
  221.                         {
  222.                             tmp += m1[i, k] * m2[k, j];
  223.                         }
  224.                         result[i, j] = tmp;
  225.                     }
  226.                 }
  227.                 return result;
  228.             }
  229.  
  230.             public bool IsSquared()
  231.             {
  232.                 if (nRows == nCols)
  233.                     return true;
  234.                 else
  235.                     return false;
  236.             }
  237.  
  238.             public static bool CompareDimension(RMatrix m1, RMatrix m2)
  239.             {
  240.                 if (m1.GetnRows == m2.GetnRows && m1.GetnCols == m2.GetnCols)
  241.                     return true;
  242.                 else
  243.                     return false;
  244.             }
  245.  
  246.             public static double Determinant(RMatrix mat)
  247.             {
  248.                 double result = 0.0;
  249.                 if (!mat.IsSquared())
  250.                 {
  251.                     throw new Exception("The matrix must be squared!");
  252.                 }
  253.                 if (mat.GetnRows == 1)
  254.                     result = mat[0, 0];
  255.                 else
  256.                 {
  257.                     for (int i = 0; i < mat.GetnRows; i++)
  258.                     {
  259.                         result += Math.Pow(-1, i) * mat[0, i] * Determinant(RMatrix.Minor(mat, 0, i));
  260.                     }
  261.                 }
  262.                 return result;
  263.             }
  264.  
  265.             public static RMatrix Minor(RMatrix mat, int row, int col)
  266.             {
  267.                 RMatrix mm = new RMatrix(mat.GetnRows - 1, mat.GetnCols - 1);
  268.                 int ii = 0, jj = 0;
  269.                 for (int i = 0; i < mat.GetnRows; i++)
  270.                 {
  271.                     if (i == row)
  272.                         continue;
  273.                     jj = 0;
  274.                     for (int j = 0; j < mat.GetnCols; j++)
  275.                     {
  276.                         if (j == col)
  277.                             continue;
  278.                         mm[ii, jj] = mat[i, j];
  279.                         jj++;
  280.                     }
  281.                     ii++;
  282.                 }
  283.                 return mm;
  284.             }
  285.         }
  286.  
  287.         static void Main(string[] args)
  288.         {
  289.             Console.WriteLine("\n\nЗадание Матрица\n\n");
  290.             // Создание матрицы А:
  291.             RMatrix A = new RMatrix(new double[3, 3] { {5.0, 1.0, 7.0},
  292.                                                        {10.0, -2.0, 1.0},
  293.                                                        {0.0, 1.0, 2.0}});
  294.             // Создание матрицы B:
  295.             RMatrix B = new RMatrix(new double[3, 3] { {2.0, 4.0, 1.0},
  296.                                                        {2.0, 1.0, 0.0},
  297.                                                        {7.0, 2.0, 1.0}});
  298.             // Создание матрицы C:
  299.             RMatrix C = new RMatrix(new double[3, 3] { {0.0, 0.0, 0.0},
  300.                                                        {0.0, 0.0, 0.0},
  301.                                                        {0.0, 0.0, 0.0}});
  302.             Console.WriteLine("\nМатрица: А = \n{0}", A);
  303.             Console.WriteLine("\nМатрица: B = \n{0}", B);
  304.  
  305.             Console.WriteLine("\nA + B = \n{0}", (A + B));
  306.             Console.WriteLine("\nA - B = \n{0}", (A - B));
  307.             Console.WriteLine("\nA * B = \n{0}", (A * B));
  308.             Console.WriteLine("\nB * A = \n{0}", (B * A));
  309.             Console.WriteLine("\nA * 5 = \n{0}", (A * 5));
  310.             Console.WriteLine("\nB * 4 = \n{0}", (B * 4));
  311.             Console.WriteLine("\nОпределитель A = {0}", RMatrix.Determinant(A));
  312.             Console.WriteLine("\nОпределитель B = {0}", RMatrix.Determinant(B));
  313.             C=2 * (A - B) * (A * A + B);
  314.             Console.WriteLine("\nС=2(A–B)(A^2 + B) = \n{0}",C);
  315.             Console.WriteLine("\nОпределитель C = {0}", RMatrix.Determinant(C));
  316.             Console.ReadLine();
  317.         }
  318.     }
  319. }

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


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

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

13   голосов , оценка 3.769 из 5

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

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

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