Пример #1
0
        /// <summary>
        /// Gets sum of two static matrix
        /// </summary>
        /// <typeparam name="T">Type of items.</typeparam>
        /// <param name="firstMatrix">First static matrix.</param>
        /// <param name="secondMatrix">Second static matrix.</param>
        /// <returns>Static matrix that represent sum of two static matrix</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when first or second matrix is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Thrown when size of two matrix are incompatible
        /// </exception>
        public static RectangleMatrix <T> Sum <T>(this StaticMatrix <T> firstMatrix, StaticMatrix <T> secondMatrix)
        {
            if (firstMatrix == null || secondMatrix == null)
            {
                throw new ArgumentNullException("Matrix must be not null");
            }

            if (firstMatrix.RowsCount != secondMatrix.RowsCount ||
                firstMatrix.ColumnsCount != secondMatrix.ColumnsCount)
            {
                throw new ArgumentException("Incompatible sizes of two matrix");
            }

            var itemsTable = new T[firstMatrix.RowsCount, firstMatrix.ColumnsCount];

            for (var i = 0; i < firstMatrix.RowsCount; i++)
            {
                for (var j = 0; j < firstMatrix.ColumnsCount; j++)
                {
                    itemsTable[i, j] = (dynamic)firstMatrix[i, j] + (dynamic)secondMatrix[i, j];
                }
            }

            return(new RectangleMatrix <T>(itemsTable));
        }
Пример #2
0
        /// <summary>
        /// Creates static matrix from another static matrix.
        /// </summary>
        /// <param name="matrix">Static matrix.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown when matrix is null.
        /// </exception>
        public StaticMatrix(StaticMatrix <T> matrix)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("Matrix must be not null");
            }

            _items = new T[matrix.RowsCount, matrix.ColumnsCount];

            for (var i = 0; i < _items.GetLength(0); i++)
            {
                for (var j = 0; j < _items.GetLength(1); j++)
                {
                    _items[i, j] = matrix[i, j];
                }
            }
        }
 /// <summary>
 /// Creates rectangle matrix from another static matrix.
 /// </summary>
 /// <param name="matrix">Static matrix.</param>
 /// <exception cref="ArgumentNullException">
 /// Thrown when matrix is null.
 /// </exception>
 public RectangleMatrix(StaticMatrix <T> matrix) : base(matrix)
 {
 }