如何在C#中将两个矩阵相乘? [英] How can I multiply two matrices in C#?

查看:84
本文介绍了如何在C#中将两个矩阵相乘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题中所述,Microsoft框架中是否有一些库可以将两个矩阵相乘?或者我必须编写自己的方法来做到这一点? //现在我已经有了答案

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

第二个问题: 我用MultiplyMatrix方法编写了这个多类,但是它不像我想要的那样工作.谁能帮忙告诉我哪里出了错?

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

        }

    }

我忘了告诉我:我想让一个Web服务在其上繁殖.

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

谢谢:)

推荐答案

.NET没有内置内容.您将必须自己编写乘法或使用某些第三方库.我已经赞了关于比较两种不同实现方式的一种实现方式:一种标准的朴素算法和一种使用不安全代码的实现方式.

There is nothing built into .NET. You will have to write the multiplication yourself or use some third party library. I've blogged about one way to achieve this comparing two different implementations : a standard naive algorithm and one using unsafe code.

这篇关于如何在C#中将两个矩阵相乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆