Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# Program to Perform Matrix Multiplication

Posted in C# Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 241 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C# Program to Perform Matrix Multiplication

Photo Credit to CodeToFun

πŸ™‹ Introduction

Matrix multiplication is a fundamental operation in linear algebra and computer science. It involves multiplying two matrices to produce a third matrix.

In this tutorial, we will explore a C# program that performs matrix multiplication.

We'll break down the logic step by step and provide a sample implementation.

πŸ“„ Example

Let's dive into the C# code that performs matrix multiplication.

MatrixMultiplication.cs
Copied
Copy To Clipboard
using System;

class MatrixMultiplication {
  // Function to perform matrix multiplication
  static void MultiplyMatrices(int[, ] firstMatrix, int[, ] secondMatrix, int[, ] resultMatrix) {
    int rowsFirst = firstMatrix.GetLength(0);
    int colsFirst = firstMatrix.GetLength(1);
    int colsSecond = secondMatrix.GetLength(1);

    for (int i = 0; i < rowsFirst; i++) {
      for (int j = 0; j < colsSecond; j++) {
        for (int k = 0; k < colsFirst; k++) {
          resultMatrix[i, j] += firstMatrix[i, k] * secondMatrix[k, j];
        }
      }
    }
  }

  // Function to display a matrix
  static void DisplayMatrix(int[, ] matrix) {
    int rows = matrix.GetLength(0);
    int cols = matrix.GetLength(1);

    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        Console.Write(matrix[i, j] + "\t");
      }
      Console.WriteLine();
    }
  }

  // Driver program
  static void Main() {
    int[, ] firstMatrix = {
      {1, 2, 3},
      {4, 5, 6},
      {7, 8, 9}
    };
    int[, ] secondMatrix = {
      {9, 8, 7},
      {6, 5, 4},
      {3, 2, 1}
    };
    int[, ] resultMatrix = new int[firstMatrix.GetLength(0), secondMatrix.GetLength(1)];

    // Call the function to perform matrix multiplication
    MultiplyMatrices(firstMatrix, secondMatrix, resultMatrix);

    // Display the matrices
    Console.WriteLine("First Matrix:");
    DisplayMatrix(firstMatrix);

    Console.WriteLine("\nSecond Matrix:");
    DisplayMatrix(secondMatrix);

    Console.WriteLine("\nResult Matrix:");
    DisplayMatrix(resultMatrix);
  }
}

πŸ’» Testing the Program

Feel free to replace the sample matrices with your own 3x3 matrices in the main function to test the program with different input.

Output
First Matrix:
1   2   3
4   5   6
7   8   9

Second Matrix:
9   8   7
6   5   4
3   2   1

Result Matrix:
30   24   18
84   69   54
138  114  90

Compiler and run the program to see the result of the matrix multiplication.

🧠 How the Program Works

  1. The program defines a class MatrixMultiplication containing static methods MultiplyMatrices to perform matrix multiplication and DisplayMatrix to display a matrix.
  2. The MultiplyMatrices method takes two matrices (firstMatrix and secondMatrix) and computes their product, storing the result in the resultMatrix.
  3. The DisplayMatrix method is used to display the contents of a matrix.
  4. In the Main method, sample input matrices (firstMatrix and secondMatrix) are provided, and the program calls the multiplication function.
  5. The program then displays the original matrices and the result matrix.

🧐 Understanding the Concept of Matrix Multiplication

Matrix multiplication is defined as follows: given two matrices A (of dimensions m x n) and B (of dimensions n x p), the product matrix C (of dimensions m x p) is obtained by multiplying each element of a row in matrix A by the corresponding element of a column in matrix B and summing up the results.

πŸŽ‰ Conclusion

Matrix multiplication is a powerful operation used in various fields, including graphics, physics simulations, and machine learning.

Understanding the logic behind matrix multiplication and implementing it in C# provides a foundational understanding of linear algebra in computer science.

Feel free to experiment with different matrix sizes and values to further explore the capabilities of this program. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C# Program to Perform Matrix Multiplication), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy