Tuesday 23 September 2014

Matrix Multiplication in C#



        #region Matrix Multiplication
        public double[] MatrixProduct(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;
        }
        #endregion

No comments:

Post a Comment