private static void ValidateMatrixSizes <T>(SquareMatrixPrototype <T> matrix, SquareMatrixPrototype <T> otherMatrix) { if (matrix.Size != otherMatrix.Size) { throw new ArgumentException($"{nameof(matrix)} and {nameof(otherMatrix)} have different sizes."); } }
private static void ValidateNullParameters <T>( SquareMatrixPrototype <T> matrix, SquareMatrixPrototype <T> otherMatrix) { if (matrix == null) { throw new ArgumentNullException($"{nameof(matrix)} cannot be null."); } if (otherMatrix == null) { throw new ArgumentNullException($"{nameof(otherMatrix)} cannot be null."); } }
/// <summary> /// Performs an addition operation on <paramref name="matrix"/> and <paramref name="otherMatrix"/>. /// </summary> /// <typeparam name="T">Matrix element type.</typeparam> /// <param name="matrix">First matrix.</param> /// <param name="otherMatrix">Second matrix.</param> /// <returns>New matrix with added elements.</returns> /// <exception cref="ArgumentNullException"><paramref name="matrix"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="otherMatrix"/> is null.</exception> /// <exception cref="ArgumentException">Sizes of two matrixes are not equal.</exception> /// <exception cref="NotSupportedException">Matrix element type does not override plus operator.</exception> public static SquareMatrixPrototype <T> Add <T>( this SquareMatrixPrototype <T> matrix, SquareMatrixPrototype <T> otherMatrix) { ValidateNullParameters(matrix, otherMatrix); ValidateMatrixSizes(matrix, otherMatrix); Func <T, T, T> f = (x, y) => (dynamic)x + (dynamic)y; try { return(Transform((dynamic)matrix, (dynamic)otherMatrix, f)); } catch (RuntimeBinderException) { throw new NotSupportedException($"Cannot add instances of this type."); } }