如何在C#中乘以矩阵?

时间:2013-11-24 21:08:15

标签: c# .net matrix matrix-multiplication

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

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

2 个答案:

答案 0 :(得分:5)

通过MSDN Magazine article阅读此James McCaffrey,并使用以下代码作为扩展方法。它们使用锯齿状阵列,比2D阵列更方便,有时更快。将任何data[i][j]更改为data[i,j],以使代码与2D数组一起使用。

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 :(得分:0)

这是我的方法的修改版本,就我一直在测试这种方法工作正常。

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