/// <summary>Performs an element-wise binary operation (involving two matricies), /// returning the result in a new matrix instance</summary> /// <param name="matrix1">First matrix</param> /// <param name="matrix2">Second matrix</param> /// <param name="operation">Binary operation delegate</param> /// <returns>Matrix instance containing the result of the binary operation</returns> public static MatrixBase <T> ElementWiseOperation( MatrixBase <T> matrix1, MatrixBase <T> matrix2, ElementBinaryOperationDelegate operation) { if (MatrixBase <T> .IsNull(matrix1)) { throw new ArgumentNullException("matrix1"); } if (MatrixBase <T> .IsNull(matrix2)) { throw new ArgumentNullException("matrix2"); } if (matrix1.ColumnCount != matrix2.ColumnCount) { throw new DimensionMismatchException("The number of columns in matrix1 does not equal the number of columns in matrix2"); } if (matrix1.RowCount != matrix2.RowCount) { throw new DimensionMismatchException("The number of rows in matrix1 does not equal the number of rows in matrix2"); } MatrixBase <T> result = new MatrixBase <T>(matrix1.ColumnCount, matrix1.RowCount); for (Int32 i = 0; i < result.ColumnCount; i++) { for (Int32 j = 0; j < result.RowCount; j++) { result[i, j] = operation(matrix1[i, j], matrix2[i, j]); } } return(result); }
/// <summary>Performs an element-wise binary operation involving a matrix and a scalar, /// returning the result in a new matrix instance</summary> /// <param name="matrix">Matrix</param> /// <param name="scalar">Scalar</param> /// <param name="operation">Binary operation delegate</param> /// <returns>Matrix instance containing the result of the binary operation</returns> public static MatrixBase <T> ElementWiseOperation( MatrixBase <T> matrix, T scalar, ElementBinaryOperationDelegate operation) { if (MatrixBase <T> .IsNull(matrix)) { throw new ArgumentNullException("matrix"); } MatrixBase <T> result = new MatrixBase <T>(matrix.ColumnCount, matrix.RowCount); for (Int32 i = 0; i < result.ColumnCount; i++) { for (Int32 j = 0; j < result.RowCount; j++) { result[i, j] = operation(matrix[i, j], scalar); } } return(result); }