Многопоточное умножение матрицы на вектор - C#

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

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

Здравствуйте! Помогите, пожалуйста!!!! Очень нужно Я написала программу умножения матрицы на вектор, но нужно сделать из неё многопоточную программу. А я не понимаю пока принципов такого программирования и зачем оно нужно.
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication11
  6. {
  7. public class Matrix
  8. {
  9. double[,] matrix;
  10. public int Row { get; protected set; }
  11. public int Column { get; protected set; }
  12. public Matrix(int row, int column)
  13. {
  14. Row = row;
  15. Column = column;
  16. matrix = new double[row, column];
  17. }
  18. public Matrix Multiple(Matrix value)
  19. {
  20. Matrix result = new Matrix(Row, value.Column);
  21. for (int i = 0; i < Row; i++)
  22. for (int j = 0; j < value.Column; j++)
  23. for (int k = 0; k < value.Row; k++)
  24. result.matrix[i, j] += matrix[i, k] * value.matrix[k, j];
  25. return result;
  26. }
  27. public void Read()
  28. {
  29. for (int i = 0; i < Row; i++)
  30. for (int j = 0; j < Column; j++)
  31. {
  32. Console.Write("Введите элемент [{0},{1}]: ", i + 1, j + 1);
  33. matrix[i, j] = System.Convert.ToDouble(Console.ReadLine());
  34. }
  35. }
  36. public void Print()
  37. {
  38. for (int i = 0; i < Row; i++)
  39. {
  40. for (int j = 0; j < Column; j++)
  41. Console.Write("{0:f2} ", matrix[i, j]);
  42. Console.WriteLine();
  43. }
  44. }
  45. static void Main(string[] args)
  46. {
  47. Console.WriteLine("Введите количество элементов");
  48. string a= Console.ReadLine();
  49. int n = Convert.ToInt32(a);
  50. Matrix vector = new Matrix(1, n);
  51. Matrix matrix = new Matrix(n, n);
  52. Console.Clear();
  53. Console.WriteLine("Ввод вектора");
  54. vector.Read();
  55. Console.WriteLine("\nВвод матрицы");
  56. matrix.Read();
  57. Console.Clear();
  58. Matrix result = vector.Multiple(matrix);
  59. Console.WriteLine("Вектор");
  60. vector.Print();
  61. Console.WriteLine("\nМатрица");
  62. matrix.Print();
  63. Console.WriteLine("\nРезультат умножения матрицы на вектор");
  64. result.Print();
  65. Console.WriteLine("\nНажмите любую клавишу для выхода из программы");
  66. Console.ReadKey(true);
  67. }
  68. }
  69. }

Решение задачи: «Многопоточное умножение матрицы на вектор»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication78
  8. {
  9.     public class Matrix
  10.     {
  11.         double[,] matrix;
  12.  
  13.         public int Row { get; protected set; }
  14.         public int Column { get; protected set; }
  15.  
  16.         public Matrix(int row, int column)
  17.         {
  18.             Row = row;
  19.             Column = column;
  20.             matrix = new double[row, column];
  21.         }
  22.  
  23.         public Matrix Multiple(Matrix value)
  24.         {
  25.             Matrix result = new Matrix(Row, value.Column);
  26.             Parallel.For(0, Row, i =>
  27.             {
  28.                 for (int j = 0; j < value.Column; j++)
  29.                     for (int k = 0; k < value.Row; k++)
  30.                         result.matrix[i, j] += matrix[i, k] * value.matrix[k, j];
  31.             });
  32.             return result;
  33.         }
  34.  
  35.         public void Read()
  36.         {
  37.             for (int i = 0; i < Row; i++)
  38.                 for (int j = 0; j < Column; j++)
  39.                 {
  40.                     Console.Write("Введите элемент [{0},{1}]: ", i + 1, j + 1);
  41.                     matrix[i, j] = System.Convert.ToDouble(Console.ReadLine());
  42.                 }
  43.         }
  44.  
  45.         public void Print()
  46.         {
  47.             for (int i = 0; i < Row; i++)
  48.             {
  49.                 for (int j = 0; j < Column; j++)
  50.                     Console.Write("{0:f2} ", matrix[i, j]);
  51.                 Console.WriteLine();
  52.             }
  53.         }
  54.  
  55.         static void Main(string[] args)
  56.         {
  57.             Console.WriteLine("Введите количество элементов");
  58.             string a = Console.ReadLine();
  59.             int n = Convert.ToInt32(a);
  60.             Matrix vector = new Matrix(1, n);
  61.             Matrix matrix = new Matrix(n, n);
  62.             Console.Clear();
  63.             Console.WriteLine("Ввод вектора");
  64.             vector.Read();
  65.             Console.WriteLine("\nВвод матрицы");
  66.             matrix.Read();
  67.             Console.Clear();
  68.             Matrix result = vector.Multiple(matrix);
  69.             Console.WriteLine("Вектор");
  70.             vector.Print();
  71.             Console.WriteLine("\nМатрица");
  72.             matrix.Print();
  73.             Console.WriteLine("\nРезультат умножения матрицы на вектор");
  74.             result.Print();
  75.             Console.WriteLine("\nНажмите любую клавишу для выхода из программы");
  76.             Console.ReadKey(true);
  77.         }
  78.     }
  79. }

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


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

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

14   голосов , оценка 4.143 из 5

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

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

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