protected override SquareMatrix <T> Add(SquareMatrix <T> left, BaseSquareMatrix <T> right) { ValidateParams(left, right); var result = new SquareMatrix <T>(left.Order); AddByEachElement(left, right, result); return(result); }
private static void ValidateParams(BaseSquareMatrix <T> left, BaseSquareMatrix <T> right) { if (left == null || right == null) { throw new InvalidMatrixOperationException("Matrix can't be null!"); } if (left.Order != right.Order) { throw new InvalidMatrixOperationException("Order of matrix should be equal!"); } }
private static void AddByEachElement(BaseSquareMatrix <T> left, BaseSquareMatrix <T> right, BaseSquareMatrix <T> result) { try { int n = left.Order; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { result[i, j] = (dynamic)left[i, j] + right[i, j]; } } } catch (RuntimeBinderException ex) { throw new NotSupportedException(nameof(T), ex); } }
/// <summary> /// Check on equal two matrix /// </summary> /// <param name="matrix"> Second matrix for checking </param> /// <returns> If matrix is simular, true, else - false </returns> public bool Equals(BaseSquareMatrix <T> matrix) { if (Order != matrix.Order) { return(false); } for (int i = 0; i < Order; i++) { for (int j = 0; j < Order; j++) { if (Comparer.Compare(this[i, j], matrix[i, j]) != 0) { return(false); } } } return(true); }
protected override SquareMatrix <T> Add(BaseSquareMatrix <T> left, SquareMatrix <T> right) { return(this.Add(right, left)); }