Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I multiply two matrices in C#?

Tags:

c#

math

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 got an answer to this by now

Second question: I wrote this multi class with a MultiplyMatrix method but it doesn't work like I want to. Can anyone help and tell where I made a mistake?

class multi
    {
        public void MultiplyMatrix(double[,] _A, double[,] _B, int _n, int _m, int _r)
        {
            int n, m, r;
            double si;
            n = _n;
            m = _m;
            r = _r;
            double[,] A = new double[n, m];
            double[,] B = new double[m, r];
            double[,] C = new double[n, r];
            A = _A;
            B = _B;
            try
            {
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < r; j++)
                    {
                        si = 0;
                        for (int k = 0; k < m; k++)
                        {
                            si += A[i, m + k] + B[k, r + j];
                        }
                        C[i, r + j] = si;
                    }
                }
                for (int i = 0; i < C.Length; i++)
                {
                    for (int j = 0; j < C.Length; j++)
                    {
                        Console.Write(C[i, j]+" ");
                        if (j % 3 == 0)
                            Console.WriteLine();
                    }
                }
            }
            catch (IndexOutOfRangeException) { } // I always get this exception

        }

    }

I forgot to tell: I want to make a webservice to multiply on it.

Thanks:)

like image 813
Harry89pl Avatar asked Jun 10 '11 19:06

Harry89pl


3 Answers

Multiplication of 2 matrixes:

    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);

        if (cA != rB)
        {
            Console.WriteLine("Matrixes can't be multiplied!!");
        }
        else
        {
            double temp = 0;
            double[,] kHasil = new double[rA, cB];

            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;
        }
    }
like image 151
Dwi Yanuar Ilham Avatar answered Oct 20 '22 19:10

Dwi Yanuar Ilham


Whilst there's no built in Maths framework to do this in .NET (could use XNA's Maths library), there is a Matrix in the System.Windows.Media namespace. The Matrix structure has a Multiply method which takes in another Matrix and outputs a Matrix.

Matrix matrix1 = new Matrix(5, 10, 15, 20, 25, 30);
Matrix matrix2 = new Matrix(2, 4, 6, 8, 10, 12);

// matrixResult is equal to (70,100,150,220,240,352) 
Matrix matrixResult = Matrix.Multiply(matrix1, matrix2);

// matrixResult2 is also
// equal to (70,100,150,220,240,352) 
Matrix matrixResult2 = matrix1 * matrix2;

This is mainly used for 2D transformation:

Represents a 3x3 affine transformation matrix used for transformations in 2-D space.

but if it suits your needs, then there's no need for any third party libraries.

like image 26
keyboardP Avatar answered Oct 20 '22 19:10

keyboardP


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.

like image 45
Aaron Seeling Avatar answered Oct 20 '22 19:10

Aaron Seeling