예제 #1
0
        /// <summary>
        ///     Get String representation of bit collection
        /// </summary>
        public string GetString()
        {
            var bitString = "";

            for (var i = 0; i < _bitCollectionLength; i++)
            {
                for (var j = 0; j < _bitArrayLength; j++)
                {
                    bitString += _bitsCollection[i].Get(j) ? "1" : "0";
                }
            }
            return(IntegerBitsetUtils.Reverse(bitString));
        }
예제 #2
0
        /// <summary>
        ///     Multiplies matrix by some number
        /// </summary>
        /// <param name="number">Multiplier</param>
        public void MultiplyByNumber(IntegerBitset number)
        {
            if (number == null)
            {
                throw new ArgumentNullException(nameof(number));
            }

            for (var i = 0; i < _columns; i++)
            {
                for (var j = 0; j < _rows; j++)
                {
                    Matrix[i][j] = new IntegerBitset(
                        IntegerBitsetUtils.BitArrayTruncate(IntegerBitsetUtils.BitArrayMultiply(
                                                                Matrix[i][j].GetBitArray(), number.GetBitArray(), new BitArray(1, false), out _), 32));
                }
            }
        }
예제 #3
0
        /// <summary>
        ///     Multiplies matrix by IntegerBitsetMatrix
        /// </summary>
        public void MultiplyByMatrix(IntegerBitsetMatrix multiplierMatrix)
        {
            if (multiplierMatrix == null)
            {
                throw new ArgumentNullException(nameof(multiplierMatrix));
            }

            var aRows = _rows;
            var aCols = _columns;
            var bRows = multiplierMatrix.GetRows();
            var bCols = multiplierMatrix.GetColumns();

            if (aCols != bRows)
            {
                throw new OperationCanceledException("Non-conformable matrices in MultiplyByMatrix");
            }
            var result = new IntegerBitsetMatrix(aRows, bCols);

            for (var i = 0; i < aCols; ++i)     // each row of A
            {
                for (var j = 0; j < bRows; ++j) // each col of B
                {
                    for (var k = 0; k < aRows; ++k)
                    {
                        //Bit by Bit Multiplying
                        result.Matrix[i][j] = new IntegerBitset(
                            IntegerBitsetUtils.BitArrayAdder(
                                Matrix[i][j].GetBitArray(),
                                IntegerBitsetUtils.BitArrayTruncate(
                                    IntegerBitsetUtils.BitArrayMultiply(
                                        Matrix[i][k].GetBitArray(),
                                        multiplierMatrix.Matrix[k][j].GetBitArray(),
                                        new BitArray(1, false), out _), 32),
                                new BitArray(1, false), out _));
                    }
                }
            }

            Matrix   = result.Matrix;
            _columns = result.GetColumns();
            _rows    = result.GetRows();
        }
예제 #4
0
        /// <summary>
        ///     Adds IntegerBitsetMatrix to this
        /// </summary>
        public void AddMatrix(IntegerBitsetMatrix additionMatrix)
        {
            if (additionMatrix == null)
            {
                throw new ArgumentNullException(nameof(additionMatrix));
            }
            if (_rows != additionMatrix.GetRows() || _columns != additionMatrix.GetColumns())
            {
                throw new OperationCanceledException("Sizes of matrix are different");
            }

            for (var i = 0; i < _columns; i++)
            {
                for (var j = 0; j < _rows; j++)
                {
                    Matrix[i][j] = new IntegerBitset(IntegerBitsetUtils.BitArrayAdder(Matrix[i][j].GetBitArray(),
                                                                                      additionMatrix.Matrix[i][j].GetBitArray(), new BitArray(1, false), out _));
                }
            }
        }
예제 #5
0
        /// <summary>
        ///     Raises matrix to some power
        /// </summary>
        /// <param name="power">Power</param>
        public void RaiseMatrixToPower(IntegerBitset power)
        {
            if (power == null)
            {
                throw new ArgumentNullException(nameof(power));
            }

            for (var i = 0; i < _columns; i++)
            {
                for (var j = 0; j < _rows; j++)
                {
                    for (var k = 1; k < power.GetInt32(); k++)
                    {
                        Matrix[i][j] = new IntegerBitset(
                            IntegerBitsetUtils.BitArrayTruncate(IntegerBitsetUtils.BitArrayMultiply(
                                                                    Matrix[i][j].GetBitArray(), Matrix[i][j].GetBitArray(), new BitArray(1, false), out _), 32));
                    }
                }
            }
        }
예제 #6
0
        private static BitArray[] StringToBitCollection(string bitArray)
        {
            var bitCollection = new BitArray[_bitCollectionLength];

            for (var i = 0; i < _bitCollectionLength; i++)
            {
                bitCollection[i] = new BitArray(_bitArrayLength);
            }

            var reverseString = IntegerBitsetUtils.Reverse(bitArray);

            var element = 0;

            for (var i = 0; i < _bitCollectionLength; i++)
            {
                for (var j = 0; j < _bitArrayLength; j++)
                {
                    bitCollection[i].Set(j, reverseString[element] == '1');
                    element++;
                }
            }

            return(bitCollection);
        }
예제 #7
0
 /// <summary>
 ///     Get Int32 representation of bit collection
 /// </summary>
 public int GetInt32()
 {
     return(IntegerBitsetUtils.Reverse(GetString()).Select((t, i) => t == '1' ? (int)Math.Pow(2, i) : 0).Sum());
 }