/// <summary> /// Extension method which allaws to sum up two matrixes /// </summary> /// <typeparam name="T">type that closes method</typeparam> /// <param name="matrix">matrix whose functions is extended by this method</param> /// <param name="other">matrix which is added to existing</param> /// <param name="sum">the way of addition</param> /// <returns>new matrix which is sum of two matrixes</returns> public static BaseMatrix <T> Sum <T>(this BaseMatrix <T> matrix, BaseMatrix <T> other, ISum <T> sum) { if (ReferenceEquals(other, null)) { throw new ArgumentNullException(nameof(other)); } var result = new Addition <T>(other, sum); matrix.Accept(result); return(result.Result); }
/// <summary> /// Finds sum with any matrix /// </summary> /// <param name="matrix">any matrix which is added to existing matrix</param> /// <returns>new square matrix as result of sum two matrixes</returns> private SquareMatrix <T> Sum(BaseMatrix <T> matrix) { int size = (int)Math.Sqrt(matrix.Length); Result = new SquareMatrix <T>(size); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { Result[i, j] = criterion.Sum(other[i, j], matrix[i, j]); } } return(Result as SquareMatrix <T>); }
/// <summary> /// Method for addition of two matrices /// </summary> /// <param name="matrixA">1st matrix</param> /// <param name="matrixB">2nd matrix</param> /// <returns>New matrix as a result of addition of two accepted matrices</returns> public static BaseMatrix <T> Add(BaseMatrix <T> matrixA, BaseMatrix <T> matrixB) { if (matrixA is null || matrixB is null) { throw new ArgumentNullException("Cannot operate addition with null"); } dynamic dynMatrixA = matrixA; dynamic dynMatrixB = matrixB; if (matrixA.Size >= matrixB.Size) { return(Addition(dynMatrixA, dynMatrixB)); } else { return(Addition(dynMatrixB, dynMatrixA)); } }
/// <summary> /// Constructor /// </summary> /// <param name="other">matrix</param> /// <param name="criterion">criterion of sum of two matrixes</param> public Addition(BaseMatrix <T> other, ISum <T> criterion) { this.other = other; this.criterion = criterion; }