예제 #1
0
        /// <summary>
        /// Obtém a soma da matriz corrente com outra matriz, onde a soma é efectuada módulo 2.
        /// </summary>
        /// <param name="right">A outra matriz.</param>
        /// <returns>O resultado da soma.</returns>
        /// <exception cref="ArgumentNullException">Se o argumento for nulo.</exception>
        /// <exception cref="ArgumentException">Se as dimensões da matriz a adicionar não coincidir com as dimensões da matriz corrente.</exception>
        public BitMathMatrix Sum(BitMathMatrix right)
        {
            if (right == null)
            {
                throw new ArgumentNullException("right");
            }
            else
            {
                if (this.numberOfLines == right.numberOfLines &&
                    this.numberOfColumns == right.numberOfColumns)
                {
                    var result = new BitMathMatrix(
                        this.numberOfLines,
                        this.numberOfColumns);
                    for (int i = 0; i < this.numberOfLines; ++i)
                    {
                        for (int j = 0; j < this.numberOfColumns; ++j)
                        {
                            result.bitMatrix[i][j] = (this.bitMatrix[i][j] + right.bitMatrix[i][j]) % 2;
                        }
                    }

                    return(result);
                }
                else
                {
                    throw new ArgumentException("Matrices don't have the same dimensions.");
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Obtém o produto da matriz corrente com outra matriz, onde as operações são efectuadas módulo 2.
        /// </summary>
        /// <param name="right">A outra matriz.</param>
        /// <returns>O resultado do produto.</returns>
        /// <exception cref="ArgumentNullException">Se o argumento for nulo.</exception>
        /// <exception cref="ArgumentException">Se as dimensões da matriz a adicionar não coincidir com as dimensões da matriz corrente.</exception>
        public BitMathMatrix Multiply(BitMathMatrix right)
        {
            if (right == null)
            {
                throw new ArgumentNullException("right");
            }
            else
            {
                var columnNumber = this.numberOfColumns;
                var lineNumber   = right.numberOfColumns;
                if (columnNumber != lineNumber)
                {
                    throw new MathematicsException("To multiply two matrices, the number of columns of the first must match the number of lines of second.");
                }
                else
                {
                    var firstDimension  = this.numberOfLines;
                    var secondDimension = right.numberOfColumns;
                    var result          = new BitMathMatrix(
                        firstDimension,
                        secondDimension);
                    for (int i = 0; i < firstDimension; ++i)
                    {
                        for (int j = 0; j < secondDimension; ++j)
                        {
                            var addResult = 0;
                            for (int k = 0; k < columnNumber; ++k)
                            {
                                var multResult = (this.bitMatrix[i][k] * right.bitMatrix[k][j]) % 2;
                                addResult = (addResult + multResult) % 2;
                            }

                            result.bitMatrix[i][j] = addResult;
                        }
                    }

                    return(result);
                }
            }
        }