/// <summary> /// Addition of matrices. /// </summary> /// <typeparam name="T">The data type.</typeparam> /// <param name="matrix">The first matrix. </param> /// <param name="otherMatrix">The second matrix.</param> /// <returns> /// Resulting matrix. /// </returns> public static AbstractSquareMatrix <T> Add <T>(this AbstractSquareMatrix <T> matrix, AbstractSquareMatrix <T> otherMatrix) { if (ReferenceEquals(matrix, null)) { throw new ArgumentNullException(nameof(matrix)); } if (ReferenceEquals(otherMatrix, null)) { throw new ArgumentNullException(nameof(otherMatrix)); } if (matrix.Order != otherMatrix.Order) { throw new ArgumentException($"{nameof(matrix)} and {nameof(otherMatrix)} should be of equal order."); } var resultMatrix = new SquareMatrix <T>(matrix.Order); for (int row = 0; row < matrix.Order; row++) { for (int column = 0; column < matrix.Order; column++) { resultMatrix[row, column] = (dynamic)matrix[row, column] + (dynamic)otherMatrix[row, column]; } } return(resultMatrix); }
/// <inheritdoc /> /// <summary> /// Creates a matrix by transforming the <paramref name="sorceMatrix"/> into a diagonal matrix. /// </summary> public DiagonalMatrix(AbstractSquareMatrix <T> sorceMatrix) : base(sorceMatrix) { this.diagonalElements = new T[this.Order]; for (int i = 0; i < this.Order; i++) { this.diagonalElements[i] = sorceMatrix[i, i]; } }
/// <summary> /// Creates a matrix based on an <paramref name="sorceMatrix"/>. /// </summary> /// <param name="sorceMatrix">array of matrix elements</param> /// <exception cref="ArgumentNullException">Exception thrown when <paramref name="sorceMatrix"/> is null.</exception> /// <exception cref="ArgumentException">Exception thrown when matrix size is not compatible.</exception> protected AbstractSquareMatrix(AbstractSquareMatrix <T> sorceMatrix) { if (ReferenceEquals(sorceMatrix, null)) { throw new ArgumentNullException(nameof(sorceMatrix)); } this.Order = sorceMatrix.Order; }
/// <summary> /// Summarizes two matrices. /// </summary> /// <typeparam name="T">matrix element type.</typeparam> /// <param name="matrix">first summand</param> /// <param name="addMatrix">second term</param> /// <returns>Sum of two matrices.</returns> /// <exception cref="ArgumentNullException">Exception thrown when <paramref name="addMatrix"/> is null.</exception> /// <exception cref="InvalidOperationException">Exception thrown when the addition operation is impossible.</exception> public static AbstractSquareMatrix <T> Add <T>(this AbstractSquareMatrix <T> matrix, AbstractSquareMatrix <T> addMatrix) { if (ReferenceEquals(addMatrix, null)) { return(matrix); } if (matrix.Order != addMatrix.Order) { throw new InvalidOperationException("It is possible to sum only matrices of the same size."); } try { return(Add((dynamic)matrix, (dynamic)addMatrix)); } catch (Exception e) { throw new NotSupportedException(e.Message, e); } }