How can I multiply two matrices in C#?

后端 未结 10 1866
不思量自难忘°
不思量自难忘° 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 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 a = DenseMatrix.OfArray(new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } });
    Matrix b = DenseMatrix.OfArray(new double[,] { { 1 }, { 2 }, { 3 } });
    Matrix 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.

提交回复
热议问题