How can I multiply two matrices in C#?

后端 未结 10 1559
不思量自难忘°
不思量自难忘° 2021-01-02 03:24

Like described in the title, is there some library in the Microsoft framework which allows to multiply two matrices or do I have to write my own method to do this? // I\'ve

相关标签:
10条回答
  • 2021-01-02 03:55

    CSML - C# Matrix Library - is a compact and lightweight package for numerical linear algebra. Many matrix operations known from Matlab, Scilab and Co. are implemented. See this!

    0 讨论(0)
  • 2021-01-02 03:55

    Here is my code : 4*4 matrix

    for (int i = 0; i < 4; i++)
    {        
        int column = 0;
        while (column < 4)
        {
            int count = 0;
            for (int j = 0; j < 4; j++)
            {
                matrixResult[i, column] += Convert.ToInt32(matrixR[i, j] * matrixT[count, column]);
                count = count + 1;
            }
            column = column + 1;
        }       
    

    }

    0 讨论(0)
  • 2021-01-02 04:01

    I wrote a small program to multiply two 3 x 3 matrices, as part of my neural network for my A-level project. Hope people find it useful.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _3_x_3_Matrix_multiplier
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[,,] matrix = new int[3, 3, 3];
                for (int z = 0; z < 2; z++)
                {
                    for (int y = 0; y < 3; y++)
                    {
                        for (int x = 0; x < 3; x++)
                        {
                            Console.WriteLine("element: {0} , {1}", x, y);
                            matrix[x, y, z] = int.Parse(Console.ReadLine());
                        }
                    }
                }
                for (int xm = 0; xm < 3; xm++)
                {
                    for (int ym = 0; ym < 3; ym++)
                    {
                        for (int zm = 0; zm < 3; zm++)
                        {
                            matrix[xm, ym, 2] += (matrix[0 + zm, ym, 0] * matrix[xm, 0 + zm, 1]);
                        }
                    }
                }
                for (int i = 0; i < 3; i++)
                {
                    Console.Write("\n");
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(matrix[j, i, 2] + " ");
                    }
                }
                Console.ReadLine();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-02 04:02
    namespace matrix_multiplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i, j;
                int[,] a = new int[2, 2];
                Console.WriteLine("Enter no for 2*2 matrix");
                for (i = 0; i < 2; i++)
                {
                    for (j = 0; j < 2; j++)
                    {
                        a[i, j] = int.Parse(Console.ReadLine());
                    }
                }
                Console.WriteLine("First matrix is:");
                for (i = 0; i < 2; i++)
                {
                    for (j = 0; j < 2; j++)
                    {
                      Console.Write(a[i,j]+"\t");
                    }
                    Console.WriteLine(); 
                }
    
    
                int[,] b = new int[2, 2];
                Console.WriteLine("Enter no for 2*2 matrix");
                for (i = 0; i < 2; i++)
                {
                    for (j = 0; j < 2; j++)
                    {
                        b[i, j] = int.Parse(Console.ReadLine());
                    }
                }
                Console.WriteLine("second matrix is:");
                for (i = 0; i < 2; i++)
                {
                    for (j = 0; j < 2; j++)
                    {
                        Console.Write(b[i, j] + "\t");
                    }
                    Console.WriteLine();
                }
    
                Console.WriteLine("Matrix multiplication is:");
                int[,] c = new int[2, 2];
                for (i = 0; i < 2; i++)
                {
                    for (j = 0; j < 2; j++)
                    {
    
    
                        c[i,j]=0;
                         for (int k = 0; k < 2; k++)
                         {
                             c[i, j] +=  a[i, k] * b[k, j];
                         }
                     }
                }
                for (i = 0; i < 2; i++)
                {
                    for (j = 0; j < 2; j++)
                    {
                        Console.Write(c[i, j]+"\t");
                    }
                    Console.WriteLine();
                }
    
                Console.ReadKey();
            }
        }
    }
    

    output

    Enter no for 2*2 matrix

    8
    7
    6
    0
    

    First matrix is:

    8       7
    6       0
    

    Enter no for 2*2 matrix

    4
    3
    2
    1
    

    second matrix is:

    4       3
    2       1
    

    Matrix multiplication is:

    46      31
    24      18
    
    0 讨论(0)
  • 2021-01-02 04:03

    Although you can multiply matrices by an iterative approach (for loops), performing the calculations with linear algebra will clean up your code and will give you performance gains that are several times faster!

    There is a free library available in nuget - MathNet.Numerics. It makes it extremely easy to multiply matrices:

    Matrix<double> a = DenseMatrix.OfArray(new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } });
    Matrix<double> b = DenseMatrix.OfArray(new double[,] { { 1 }, { 2 }, { 3 } });
    Matrix<double> result = a * b;
    

    It has no dependencies and can be used in .net core 2.0, making it an excellent choice to avoid iterative matrix multiplication techniques and take advantage of linear algebra.

    0 讨论(0)
  • 2021-01-02 04:04

    multiply 2 matrix :

    public double[,] MultiplyMatrix(double[,] A, double[,] B)
        {
            int rA = A.GetLength(0);
            int cA = A.GetLength(1);
            int rB = B.GetLength(0);
            int cB = B.GetLength(1);
            double temp = 0;
            double[,] kHasil = new double[rA, cB];
            if (cA != rB)
            {
                Console.WriteLine("matrik can't be multiplied !!");
            }
            else
            {
                for (int i = 0; i < rA; i++)
                {
                    for (int j = 0; j < cB; j++)
                    {
                        temp = 0;
                        for (int k = 0; k < cA; k++)
                        {
                            temp += A[i, k] * B[k, j];
                        }
                        kHasil[i, j] = temp;
                    }
                }
            return kHasil;
            }
        }
    
    0 讨论(0)
提交回复
热议问题