/// <summary> /// Matrix multiplication /// </summary> /// <param name="L">Left matrix</param> /// <param name="R">Right matrix</param> /// <returns>new Matrix(L*R)</returns> public static Matrix <U> operator *(Matrix <U> L, Matrix <U> R) { int m = L.M; int n = L.N; int p = R.N; U[][] result = new U[m][]; if (R.numRows != L.numColumns) { throw new InvalidOperationException("Invalid dimensions for matrix multiplication"); } else { //create new 2d array for (int i = 0; i < m; i++) { result[i] = new U[p]; } Parallel.For(0, m, i =>//for (int i = 0; i < m; i++) { for (int j = 0; j < p; j++) { result[i][j] = operators.GetZeroValue(); for (int k = 0; k < n; k++) { result[i][j] = operators.add(operators.multiply(L.values[i][k], R.values[k][j]), result[i][j]); } } }); return(new Matrix <U>(result)); } }
/// <summary> /// Vector multiplication /// </summary> /// <param name="L">Left matrix</param> /// <param name="R">Right matrix</param> /// <returns>scalar(L*R)</returns> public static T operator *(Vector <T> L, Vector <T> R) { T result; if (R.numEntries != L.numEntries) { throw new InvalidOperationException("Invalid dimensions for vector multiplication"); } else { result = operators.GetZeroValue(); for (int i = 0; i < R.numEntries; i++) { result = operators.add(operators.multiply(L.values[i], R.values[i]), result); } return(result); } }