C#实现矩阵乘法实例分析
程序员文章站
2022-06-09 14:07:55
本文实例讲述了c#实现矩阵乘法的方法。分享给大家供大家参考。具体如下:
static double[][] matrixmultiplication(double...
本文实例讲述了c#实现矩阵乘法的方法。分享给大家供大家参考。具体如下:
static double[][] matrixmultiplication(double[][] matrixone, double[][] matrixtwo) { int arows = matrixone.length; int acols = matrixone[0].length; int brows = matrixtwo.length; int bcols = matrixtwo[0].length; if (acols != brows) throw new exception("out of shape matrices"); double[][] result = creatematrix(arows, bcols); for (int i = 0; i < arows; ++i) // each row of matrixone for (int j = 0; j < bcols; ++j) // each col of matrixtwo for (int k = 0; k < acols; ++k) result[i][j] += matrixone[i][k] * matrixtwo[k][j]; return result; } static double[][] creatematrix(int rows, int cols) { double[][] result = new double[rows][]; for (int i = 0; i < rows; ++i) result[i] = new double[cols]; return result; }
希望本文所述对大家的c#程序设计有所帮助。