Example #1
0
        public void FromMultiDimArray(Array dotNetArray)
        {
            if (dotNetArray.GetType().GetElementType().IsArray)
            {
                throw new Exception("Jagged arrays are not allowed here!");
            }

            int[] dims = new int[dotNetArray.Rank];

            for (int idx = 0; idx < dims.Length; idx++)
            {
                dims[idx] = dotNetArray.GetLength(idx);
            }

            Storage = new NDStorage();
            Storage.Allocate(dotNetArray.GetType().GetElementType(), new Shape(dims), 1);

            Array internalStrg = Storage.GetData();

            var pufferShape = new Shape(dims);

            pufferShape.ChangeTensorLayout(2);

            int[]  idxDims  = null;
            object valueIdx = null;

            for (int idx = 0; idx < Storage.Shape.Size; idx++)
            {
                idxDims  = pufferShape.GetDimIndexOutShape(idx);
                valueIdx = dotNetArray.GetValue(pufferShape.GetDimIndexOutShape(idx));
                internalStrg.SetValue(valueIdx, Storage.Shape.GetIndexInShape(idxDims));
            }
        }
        public void FromJaggedArray(Array dotNetArray)
        {
            if (!dotNetArray.GetType().GetElementType().IsArray)
            {
                throw new Exception("Multi dim arrays are not allowed here!");
            }

            List <int> dimList = new List <int>();

            dimList.Add(dotNetArray.Length);

            object currentArr = dotNetArray;

            while (currentArr.GetType().GetElementType().IsArray)
            {
                Array child = (Array)((Array)currentArr).GetValue(0);
                dimList.Add(child.Length);
                currentArr = child;
            }

            Type elementType = currentArr.GetType().GetElementType();

            int[] dims = dimList.ToArray();

            Shape shape = new Shape(dims);

            shape.ChangeTensorLayout(1);

            NDArray nd = new NDArray(elementType, shape);

            Array ndStrg = nd.Storage.GetData();

            if (dims.Length == 1)
            {
                throw new NotImplementedException("FromJaggedArray dims.Length == 1");
            }
            else if (dims.Length == 2)
            {
                switch (dotNetArray)
                {
                case double[][] array:
                    for (int i = 0; i < dims[0]; i++)
                    {
                        for (int j = 0; j < dims[1]; j++)
                        {
                            nd[i, j] = array[i][j];
                        }
                    }
                    break;
                }
            }

            this.Storage = nd.Storage;
        }
Example #3
0
        /// <summary>
        /// Clone the whole NDArray
        /// internal storage is also cloned into 2nd memory area
        /// </summary>
        /// <returns>Cloned NDArray</returns>
        public object Clone()
        {
            var puffer      = new NDArray(this.dtype);
            var shapePuffer = new Shape(this.shape);

            shapePuffer.ChangeTensorLayout(this.Storage.Shape.TensorLayout);

            puffer.Storage.Allocate(this.dtype, shapePuffer, this.Storage.TensorLayout);

            puffer.Storage.SetData(this.Storage.CloneData());

            return(puffer);
        }
Example #4
0
 public void Reshape(params int[] dimensions)
 {
     if (_TensorLayout == 2)
     {
         _Shape = new Shape(dimensions);
     }
     else
     {
         ChangeTensorLayout(2);
         _Shape = new Shape(dimensions);
         _Shape.ChangeTensorLayout(2);
         ChangeTensorLayout(1);
     }
 }
Example #5
0
        /// <summary>
        /// Allocate memory by dtype, shape, tensororder (default column wise)
        /// </summary>
        /// <param name="dtype">storage data type</param>
        /// <param name="shape">storage data shape</param>
        /// <param name="tensorOrder">row or column wise</param>
        public void Allocate(Type dtype, Shape shape, int tensorOrder = 1)
        {
            _DType = dtype;
            _Shape = shape;
            _Shape.ChangeTensorLayout(tensorOrder);
            int elementNumber = 1;

            for (int idx = 0; idx < shape.Dimensions.Length; idx++)
            {
                elementNumber *= shape.Dimensions[idx];
            }

            _values       = Array.CreateInstance(dtype, elementNumber);
            _TensorLayout = tensorOrder;
        }
        public void FromJaggedArray(Array dotNetArray)
        {
            if (!dotNetArray.GetType().GetElementType().IsArray)
            {
                throw new Exception("Multi dim arrays are not allowed here!");
            }

            List <int> dimList = new List <int>();

            dimList.Add(dotNetArray.Length);

            object currentArr = dotNetArray;

            while (currentArr.GetType().GetElementType().IsArray)
            {
                Array child = (Array)((Array)currentArr).GetValue(0);
                dimList.Add(child.Length);
                currentArr = child;
            }

            Type elementType = currentArr.GetType().GetElementType();

            int[] dims = dimList.ToArray();

            Shape shape = new Shape(dims);

            shape.ChangeTensorLayout(1);

            NDArray nd = new NDArray(elementType, shape);

            Array ndStrg = nd.Storage.GetData();

            for (int idx = 0; idx < shape.Size; idx++)
            {
                int[] indexes = shape.GetDimIndexOutShape(idx);

                Array puffer = (Array)dotNetArray.GetValue(indexes[0]);

                for (int jdx = 1; jdx < indexes.Length - 1; jdx++)
                {
                    puffer = (Array)puffer.GetValue(indexes[jdx]);
                }

                ndStrg.SetValue(puffer.GetValue(indexes[indexes.Length - 1]), nd.Storage.Shape.GetIndexInShape(indexes));
            }

            this.Storage = nd.Storage;
        }
        public Array ToMuliDimArray <T>()
        {
            Array dotNetArray = Array.CreateInstance(typeof(T), this.shape);

            var pufferShape = new Shape(shape);

            pufferShape.ChangeTensorLayout(2);

            int[]  indexes  = null;
            object idxValue = null;

            T[] array = Storage.GetData <T>();

            for (int idx = 0; idx < this.size; idx++)
            {
                indexes  = pufferShape.GetDimIndexOutShape(idx);
                idxValue = array[Storage.Shape.GetIndexInShape(indexes)];
                dotNetArray.SetValue(idxValue, indexes);
            }

            return(dotNetArray);
        }
Example #8
0
        protected void _ChangeColumnToRowLayout()
        {
            if (_Shape.NDim == 1)
            {
            }
            else if (_Shape.NDim == 2)
            {
                var puffer = Array.CreateInstance(_values.GetType().GetElementType(), _values.Length);

                var pufferShape = new Shape(_Shape.Dimensions);
                pufferShape.ChangeTensorLayout(1);

                for (int idx = 0; idx < _values.Length; idx++)
                {
                    puffer.SetValue(_values.GetValue(idx), pufferShape.GetIndexInShape(Shape.GetDimIndexOutShape(idx)));
                }

                _values = puffer;
            }
            else
            {
                var puffer = Array.CreateInstance(_values.GetType().GetElementType(), _values.Length);

                var pufferShape = new Shape(_Shape.Dimensions);
                pufferShape.ChangeTensorLayout(1);

                for (int idx = 0; idx < _values.Length; idx++)
                {
                    puffer.SetValue(_values.GetValue(idx), pufferShape.GetIndexInShape(Shape.GetDimIndexOutShape(idx)));
                }

                _values = puffer;
            }
            _TensorLayout = 1;
            Shape.ChangeTensorLayout(1);
        }
Example #9
0
        public NDArray hstack <T>(params NDArray[] nps)
        {
            if (nps == null || nps.Length == 0)
            {
                throw new Exception("Input arrays can not be empty");
            }

            var npAll = new NDArray[nps.Length + 1];

            npAll[0] = this;

            for (int idx = 0; idx < nps.Length; idx++)
            {
                if (nps[0].Storage.Shape != nps[idx].Storage.Shape)
                {
                    throw new Exception("Arrays mush have same shapes");
                }
                else
                {
                    npAll[idx + 1] = nps[idx];
                }
            }

            NDArray nd = new NDArray(dtype);

            // easy 1D case
            if (this.ndim == 1)
            {
                var list1D = new List <T>();
                for (int idx = 0; idx < npAll.Length; idx++)
                {
                    list1D.AddRange(npAll[idx].Storage.GetData <T>().ToList());
                }

                nd = np.array(list1D.ToArray(), this.dtype);
            }
            else
            {
                for (int idx = 0; idx < npAll.Length; idx++)
                {
                    npAll[idx].Storage.ChangeTensorLayout(2);
                }
                int total = npAll[0].ndim == 1 ? 1 : npAll[0].shape[0];
                var list  = new List <T>();

                for (int i = 0; i < total; i++)
                {
                    for (int k = 0; k < npAll.Length; k++)
                    {
                        var pufferShape = new Shape(npAll[k].shape);
                        pufferShape.ChangeTensorLayout(this.Storage.Shape.TensorLayout);
                        int pageSize = npAll[k].ndim == 1 ? npAll[k].shape[0] : pufferShape.DimOffset[0];
                        for (int j = i * pageSize; j < (i + 1) * pageSize; j++)
                        {
                            var ele = npAll[k].Storage.GetData <T>()[j];
                            list.Add(ele);
                        }
                    }
                }

                int[] shapes = new int[npAll[0].shape.Length];
                npAll[0].shape.CopyTo(shapes, 0);
                if (shapes.Length == 1)
                {
                    shapes[0] *= npAll.Length;
                }
                else
                {
                    shapes[1] = npAll.Sum(x => x.shape[1]);
                }

                nd.Storage.Allocate(nd.Storage.DType, new Shape(shapes), 2);
                nd.Storage.SetData(list.ToArray());
                nd.Storage.ChangeTensorLayout(1);
            }

            return(nd);
        }