如何在C#中将矩阵相乘?

How to multiply a matrix in C#?
2021-05-11
  •  译文(汉语)
  •  原文(英语)

我无法使用此方法.它打算将一个矩阵乘以给定的矩阵.有人可以帮我改正吗?

    class Matriz
    {
        public double[,] structure;

        //Other class methods

        public void multiplyBy(Matrix m)
        {
            if (this.structure.GetLength(1) == m.structure.GetLength(0))
            {
                Matriz resultant = new Matriz(this.structure.GetLength(0), m.structure.GetLength(1));
                for (int i = 0; i < this.structure.GetLength(0) - 1; i++)
                {
                    for (int j = 0; j < m.structure.GetLength(1) - 1; j++)
                    {
                        resultant.structure[i, j] = 0;
                        for (int z = 0; z < this.structure.GetLength(1) - 1; z++)
                        {
                            resultant.structure[i, j] += this.structure[i, z] * m.structure[z, j];
                        }
                    }
                }
                this.structure= resultant.structure;
            }
            else
            {
                Console.WriteLine("Selected matrixs cannot be multiply");
            }
        }
    }
速聊1:
有人可以向我解释代码中的错误,我看不到与相关文章的区别.
速聊2:
你遇到了什么错误?"我无法获得此方法"是什么意思?
速聊3:
这是Microsoft的大量资源,内容涉及如何使用C#(msdn.microsoft.com/en-us/magazine/jj863137.aspx)执行矩阵运算.
速聊4:
改写问题.
解决过程1

阅读James McCaffrey撰写的这篇《MSDN杂志》文章,并将下面的代码用作扩展方法.他们使用锯齿状阵列,该阵列比2D阵列更方便,有时甚至更快.更改任何值以使代码与2D数组一起使用.data[i][j]data[i,j]

public static double[] MatrixProduct(this double[][] matrixA,
    double[] vectorB)
{
    int aRows=matrixA.Length; int aCols=matrixA[0].Length;
    int bRows=vectorB.Length;
    if (aCols!=bRows)
        throw new Exception("Non-conformable matrices in MatrixProduct");
    double[] result=new double[aRows];
    for (int i=0; i<aRows; ++i) // each row of A
        for (int k=0; k<aCols; ++k)
            result[i]+=matrixA[i][k]*vectorB[k];
    return result;
}
public static double[][] MatrixProduct(this double[][] matrixA,
    double[][] matrixB)
{
    int aRows=matrixA.Length; int aCols=matrixA[0].Length;
    int bRows=matrixB.Length; int bCols=matrixB[0].Length;
    if (aCols!=bRows)
        throw new Exception("Non-conformable matrices in MatrixProduct");
    double[][] result=MatrixCreate(aRows, bCols);
    for (int i=0; i<aRows; ++i) // each row of A
        for (int j=0; j<bCols; ++j) // each col of B
            for (int k=0; k<aCols; ++k)
                result[i][j]+=matrixA[i][k]*matrixB[k][j];
    return result;
}
public static double[][] MatrixCreate(int rows, int cols)
{
    // creates a matrix initialized to all 0.0s  
    // do error checking here?  
    double[][] result=new double[rows][];
    for (int i=0; i<rows; ++i)
        result[i]=new double[cols];
    // auto init to 0.0  
    return result;
}
速聊1:
如果您使用的是C样式矩阵(每个行按顺序排列一个大数组),则可以使用进行索引data[i*cols+j].
解决过程2

就我已经测试过的方法而言,这是我方法的修改版本.

    public void multiplicarPor(Matriz m)
    {
        if (this.estructura.GetLength(1) == m.estructura.GetLength(0))
        {
            Matriz resultante = new Matriz(this.estructura.GetLength(0), m.estructura.GetLength(1));
            for (int i = 0; i < this.estructura.GetLength(0); i++)
            {
                for (int j = 0; j < m.estructura.GetLength(1); j++)
                {
                    resultante.estructura[i, j] = 0;
                    for (int z = 0; z < this.estructura.GetLength(1); z++)
                    {
                        resultante.estructura[i, j] += this.estructura[i, z] * m.estructura[z, j];
                    }
                }
            }
            this.estructura = resultante.estructura;
        }
        else
        {
            Console.WriteLine("No se pueden multiplicar estas matrices");
        }
    }
速聊1:
而不是estructura[i, j]在内部循环中z写入,而是在循环之后写入局部变量sum并分配值estructura[i, j]=sum.也尽量不要调用GetLength()太多次,而还要将其值存储在局部变量中.

I cannot get this method to work. It intends to multiply a matrix by a given one. Could someone help me to correct it please?

    class Matriz
    {
        public double[,] structure;

        //Other class methods

        public void multiplyBy(Matrix m)
        {
            if (this.structure.GetLength(1) == m.structure.GetLength(0))
            {
                Matriz resultant = new Matriz(this.structure.GetLength(0), m.structure.GetLength(1));
                for (int i = 0; i < this.structure.GetLength(0) - 1; i++)
                {
                    for (int j = 0; j < m.structure.GetLength(1) - 1; j++)
                    {
                        resultant.structure[i, j] = 0;
                        for (int z = 0; z < this.structure.GetLength(1) - 1; z++)
                        {
                            resultant.structure[i, j] += this.structure[i, z] * m.structure[z, j];
                        }
                    }
                }
                this.structure= resultant.structure;
            }
            else
            {
                Console.WriteLine("Selected matrixs cannot be multiply");
            }
        }
    }
Talk1:
Could someone please explain me the error in the code, I cannot see the difference with the related posts.
Talk2:
What error are you getting? What does "I cannot get this method," mean?
Talk3:
Here is a great resource from Microsoft on how to do Matrix operations with C# (msdn.microsoft.com/en-us/magazine/jj863137.aspx)
Talk4:
Rewrote the question.
Solutions1

Read this MSDN Magazine article by James McCaffrey and use the code below as an extension method. They use a jagged array which is more convenient and sometimes faster than a 2D array. Change any data[i][j] to data[i,j] to make the code work with a 2D array.

public static double[] MatrixProduct(this double[][] matrixA,
    double[] vectorB)
{
    int aRows=matrixA.Length; int aCols=matrixA[0].Length;
    int bRows=vectorB.Length;
    if (aCols!=bRows)
        throw new Exception("Non-conformable matrices in MatrixProduct");
    double[] result=new double[aRows];
    for (int i=0; i<aRows; ++i) // each row of A
        for (int k=0; k<aCols; ++k)
            result[i]+=matrixA[i][k]*vectorB[k];
    return result;
}
public static double[][] MatrixProduct(this double[][] matrixA,
    double[][] matrixB)
{
    int aRows=matrixA.Length; int aCols=matrixA[0].Length;
    int bRows=matrixB.Length; int bCols=matrixB[0].Length;
    if (aCols!=bRows)
        throw new Exception("Non-conformable matrices in MatrixProduct");
    double[][] result=MatrixCreate(aRows, bCols);
    for (int i=0; i<aRows; ++i) // each row of A
        for (int j=0; j<bCols; ++j) // each col of B
            for (int k=0; k<aCols; ++k)
                result[i][j]+=matrixA[i][k]*matrixB[k][j];
    return result;
}
public static double[][] MatrixCreate(int rows, int cols)
{
    // creates a matrix initialized to all 0.0s  
    // do error checking here?  
    double[][] result=new double[rows][];
    for (int i=0; i<rows; ++i)
        result[i]=new double[cols];
    // auto init to 0.0  
    return result;
}
Talk1:
If you are using C style matrices (one big array with each row in sequence) then you can index it with data[i*cols+j].
Solutions2

This is the modified version of my method, as far as I've been testing this approach works fine.

    public void multiplicarPor(Matriz m)
    {
        if (this.estructura.GetLength(1) == m.estructura.GetLength(0))
        {
            Matriz resultante = new Matriz(this.estructura.GetLength(0), m.estructura.GetLength(1));
            for (int i = 0; i < this.estructura.GetLength(0); i++)
            {
                for (int j = 0; j < m.estructura.GetLength(1); j++)
                {
                    resultante.estructura[i, j] = 0;
                    for (int z = 0; z < this.estructura.GetLength(1); z++)
                    {
                        resultante.estructura[i, j] += this.estructura[i, z] * m.estructura[z, j];
                    }
                }
            }
            this.estructura = resultante.estructura;
        }
        else
        {
            Console.WriteLine("No se pueden multiplicar estas matrices");
        }
    }
Talk1:
Instead of writing to estructura[i, j] in the inner loop z write to a local variable sum and assign the value estructura[i, j]=sum after the loop. Also try not to call GetLength() so many times, but store its value in a local variable also.
转载于:https://stackoverflow.com/questions/20181020/how-to-multiply-a-matrix-in-c

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入