Пример #1
0
        /// <summary>
        /// 矩阵加法
        /// </summary>
        /// <param name="firstMatrix">矩阵1</param>
        /// <param name="secondMatrix">矩阵2</param>
        /// <returns></returns>
        public static SimpleMatrix operator +(SimpleMatrix firstMatrix, SimpleMatrix secondMatrix)
        {
            if (!(firstMatrix.Row == secondMatrix.Row && firstMatrix.Column == secondMatrix.Column))
            {
                throw new ArgumentException("相加的两个矩阵行数列数应相同");
            }
            int          rowSize     = firstMatrix.Row;
            int          columnSize  = firstMatrix.Column;
            SimpleMatrix addedMatrix = new SimpleMatrix(rowSize, columnSize);

            for (int row = 0; row < rowSize; row++)
            {
                for (int col = 0; col < columnSize; col++)
                {
                    addedMatrix[row, col] = firstMatrix[row, col] + secondMatrix[row, col];
                }
            }
            return(addedMatrix);
        }
Пример #2
0
        /// <summary>
        /// 矩阵减法
        /// </summary>
        /// <param name="firstMatrix">矩阵1</param>
        /// <param name="secondMatrix">矩阵2</param>
        /// <returns></returns>
        public static SimpleMatrix operator -(SimpleMatrix firstMatrix, SimpleMatrix secondMatrix)
        {
            SimpleMatrix nMatrix = -secondMatrix;

            return(firstMatrix + nMatrix);
        }