Ejemplo n.º 1
0
        /// <summary>
        /// Append a row to the matrix
        /// </summary>
        /// <param name="row"></param>
        protected int AddRow(SparseByteArray row)
        {
            int added = _backingArray.AppendRow(row);

            _dimensions        = _backingArray.GetDimensions();
            dimensionMultiples = InitDimensionMultiples(
                isColumnMajor ? Reverse(_dimensions) : _dimensions);
            return(added);
        }
Ejemplo n.º 2
0
        /**
         * Called during mutation operations to simultaneously set the value
         * on the backing array dynamically.
         * @param val
         * @param coordinates
         */
        private void Back(byte val, params int[] coordinates)
        {
            //ArrayUtils.SetValue(_backingArray, val, coordinates);
            _backingArray[coordinates] = val;
            //update true counts
            var             backingArrayLocal = _backingArray;
            SparseByteArray coordArray        = (SparseByteArray)backingArrayLocal.GetDimensionData(coordinates[0]);

            //SetTrueCount(coordinates[0], ArrayUtils.AggregateArray(coordArray));
            SetTrueCount(coordinates[0], coordArray.AggregateSum());
        }
Ejemplo n.º 3
0
        //public virtual AbstractSparseBinaryMatrix Set(int index, object value)
        //{
        //    Set(index, (int)(value));
        //    return this;
        //}

        /**
         * Clears the true counts prior to a cycle where they're
         * being set
         */
        public override void ClearStatistics(int row)
        {
            SetTrueCount(row, 0);

            SparseByteArray slice = _backingArray.GetRow(row);

            if (slice != null)
            {
                slice.Fill(0);
            }
            //Arrays.Fill(slice, 0);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create sparseArray from given int array
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static SparseByteArray FromArray(byte[] array)
        {
            if (array == null || array.Length == 0)
            {
                throw new ArgumentOutOfRangeException("array");
            }
            var indices  = ArrayUtils.Where(array, b => b == 1);
            var indexMap = new ConcurrentDictionary <int, object>(indices.ToDictionary(k => k, v => (object)1));
            var spArray  = new SparseByteArray(indexMap, new[] { array.Length }, new[] { indices.Length });

            return(spArray);
        }
Ejemplo n.º 5
0
 public void SetRow(int row, SparseByteArray srow)
 {
     if (Rank != 2)
     {
         throw new InvalidOperationException("This operation only works on 2D matrices");
     }
     _concreteArray[row] = srow._concreteArray;
     if (row >= _dimensions[0])
     {
         _dimensions[0]    = row + 1;
         Length            = _dimensions.Aggregate(1, (i, i1) => i * i1);
         _dimensionSums[1] = srow._concreteArray.Select(i => (int)i.Value).Sum();
     }
 }
Ejemplo n.º 6
0
        public int AppendRow(SparseByteArray row)
        {
            if (Rank != 2)
            {
                throw new InvalidOperationException("This operation only works on 2D matrices");
            }
            int nextId = _concreteArray.Keys.Any() ? _concreteArray.Keys.Max() + 1 : 0;

            _concreteArray.TryAdd(nextId, row._concreteArray);
            if (nextId >= _dimensions[0])
            {
                _dimensions[0]    = nextId + 1;
                Length            = _dimensions.Aggregate(1, (i, i1) => i * i1);
                _dimensionSums[1] = row._concreteArray.Select(i => (int)i.Value).Sum();
            }
            return(nextId);
        }
Ejemplo n.º 7
0
        /**
         * Fills the specified results array with the result of the
         * matrix vector multiplication.
         *
         * @param inputVector		the right side vector
         * @param results			the results array
         */
        //public override void RightVecSumAtNZ(int[] inputVector, int[] results)
        //{
        //    for (int i = 0; i < _dimensions[0]; i++)
        //    {
        //        SparseArray<byte> slice = (SparseArray<byte>)(_dimensions.Length > 1 ? GetSlice(i) : _backingArray);
        //        for (int j = 0; j < slice.Length; j++)
        //        {
        //            results[i] += (inputVector[j] * slice[j]);
        //        }
        //    }
        //}

        public override void RightVecSumAtNZ(int[] inputVector, int[] results)
        {
            ////////

            var mainMatrixPositions = _backingArray.GetSparseIndices();

            foreach (int matrixRowIndex in mainMatrixPositions)
            {
                SparseByteArray slice        = (SparseByteArray)(_dimensions.Length > 1 ? GetSlice(matrixRowIndex) : _backingArray);
                var             sliceIndices = slice.GetSparseIndices();

                foreach (int index in sliceIndices)
                {
                    results[matrixRowIndex] += (inputVector[index] * slice[index]);
                }
            }

            ////////
        }
Ejemplo n.º 8
0
        /**
         * Fills the specified results array with the result of the
         * matrix vector multiplication.
         *
         * @param inputVector       the right side vector
         * @param results           the results array
         */
        public override void RightVecSumAtNZ(int[] inputVector, int[] results, double stimulusThreshold)
        {
            ////////

            var mainMatrixPositions = _backingArray.GetSparseIndices();

            foreach (int matrixRowIndex in mainMatrixPositions)
            {
                SparseByteArray slice        = (SparseByteArray)(_dimensions.Length > 1 ? GetSlice(matrixRowIndex) : _backingArray);
                var             sliceIndices = slice.GetSparseIndices();

                foreach (int index in sliceIndices)
                {
                    results[matrixRowIndex] += (inputVector[index] * slice[index]);
                }
                results[matrixRowIndex] -= results[matrixRowIndex] < stimulusThreshold ? results[matrixRowIndex] : 0;
            }

            ////////
            ///
            //int dimension = _dimensions[0];
            //int[] inputVectorIndices = ArrayUtils.Where(inputVector, ArrayUtils.WHERE_1);

            ////for (int i = 0; i < dimension; i++)
            ////{
            //Parallel.For(0, dimension, i =>
            //{
            //    SparseArray<byte> slice = (SparseArray<byte>)(_dimensions.Length > 1 ? GetSlice(i) : _backingArray);
            //    var sliceIndices = slice.GetSparseIndices();

            //    int j = 0;
            //    foreach (int index in inputVectorIndices.Union(sliceIndices))
            //    {
            //        results[i] += inputVector[index] * slice[index];
            //        if (j == slice.Length - 1)
            //        {
            //            results[i] -= results[i] < stimulusThreshold ? results[i] : 0;
            //        }
            //        j++;
            //    }
            //});
        }
Ejemplo n.º 9
0
 /**
  * Constructs a new {@code SparseBinaryMatrix} with the specified dimensions,
  * allowing the specification of column major ordering if desired.
  * (defaults to row major ordering)
  *
  * @param dimensions                each indexed value is a dimension size
  * @param useColumnMajorOrdering    if true, indicates column first iteration, otherwise
  *                                  row first iteration is the default (if false).
  */
 public SparseBinaryMatrix(int[] dimensions, bool useColumnMajorOrdering)
     : base(dimensions, useColumnMajorOrdering)
 {
     _backingArray = SparseByteArray.CreateInstance(dimensions);
 }
Ejemplo n.º 10
0
        public object GetDimensionData(params int[] indices)
        {
            if (indices == null || indices.Length == 0)
            {
                throw new ArgumentOutOfRangeException("indices", "Cannot get a dimension-date of nothing (too few indices given)");
            }
            if (indices.Length > Rank)
            {
                throw new ArgumentOutOfRangeException("indices", "Cannot get a dimension-date of dimensions who are not there! (too many indices given)");
            }

            int outputArrayRank = Rank - indices.Length;

            if (outputArrayRank == 0)
            {
                // Just return the value
                return(GetValue(indices));
            }

            int[] newArrayDimensions = new int[outputArrayRank];
            int   startRankOffset    = Rank - outputArrayRank;

            for (int i = 0, j = startRankOffset; i < outputArrayRank; i++)
            {
                newArrayDimensions[i] = GetLength(j++);
            }
            // Create new destination array
            SparseByteArray newArray = CreateInstance(newArrayDimensions);
            // Fillup the new array
            List <int> origIndices = new List <int>(indices);

            if (newArray.Rank == 1) // to 1D
            {
                //var destArray = newArray;
                int index1 = origIndices[0];
                if (Rank == 2) // from 2D
                {
                    //Buffer.BlockCopy(givenArray, (index1 * newArray.Length) * Marshal.SizeOf<T>(),
                    //    newArray, 0, Marshal.SizeOf<T>() * newArray.Length);
                    newArray = GetRow(index1);
                }
                else if (Rank == 3)
                {
                    //T[,,] srcArray = (T[,,])givenArray;
                    int index2 = origIndices[1];
                    for (int i = 0; i < newArray.GetLength(0); i++)
                    {
                        byte origValue = this[index1, index2, i];
                        newArray[i] = origValue;
                    }
                }
                else
                {
                    throw new NotSupportedException("Not yet supported");
                }
            }
            else if (newArray.Rank == 2)
            {
                var destArray = newArray;
                int index1    = origIndices[0];

                if (Rank == 3)
                {
                    //T[,,] srcArray = (T[,,])givenArray;
                    for (int r0 = 0; r0 < newArray.GetLength(0); r0++)
                    {
                        for (int r1 = 0; r1 < newArray.GetLength(1); r1++)
                        {
                            byte origValue = this[index1, r0, r1];
                            destArray[r0, r1] = origValue;
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException("Not yet supported (Rank " + newArray.Rank + ")");
                }

                //for (int r0 = 0; r0 < newArray.GetLength(0); r0++)
                //{
                //    for (int r1 = 0; r1 < newArray.GetLength(1); r1++)
                //    {
                //        int[] rankList = { r0, r1 };
                //        int[] rankListGet = new int[0];

                //        if (origIndices.Count == 1)
                //        {
                //            rankListGet = new[] { origIndices[0], r0, r1 };
                //        }
                //        else if (origIndices.Count == 2)
                //        {
                //            rankListGet = new[] { origIndices[0], origIndices[1], r0, r1 };
                //        }

                //        T origValue = (T)givenArray.GetValue(rankListGet);

                //        newArray.SetValue(origValue, rankList);
                //    }
                //}
            }

            return(newArray);
        }